blob: 3626ea3ca98563d4e5e478464bab7d17d86bd80b [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800115 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118};
119
Jouni Malinendc7b7132012-09-14 12:53:47 -0700120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122static struct wpa_supplicant *
123wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800125static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800127static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800131static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800144static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800145static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800147static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800148static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700149static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700150static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800151static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700153static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160static void
161wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167/*
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
170 */
171static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172{
173 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
177 if (!freqs)
178 return -1;
179
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
182 os_free(freqs);
183
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187}
188
189
190/*
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
193 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194static unsigned int
195wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700199 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 unsigned int num, i, j;
201
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700211
212 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800220
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700221 return j;
222}
223
224
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700225static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
226 int freq)
227{
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
229 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700230
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
233
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
238 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800240 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242}
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247{
248 size_t i;
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
254 }
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
257 return;
258
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
261
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700265 const u8 *ies;
266 size_t ies_len;
267
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700271
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
279 ies = ies + ies_len;
280 ies_len = bss->beacon_ie_len;
281 }
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800285 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700286 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291}
292
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
295{
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
298 int ret;
299
300 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 return;
308 }
309
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800310 if (wpa_s->clear_driver_scan_cache) {
311 wpa_printf(MSG_DEBUG,
312 "Request driver to clear scan cache due to local BSS flush");
313 params->only_new_results = 1;
314 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800315 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800316 if (ret == 0)
317 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 wpa_scan_free_params(params);
319 work->ctx = NULL;
320 if (ret) {
321 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800322 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800323 return;
324 }
325
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800326 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 os_get_reltime(&wpa_s->scan_trigger_time);
328 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
329 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800330 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 wpa_s->p2p_scan_work = work;
332}
333
334
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800335static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
336 int freq)
337{
338 if (wpa_s->global->p2p_24ghz_social_channels &&
339 (freq == 2412 || freq == 2437 || freq == 2462)) {
340 /*
341 * Search all social channels regardless of whether these have
342 * been disabled for P2P operating channel use to avoid missing
343 * peers.
344 */
345 return 1;
346 }
347 return p2p_supported_freq(wpa_s->global->p2p, freq);
348}
349
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
352 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354{
355 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800356 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700358 unsigned int num_channels = 0;
359 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700361 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800362 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
364 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
365 return -1;
366
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (wpa_s->p2p_scan_work) {
368 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
369 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700370 }
371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372 params = os_zalloc(sizeof(*params));
373 if (params == NULL)
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800377 params->num_ssids = 1;
378 n = os_malloc(P2P_WILDCARD_SSID_LEN);
379 if (n == NULL)
380 goto fail;
381 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
382 params->ssids[0].ssid = n;
383 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
385 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700386 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
387 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 num_req_dev_types, req_dev_types);
389 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 switch (type) {
393 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700394 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
395 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800396 if (params->freqs == NULL)
397 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700398 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800399 if (wpas_p2p_search_social_channel(
400 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700401 params->freqs[num_channels++] =
402 social_channels_freq[i];
403 }
404 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 break;
406 case P2P_SCAN_FULL:
407 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800408 case P2P_SCAN_SPECIFIC:
409 params->freqs = os_calloc(2, sizeof(int));
410 if (params->freqs == NULL)
411 goto fail;
412 params->freqs[0] = freq;
413 params->freqs[1] = 0;
414 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700416 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
417 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800418 if (params->freqs == NULL)
419 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700420 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800421 if (wpas_p2p_search_social_channel(
422 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700423 params->freqs[num_channels++] =
424 social_channels_freq[i];
425 }
426 if (p2p_supported_freq(wpa_s->global->p2p, freq))
427 params->freqs[num_channels++] = freq;
428 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 break;
430 }
431
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800432 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
433 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
434 if (ies == NULL) {
435 wpabuf_free(wps_ie);
436 goto fail;
437 }
438 wpabuf_put_buf(ies, wps_ie);
439 wpabuf_free(wps_ie);
440
441 bands = wpas_get_bands(wpa_s, params->freqs);
442 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
443
444 params->p2p_probe = 1;
445 n = os_malloc(wpabuf_len(ies));
446 if (n == NULL) {
447 wpabuf_free(ies);
448 goto fail;
449 }
450 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
451 params->extra_ies = n;
452 params->extra_ies_len = wpabuf_len(ies);
453 wpabuf_free(ies);
454
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800455 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800456 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
457 params) < 0)
458 goto fail;
459 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800461fail:
462 wpa_scan_free_params(params);
463 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464}
465
466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700467static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
468{
469 switch (p2p_group_interface) {
470 case P2P_GROUP_INTERFACE_PENDING:
471 return WPA_IF_P2P_GROUP;
472 case P2P_GROUP_INTERFACE_GO:
473 return WPA_IF_P2P_GO;
474 case P2P_GROUP_INTERFACE_CLIENT:
475 return WPA_IF_P2P_CLIENT;
476 }
477
478 return WPA_IF_P2P_GROUP;
479}
480
481
482static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
483 const u8 *ssid,
484 size_t ssid_len, int *go)
485{
486 struct wpa_ssid *s;
487
488 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
489 for (s = wpa_s->conf->ssid; s; s = s->next) {
490 if (s->disabled != 0 || !s->p2p_group ||
491 s->ssid_len != ssid_len ||
492 os_memcmp(ssid, s->ssid, ssid_len) != 0)
493 continue;
494 if (s->mode == WPAS_MODE_P2P_GO &&
495 s != wpa_s->current_ssid)
496 continue;
497 if (go)
498 *go = s->mode == WPAS_MODE_P2P_GO;
499 return wpa_s;
500 }
501 }
502
503 return NULL;
504}
505
506
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800507static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
508{
509 struct wpa_supplicant *wpa_s = eloop_ctx;
510 wpa_printf(MSG_DEBUG,
511 "P2P: Complete previously requested removal of %s",
512 wpa_s->ifname);
513 wpas_p2p_disconnect(wpa_s);
514}
515
516
517static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
518 struct wpa_supplicant *calling_wpa_s)
519{
520 if (calling_wpa_s == wpa_s && wpa_s &&
521 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
522 /*
523 * The calling wpa_s instance is going to be removed. Do that
524 * from an eloop callback to keep the instance available until
525 * the caller has returned. This my be needed, e.g., to provide
526 * control interface responses on the per-interface socket.
527 */
528 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
529 wpa_s, NULL) < 0)
530 return -1;
531 return 0;
532 }
533
534 return wpas_p2p_disconnect(wpa_s);
535}
536
537
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800538/* Determine total number of clients in active groups where we are the GO */
539static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
540{
541 unsigned int count = 0;
542 struct wpa_ssid *s;
543
544 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
545 for (s = wpa_s->conf->ssid; s; s = s->next) {
546 wpa_printf(MSG_DEBUG,
547 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
548 wpa_s, s, s->disabled, s->p2p_group,
549 s->mode);
550 if (!s->disabled && s->p2p_group &&
551 s->mode == WPAS_MODE_P2P_GO) {
552 count += p2p_get_group_num_members(
553 wpa_s->p2p_group);
554 }
555 }
556 }
557
558 return count;
559}
560
561
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800562static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
563{
564 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
565 !wpa_s->current_ssid->disabled &&
566 wpa_s->current_ssid->p2p_group &&
567 wpa_s->current_ssid->p2p_persistent_group;
568}
569
570
571static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
572{
573 return p2p_is_active_persistent_group(wpa_s) &&
574 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
575}
576
577
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800578/* Find an interface for a P2P group where we are the GO */
579static struct wpa_supplicant *
580wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
581{
582 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800583
584 if (!wpa_s)
585 return NULL;
586
587 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800588 if (!p2p_is_active_persistent_go(wpa_s))
589 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800590
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800591 /* Prefer a group with connected clients */
592 if (p2p_get_group_num_members(wpa_s->p2p_group))
593 return wpa_s;
594 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800595 }
596
597 /* No group with connected clients, so pick the one without (if any) */
598 return save;
599}
600
601
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800602static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800603{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800604 return p2p_is_active_persistent_group(wpa_s) &&
605 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
606}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800608
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800609/* Find an interface for a P2P group where we are the P2P Client */
610static struct wpa_supplicant *
611wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
612{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800614 if (p2p_is_active_persistent_cli(wpa_s))
615 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800616 }
617
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800619}
620
621
622/* Find a persistent group where we are the GO */
623static struct wpa_ssid *
624wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
625{
626 struct wpa_ssid *s;
627
628 for (s = wpa_s->conf->ssid; s; s = s->next) {
629 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
630 return s;
631 }
632
633 return NULL;
634}
635
636
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800637static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
638 unsigned int *force_freq,
639 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800641 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800642 struct wpa_ssid *s;
643 u8 conncap = P2PS_SETUP_NONE;
644 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800645 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800646 struct wpa_ssid *persistent_go;
647 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800648 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800649
650 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
651
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800652 if (force_freq)
653 *force_freq = 0;
654 if (pref_freq)
655 *pref_freq = 0;
656
657 size = P2P_MAX_PREF_CHANNELS;
658 if (force_freq && pref_freq &&
659 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
660 (int *) pref_freq, 0, pref_freq_list, &size))
661 wpas_p2p_set_own_freq_preference(wpa_s,
662 *force_freq ? *force_freq :
663 *pref_freq);
664
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800665 /*
666 * For non-concurrent capable devices:
667 * If persistent_go, then no new.
668 * If GO, then no client.
669 * If client, then no GO.
670 */
671 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 if (go_wpa_s)
673 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800674 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800675 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
676 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800677
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800678 wpa_printf(MSG_DEBUG,
679 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
680 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681
682 /* If not concurrent, restrict our choices */
683 if (p2p_no_group_iface) {
684 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
685
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800686 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800687 return P2PS_SETUP_NONE;
688
689 if (go_wpa_s) {
690 if (role == P2PS_SETUP_CLIENT ||
691 incoming == P2PS_SETUP_GROUP_OWNER ||
692 p2p_client_limit_reached(go_wpa_s->p2p_group))
693 return P2PS_SETUP_NONE;
694
695 return P2PS_SETUP_GROUP_OWNER;
696 }
697
698 if (persistent_go) {
699 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
700 if (!incoming)
701 return P2PS_SETUP_GROUP_OWNER |
702 P2PS_SETUP_CLIENT;
703 if (incoming == P2PS_SETUP_NEW) {
704 u8 r;
705
706 if (os_get_random(&r, sizeof(r)) < 0 ||
707 (r & 1))
708 return P2PS_SETUP_CLIENT;
709 return P2PS_SETUP_GROUP_OWNER;
710 }
711 }
712 }
713 }
714
715 /* If a required role has been specified, handle it here */
716 if (role && role != P2PS_SETUP_NEW) {
717 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800718 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
719 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
720 /*
721 * Peer has an active GO, so if the role allows it and
722 * we do not have any active roles, become client.
723 */
724 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
725 !cli_wpa_s)
726 return P2PS_SETUP_CLIENT;
727
728 /* fall through */
729
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800730 case P2PS_SETUP_NONE:
731 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800732 conncap = role;
733 goto grp_owner;
734
735 case P2PS_SETUP_GROUP_OWNER:
736 /*
737 * Must be a complimentary role - cannot be a client to
738 * more than one peer.
739 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800740 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800741 return P2PS_SETUP_NONE;
742
743 return P2PS_SETUP_CLIENT;
744
745 case P2PS_SETUP_CLIENT:
746 /* Must be a complimentary role */
747 if (incoming != role) {
748 conncap = P2PS_SETUP_GROUP_OWNER;
749 goto grp_owner;
750 }
Roshan Pius3a1667e2018-07-03 15:17:14 -0700751 /* fall through */
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800752
753 default:
754 return P2PS_SETUP_NONE;
755 }
756 }
757
758 /*
759 * For now, we only will support ownership of one group, and being a
760 * client of one group. Therefore, if we have either an existing GO
761 * group, or an existing client group, we will not do a new GO
762 * negotiation, but rather try to re-use the existing groups.
763 */
764 switch (incoming) {
765 case P2PS_SETUP_NONE:
766 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800767 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800768 conncap = P2PS_SETUP_GROUP_OWNER;
769 else if (!owned_members)
770 conncap = P2PS_SETUP_NEW;
771 else if (incoming == P2PS_SETUP_NONE)
772 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
773 else
774 conncap = P2PS_SETUP_CLIENT;
775 break;
776
777 case P2PS_SETUP_CLIENT:
778 conncap = P2PS_SETUP_GROUP_OWNER;
779 break;
780
781 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800782 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800783 conncap = P2PS_SETUP_CLIENT;
784 break;
785
786 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
787 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800788 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800789 conncap = P2PS_SETUP_GROUP_OWNER;
790 else {
791 u8 r;
792
793 if (os_get_random(&r, sizeof(r)) < 0 ||
794 (r & 1))
795 conncap = P2PS_SETUP_CLIENT;
796 else
797 conncap = P2PS_SETUP_GROUP_OWNER;
798 }
799 break;
800
801 default:
802 return P2PS_SETUP_NONE;
803 }
804
805grp_owner:
806 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
807 (!incoming && (conncap & P2PS_SETUP_NEW))) {
808 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
809 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800810
811 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800812 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800814 wpa_s->p2p_mgmt ?
815 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800816 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800817 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800818 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800819 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800820 wpa_printf(MSG_ERROR,
821 "P2P: Failed to allocate a new interface for the group");
822 return P2PS_SETUP_NONE;
823 }
824 wpa_s->global->pending_group_iface_for_p2ps = 1;
825 p2p_set_intended_addr(wpa_s->global->p2p,
826 wpa_s->pending_interface_addr);
827 }
828 }
829
830 return conncap;
831}
832
833
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700834static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
835 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836{
837 struct wpa_ssid *ssid;
838 char *gtype;
839 const char *reason;
840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 ssid = wpa_s->current_ssid;
842 if (ssid == NULL) {
843 /*
844 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700845 * pending P2P group interface waiting for provisioning or a
846 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 */
848 ssid = wpa_s->conf->ssid;
849 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700850 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851 break;
852 ssid = ssid->next;
853 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300854 if (ssid == NULL &&
855 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
856 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700857 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
858 "not found");
859 return -1;
860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 }
862 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
863 gtype = "GO";
864 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
865 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
866 wpa_s->reassociate = 0;
867 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 gtype = "client";
869 } else
870 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700871
872 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
873 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
874
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800875 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700876 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800877 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
878 wpa_s, NULL)) {
879 wpa_printf(MSG_DEBUG,
880 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
881 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
882 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
883 wpa_s, NULL);
884 }
885 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 if (wpa_s->cross_connect_in_use) {
888 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800889 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700890 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
891 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700893 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 case P2P_GROUP_REMOVAL_REQUESTED:
895 reason = " reason=REQUESTED";
896 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700897 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
898 reason = " reason=FORMATION_FAILED";
899 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
901 reason = " reason=IDLE";
902 break;
903 case P2P_GROUP_REMOVAL_UNAVAILABLE:
904 reason = " reason=UNAVAILABLE";
905 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700906 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
907 reason = " reason=GO_ENDING_SESSION";
908 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700909 case P2P_GROUP_REMOVAL_PSK_FAILURE:
910 reason = " reason=PSK_FAILURE";
911 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800912 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
913 reason = " reason=FREQ_CONFLICT";
914 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 default:
916 reason = "";
917 break;
918 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700919 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800920 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700921 P2P_EVENT_GROUP_REMOVED "%s %s%s",
922 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800925 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
926 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
928 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800929 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800930 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800931 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
932 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700933 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800934 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700935 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700936
Dmitry Shmidt15907092014-03-25 10:42:57 -0700937 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800938 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
939 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700940
Dmitry Shmidt96571392013-10-14 12:54:46 -0700941 /*
942 * Make sure wait for the first client does not remain active after the
943 * group has been removed.
944 */
945 wpa_s->global->p2p_go_wait_client.sec = 0;
946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
948 struct wpa_global *global;
949 char *ifname;
950 enum wpa_driver_if_type type;
951 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
952 wpa_s->ifname);
953 global = wpa_s->global;
954 ifname = os_strdup(wpa_s->ifname);
955 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800956 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700957 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 wpa_s = global->ifaces;
959 if (wpa_s && ifname)
960 wpa_drv_if_remove(wpa_s, type, ifname);
961 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300962 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 }
964
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800965 /*
966 * The primary interface was used for P2P group operations, so
967 * need to reset its p2pdev.
968 */
969 wpa_s->p2pdev = wpa_s->parent;
970
Dmitry Shmidt96571392013-10-14 12:54:46 -0700971 if (!wpa_s->p2p_go_group_formation_completed) {
972 wpa_s->global->p2p_group_formation = NULL;
973 wpa_s->p2p_in_provisioning = 0;
974 }
975
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800976 wpa_s->show_group_started = 0;
977 os_free(wpa_s->go_params);
978 wpa_s->go_params = NULL;
979
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800980 os_free(wpa_s->p2p_group_common_freqs);
981 wpa_s->p2p_group_common_freqs = NULL;
982 wpa_s->p2p_group_common_freqs_num = 0;
983
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800984 wpa_s->waiting_presence_resp = 0;
985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
987 if (ssid && (ssid->p2p_group ||
988 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
989 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
990 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700991 if (ssid == wpa_s->current_ssid) {
992 wpa_sm_set_config(wpa_s->wpa, NULL);
993 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700995 }
996 /*
997 * Networks objects created during any P2P activities are not
998 * exposed out as they might/will confuse certain non-P2P aware
999 * applications since these network objects won't behave like
1000 * regular ones.
1001 *
1002 * Likewise, we don't send out network removed signals for such
1003 * network objects.
1004 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_config_remove_network(wpa_s->conf, id);
1006 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001007 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 } else {
1009 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1010 "found");
1011 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001012 if (wpa_s->ap_iface)
1013 wpa_supplicant_ap_deinit(wpa_s);
1014 else
1015 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001016
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001017 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1018
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001019 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020}
1021
1022
1023static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1024 u8 *go_dev_addr,
1025 const u8 *ssid, size_t ssid_len)
1026{
1027 struct wpa_bss *bss;
1028 const u8 *bssid;
1029 struct wpabuf *p2p;
1030 u8 group_capab;
1031 const u8 *addr;
1032
1033 if (wpa_s->go_params)
1034 bssid = wpa_s->go_params->peer_interface_addr;
1035 else
1036 bssid = wpa_s->bssid;
1037
1038 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001039 if (bss == NULL && wpa_s->go_params &&
1040 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1041 bss = wpa_bss_get_p2p_dev_addr(
1042 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 if (bss == NULL) {
1044 u8 iface_addr[ETH_ALEN];
1045 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1046 iface_addr) == 0)
1047 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1048 }
1049 if (bss == NULL) {
1050 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1051 "group is persistent - BSS " MACSTR " not found",
1052 MAC2STR(bssid));
1053 return 0;
1054 }
1055
1056 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001057 if (p2p == NULL)
1058 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1059 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 if (p2p == NULL) {
1061 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1062 "group is persistent - BSS " MACSTR
1063 " did not include P2P IE", MAC2STR(bssid));
1064 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1065 (u8 *) (bss + 1), bss->ie_len);
1066 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1067 ((u8 *) bss + 1) + bss->ie_len,
1068 bss->beacon_ie_len);
1069 return 0;
1070 }
1071
1072 group_capab = p2p_get_group_capab(p2p);
1073 addr = p2p_get_go_dev_addr(p2p);
1074 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1075 "group_capab=0x%x", group_capab);
1076 if (addr) {
1077 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1078 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1079 MAC2STR(addr));
1080 } else
1081 os_memset(go_dev_addr, 0, ETH_ALEN);
1082 wpabuf_free(p2p);
1083
1084 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1085 "go_dev_addr=" MACSTR,
1086 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1087
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001088 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
1091
Jouni Malinen75ecf522011-06-27 15:19:46 -07001092static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1093 struct wpa_ssid *ssid,
1094 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095{
1096 struct wpa_ssid *s;
1097 int changed = 0;
1098
1099 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1100 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1101 for (s = wpa_s->conf->ssid; s; s = s->next) {
1102 if (s->disabled == 2 &&
1103 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1104 s->ssid_len == ssid->ssid_len &&
1105 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1106 break;
1107 }
1108
1109 if (s) {
1110 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1111 "entry");
1112 if (ssid->passphrase && !s->passphrase)
1113 changed = 1;
1114 else if (ssid->passphrase && s->passphrase &&
1115 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1116 changed = 1;
1117 } else {
1118 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1119 "entry");
1120 changed = 1;
1121 s = wpa_config_add_network(wpa_s->conf);
1122 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001123 return -1;
1124
1125 /*
1126 * Instead of network_added we emit persistent_group_added
1127 * notification. Also to keep the defense checks in
1128 * persistent_group obj registration method, we set the
1129 * relevant flags in s to designate it as a persistent group.
1130 */
1131 s->p2p_group = 1;
1132 s->p2p_persistent_group = 1;
1133 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 wpa_config_set_network_defaults(s);
1135 }
1136
1137 s->p2p_group = 1;
1138 s->p2p_persistent_group = 1;
1139 s->disabled = 2;
1140 s->bssid_set = 1;
1141 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1142 s->mode = ssid->mode;
1143 s->auth_alg = WPA_AUTH_ALG_OPEN;
1144 s->key_mgmt = WPA_KEY_MGMT_PSK;
1145 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001146 s->pbss = ssid->pbss;
1147 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001148 s->export_keys = 1;
1149 if (ssid->passphrase) {
1150 os_free(s->passphrase);
1151 s->passphrase = os_strdup(ssid->passphrase);
1152 }
1153 if (ssid->psk_set) {
1154 s->psk_set = 1;
1155 os_memcpy(s->psk, ssid->psk, 32);
1156 }
1157 if (s->passphrase && !s->psk_set)
1158 wpa_config_update_psk(s);
1159 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1160 os_free(s->ssid);
1161 s->ssid = os_malloc(ssid->ssid_len);
1162 }
1163 if (s->ssid) {
1164 s->ssid_len = ssid->ssid_len;
1165 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1166 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001167 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1168 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1169 wpa_s->global->add_psk = NULL;
1170 changed = 1;
1171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 if (changed && wpa_s->conf->update_config &&
1174 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1175 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1176 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001177
1178 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179}
1180
1181
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001182static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1183 const u8 *addr)
1184{
1185 struct wpa_ssid *ssid, *s;
1186 u8 *n;
1187 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001188 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190
1191 ssid = wpa_s->current_ssid;
1192 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1193 !ssid->p2p_persistent_group)
1194 return;
1195
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001196 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1198 continue;
1199
1200 if (s->ssid_len == ssid->ssid_len &&
1201 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1202 break;
1203 }
1204
1205 if (s == NULL)
1206 return;
1207
1208 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001209 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001210 ETH_ALEN) != 0)
1211 continue;
1212
1213 if (i == s->num_p2p_clients - 1)
1214 return; /* already the most recent entry */
1215
1216 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001217 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1218 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1219 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001220 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001221 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1222 ETH_ALEN);
1223 os_memset(s->p2p_client_list +
1224 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1225 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 found = 1;
1227 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001228 }
1229
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001230 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1231 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001232 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001233 if (n == NULL)
1234 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001235 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1236 ETH_ALEN);
1237 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1238 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001239 s->p2p_client_list = n;
1240 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001241 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 /* Not enough room for an additional entry - drop the oldest
1243 * entry */
1244 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001245 s->p2p_client_list + 2 * ETH_ALEN,
1246 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001247 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001248 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001249 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001250 os_memset(s->p2p_client_list +
1251 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1252 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001253 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001254
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001255 if (p2p_wpa_s->conf->update_config &&
1256 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001258}
1259
1260
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001261static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1262 int go, struct wpa_ssid *ssid, int freq,
1263 const u8 *psk, const char *passphrase,
1264 const u8 *go_dev_addr, int persistent,
1265 const char *extra)
1266{
1267 const char *ssid_txt;
1268 char psk_txt[65];
1269
1270 if (psk)
1271 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1272 else
1273 psk_txt[0] = '\0';
1274
1275 if (ssid)
1276 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1277 else
1278 ssid_txt = "";
1279
1280 if (passphrase && passphrase[0] == '\0')
1281 passphrase = NULL;
1282
1283 /*
1284 * Include PSK/passphrase only in the control interface message and
1285 * leave it out from the debug log entry.
1286 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001287 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001288 P2P_EVENT_GROUP_STARTED
1289 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1290 MACSTR "%s%s",
1291 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1292 psk ? " psk=" : "", psk_txt,
1293 passphrase ? " passphrase=\"" : "",
1294 passphrase ? passphrase : "",
1295 passphrase ? "\"" : "",
1296 MAC2STR(go_dev_addr),
1297 persistent ? " [PERSISTENT]" : "", extra);
1298 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1299 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1300 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1301 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1302 extra);
1303}
1304
1305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001307 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308{
1309 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 int client;
1311 int persistent;
1312 u8 go_dev_addr[ETH_ALEN];
1313
1314 /*
1315 * This callback is likely called for the main interface. Update wpa_s
1316 * to use the group interface if a new interface was created for the
1317 * group.
1318 */
1319 if (wpa_s->global->p2p_group_formation)
1320 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001321 if (wpa_s->p2p_go_group_formation_completed) {
1322 wpa_s->global->p2p_group_formation = NULL;
1323 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001324 } else if (wpa_s->p2p_in_provisioning && !success) {
1325 wpa_msg(wpa_s, MSG_DEBUG,
1326 "P2P: Stop provisioning state due to failure");
1327 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001328 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001329 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001330 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001331
1332 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001333 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001334 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001335 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001336 if (already_deleted)
1337 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001338 wpas_p2p_group_delete(wpa_s,
1339 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 return;
1341 }
1342
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001343 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001344 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345
1346 ssid = wpa_s->current_ssid;
1347 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1348 ssid->mode = WPAS_MODE_P2P_GO;
1349 p2p_group_notif_formation_done(wpa_s->p2p_group);
1350 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1351 }
1352
1353 persistent = 0;
1354 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 client = ssid->mode == WPAS_MODE_INFRA;
1356 if (ssid->mode == WPAS_MODE_P2P_GO) {
1357 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001358 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1359 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 } else
1361 persistent = wpas_p2p_persistent_group(wpa_s,
1362 go_dev_addr,
1363 ssid->ssid,
1364 ssid->ssid_len);
1365 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001366 client = wpa_s->p2p_group_interface ==
1367 P2P_GROUP_INTERFACE_CLIENT;
1368 os_memset(go_dev_addr, 0, ETH_ALEN);
1369 }
1370
1371 wpa_s->show_group_started = 0;
1372 if (client) {
1373 /*
1374 * Indicate event only after successfully completed 4-way
1375 * handshake, i.e., when the interface is ready for data
1376 * packets.
1377 */
1378 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001380 wpas_p2p_group_started(wpa_s, 1, ssid,
1381 ssid ? ssid->frequency : 0,
1382 ssid && ssid->passphrase == NULL &&
1383 ssid->psk_set ? ssid->psk : NULL,
1384 ssid ? ssid->passphrase : NULL,
1385 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 wpas_p2p_cross_connect_setup(wpa_s);
1387 wpas_p2p_set_group_idle_timeout(wpa_s);
1388 }
1389
1390 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001391 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1392 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001393 else {
1394 os_free(wpa_s->global->add_psk);
1395 wpa_s->global->add_psk = NULL;
1396 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001397
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001398 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001399 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001400 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402}
1403
1404
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001405struct send_action_work {
1406 unsigned int freq;
1407 u8 dst[ETH_ALEN];
1408 u8 src[ETH_ALEN];
1409 u8 bssid[ETH_ALEN];
1410 size_t len;
1411 unsigned int wait_time;
1412 u8 buf[0];
1413};
1414
1415
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001416static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1417{
1418 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1419
1420 wpa_printf(MSG_DEBUG,
1421 "P2P: Free Action frame radio work @%p (freq=%u dst="
1422 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1423 wpa_s->p2p_send_action_work, awork->freq,
1424 MAC2STR(awork->dst), MAC2STR(awork->src),
1425 MAC2STR(awork->bssid), awork->wait_time);
1426 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1427 awork->buf, awork->len);
1428 os_free(awork);
1429 wpa_s->p2p_send_action_work->ctx = NULL;
1430 radio_work_done(wpa_s->p2p_send_action_work);
1431 wpa_s->p2p_send_action_work = NULL;
1432}
1433
1434
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001435static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1436 void *timeout_ctx)
1437{
1438 struct wpa_supplicant *wpa_s = eloop_ctx;
1439
1440 if (!wpa_s->p2p_send_action_work)
1441 return;
1442
1443 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001444 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001445}
1446
1447
Dmitry Shmidt03658832014-08-13 11:03:49 -07001448static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001450 if (wpa_s->p2p_send_action_work) {
1451 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001452
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001453 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001454 wpa_printf(MSG_DEBUG,
1455 "P2P: Clear Action TX work @%p (wait_time=%u)",
1456 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001457 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001458 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001459 } else {
1460 /*
1461 * In theory, this should not be needed, but number of
1462 * places in the P2P code is still using non-zero wait
1463 * time for the last Action frame in the sequence and
1464 * some of these do not call send_action_done().
1465 */
1466 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1467 wpa_s, NULL);
1468 eloop_register_timeout(
1469 0, awork->wait_time * 1000,
1470 wpas_p2p_send_action_work_timeout,
1471 wpa_s, NULL);
1472 }
1473 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001474}
1475
1476
1477static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1478 unsigned int freq,
1479 const u8 *dst, const u8 *src,
1480 const u8 *bssid,
1481 const u8 *data, size_t data_len,
1482 enum offchannel_send_action_result
1483 result)
1484{
1485 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1486
1487 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1490 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001492 switch (result) {
1493 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1494 res = P2P_SEND_ACTION_SUCCESS;
1495 break;
1496 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1497 res = P2P_SEND_ACTION_NO_ACK;
1498 break;
1499 case OFFCHANNEL_SEND_ACTION_FAILED:
1500 res = P2P_SEND_ACTION_FAILED;
1501 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 }
1503
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001504 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001506 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1507 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001508 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001509 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1510 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1513 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001514 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001515 P2P_EVENT_FALLBACK_TO_GO_NEG
1516 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001517 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1518 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 }
1520}
1521
1522
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001523static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1524{
1525 struct wpa_supplicant *wpa_s = work->wpa_s;
1526 struct send_action_work *awork = work->ctx;
1527
1528 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001529 if (work->started) {
1530 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1531 wpa_s, NULL);
1532 wpa_s->p2p_send_action_work = NULL;
1533 offchannel_send_action_done(wpa_s);
1534 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001535 os_free(awork);
1536 return;
1537 }
1538
1539 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1540 awork->bssid, awork->buf, awork->len,
1541 awork->wait_time,
1542 wpas_p2p_send_action_tx_status, 1) < 0) {
1543 os_free(awork);
1544 radio_work_done(work);
1545 return;
1546 }
1547 wpa_s->p2p_send_action_work = work;
1548}
1549
1550
1551static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1552 unsigned int freq, const u8 *dst,
1553 const u8 *src, const u8 *bssid, const u8 *buf,
1554 size_t len, unsigned int wait_time)
1555{
1556 struct send_action_work *awork;
1557
1558 if (wpa_s->p2p_send_action_work) {
1559 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1560 return -1;
1561 }
1562
1563 awork = os_zalloc(sizeof(*awork) + len);
1564 if (awork == NULL)
1565 return -1;
1566
1567 awork->freq = freq;
1568 os_memcpy(awork->dst, dst, ETH_ALEN);
1569 os_memcpy(awork->src, src, ETH_ALEN);
1570 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1571 awork->len = len;
1572 awork->wait_time = wait_time;
1573 os_memcpy(awork->buf, buf, len);
1574
1575 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1576 wpas_send_action_cb, awork) < 0) {
1577 os_free(awork);
1578 return -1;
1579 }
1580
1581 return 0;
1582}
1583
1584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1586 const u8 *src, const u8 *bssid, const u8 *buf,
1587 size_t len, unsigned int wait_time)
1588{
1589 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001590 int listen_freq = -1, send_freq = -1;
1591
1592 if (wpa_s->p2p_listen_work)
1593 listen_freq = wpa_s->p2p_listen_work->freq;
1594 if (wpa_s->p2p_send_action_work)
1595 send_freq = wpa_s->p2p_send_action_work->freq;
1596 if (listen_freq != (int) freq && send_freq != (int) freq) {
1597 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1598 listen_freq, send_freq);
1599 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1600 len, wait_time);
1601 }
1602
1603 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001604 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1605 wait_time,
1606 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607}
1608
1609
1610static void wpas_send_action_done(void *ctx)
1611{
1612 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001613
1614 if (wpa_s->p2p_send_action_work) {
1615 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1616 wpa_s, NULL);
1617 os_free(wpa_s->p2p_send_action_work->ctx);
1618 radio_work_done(wpa_s->p2p_send_action_work);
1619 wpa_s->p2p_send_action_work = NULL;
1620 }
1621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001622 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623}
1624
1625
1626static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1627 struct p2p_go_neg_results *params)
1628{
1629 if (wpa_s->go_params == NULL) {
1630 wpa_s->go_params = os_malloc(sizeof(*params));
1631 if (wpa_s->go_params == NULL)
1632 return -1;
1633 }
1634 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1635 return 0;
1636}
1637
1638
1639static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1640 struct p2p_go_neg_results *res)
1641{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001642 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001643 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1644 " dev_addr " MACSTR " wps_method %d",
1645 MAC2STR(res->peer_interface_addr),
1646 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1648 res->ssid, res->ssid_len);
1649 wpa_supplicant_ap_deinit(wpa_s);
1650 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001653#ifdef CONFIG_WPS_NFC
1654 } else if (res->wps_method == WPS_NFC) {
1655 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1656 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001657 wpa_s->p2pdev->p2p_oob_dev_pw,
1658 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1659 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001660 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001661 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001662 NULL,
1663 NULL, 0, 0);
1664#endif /* CONFIG_WPS_NFC */
1665 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001667 if (wpa_s->p2p_wps_method == WPS_P2PS)
1668 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1670 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1671 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1672 wpa_s->p2p_pin, 1, dev_pw_id);
1673 }
1674}
1675
1676
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001677static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1678 struct wpa_ssid *ssid)
1679{
1680 struct wpa_ssid *persistent;
1681 struct psk_list_entry *psk;
1682 struct hostapd_data *hapd;
1683
1684 if (!wpa_s->ap_iface)
1685 return;
1686
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001687 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001688 ssid->ssid_len);
1689 if (persistent == NULL)
1690 return;
1691
1692 hapd = wpa_s->ap_iface->bss[0];
1693
1694 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1695 list) {
1696 struct hostapd_wpa_psk *hpsk;
1697
1698 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1699 MACSTR " psk=%d",
1700 MAC2STR(psk->addr), psk->p2p);
1701 hpsk = os_zalloc(sizeof(*hpsk));
1702 if (hpsk == NULL)
1703 break;
1704 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1705 if (psk->p2p)
1706 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1707 else
1708 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1709 hpsk->next = hapd->conf->ssid.wpa_psk;
1710 hapd->conf->ssid.wpa_psk = hpsk;
1711 }
1712}
1713
1714
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001715static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1716{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001717 char buf[20 + P2P_MAX_CHANNELS * 6];
1718 char *pos, *end;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001719 unsigned int i;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001720 int res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001721
Roshan Pius3a1667e2018-07-03 15:17:14 -07001722 pos = buf;
1723 end = pos + sizeof(buf);
1724 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
1725 res = os_snprintf(pos, end - pos, " %d",
1726 wpa_s->p2p_group_common_freqs[i]);
1727 if (os_snprintf_error(end - pos, res))
1728 break;
1729 pos += res;
1730 }
1731 *pos = '\0';
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001732
Roshan Pius3a1667e2018-07-03 15:17:14 -07001733 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies:%s", buf);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001734}
1735
1736
1737static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1738 struct p2p_go_neg_results *params)
1739{
1740 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1741
1742 wpa_s->p2p_group_common_freqs_num = 0;
1743 os_free(wpa_s->p2p_group_common_freqs);
1744 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1745 if (!wpa_s->p2p_group_common_freqs)
1746 return;
1747
1748 for (i = 0; i < len; i++) {
1749 if (!wpa_s->go_params->freq_list[i])
1750 break;
1751 wpa_s->p2p_group_common_freqs[i] =
1752 wpa_s->go_params->freq_list[i];
1753 }
1754 wpa_s->p2p_group_common_freqs_num = i;
1755}
1756
1757
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001758static void p2p_config_write(struct wpa_supplicant *wpa_s)
1759{
1760#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001761 if (wpa_s->p2pdev->conf->update_config &&
1762 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001763 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1764#endif /* CONFIG_NO_CONFIG_WRITE */
1765}
1766
1767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768static void p2p_go_configured(void *ctx, void *data)
1769{
1770 struct wpa_supplicant *wpa_s = ctx;
1771 struct p2p_go_neg_results *params = data;
1772 struct wpa_ssid *ssid;
1773
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001774 wpa_s->ap_configured_cb = NULL;
1775 wpa_s->ap_configured_cb_ctx = NULL;
1776 wpa_s->ap_configured_cb_data = NULL;
1777 if (!wpa_s->go_params) {
1778 wpa_printf(MSG_ERROR,
1779 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1780 return;
1781 }
1782
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001783 p2p_go_save_group_common_freqs(wpa_s, params);
1784 p2p_go_dump_common_freqs(wpa_s);
1785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 ssid = wpa_s->current_ssid;
1787 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1788 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1789 if (wpa_s->global->p2p_group_formation == wpa_s)
1790 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001791 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1792 params->passphrase[0] == '\0' ?
1793 params->psk : NULL,
1794 params->passphrase,
1795 wpa_s->global->p2p_dev_addr,
1796 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001797 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001799 if (wpa_s->p2pdev->p2ps_method_config_any) {
1800 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001801 wpa_dbg(wpa_s, MSG_DEBUG,
1802 "P2PS: Setting default PIN for ANY");
1803 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1804 "12345670", NULL, 0,
1805 0);
1806 } else {
1807 wpa_dbg(wpa_s, MSG_DEBUG,
1808 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001809 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001811 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001812 "12345670", NULL, 0, 0);
1813 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001814 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001815 }
1816
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001817 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001818 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001819 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001820 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001821 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001822 wpas_p2p_add_psk_list(wpa_s, ssid);
1823 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001824
1825 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001826 params->persistent_group, 0,
1827 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 wpas_p2p_cross_connect_setup(wpa_s);
1829 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001830
1831 if (wpa_s->p2p_first_connection_timeout) {
1832 wpa_dbg(wpa_s, MSG_DEBUG,
1833 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1834 wpa_s->p2p_first_connection_timeout);
1835 wpa_s->p2p_go_group_formation_completed = 0;
1836 wpa_s->global->p2p_group_formation = wpa_s;
1837 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001838 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001839 eloop_register_timeout(
1840 wpa_s->p2p_first_connection_timeout, 0,
1841 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001842 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001843 }
1844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 return;
1846 }
1847
1848 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1849 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1850 params->peer_interface_addr)) {
1851 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1852 "filtering");
1853 return;
1854 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001855 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001857 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001858#ifdef CONFIG_WPS_NFC
1859 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001860 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001861 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001862 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001863 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1864 return;
1865 }
1866 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001867 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1868 wpa_s->p2pdev->p2p_oob_dev_pw,
1869 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1870 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001871#endif /* CONFIG_WPS_NFC */
1872 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001873 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001874 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001875 os_free(wpa_s->go_params);
1876 wpa_s->go_params = NULL;
1877}
1878
1879
1880static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1881 struct p2p_go_neg_results *params,
1882 int group_formation)
1883{
1884 struct wpa_ssid *ssid;
1885
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1887 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1889 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001891 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892
1893 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001894 if (ssid == NULL) {
1895 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001899 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001900 wpa_s->p2p_go_group_formation_completed = 0;
1901 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001902 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 wpa_config_set_network_defaults(ssid);
1905 ssid->temporary = 1;
1906 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001907 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1909 WPAS_MODE_P2P_GO;
1910 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001911 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001912 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001913 ssid->max_oper_chwidth = params->max_oper_chwidth;
1914 ssid->vht_center_freq2 = params->vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001915 ssid->ssid = os_zalloc(params->ssid_len + 1);
1916 if (ssid->ssid) {
1917 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1918 ssid->ssid_len = params->ssid_len;
1919 }
1920 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1921 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1922 ssid->proto = WPA_PROTO_RSN;
1923 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 ssid->group_cipher = WPA_CIPHER_CCMP;
1925 if (params->freq > 56160) {
1926 /*
1927 * Enable GCMP instead of CCMP as pairwise_cipher and
1928 * group_cipher in 60 GHz.
1929 */
1930 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1931 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001932 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1933 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001934 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001935 if (os_strlen(params->passphrase) > 0) {
1936 ssid->passphrase = os_strdup(params->passphrase);
1937 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001938 wpa_msg_global(wpa_s, MSG_ERROR,
1939 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001940 wpa_config_remove_network(wpa_s->conf, ssid->id);
1941 return;
1942 }
1943 } else
1944 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001945 ssid->psk_set = params->psk_set;
1946 if (ssid->psk_set)
1947 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001948 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001949 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001950 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951
1952 wpa_s->ap_configured_cb = p2p_go_configured;
1953 wpa_s->ap_configured_cb_ctx = wpa_s;
1954 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001955 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001956 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 wpa_s->reassociate = 1;
1958 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1960 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpa_supplicant_req_scan(wpa_s, 0, 0);
1962}
1963
1964
1965static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1966 const struct wpa_supplicant *src)
1967{
1968 struct wpa_config *d;
1969 const struct wpa_config *s;
1970
1971 d = dst->conf;
1972 s = src->conf;
1973
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001974#define C(n) \
1975do { \
1976 if (s->n && !d->n) \
1977 d->n = os_strdup(s->n); \
1978} while (0)
1979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 C(device_name);
1981 C(manufacturer);
1982 C(model_name);
1983 C(model_number);
1984 C(serial_number);
1985 C(config_methods);
1986#undef C
1987
1988 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1989 os_memcpy(d->sec_device_type, s->sec_device_type,
1990 sizeof(d->sec_device_type));
1991 d->num_sec_device_types = s->num_sec_device_types;
1992
1993 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001994 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001996 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001997 d->max_num_sta = s->max_num_sta;
1998 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001999 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07002000 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08002001 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002002 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002003 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07002004 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002005 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002006
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002007 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
2008 !d->wps_nfc_pw_from_config) {
2009 wpabuf_free(d->wps_nfc_dh_privkey);
2010 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002011 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2012 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2013 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002014 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002015 d->go_interworking = s->go_interworking;
2016 d->go_access_network_type = s->go_access_network_type;
2017 d->go_internet = s->go_internet;
2018 d->go_venue_group = s->go_venue_group;
2019 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020}
2021
2022
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002023static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2024 char *ifname, size_t len)
2025{
2026 char *ifname_ptr = wpa_s->ifname;
2027
2028 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2029 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2030 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2031 }
2032
2033 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2034 if (os_strlen(ifname) >= IFNAMSIZ &&
2035 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002036 int res;
2037
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002039 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2040 if (os_snprintf_error(len, res) && len)
2041 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002042 }
2043}
2044
2045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2047 enum wpa_driver_if_type type)
2048{
2049 char ifname[120], force_ifname[120];
2050
2051 if (wpa_s->pending_interface_name[0]) {
2052 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2053 "- skip creation of a new one");
2054 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2055 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2056 "unknown?! ifname='%s'",
2057 wpa_s->pending_interface_name);
2058 return -1;
2059 }
2060 return 0;
2061 }
2062
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002063 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064 force_ifname[0] = '\0';
2065
2066 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2067 ifname);
2068 wpa_s->p2p_group_idx++;
2069
2070 wpa_s->pending_interface_type = type;
2071 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2072 wpa_s->pending_interface_addr, NULL) < 0) {
2073 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2074 "interface");
2075 return -1;
2076 }
2077
Jimmy Chen36c21992018-11-29 16:46:43 +08002078 if (wpa_s->conf->p2p_interface_random_mac_addr) {
2079 if (random_mac_addr(wpa_s->pending_interface_addr) < 0) {
2080 wpa_printf(MSG_ERROR, "P2P: Failed to generate random MAC address "
2081 "for the group interface");
2082 return -1;
2083 }
2084 wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR " for the group",
2085 MAC2STR(wpa_s->pending_interface_addr));
2086 }
2087
2088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089 if (force_ifname[0]) {
2090 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2091 force_ifname);
2092 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2093 sizeof(wpa_s->pending_interface_name));
2094 } else
2095 os_strlcpy(wpa_s->pending_interface_name, ifname,
2096 sizeof(wpa_s->pending_interface_name));
2097 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2098 MACSTR, wpa_s->pending_interface_name,
2099 MAC2STR(wpa_s->pending_interface_addr));
2100
2101 return 0;
2102}
2103
2104
2105static void wpas_p2p_remove_pending_group_interface(
2106 struct wpa_supplicant *wpa_s)
2107{
2108 if (!wpa_s->pending_interface_name[0] ||
2109 is_zero_ether_addr(wpa_s->pending_interface_addr))
2110 return; /* No pending virtual interface */
2111
2112 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2113 wpa_s->pending_interface_name);
2114 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2115 wpa_s->pending_interface_name);
2116 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2117 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002118 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119}
2120
2121
2122static struct wpa_supplicant *
2123wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2124{
2125 struct wpa_interface iface;
2126 struct wpa_supplicant *group_wpa_s;
2127
2128 if (!wpa_s->pending_interface_name[0]) {
2129 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2130 if (!wpas_p2p_create_iface(wpa_s))
2131 return NULL;
2132 /*
2133 * Something has forced us to remove the pending interface; try
2134 * to create a new one and hope for the best that we will get
2135 * the same local address.
2136 */
2137 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2138 WPA_IF_P2P_CLIENT) < 0)
2139 return NULL;
2140 }
2141
2142 os_memset(&iface, 0, sizeof(iface));
2143 iface.ifname = wpa_s->pending_interface_name;
2144 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002145 if (wpa_s->conf->ctrl_interface == NULL &&
2146 wpa_s->parent != wpa_s &&
2147 wpa_s->p2p_mgmt &&
2148 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2149 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2150 else
2151 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002153 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 if (group_wpa_s == NULL) {
2155 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2156 "wpa_supplicant interface");
2157 return NULL;
2158 }
2159 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2161 P2P_GROUP_INTERFACE_CLIENT;
2162 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002163 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164
2165 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2166
Jimmy Chen36c21992018-11-29 16:46:43 +08002167 if (wpa_s->conf->p2p_interface_random_mac_addr) {
2168 if (wpa_drv_set_mac_addr(group_wpa_s, wpa_s->pending_interface_addr) < 0) {
2169 wpa_msg(group_wpa_s, MSG_INFO,
2170 "Failed to set random MAC address");
2171 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 0);
2172 return NULL;
2173 }
2174
2175 if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
2176 wpa_msg(group_wpa_s, MSG_INFO,
2177 "Could not update MAC address information");
2178 wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 0);
2179 return NULL;
2180 }
2181
2182 wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR " for the group",
2183 MAC2STR(wpa_s->pending_interface_addr));
2184 }
2185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002186 return group_wpa_s;
2187}
2188
2189
2190static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2191 void *timeout_ctx)
2192{
2193 struct wpa_supplicant *wpa_s = eloop_ctx;
2194 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002195 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002196}
2197
2198
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002199static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2200 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002201{
2202 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002203 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 if (wpa_s->global->p2p)
2205 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002206 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207}
2208
2209
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002210static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2211{
2212 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2213 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002214 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002215 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002216 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002217 wpa_s->global->p2p_fail_on_wps_complete = 0;
2218}
2219
2220
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002221void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2222{
2223 if (wpa_s->global->p2p_group_formation != wpa_s)
2224 return;
2225 /* Speed up group formation timeout since this cannot succeed */
2226 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002227 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002228 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002229 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002230}
2231
2232
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002233static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234{
2235 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002236 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237
2238 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2239 wpa_drv_cancel_remain_on_channel(wpa_s);
2240 wpa_s->off_channel_freq = 0;
2241 wpa_s->roc_waiting_drv_freq = 0;
2242 }
2243
2244 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002245 wpa_msg_global(wpa_s, MSG_INFO,
2246 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2247 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002248 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 wpas_p2p_remove_pending_group_interface(wpa_s);
2250 return;
2251 }
2252
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002253 if (!res->role_go) {
2254 /* Inform driver of the operating channel of GO. */
2255 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2256 }
2257
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002258 if (wpa_s->p2p_go_ht40)
2259 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002260 if (wpa_s->p2p_go_vht)
2261 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002262 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2263 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002264
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002265 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2266 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2267 " wps_method=%s",
2268 res->role_go ? "GO" : "client", res->freq, res->ht40,
2269 MAC2STR(res->peer_device_addr),
2270 MAC2STR(res->peer_interface_addr),
2271 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002272 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273
Dmitry Shmidt04949592012-07-19 12:16:46 -07002274 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2275 struct wpa_ssid *ssid;
2276 ssid = wpa_config_get_network(wpa_s->conf,
2277 wpa_s->p2p_persistent_id);
2278 if (ssid && ssid->disabled == 2 &&
2279 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2280 size_t len = os_strlen(ssid->passphrase);
2281 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2282 "on requested persistent group");
2283 os_memcpy(res->passphrase, ssid->passphrase, len);
2284 res->passphrase[len] = '\0';
2285 }
2286 }
2287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002289 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2291 if (group_wpa_s == NULL) {
2292 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002293 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2294 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002295 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 return;
2297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2299 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002301 group_wpa_s = wpa_s->parent;
2302 wpa_s->global->p2p_group_formation = group_wpa_s;
2303 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002304 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002305 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002307 group_wpa_s->p2p_in_provisioning = 1;
2308 group_wpa_s->p2pdev = wpa_s;
2309 if (group_wpa_s != wpa_s) {
2310 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2311 sizeof(group_wpa_s->p2p_pin));
2312 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2313 }
2314 if (res->role_go) {
2315 wpas_start_wps_go(group_wpa_s, res, 1);
2316 } else {
2317 os_get_reltime(&group_wpa_s->scan_min_time);
2318 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 }
2320
2321 wpa_s->p2p_long_listen = 0;
2322 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2323
2324 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2325 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2326 (res->peer_config_timeout % 100) * 10000,
2327 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2328}
2329
2330
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002331static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2332 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333{
2334 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002335 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002336 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2337 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002339 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340}
2341
2342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002343static void wpas_dev_found(void *ctx, const u8 *addr,
2344 const struct p2p_peer_info *info,
2345 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002347 u8 *wfd_dev_info = NULL;
2348 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002349#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 struct wpa_supplicant *wpa_s = ctx;
2351 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002352 char *wfd_dev_info_hex = NULL;
2353
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002354#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002355 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2356 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002357 if (wfd_dev_info_hex) {
2358 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2359 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2360 // Only used for notification, so not handling error.
2361 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2362 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002363#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002364
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002365 if (info->p2ps_instance) {
2366 char str[256];
2367 const u8 *buf = wpabuf_head(info->p2ps_instance);
2368 size_t len = wpabuf_len(info->p2ps_instance);
2369
2370 while (len) {
2371 u32 id;
2372 u16 methods;
2373 u8 str_len;
2374
2375 if (len < 4 + 2 + 1)
2376 break;
2377 id = WPA_GET_LE32(buf);
2378 buf += sizeof(u32);
2379 methods = WPA_GET_BE16(buf);
2380 buf += sizeof(u16);
2381 str_len = *buf++;
2382 if (str_len > len - 4 - 2 - 1)
2383 break;
2384 os_memcpy(str, buf, str_len);
2385 str[str_len] = '\0';
2386 buf += str_len;
2387 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2388
2389 wpa_msg_global(wpa_s, MSG_INFO,
2390 P2P_EVENT_DEVICE_FOUND MACSTR
2391 " p2p_dev_addr=" MACSTR
2392 " pri_dev_type=%s name='%s'"
2393 " config_methods=0x%x"
2394 " dev_capab=0x%x"
2395 " group_capab=0x%x"
2396 " adv_id=%x asp_svc=%s%s",
2397 MAC2STR(addr),
2398 MAC2STR(info->p2p_device_addr),
2399 wps_dev_type_bin2str(
2400 info->pri_dev_type,
2401 devtype, sizeof(devtype)),
2402 info->device_name, methods,
2403 info->dev_capab, info->group_capab,
2404 id, str,
2405 info->vendor_elems ?
2406 " vendor_elems=1" : "");
2407 }
2408 goto done;
2409 }
2410
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002411 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2412 " p2p_dev_addr=" MACSTR
2413 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002414 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002415 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2416 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2417 sizeof(devtype)),
2418 info->device_name, info->config_methods,
2419 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002420 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002421 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002422 info->vendor_elems ? " vendor_elems=1" : "",
2423 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002425done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002426 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002427#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002428
Roshan Piusfd2fd662017-01-23 13:41:57 -08002429 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2430 wfd_dev_info_len, new_device);
2431 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432}
2433
2434
2435static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2436{
2437 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002438
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2440 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2443}
2444
2445
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002446static void wpas_find_stopped(void *ctx)
2447{
2448 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002449
2450 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2451 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2452
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002453 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002454 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002455}
2456
2457
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002458struct wpas_p2p_listen_work {
2459 unsigned int freq;
2460 unsigned int duration;
2461 struct wpabuf *probe_resp_ie;
2462};
2463
2464
2465static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2466{
2467 if (lwork == NULL)
2468 return;
2469 wpabuf_free(lwork->probe_resp_ie);
2470 os_free(lwork);
2471}
2472
2473
2474static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2475{
2476 struct wpas_p2p_listen_work *lwork;
2477
2478 if (!wpa_s->p2p_listen_work)
2479 return;
2480
2481 lwork = wpa_s->p2p_listen_work->ctx;
2482 wpas_p2p_listen_work_free(lwork);
2483 radio_work_done(wpa_s->p2p_listen_work);
2484 wpa_s->p2p_listen_work = NULL;
2485}
2486
2487
2488static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2489{
2490 struct wpa_supplicant *wpa_s = work->wpa_s;
2491 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002492 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002493
2494 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002495 if (work->started) {
2496 wpa_s->p2p_listen_work = NULL;
2497 wpas_stop_listen(wpa_s);
2498 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002499 wpas_p2p_listen_work_free(lwork);
2500 return;
2501 }
2502
2503 wpa_s->p2p_listen_work = work;
2504
2505 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2506
2507 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2508 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2509 "report received Probe Request frames");
2510 wpas_p2p_listen_work_done(wpa_s);
2511 return;
2512 }
2513
2514 wpa_s->pending_listen_freq = lwork->freq;
2515 wpa_s->pending_listen_duration = lwork->duration;
2516
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002517 duration = lwork->duration;
2518#ifdef CONFIG_TESTING_OPTIONS
2519 if (wpa_s->extra_roc_dur) {
2520 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2521 duration, duration + wpa_s->extra_roc_dur);
2522 duration += wpa_s->extra_roc_dur;
2523 }
2524#endif /* CONFIG_TESTING_OPTIONS */
2525
2526 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002527 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2528 "to remain on channel (%u MHz) for Listen "
2529 "state", lwork->freq);
2530 wpas_p2p_listen_work_done(wpa_s);
2531 wpa_s->pending_listen_freq = 0;
2532 return;
2533 }
2534 wpa_s->off_channel_freq = 0;
2535 wpa_s->roc_waiting_drv_freq = lwork->freq;
2536}
2537
2538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539static int wpas_start_listen(void *ctx, unsigned int freq,
2540 unsigned int duration,
2541 const struct wpabuf *probe_resp_ie)
2542{
2543 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002544 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002546 if (wpa_s->p2p_listen_work) {
2547 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 return -1;
2549 }
2550
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002551 lwork = os_zalloc(sizeof(*lwork));
2552 if (lwork == NULL)
2553 return -1;
2554 lwork->freq = freq;
2555 lwork->duration = duration;
2556 if (probe_resp_ie) {
2557 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2558 if (lwork->probe_resp_ie == NULL) {
2559 wpas_p2p_listen_work_free(lwork);
2560 return -1;
2561 }
2562 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002564 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2565 lwork) < 0) {
2566 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567 return -1;
2568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569
2570 return 0;
2571}
2572
2573
2574static void wpas_stop_listen(void *ctx)
2575{
2576 struct wpa_supplicant *wpa_s = ctx;
2577 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2578 wpa_drv_cancel_remain_on_channel(wpa_s);
2579 wpa_s->off_channel_freq = 0;
2580 wpa_s->roc_waiting_drv_freq = 0;
2581 }
2582 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002583
2584 /*
2585 * Don't cancel Probe Request RX reporting for a connected P2P Client
2586 * handling Probe Request frames.
2587 */
2588 if (!wpa_s->p2p_cli_probe)
2589 wpa_drv_probe_req_report(wpa_s, 0);
2590
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002591 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592}
2593
2594
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002595static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2596 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597{
2598 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002599 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2600 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601}
2602
2603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2605 const u8 *peer, const char *params,
2606 unsigned int generated_pin)
2607{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002608 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2609 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610}
2611
2612
2613static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2614 const u8 *peer, const char *params)
2615{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002616 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2617 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618}
2619
2620
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002621static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2622 const u8 *dev_addr, const u8 *pri_dev_type,
2623 const char *dev_name, u16 supp_config_methods,
2624 u8 dev_capab, u8 group_capab, const u8 *group_id,
2625 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626{
2627 struct wpa_supplicant *wpa_s = ctx;
2628 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002629 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002630 u8 empty_dev_type[8];
2631 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002632 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002633 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002634
2635 if (group_id) {
2636 for (group = wpa_s->global->ifaces; group; group = group->next)
2637 {
2638 struct wpa_ssid *s = group->current_ssid;
2639 if (s != NULL &&
2640 s->mode == WPAS_MODE_P2P_GO &&
2641 group_id_len - ETH_ALEN == s->ssid_len &&
2642 os_memcmp(group_id + ETH_ALEN, s->ssid,
2643 s->ssid_len) == 0)
2644 break;
2645 }
2646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002647
2648 if (pri_dev_type == NULL) {
2649 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2650 pri_dev_type = empty_dev_type;
2651 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002652 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2653 " pri_dev_type=%s name='%s' config_methods=0x%x "
2654 "dev_capab=0x%x group_capab=0x%x%s%s",
2655 MAC2STR(dev_addr),
2656 wps_dev_type_bin2str(pri_dev_type, devtype,
2657 sizeof(devtype)),
2658 dev_name, supp_config_methods, dev_capab, group_capab,
2659 group ? " group=" : "",
2660 group ? group->ifname : "");
2661 if (os_snprintf_error(sizeof(params), res))
2662 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002665 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002666 if (wps_generate_pin(&generated_pin) < 0) {
2667 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2668 wpas_notify_p2p_provision_discovery(
2669 wpa_s, peer, 0 /* response */,
2670 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2671 return;
2672 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 wpas_prov_disc_local_display(wpa_s, peer, params,
2674 generated_pin);
2675 } else if (config_methods & WPS_CONFIG_KEYPAD)
2676 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2677 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002678 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2679 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002680
2681 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2682 P2P_PROV_DISC_SUCCESS,
2683 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684}
2685
2686
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002687static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002688{
2689 struct wpa_supplicant *wpa_s = ctx;
2690 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002691 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002693 if (wpa_s->pending_pd_before_join &&
2694 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2695 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2696 wpa_s->pending_pd_before_join = 0;
2697 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2698 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002699 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002700 return;
2701 }
2702
Dmitry Shmidt04949592012-07-19 12:16:46 -07002703 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002704 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2705 int res;
2706
2707 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2708 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2709 if (os_snprintf_error(sizeof(params), res))
2710 params[sizeof(params) - 1] = '\0';
2711 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712 params[0] = '\0';
2713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002715 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002716 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002717 if (wps_generate_pin(&generated_pin) < 0) {
2718 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2719 wpas_notify_p2p_provision_discovery(
2720 wpa_s, peer, 0 /* response */,
2721 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2722 return;
2723 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002724 wpas_prov_disc_local_display(wpa_s, peer, params,
2725 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002727 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2728 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729
Jouni Malinen75ecf522011-06-27 15:19:46 -07002730 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2731 P2P_PROV_DISC_SUCCESS,
2732 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733}
2734
2735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002737 enum p2p_prov_disc_status status,
2738 u32 adv_id, const u8 *adv_mac,
2739 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002740{
2741 struct wpa_supplicant *wpa_s = ctx;
2742
Dmitry Shmidt04949592012-07-19 12:16:46 -07002743 if (wpa_s->p2p_fallback_to_go_neg) {
2744 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2745 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002746 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002747 P2P_EVENT_FALLBACK_TO_GO_NEG
2748 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002749 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2750 return;
2751 }
2752
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002753 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002754 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002755 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2756 "join-existing-group operation (no ACK for PD "
2757 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002758 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002759 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002760 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002761
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002762 if (adv_id && adv_mac && deferred_session_resp) {
2763 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2764 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2765 " deferred_session_resp='%s'",
2766 MAC2STR(peer), status, adv_id,
2767 deferred_session_resp);
2768 } else if (adv_id && adv_mac) {
2769 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2770 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2771 MAC2STR(peer), status, adv_id);
2772 } else {
2773 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2774 " p2p_dev_addr=" MACSTR " status=%d",
2775 MAC2STR(peer), status);
2776 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002777
Jouni Malinen75ecf522011-06-27 15:19:46 -07002778 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2779 status, 0, 0);
2780}
2781
2782
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002783static int freq_included(struct wpa_supplicant *wpa_s,
2784 const struct p2p_channels *channels,
2785 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002786{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002787 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2788 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2789 return 1;
2790 return 0;
2791}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002792
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002793
2794static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2795{
2796 unsigned int num = P2P_MAX_CHANNELS;
2797 int *common_freqs;
2798 int ret;
2799
2800 p2p_go_dump_common_freqs(wpa_s);
2801 common_freqs = os_calloc(num, sizeof(int));
2802 if (!common_freqs)
2803 return;
2804
2805 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2806 if (ret < 0) {
2807 wpa_dbg(wpa_s, MSG_DEBUG,
2808 "P2P: Failed to get group common freqs");
2809 os_free(common_freqs);
2810 return;
2811 }
2812
2813 os_free(wpa_s->p2p_group_common_freqs);
2814 wpa_s->p2p_group_common_freqs = common_freqs;
2815 wpa_s->p2p_group_common_freqs_num = num;
2816 p2p_go_dump_common_freqs(wpa_s);
2817}
2818
2819
2820/*
2821 * Check if the given frequency is one of the possible operating frequencies
2822 * set after the completion of the GO Negotiation.
2823 */
2824static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2825{
2826 unsigned int i;
2827
2828 p2p_go_dump_common_freqs(wpa_s);
2829
2830 /* assume no restrictions */
2831 if (!wpa_s->p2p_group_common_freqs_num)
2832 return 1;
2833
2834 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2835 if (wpa_s->p2p_group_common_freqs[i] == freq)
2836 return 1;
2837 }
2838 return 0;
2839}
2840
2841
2842static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2843 struct sta_info *sta, void *ctx)
2844{
2845 int *ecsa_support = ctx;
2846
2847 *ecsa_support &= sta->ecsa_supported;
2848
2849 return 0;
2850}
2851
2852
2853/* Check if all the peers support eCSA */
2854static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2855{
2856 int ecsa_support = 1;
2857
2858 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2859 &ecsa_support);
2860
2861 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002862}
2863
2864
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002865/**
2866 * Pick the best frequency to use from all the currently used frequencies.
2867 */
2868static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2869 struct wpa_used_freq_data *freqs,
2870 unsigned int num)
2871{
2872 unsigned int i, c;
2873
2874 /* find a candidate freq that is supported by P2P */
2875 for (c = 0; c < num; c++)
2876 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2877 break;
2878
2879 if (c == num)
2880 return 0;
2881
2882 /* once we have a candidate, try to find a 'better' one */
2883 for (i = c + 1; i < num; i++) {
2884 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2885 continue;
2886
2887 /*
2888 * 1. Infrastructure station interfaces have higher preference.
2889 * 2. P2P Clients have higher preference.
2890 * 3. All others.
2891 */
2892 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2893 c = i;
2894 break;
2895 }
2896
2897 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2898 c = i;
2899 }
2900 return freqs[c].freq;
2901}
2902
2903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2905 const u8 *go_dev_addr, const u8 *ssid,
2906 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002907 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002908 const struct p2p_channels *channels,
2909 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910{
2911 struct wpa_supplicant *wpa_s = ctx;
2912 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002913 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002915 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916
2917 if (!persistent_group) {
2918 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002919 " to join an active group (SSID: %s)",
2920 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2922 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2923 == 0 ||
2924 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2925 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2926 "authorized invitation");
2927 goto accept_inv;
2928 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002929
2930#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002931 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2932 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002933 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002934 wpa_s->p2p_wps_method = WPS_NFC;
2935 wpa_s->pending_join_wps_method = WPS_NFC;
2936 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002937 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002938 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002939 bssid, ETH_ALEN);
2940 goto accept_inv;
2941 }
2942#endif /* CONFIG_WPS_NFC */
2943
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944 /*
2945 * Do not accept the invitation automatically; notify user and
2946 * request approval.
2947 */
2948 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2949 }
2950
2951 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2952 if (grp) {
2953 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2954 "running persistent group");
2955 if (*go)
2956 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2957 goto accept_inv;
2958 }
2959
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002960 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2961 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2962 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2963 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002964 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002965 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2967
2968 for (s = wpa_s->conf->ssid; s; s = s->next) {
2969 if (s->disabled == 2 &&
2970 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2971 s->ssid_len == ssid_len &&
2972 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2973 break;
2974 }
2975
2976 if (!s) {
2977 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2978 " requested reinvocation of an unknown group",
2979 MAC2STR(sa));
2980 return P2P_SC_FAIL_UNKNOWN_GROUP;
2981 }
2982
2983 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2984 *go = 1;
2985 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2986 wpa_printf(MSG_DEBUG, "P2P: The only available "
2987 "interface is already in use - reject "
2988 "invitation");
2989 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2990 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002991 if (wpa_s->p2p_mgmt)
2992 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2993 ETH_ALEN);
2994 else
2995 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996 } else if (s->mode == WPAS_MODE_P2P_GO) {
2997 *go = 1;
2998 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2999 {
3000 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3001 "interface address for the group");
3002 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3003 }
3004 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3005 ETH_ALEN);
3006 }
3007
3008accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003009 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3010
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003011 best_freq = 0;
3012 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3013 sizeof(struct wpa_used_freq_data));
3014 if (freqs) {
3015 int num_channels = wpa_s->num_multichan_concurrent;
3016 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3017 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3018 os_free(freqs);
3019 }
3020
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003021 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003022 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003023 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003024 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003025
3026 if (wpa_s->num_multichan_concurrent < 2 ||
3027 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3028 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 -07003029 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003030 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031 }
3032
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003033 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3034 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003035 if (*go == 0) {
3036 /* We are the client */
3037 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3038 "running a GO but we are capable of MCC, "
3039 "figure out the best channel to use");
3040 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003041 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003042 /* We are the GO, and *force_freq is not in the
3043 * intersection */
3044 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3045 "in intersection but we are capable of MCC, "
3046 "figure out the best channel to use",
3047 *force_freq);
3048 *force_freq = 0;
3049 }
3050 }
3051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 return P2P_SC_SUCCESS;
3053}
3054
3055
3056static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3057 const u8 *ssid, size_t ssid_len,
3058 const u8 *go_dev_addr, u8 status,
3059 int op_freq)
3060{
3061 struct wpa_supplicant *wpa_s = ctx;
3062 struct wpa_ssid *s;
3063
3064 for (s = wpa_s->conf->ssid; s; s = s->next) {
3065 if (s->disabled == 2 &&
3066 s->ssid_len == ssid_len &&
3067 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3068 break;
3069 }
3070
3071 if (status == P2P_SC_SUCCESS) {
3072 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003073 " was accepted; op_freq=%d MHz, SSID=%s",
3074 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003076 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003077 if (go) {
3078 wpa_msg_global(wpa_s, MSG_INFO,
3079 P2P_EVENT_INVITATION_ACCEPTED
3080 "sa=" MACSTR
3081 " persistent=%d freq=%d",
3082 MAC2STR(sa), s->id, op_freq);
3083 } else {
3084 wpa_msg_global(wpa_s, MSG_INFO,
3085 P2P_EVENT_INVITATION_ACCEPTED
3086 "sa=" MACSTR
3087 " persistent=%d",
3088 MAC2STR(sa), s->id);
3089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003091 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003092 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3093 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003095 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003096 wpa_msg_global(wpa_s, MSG_INFO,
3097 P2P_EVENT_INVITATION_ACCEPTED
3098 "sa=" MACSTR " go_dev_addr=" MACSTR
3099 " bssid=" MACSTR " unknown-network",
3100 MAC2STR(sa), MAC2STR(go_dev_addr),
3101 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003103 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003104 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 }
3106 return;
3107 }
3108
3109 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3110 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3111 " was rejected (status %u)", MAC2STR(sa), status);
3112 return;
3113 }
3114
3115 if (!s) {
3116 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003117 wpa_msg_global(wpa_s, MSG_INFO,
3118 P2P_EVENT_INVITATION_RECEIVED
3119 "sa=" MACSTR " go_dev_addr=" MACSTR
3120 " bssid=" MACSTR " unknown-network",
3121 MAC2STR(sa), MAC2STR(go_dev_addr),
3122 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003124 wpa_msg_global(wpa_s, MSG_INFO,
3125 P2P_EVENT_INVITATION_RECEIVED
3126 "sa=" MACSTR " go_dev_addr=" MACSTR
3127 " unknown-network",
3128 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003130 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3131 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 return;
3133 }
3134
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003135 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003136 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3137 "sa=" MACSTR " persistent=%d freq=%d",
3138 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003139 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003140 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3141 "sa=" MACSTR " persistent=%d",
3142 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003143 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003144 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3145 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146}
3147
3148
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003149static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3150 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003151 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003152{
3153 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003154 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003155
3156 if (ssid == NULL)
3157 return;
3158
3159 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003160 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003161 ETH_ALEN) == 0)
3162 break;
3163 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003164 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003165 if (ssid->mode != WPAS_MODE_P2P_GO &&
3166 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3167 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3168 "due to invitation result", ssid->id);
3169 wpas_notify_network_removed(wpa_s, ssid);
3170 wpa_config_remove_network(wpa_s->conf, ssid->id);
3171 return;
3172 }
3173 return; /* Peer not found in client list */
3174 }
3175
3176 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003177 "group %d client list%s",
3178 MAC2STR(peer), ssid->id,
3179 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003180 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3181 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3182 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003183 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003184 if (p2p_wpa_s->conf->update_config &&
3185 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003186 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003187}
3188
3189
3190static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3191 const u8 *peer)
3192{
3193 struct wpa_ssid *ssid;
3194
3195 wpa_s = wpa_s->global->p2p_invite_group;
3196 if (wpa_s == NULL)
3197 return; /* No known invitation group */
3198 ssid = wpa_s->current_ssid;
3199 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3200 !ssid->p2p_persistent_group)
3201 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003202 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003203 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003204 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003205}
3206
3207
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003208static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003209 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003210 const u8 *peer, int neg_freq,
3211 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212{
3213 struct wpa_supplicant *wpa_s = ctx;
3214 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003215 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216
3217 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003218 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3219 "status=%d " MACSTR,
3220 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003222 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3223 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 }
3225 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3226
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003227 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3228 status, MAC2STR(peer));
3229 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003230 struct wpa_supplicant *group_if =
3231 wpa_s->global->p2p_invite_group;
3232
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003233 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3234 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003235
3236 /*
3237 * Invitation to an active group. If this is successful and we
3238 * are the GO, set the client wait to postpone some concurrent
3239 * operations and to allow provisioning and connection to happen
3240 * more quickly.
3241 */
3242 if (status == P2P_SC_SUCCESS &&
3243 group_if && group_if->current_ssid &&
3244 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3245 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3246#ifdef CONFIG_TESTING_OPTIONS
3247 if (group_if->p2p_go_csa_on_inv) {
3248 wpa_printf(MSG_DEBUG,
3249 "Testing: force P2P GO CSA after invitation");
3250 eloop_cancel_timeout(
3251 wpas_p2p_reconsider_moving_go,
3252 wpa_s, NULL);
3253 eloop_register_timeout(
3254 0, 50000,
3255 wpas_p2p_reconsider_moving_go,
3256 wpa_s, NULL);
3257 }
3258#endif /* CONFIG_TESTING_OPTIONS */
3259 }
3260 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003261 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003263 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3264 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3265 "invitation exchange to indicate readiness for "
3266 "re-invocation");
3267 }
3268
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003270 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3271 ssid = wpa_config_get_network(
3272 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003273 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 wpas_p2p_remove_pending_group_interface(wpa_s);
3276 return;
3277 }
3278
3279 ssid = wpa_config_get_network(wpa_s->conf,
3280 wpa_s->pending_invite_ssid_id);
3281 if (ssid == NULL) {
3282 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3283 "data matching with invitation");
3284 return;
3285 }
3286
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003287 /*
3288 * The peer could have missed our ctrl::ack frame for Invitation
3289 * Response and continue retransmitting the frame. To reduce the
3290 * likelihood of the peer not getting successful TX status for the
3291 * Invitation Response frame, wait a short time here before starting
3292 * the persistent group so that we will remain on the current channel to
3293 * acknowledge any possible retransmission from the peer.
3294 */
3295 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3296 "starting persistent group");
3297 os_sleep(0, 50000);
3298
Dmitry Shmidt15907092014-03-25 10:42:57 -07003299 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003300 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003301 freq = neg_freq;
3302 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003303 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003304 freq = peer_oper_freq;
3305 else
3306 freq = 0;
3307
3308 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3309 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003311 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003312 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003313 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003314 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003315 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003316 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003317 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003318 ssid->mode == WPAS_MODE_P2P_GO ?
3319 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003320 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321}
3322
3323
Dmitry Shmidt04949592012-07-19 12:16:46 -07003324static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3325 unsigned int freq)
3326{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003327 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3328 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003329 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003330}
3331
3332
3333static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3334{
3335 reg->channel[reg->channels] = chan;
3336 reg->channels++;
3337}
3338
3339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003341 struct p2p_channels *chan,
3342 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343{
3344 int i, cla = 0;
3345
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003346 wpa_s->global->p2p_24ghz_social_channels = 1;
3347
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003348 os_memset(cli_chan, 0, sizeof(*cli_chan));
3349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3351 "band");
3352
3353 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3354 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003355 chan->reg_class[cla].channels = 0;
3356 for (i = 0; i < 11; i++) {
3357 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3358 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3359 }
3360 if (chan->reg_class[cla].channels)
3361 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362
3363 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3364 "band");
3365
3366 /* Operating class 115 - 5 GHz, channels 36-48 */
3367 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003368 chan->reg_class[cla].channels = 0;
3369 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3370 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3371 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3372 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3373 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3374 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3375 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3376 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3377 if (chan->reg_class[cla].channels)
3378 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003379
3380 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3381 "band");
3382
3383 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3384 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003385 chan->reg_class[cla].channels = 0;
3386 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3387 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3388 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3389 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3390 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3391 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3392 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3393 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3394 if (chan->reg_class[cla].channels)
3395 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396
3397 chan->reg_classes = cla;
3398 return 0;
3399}
3400
3401
Dmitry Shmidt04949592012-07-19 12:16:46 -07003402static int has_channel(struct wpa_global *global,
3403 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003404{
3405 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003406 unsigned int freq;
3407
3408 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3409 chan * 5;
3410 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003411 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412
3413 for (i = 0; i < mode->num_channels; i++) {
3414 if (mode->channels[i].chan == chan) {
3415 if (flags)
3416 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003417 if (mode->channels[i].flag &
3418 (HOSTAPD_CHAN_DISABLED |
3419 HOSTAPD_CHAN_RADAR))
3420 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003421 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3422 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003423 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424 }
3425 }
3426
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003427 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003428}
3429
3430
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003431static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3432 struct hostapd_hw_modes *mode,
3433 u8 channel)
3434{
3435 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003436 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003437
3438 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3439 return 0;
3440
3441 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3442 /*
3443 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3444 * so the center channel is 6 channels away from the start/end.
3445 */
3446 if (channel >= center_channels[i] - 6 &&
3447 channel <= center_channels[i] + 6)
3448 return center_channels[i];
3449
3450 return 0;
3451}
3452
3453
3454static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3455 struct hostapd_hw_modes *mode,
3456 u8 channel, u8 bw)
3457{
3458 u8 center_chan;
3459 int i, flags;
3460 enum chan_allowed res, ret = ALLOWED;
3461
3462 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3463 if (!center_chan)
3464 return NOT_ALLOWED;
3465 if (center_chan >= 58 && center_chan <= 138)
3466 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3467
3468 /* check all the channels are available */
3469 for (i = 0; i < 4; i++) {
3470 int adj_chan = center_chan - 6 + i * 4;
3471
3472 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3473 if (res == NOT_ALLOWED)
3474 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003475 if (res == NO_IR)
3476 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003477
3478 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3479 return NOT_ALLOWED;
3480 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3481 return NOT_ALLOWED;
3482 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3483 return NOT_ALLOWED;
3484 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3485 return NOT_ALLOWED;
3486 }
3487
3488 return ret;
3489}
3490
3491
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003492static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3493 struct hostapd_hw_modes *mode,
3494 u8 channel)
3495{
3496 u8 center_channels[] = { 50, 114 };
3497 unsigned int i;
3498
3499 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3500 return 0;
3501
3502 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3503 /*
3504 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3505 * so the center channel is 14 channels away from the start/end.
3506 */
3507 if (channel >= center_channels[i] - 14 &&
3508 channel <= center_channels[i] + 14)
3509 return center_channels[i];
3510
3511 return 0;
3512}
3513
3514
3515static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3516 struct hostapd_hw_modes *mode,
3517 u8 channel, u8 bw)
3518{
3519 u8 center_chan;
3520 int i, flags;
3521 enum chan_allowed res, ret = ALLOWED;
3522
3523 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3524 if (!center_chan)
3525 return NOT_ALLOWED;
3526 /* VHT 160 MHz uses DFS channels in most countries. */
3527
3528 /* Check all the channels are available */
3529 for (i = 0; i < 8; i++) {
3530 int adj_chan = center_chan - 14 + i * 4;
3531
3532 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3533 if (res == NOT_ALLOWED)
3534 return NOT_ALLOWED;
3535
3536 if (res == NO_IR)
3537 ret = NO_IR;
3538
3539 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3540 return NOT_ALLOWED;
3541 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3542 return NOT_ALLOWED;
3543 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3544 return NOT_ALLOWED;
3545 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3546 return NOT_ALLOWED;
3547 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3548 return NOT_ALLOWED;
3549 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3550 return NOT_ALLOWED;
3551 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3552 return NOT_ALLOWED;
3553 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3554 return NOT_ALLOWED;
3555 }
3556
3557 return ret;
3558}
3559
3560
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003561static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3562 struct hostapd_hw_modes *mode,
3563 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003564{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003565 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003566 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003567
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003568 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3569 if (bw == BW40MINUS) {
3570 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3571 return NOT_ALLOWED;
3572 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3573 } else if (bw == BW40PLUS) {
3574 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3575 return NOT_ALLOWED;
3576 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3577 } else if (bw == BW80) {
3578 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003579 } else if (bw == BW160) {
3580 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003581 }
3582
3583 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3584 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003585 if (res == NO_IR || res2 == NO_IR)
3586 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003587 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003588}
3589
3590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003592 struct p2p_channels *chan,
3593 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003595 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003596 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003598 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3600 "of all supported channels; assume dualband "
3601 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003602 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 }
3604
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003605 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003606
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003607 for (op = 0; global_op_class[op].op_class; op++) {
3608 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003610 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003612 if (o->p2p == NO_P2P_SUPP)
3613 continue;
3614
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003615 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 if (mode == NULL)
3617 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003618 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3619 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003621 enum chan_allowed res;
3622 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3623 if (res == ALLOWED) {
3624 if (reg == NULL) {
3625 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3626 o->op_class);
3627 reg = &chan->reg_class[cla];
3628 cla++;
3629 reg->reg_class = o->op_class;
3630 }
3631 reg->channel[reg->channels] = ch;
3632 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003633 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003634 wpa_s->conf->p2p_add_cli_chan) {
3635 if (cli_reg == NULL) {
3636 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3637 o->op_class);
3638 cli_reg = &cli_chan->reg_class[cli_cla];
3639 cli_cla++;
3640 cli_reg->reg_class = o->op_class;
3641 }
3642 cli_reg->channel[cli_reg->channels] = ch;
3643 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645 }
3646 if (reg) {
3647 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3648 reg->channel, reg->channels);
3649 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003650 if (cli_reg) {
3651 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3652 cli_reg->channel, cli_reg->channels);
3653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654 }
3655
3656 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003657 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 return 0;
3660}
3661
3662
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003663int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3664 struct hostapd_hw_modes *mode, u8 channel)
3665{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003666 int op;
3667 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003668
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003669 for (op = 0; global_op_class[op].op_class; op++) {
3670 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003671 u8 ch;
3672
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003673 if (o->p2p == NO_P2P_SUPP)
3674 continue;
3675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003676 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3677 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003678 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3679 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003680 continue;
3681 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003682 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003683 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003684 }
3685 }
3686 return 0;
3687}
3688
3689
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003690int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3691 struct hostapd_hw_modes *mode, u8 channel)
3692{
3693 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3694 return 0;
3695
3696 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3697}
3698
3699
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003700int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3701 struct hostapd_hw_modes *mode, u8 channel)
3702{
3703 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3704 return 0;
3705 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3706}
3707
3708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3710 size_t buf_len)
3711{
3712 struct wpa_supplicant *wpa_s = ctx;
3713
3714 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3715 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3716 break;
3717 }
3718 if (wpa_s == NULL)
3719 return -1;
3720
3721 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3722}
3723
3724
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003725struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3726 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003727{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003728 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3729 struct wpa_ssid *s = wpa_s->current_ssid;
3730 if (s == NULL)
3731 continue;
3732 if (s->mode != WPAS_MODE_P2P_GO &&
3733 s->mode != WPAS_MODE_AP &&
3734 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3735 continue;
3736 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003737 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003738 continue;
3739 return wpa_s;
3740 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003741
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003742 return NULL;
3743
3744}
3745
3746
3747struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3748 const u8 *peer_dev_addr)
3749{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003750 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3751 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003752 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003753 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003754 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3755 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003756 }
3757
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003758 return NULL;
3759}
3760
3761
3762static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3763{
3764 struct wpa_supplicant *wpa_s = ctx;
3765
3766 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003767}
3768
3769
Dmitry Shmidt18463232014-01-24 12:29:41 -08003770static int wpas_is_concurrent_session_active(void *ctx)
3771{
3772 struct wpa_supplicant *wpa_s = ctx;
3773 struct wpa_supplicant *ifs;
3774
3775 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3776 if (ifs == wpa_s)
3777 continue;
3778 if (ifs->wpa_state > WPA_ASSOCIATED)
3779 return 1;
3780 }
3781 return 0;
3782}
3783
3784
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003785static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3786{
3787 struct wpa_supplicant *wpa_s = ctx;
3788 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3789}
3790
3791
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003792int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3793 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003794{
3795 struct wpa_interface iface;
3796 struct wpa_supplicant *p2pdev_wpa_s;
3797 char ifname[100];
3798 char force_name[100];
3799 int ret;
3800
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003801 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3802 wpa_s->ifname);
3803 if (os_snprintf_error(sizeof(ifname), ret))
3804 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003805 force_name[0] = '\0';
3806 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3807 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3808 force_name, wpa_s->pending_interface_addr, NULL);
3809 if (ret < 0) {
3810 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3811 return ret;
3812 }
3813 os_strlcpy(wpa_s->pending_interface_name, ifname,
3814 sizeof(wpa_s->pending_interface_name));
3815
3816 os_memset(&iface, 0, sizeof(iface));
3817 iface.p2p_mgmt = 1;
3818 iface.ifname = wpa_s->pending_interface_name;
3819 iface.driver = wpa_s->driver->name;
3820 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003821
3822 /*
3823 * If a P2P Device configuration file was given, use it as the interface
3824 * configuration file (instead of using parent's configuration file.
3825 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003826 if (conf_p2p_dev) {
3827 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003828 iface.ctrl_interface = NULL;
3829 } else {
3830 iface.confname = wpa_s->confname;
3831 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3832 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003833
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003834 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003835 if (!p2pdev_wpa_s) {
3836 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3837 return -1;
3838 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003839
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003840 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003841 wpa_s->pending_interface_name[0] = '\0';
3842 return 0;
3843}
3844
3845
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003846static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3847 const u8 *noa, size_t noa_len)
3848{
3849 struct wpa_supplicant *wpa_s, *intf = ctx;
3850 char hex[100];
3851
3852 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3853 if (wpa_s->waiting_presence_resp)
3854 break;
3855 }
3856 if (!wpa_s) {
3857 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3858 return;
3859 }
3860 wpa_s->waiting_presence_resp = 0;
3861
3862 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3863 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3864 " status=%u noa=%s", MAC2STR(src), status, hex);
3865}
3866
3867
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003868static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3869 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003870 u8 *ret_ssid, size_t *ret_ssid_len,
3871 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003872{
3873 struct wpa_supplicant *wpa_s = ctx;
3874 struct wpa_ssid *s;
3875
3876 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3877 if (s) {
3878 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3879 *ret_ssid_len = s->ssid_len;
3880 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003881
3882 if (s->mode != WPAS_MODE_P2P_GO) {
3883 os_memset(intended_iface_addr, 0, ETH_ALEN);
3884 } else if (wpas_p2p_create_iface(wpa_s)) {
3885 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3886 return 0;
3887
3888 os_memcpy(intended_iface_addr,
3889 wpa_s->pending_interface_addr, ETH_ALEN);
3890 } else {
3891 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3892 ETH_ALEN);
3893 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003894 return 1;
3895 }
3896
3897 return 0;
3898}
3899
3900
3901static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003902 u8 *ssid, size_t *ssid_len, int *group_iface,
3903 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003904{
3905 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003906 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003907 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003908
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003909 /*
3910 * group_iface will be set to 1 only if a dedicated interface for P2P
3911 * role is required. First, we try to reuse an active GO. However,
3912 * if it is not present, we will try to reactivate an existing
3913 * persistent group and set group_iface to 1, so the caller will know
3914 * that the pending interface should be used.
3915 */
3916 *group_iface = 0;
3917
3918 if (freq)
3919 *freq = 0;
3920
3921 go = wpas_p2p_get_go_group(wpa_s);
3922 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003923 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003924 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003925 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003926 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3927 else
3928 return 0;
3929 } else {
3930 s = go->current_ssid;
3931 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3932 if (freq)
3933 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003934 }
3935
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003936 os_memcpy(ssid, s->ssid, s->ssid_len);
3937 *ssid_len = s->ssid_len;
3938
3939 return 1;
3940}
3941
3942
3943static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3944 const u8 *ssid, size_t ssid_len)
3945{
3946 struct wpa_supplicant *wpa_s = ctx;
3947 struct wpa_ssid *s;
3948 int save_config = 0;
3949 size_t i;
3950
3951 /* Start with our first choice of Persistent Groups */
3952 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3953 if (go && ssid && ssid_len &&
3954 s->ssid_len == ssid_len &&
3955 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3956 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3957 break;
3958
3959 /* Remove stale persistent group */
3960 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3961 wpa_config_remove_network(wpa_s->conf, s->id);
3962 save_config = 1;
3963 continue;
3964 }
3965
3966 for (i = 0; i < s->num_p2p_clients; i++) {
3967 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3968 peer, ETH_ALEN) != 0)
3969 continue;
3970
3971 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3972 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3973 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3974 break;
3975 }
3976 s->num_p2p_clients--;
3977 save_config = 1;
3978 }
3979
3980 if (save_config)
3981 p2p_config_write(wpa_s);
3982
3983 /* Return TRUE if valid SSID remains */
3984 return s != NULL;
3985}
3986
3987
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003988static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3989 const u8 *feat_cap, size_t feat_cap_len)
3990{
3991 static const char pref[] = " feature_cap=";
3992 int ret;
3993
3994 buf[0] = '\0';
3995
3996 /*
3997 * We expect a feature capability to contain at least one byte to be
3998 * reported. The string buffer provided by the caller function is
3999 * expected to be big enough to contain all bytes of the attribute for
4000 * known specifications. This function truncates the reported bytes if
4001 * the feature capability data exceeds the string buffer size.
4002 */
4003 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
4004 return;
4005
4006 os_memcpy(buf, pref, sizeof(pref));
4007 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
4008 buf_len - sizeof(pref) + 1,
4009 feat_cap, feat_cap_len);
4010
4011 if (ret != (2 * (int) feat_cap_len))
4012 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
4013}
4014
4015
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004016static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4017 const u8 *adv_mac, const u8 *ses_mac,
4018 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4019 u8 conncap, int passwd_id,
4020 const u8 *persist_ssid,
4021 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004022 int prov_start, const char *session_info,
4023 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004024 unsigned int freq,
4025 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004026{
4027 struct wpa_supplicant *wpa_s = ctx;
4028 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004029 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004030 int save_config = 0;
4031 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004032 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004033
4034 if (!dev)
4035 return;
4036
4037 os_memset(mac, 0, ETH_ALEN);
4038 if (!adv_mac)
4039 adv_mac = mac;
4040 if (!ses_mac)
4041 ses_mac = mac;
4042 if (!grp_mac)
4043 grp_mac = mac;
4044
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004045 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4046 feat_cap, feat_cap_len);
4047
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004048 if (prov_start) {
4049 if (session_info == NULL) {
4050 wpa_msg_global(wpa_s, MSG_INFO,
4051 P2P_EVENT_P2PS_PROVISION_START MACSTR
4052 " adv_id=%x conncap=%x"
4053 " adv_mac=" MACSTR
4054 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004055 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004056 MAC2STR(dev), adv_id, conncap,
4057 MAC2STR(adv_mac),
4058 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004059 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004060 } else {
4061 wpa_msg_global(wpa_s, MSG_INFO,
4062 P2P_EVENT_P2PS_PROVISION_START MACSTR
4063 " adv_id=%x conncap=%x"
4064 " adv_mac=" MACSTR
4065 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004066 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004067 MAC2STR(dev), adv_id, conncap,
4068 MAC2STR(adv_mac),
4069 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004070 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004071 }
4072 return;
4073 }
4074
4075 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4076 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4077
4078 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4079 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4080 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4081
4082 if (persistent_go && !persistent_go->num_p2p_clients) {
4083 /* remove empty persistent GO */
4084 wpa_config_remove_network(wpa_s->conf,
4085 persistent_go->id);
4086 }
4087
4088 wpa_msg_global(wpa_s, MSG_INFO,
4089 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4090 " status=%d"
4091 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004092 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004093 MAC2STR(dev), status,
4094 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004095 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004096 return;
4097 }
4098
4099 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004100 if (persist_ssid && persist_ssid_size)
4101 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4102 persist_ssid_size);
4103
4104 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4105 is_zero_ether_addr(grp_mac)) {
4106 wpa_dbg(wpa_s, MSG_ERROR,
4107 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4108 return;
4109 }
4110
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004111 for (;;) {
4112 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4113 if (!stale)
4114 break;
4115
4116 if (s && s->ssid_len == stale->ssid_len &&
4117 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4118 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4119 break;
4120
4121 /* Remove stale persistent group */
4122 if (stale->mode != WPAS_MODE_P2P_GO ||
4123 stale->num_p2p_clients <= 1) {
4124 wpa_config_remove_network(wpa_s->conf, stale->id);
4125 } else {
4126 size_t i;
4127
4128 for (i = 0; i < stale->num_p2p_clients; i++) {
4129 if (os_memcmp(stale->p2p_client_list +
4130 i * ETH_ALEN,
4131 dev, ETH_ALEN) == 0) {
4132 os_memmove(stale->p2p_client_list +
4133 i * ETH_ALEN,
4134 stale->p2p_client_list +
4135 (i + 1) * ETH_ALEN,
4136 (stale->num_p2p_clients -
4137 i - 1) * ETH_ALEN);
4138 break;
4139 }
4140 }
4141 stale->num_p2p_clients--;
4142 }
4143 save_config = 1;
4144 }
4145
4146 if (save_config)
4147 p2p_config_write(wpa_s);
4148
4149 if (s) {
4150 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4151 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4152
4153 if (persistent_go && s != persistent_go &&
4154 !persistent_go->num_p2p_clients) {
4155 /* remove empty persistent GO */
4156 wpa_config_remove_network(wpa_s->conf,
4157 persistent_go->id);
4158 /* Save config */
4159 }
4160
4161 wpa_msg_global(wpa_s, MSG_INFO,
4162 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4163 " status=%d"
4164 " adv_id=%x adv_mac=" MACSTR
4165 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004166 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004167 MAC2STR(dev), status,
4168 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004169 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004170 return;
4171 }
4172
4173 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004174 /*
4175 * We need to copy the interface name. Simply saving a
4176 * pointer isn't enough, since if we use pending_interface_name
4177 * it will be overwritten when the group is added.
4178 */
4179 char go_ifname[100];
4180
4181 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004182 if (!go_wpa_s) {
4183 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004184 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004186 if (!wpas_p2p_create_iface(wpa_s))
4187 os_memcpy(go_ifname, wpa_s->ifname,
4188 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004189 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004190 os_memcpy(go_ifname,
4191 wpa_s->pending_interface_name,
4192 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004193
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004194 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004195 wpas_p2ps_prov_complete(
4196 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4197 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004198 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004199 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4200 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004201 return;
4202 }
4203
4204 /* If PD Resp complete, start up the GO */
4205 if (response_done && persistent_go) {
4206 wpas_p2p_group_add_persistent(
4207 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004208 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004209 persistent_go->mode ==
4210 WPAS_MODE_P2P_GO ?
4211 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004212 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004213 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004214 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004215 }
4216
4217 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004218 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4219 ETH_ALEN);
4220 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004221 }
4222 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004223 os_memcpy(go_ifname, go_wpa_s->ifname,
4224 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004225
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004226 if (is_zero_ether_addr(grp_mac)) {
4227 wpa_dbg(go_wpa_s, MSG_DEBUG,
4228 "P2P: Setting PIN-1 for ANY");
4229 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4230 "12345670", NULL, 0,
4231 0);
4232 } else {
4233 wpa_dbg(go_wpa_s, MSG_DEBUG,
4234 "P2P: Setting PIN-1 for " MACSTR,
4235 MAC2STR(grp_mac));
4236 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4237 "12345670", NULL, 0,
4238 0);
4239 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004240
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004241 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4242 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004243 }
4244
4245 wpa_msg_global(wpa_s, MSG_INFO,
4246 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4247 " status=%d conncap=%x"
4248 " adv_id=%x adv_mac=" MACSTR
4249 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004250 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004251 MAC2STR(dev), status, conncap,
4252 adv_id, MAC2STR(adv_mac),
4253 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004254 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004255 return;
4256 }
4257
4258 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4259 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4260
4261 if (persistent_go && !persistent_go->num_p2p_clients) {
4262 /* remove empty persistent GO */
4263 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4264 }
4265
4266 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004267 char ssid_hex[32 * 2 + 1];
4268
4269 if (group_ssid)
4270 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4271 group_ssid, group_ssid_len);
4272 else
4273 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274 wpa_msg_global(wpa_s, MSG_INFO,
4275 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4276 " status=%d conncap=%x"
4277 " adv_id=%x adv_mac=" MACSTR
4278 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004279 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004280 MAC2STR(dev), status, conncap,
4281 adv_id, MAC2STR(adv_mac),
4282 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004283 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4284 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004285 } else {
4286 wpa_msg_global(wpa_s, MSG_INFO,
4287 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4288 " status=%d conncap=%x"
4289 " adv_id=%x adv_mac=" MACSTR
4290 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004291 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004292 MAC2STR(dev), status, conncap,
4293 adv_id, MAC2STR(adv_mac),
4294 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004295 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004296 }
4297}
4298
4299
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004300static int _wpas_p2p_in_progress(void *ctx)
4301{
4302 struct wpa_supplicant *wpa_s = ctx;
4303 return wpas_p2p_in_progress(wpa_s);
4304}
4305
4306
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004307static int wpas_prov_disc_resp_cb(void *ctx)
4308{
4309 struct wpa_supplicant *wpa_s = ctx;
4310 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004311 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004312
4313 if (!wpa_s->global->pending_p2ps_group)
4314 return 0;
4315
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004316 freq = wpa_s->global->pending_p2ps_group_freq;
4317 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004318 wpa_s->global->pending_p2ps_group = 0;
4319
4320 if (wpas_p2p_get_go_group(wpa_s))
4321 return 0;
4322 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4323
4324 if (persistent_go) {
4325 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004326 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004327 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004328 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004329 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004330 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004331 }
4332
4333 return 1;
4334}
4335
4336
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004337static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4338 unsigned int *len,
4339 unsigned int *freq_list)
4340{
4341 struct wpa_supplicant *wpa_s = ctx;
4342
4343 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4344 WPA_IF_P2P_CLIENT, len, freq_list);
4345}
4346
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004347int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
4348{
4349 u8 addr[ETH_ALEN] = {0};
4350
4351 if (wpa_s->conf->p2p_device_random_mac_addr == 0)
4352 return 0;
4353
4354 if (wpa_s->conf->ssid == NULL) {
4355 if (random_mac_addr(addr) < 0) {
4356 wpa_msg(wpa_s, MSG_INFO,
4357 "Failed to generate random MAC address");
4358 return -EINVAL;
4359 }
4360
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004361 /* Store generated MAC address. */
4362 os_memcpy(wpa_s->conf->p2p_device_persistent_mac_addr, addr,
4363 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004364 } else {
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004365 /* If there are existing saved groups, restore last MAC address.
4366 * if there is no last used MAC address, the last one is
4367 * factory MAC. */
4368 if (is_zero_ether_addr(
4369 wpa_s->conf->p2p_device_persistent_mac_addr))
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004370 return 0;
Hai Shalom39ba6fc2019-01-22 12:40:38 -08004371 os_memcpy(addr, wpa_s->conf->p2p_device_persistent_mac_addr,
4372 ETH_ALEN);
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004373 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4374 }
4375
4376 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4377 wpa_msg(wpa_s, MSG_INFO,
4378 "Failed to set random MAC address");
4379 return -EINVAL;
4380 }
4381
4382 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4383 wpa_msg(wpa_s, MSG_INFO,
4384 "Could not update MAC address information");
4385 return -EINVAL;
4386 }
4387
4388 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4389 MAC2STR(addr));
4390
4391 return 0;
4392}
4393
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004394
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395/**
4396 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4397 * @global: Pointer to global data from wpa_supplicant_init()
4398 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4399 * Returns: 0 on success, -1 on failure
4400 */
4401int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4402{
4403 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 int i;
4405
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004406 if (wpa_s->conf->p2p_disabled)
4407 return 0;
4408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4410 return 0;
4411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412 if (global->p2p)
4413 return 0;
4414
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004415 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4416 wpa_msg(wpa_s, MSG_ERROR,
4417 "Failed to initialize P2P random MAC address.");
4418 return -1;
4419 }
4420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004421 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004423 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 p2p.p2p_scan = wpas_p2p_scan;
4425 p2p.send_action = wpas_send_action;
4426 p2p.send_action_done = wpas_send_action_done;
4427 p2p.go_neg_completed = wpas_go_neg_completed;
4428 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4429 p2p.dev_found = wpas_dev_found;
4430 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004431 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 p2p.start_listen = wpas_start_listen;
4433 p2p.stop_listen = wpas_stop_listen;
4434 p2p.send_probe_resp = wpas_send_probe_resp;
4435 p2p.sd_request = wpas_sd_request;
4436 p2p.sd_response = wpas_sd_response;
4437 p2p.prov_disc_req = wpas_prov_disc_req;
4438 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004439 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440 p2p.invitation_process = wpas_invitation_process;
4441 p2p.invitation_received = wpas_invitation_received;
4442 p2p.invitation_result = wpas_invitation_result;
4443 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004444 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004445 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004446 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004447 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004448 p2p.get_persistent_group = wpas_get_persistent_group;
4449 p2p.get_go_info = wpas_get_go_info;
4450 p2p.remove_stale_groups = wpas_remove_stale_groups;
4451 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4452 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4453 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004454 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004455
4456 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004457 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 p2p.dev_name = wpa_s->conf->device_name;
4459 p2p.manufacturer = wpa_s->conf->manufacturer;
4460 p2p.model_name = wpa_s->conf->model_name;
4461 p2p.model_number = wpa_s->conf->model_number;
4462 p2p.serial_number = wpa_s->conf->serial_number;
4463 if (wpa_s->wps) {
4464 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4465 p2p.config_methods = wpa_s->wps->config_methods;
4466 }
4467
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004468 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4469 wpa_printf(MSG_ERROR,
4470 "P2P: Failed to configure supported channel list");
4471 return -1;
4472 }
4473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474 if (wpa_s->conf->p2p_listen_reg_class &&
4475 wpa_s->conf->p2p_listen_channel) {
4476 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4477 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004478 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 /*
4481 * Pick one of the social channels randomly as the listen
4482 * channel.
4483 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004484 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4485 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004486 wpa_printf(MSG_INFO,
4487 "P2P: No social channels supported by the driver - do not enable P2P");
4488 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004489 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004490 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004492 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4493 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494
4495 if (wpa_s->conf->p2p_oper_reg_class &&
4496 wpa_s->conf->p2p_oper_channel) {
4497 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4498 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4499 p2p.cfg_op_channel = 1;
4500 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4501 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4502
4503 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004504 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004505 * Use random operation channel from 2.4 GHz band social
4506 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4507 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004509 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4510 &p2p.op_channel) != 0) {
4511 wpa_printf(MSG_ERROR,
4512 "P2P: Failed to select random social channel as operation channel");
4513 return -1;
4514 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 p2p.cfg_op_channel = 0;
4516 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4517 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4518 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004519
4520 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4521 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4522 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4523 }
4524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4526 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4527 p2p.country[2] = 0x04;
4528 } else
4529 os_memcpy(p2p.country, "XX\x04", 3);
4530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4532 WPS_DEV_TYPE_LEN);
4533
4534 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4535 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4536 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4537
4538 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4539 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4540
4541 p2p.max_peers = 100;
4542
4543 if (wpa_s->conf->p2p_ssid_postfix) {
4544 p2p.ssid_postfix_len =
4545 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4546 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4547 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4548 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4549 p2p.ssid_postfix_len);
4550 }
4551
4552 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4553
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004554 p2p.max_listen = wpa_s->max_remain_on_chan;
4555
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004556 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4557 wpa_s->conf->p2p_passphrase_len <= 63)
4558 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4559 else
4560 p2p.passphrase_len = 8;
4561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 global->p2p = p2p_init(&p2p);
4563 if (global->p2p == NULL)
4564 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004565 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566
4567 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4568 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4569 continue;
4570 p2p_add_wps_vendor_extension(
4571 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4572 }
4573
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004574 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576 return 0;
4577}
4578
4579
4580/**
4581 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4582 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4583 *
4584 * This function deinitialize per-interface P2P data.
4585 */
4586void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4587{
4588 if (wpa_s->driver && wpa_s->drv_priv)
4589 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004590
4591 if (wpa_s->go_params) {
4592 /* Clear any stored provisioning info */
4593 p2p_clear_provisioning_info(
4594 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004595 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004596 }
4597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 os_free(wpa_s->go_params);
4599 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004600 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4602 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4603 wpa_s->p2p_long_listen = 0;
4604 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4605 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4606 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004607 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004608 wpas_p2p_listen_work_done(wpa_s);
4609 if (wpa_s->p2p_send_action_work) {
4610 os_free(wpa_s->p2p_send_action_work->ctx);
4611 radio_work_done(wpa_s->p2p_send_action_work);
4612 wpa_s->p2p_send_action_work = NULL;
4613 }
4614 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004616 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4617 wpa_s->p2p_oob_dev_pw = NULL;
4618
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004619 os_free(wpa_s->p2p_group_common_freqs);
4620 wpa_s->p2p_group_common_freqs = NULL;
4621 wpa_s->p2p_group_common_freqs_num = 0;
4622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 /* TODO: remove group interface from the driver if this wpa_s instance
4624 * is on top of a P2P group interface */
4625}
4626
4627
4628/**
4629 * wpas_p2p_deinit_global - Deinitialize global P2P module
4630 * @global: Pointer to global data from wpa_supplicant_init()
4631 *
4632 * This function deinitializes the global (per device) P2P module.
4633 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004634static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635{
4636 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004637
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004638 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004639
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004640 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641
4642 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4644 wpa_s = wpa_s->next;
4645 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 tmp = global->ifaces;
4647 while (tmp &&
4648 (tmp == wpa_s ||
4649 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4650 tmp = tmp->next;
4651 }
4652 if (tmp == NULL)
4653 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004654 /* Disconnect from the P2P group and deinit the interface */
4655 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004656 }
4657
4658 /*
4659 * Deinit GO data on any possibly remaining interface (if main
4660 * interface is used as GO).
4661 */
4662 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4663 if (wpa_s->ap_iface)
4664 wpas_p2p_group_deinit(wpa_s);
4665 }
4666
4667 p2p_deinit(global->p2p);
4668 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004669 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670}
4671
4672
4673static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4674{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004675 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004676 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004677 if (wpa_s->drv_flags &
4678 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4679 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4680 return 1; /* P2P group requires a new interface in every case
4681 */
4682 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4683 return 0; /* driver does not support concurrent operations */
4684 if (wpa_s->global->ifaces->next)
4685 return 1; /* more that one interface already in use */
4686 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4687 return 1; /* this interface is already in use */
4688 return 0;
4689}
4690
4691
4692static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4693 const u8 *peer_addr,
4694 enum p2p_wps_method wps_method,
4695 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004696 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004697 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004698{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004699 if (persistent_group && wpa_s->conf->persistent_reconnect)
4700 persistent_group = 2;
4701
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004702 /*
4703 * Increase GO config timeout if HT40 is used since it takes some time
4704 * to scan channels for coex purposes before the BSS can be started.
4705 */
4706 p2p_set_config_timeout(wpa_s->global->p2p,
4707 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4710 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004711 persistent_group, ssid ? ssid->ssid : NULL,
4712 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004713 wpa_s->p2p_pd_before_go_neg, pref_freq,
4714 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4715 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716}
4717
4718
4719static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4720 const u8 *peer_addr,
4721 enum p2p_wps_method wps_method,
4722 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004723 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004724 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004726 if (persistent_group && wpa_s->conf->persistent_reconnect)
4727 persistent_group = 2;
4728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4730 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004731 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004732 ssid ? ssid->ssid_len : 0, pref_freq,
4733 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4734 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735}
4736
4737
4738static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4739{
4740 wpa_s->p2p_join_scan_count++;
4741 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4742 wpa_s->p2p_join_scan_count);
4743 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4744 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4745 " for join operationg - stop join attempt",
4746 MAC2STR(wpa_s->pending_join_iface_addr));
4747 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004748 if (wpa_s->p2p_auto_pd) {
4749 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004750 wpa_msg_global(wpa_s, MSG_INFO,
4751 P2P_EVENT_PROV_DISC_FAILURE
4752 " p2p_dev_addr=" MACSTR " status=N/A",
4753 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004754 return;
4755 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004756 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004757 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004758 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759 }
4760}
4761
4762
Dmitry Shmidt04949592012-07-19 12:16:46 -07004763static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4764{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004765 int res;
4766 unsigned int num, i;
4767 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004768
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004769 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4770 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004771 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004772 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004773
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004774 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4775 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004776 if (!freqs)
4777 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004778
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004779 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4780 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004781
4782 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004783 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004784 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4785 freq);
4786 res = 0;
4787 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004788 }
4789 }
4790
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004791 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004792 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004793
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004794exit_free:
4795 os_free(freqs);
4796 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004797}
4798
4799
4800static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4801 const u8 *peer_dev_addr)
4802{
4803 struct wpa_bss *bss;
4804 int updated;
4805
4806 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4807 if (bss == NULL)
4808 return -1;
4809 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4810 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4811 "last scan");
4812 return 0;
4813 }
4814
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004815 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4816 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004817 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4818 "%ld.%06ld (%supdated in last scan)",
4819 bss->last_update.sec, bss->last_update.usec,
4820 updated ? "": "not ");
4821
4822 return updated;
4823}
4824
4825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4827 struct wpa_scan_results *scan_res)
4828{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004829 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830 int freq;
4831 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4834
4835 if (wpa_s->global->p2p_disabled)
4836 return;
4837
Dmitry Shmidt04949592012-07-19 12:16:46 -07004838 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4839 scan_res ? (int) scan_res->num : -1,
4840 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841
4842 if (scan_res)
4843 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4844
Dmitry Shmidt04949592012-07-19 12:16:46 -07004845 if (wpa_s->p2p_auto_pd) {
4846 int join = wpas_p2p_peer_go(wpa_s,
4847 wpa_s->pending_join_dev_addr);
4848 if (join == 0 &&
4849 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4850 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004851 bss = wpa_bss_get_bssid_latest(
4852 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004853 if (bss) {
4854 freq = bss->freq;
4855 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4856 "the peer " MACSTR " at %d MHz",
4857 wpa_s->auto_pd_scan_retry,
4858 MAC2STR(wpa_s->
4859 pending_join_dev_addr),
4860 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004861 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004862 return;
4863 }
4864 }
4865
4866 if (join < 0)
4867 join = 0;
4868
4869 wpa_s->p2p_auto_pd = 0;
4870 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4871 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4872 MAC2STR(wpa_s->pending_join_dev_addr), join);
4873 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004874 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004875 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004876 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004877 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004878 wpa_msg_global(wpa_s, MSG_INFO,
4879 P2P_EVENT_PROV_DISC_FAILURE
4880 " p2p_dev_addr=" MACSTR " status=N/A",
4881 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004882 }
4883 return;
4884 }
4885
4886 if (wpa_s->p2p_auto_join) {
4887 int join = wpas_p2p_peer_go(wpa_s,
4888 wpa_s->pending_join_dev_addr);
4889 if (join < 0) {
4890 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4891 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004892 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004893 P2P_EVENT_FALLBACK_TO_GO_NEG
4894 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004895 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4896 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4897 wpa_s->p2p_persistent_group, 0, 0, 0,
4898 wpa_s->p2p_go_intent,
4899 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004900 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004901 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004902 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004903 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004904 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004905 wpa_s->p2p_go_max_oper_chwidth,
4906 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004907 return;
4908 }
4909
4910 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4911 "try to join the group", join ? "" :
4912 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004913 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004914 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004915 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004916 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004917 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004918 }
4919
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4921 wpa_s->pending_join_iface_addr);
4922 if (freq < 0 &&
4923 p2p_get_interface_addr(wpa_s->global->p2p,
4924 wpa_s->pending_join_dev_addr,
4925 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004926 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4927 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004928 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4929 "address for join from " MACSTR " to " MACSTR
4930 " based on newly discovered P2P peer entry",
4931 MAC2STR(wpa_s->pending_join_iface_addr),
4932 MAC2STR(iface_addr));
4933 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4934 ETH_ALEN);
4935
4936 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4937 wpa_s->pending_join_iface_addr);
4938 }
4939 if (freq >= 0) {
4940 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4941 "from P2P peer table: %d MHz", freq);
4942 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004943 if (wpa_s->p2p_join_ssid_len) {
4944 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4945 MACSTR " and SSID %s",
4946 MAC2STR(wpa_s->pending_join_iface_addr),
4947 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4948 wpa_s->p2p_join_ssid_len));
4949 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4950 wpa_s->p2p_join_ssid,
4951 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004952 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004953 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4954 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4955 bss = wpa_bss_get_bssid_latest(wpa_s,
4956 wpa_s->pending_join_iface_addr);
4957 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004959 u8 dev_addr[ETH_ALEN];
4960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961 freq = bss->freq;
4962 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004963 "from BSS table: %d MHz (SSID %s)", freq,
4964 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004965 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4966 dev_addr) == 0 &&
4967 os_memcmp(wpa_s->pending_join_dev_addr,
4968 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4969 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4970 ETH_ALEN) != 0) {
4971 wpa_printf(MSG_DEBUG,
4972 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4973 MAC2STR(dev_addr),
4974 MAC2STR(wpa_s->pending_join_dev_addr));
4975 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4976 ETH_ALEN);
4977 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 }
4979 if (freq > 0) {
4980 u16 method;
4981
Dmitry Shmidt04949592012-07-19 12:16:46 -07004982 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004983 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004984 P2P_EVENT_GROUP_FORMATION_FAILURE
4985 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004986 wpas_notify_p2p_group_formation_failure(
4987 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004988 return;
4989 }
4990
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4992 "prior to joining an existing group (GO " MACSTR
4993 " freq=%u MHz)",
4994 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4995 wpa_s->pending_pd_before_join = 1;
4996
4997 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 case WPS_PIN_DISPLAY:
4999 method = WPS_CONFIG_KEYPAD;
5000 break;
5001 case WPS_PIN_KEYPAD:
5002 method = WPS_CONFIG_DISPLAY;
5003 break;
5004 case WPS_PBC:
5005 method = WPS_CONFIG_PUSHBUTTON;
5006 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005007 case WPS_P2PS:
5008 method = WPS_CONFIG_P2PS;
5009 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 default:
5011 method = 0;
5012 break;
5013 }
5014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005015 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5016 wpa_s->pending_join_dev_addr) ==
5017 method)) {
5018 /*
5019 * We have already performed provision discovery for
5020 * joining the group. Proceed directly to join
5021 * operation without duplicated provision discovery. */
5022 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5023 "with " MACSTR " already done - proceed to "
5024 "join",
5025 MAC2STR(wpa_s->pending_join_dev_addr));
5026 wpa_s->pending_pd_before_join = 0;
5027 goto start;
5028 }
5029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005031 wpa_s->pending_join_dev_addr,
5032 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005033 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5035 "Discovery Request before joining an "
5036 "existing group");
5037 wpa_s->pending_pd_before_join = 0;
5038 goto start;
5039 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005040 return;
5041 }
5042
5043 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5044 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5045 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5046 wpas_p2p_check_join_scan_limit(wpa_s);
5047 return;
5048
5049start:
5050 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005051 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5052 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053}
5054
5055
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005056static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5057 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 int ret;
5060 struct wpa_driver_scan_params params;
5061 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005062 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005063 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005064 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065
5066 os_memset(&params, 0, sizeof(params));
5067
5068 /* P2P Wildcard SSID */
5069 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005070 if (ssid && ssid_len) {
5071 params.ssids[0].ssid = ssid;
5072 params.ssids[0].ssid_len = ssid_len;
5073 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5074 wpa_s->p2p_join_ssid_len = ssid_len;
5075 } else {
5076 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5077 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5078 wpa_s->p2p_join_ssid_len = 0;
5079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080
5081 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005082 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5083 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5084 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085 if (wps_ie == NULL) {
5086 wpas_p2p_scan_res_join(wpa_s, NULL);
5087 return;
5088 }
5089
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005090 if (!freq) {
5091 int oper_freq;
5092 /*
5093 * If freq is not provided, check the operating freq of the GO
5094 * and use a single channel scan on if possible.
5095 */
5096 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5097 wpa_s->pending_join_iface_addr);
5098 if (oper_freq > 0)
5099 freq = oper_freq;
5100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005101 if (freq > 0) {
5102 freqs[0] = freq;
5103 params.freqs = freqs;
5104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005106 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5107 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5108 if (ies == NULL) {
5109 wpabuf_free(wps_ie);
5110 wpas_p2p_scan_res_join(wpa_s, NULL);
5111 return;
5112 }
5113 wpabuf_put_buf(ies, wps_ie);
5114 wpabuf_free(wps_ie);
5115
5116 bands = wpas_get_bands(wpa_s, freqs);
5117 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5118
5119 params.p2p_probe = 1;
5120 params.extra_ies = wpabuf_head(ies);
5121 params.extra_ies_len = wpabuf_len(ies);
5122
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005123 if (wpa_s->clear_driver_scan_cache) {
5124 wpa_printf(MSG_DEBUG,
5125 "Request driver to clear scan cache due to local BSS flush");
5126 params.only_new_results = 1;
5127 }
5128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 /*
5130 * Run a scan to update BSS table and start Provision Discovery once
5131 * the new scan results become available.
5132 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005133 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005134 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005135 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005136 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005137 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005138 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140
5141 wpabuf_free(ies);
5142
5143 if (ret) {
5144 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5145 "try again later");
5146 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5147 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5148 wpas_p2p_check_join_scan_limit(wpa_s);
5149 }
5150}
5151
5152
Dmitry Shmidt04949592012-07-19 12:16:46 -07005153static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5154{
5155 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005156 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005157}
5158
5159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005160static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005161 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005162 int auto_join, int op_freq,
5163 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164{
5165 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005166 MACSTR " dev " MACSTR " op_freq=%d)%s",
5167 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005168 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005169 if (ssid && ssid_len) {
5170 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5171 wpa_ssid_txt(ssid, ssid_len));
5172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173
Dmitry Shmidt04949592012-07-19 12:16:46 -07005174 wpa_s->p2p_auto_pd = 0;
5175 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005176 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5177 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5178 wpa_s->pending_join_wps_method = wps_method;
5179
5180 /* Make sure we are not running find during connection establishment */
5181 wpas_p2p_stop_find(wpa_s);
5182
5183 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005184 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 return 0;
5186}
5187
5188
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005189static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5190 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005191{
5192 struct wpa_supplicant *group;
5193 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005194 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195
5196 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5197 if (group == NULL)
5198 return -1;
5199 if (group != wpa_s) {
5200 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5201 sizeof(group->p2p_pin));
5202 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005203 } else {
5204 /*
5205 * Need to mark the current interface for p2p_group_formation
5206 * when a separate group interface is not used. This is needed
5207 * to allow p2p_cancel stop a pending p2p_connect-join.
5208 * wpas_p2p_init_group_interface() addresses this for the case
5209 * where a separate group interface is used.
5210 */
5211 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005212 }
5213
5214 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005215 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216
5217 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005218 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005219 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5220 ETH_ALEN);
5221 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005222 if (freq && ssid && ssid_len) {
5223 res.freq = freq;
5224 res.ssid_len = ssid_len;
5225 os_memcpy(res.ssid, ssid, ssid_len);
5226 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005227 if (ssid && ssid_len) {
5228 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5229 ssid, ssid_len);
5230 } else {
5231 bss = wpa_bss_get_bssid_latest(
5232 wpa_s, wpa_s->pending_join_iface_addr);
5233 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005234 if (bss) {
5235 res.freq = bss->freq;
5236 res.ssid_len = bss->ssid_len;
5237 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5238 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5239 bss->freq,
5240 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005241 } else if (ssid && ssid_len) {
5242 res.ssid_len = ssid_len;
5243 os_memcpy(res.ssid, ssid, ssid_len);
5244 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5245 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005246 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005247 }
5248
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005249 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5250 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5251 "starting client");
5252 wpa_drv_cancel_remain_on_channel(wpa_s);
5253 wpa_s->off_channel_freq = 0;
5254 wpa_s->roc_waiting_drv_freq = 0;
5255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005256 wpas_start_wps_enrollee(group, &res);
5257
5258 /*
5259 * Allow a longer timeout for join-a-running-group than normal 15
5260 * second group formation timeout since the GO may not have authorized
5261 * our connection yet.
5262 */
5263 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5264 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5265 wpa_s, NULL);
5266
5267 return 0;
5268}
5269
5270
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005271static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005272 int *force_freq, int *pref_freq, int go,
5273 unsigned int *pref_freq_list,
5274 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005275{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005276 struct wpa_used_freq_data *freqs;
5277 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005278 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5279
5280 max_pref_freq = *num_pref_freq;
5281 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005282
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005283 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5284 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005285 if (!freqs)
5286 return -1;
5287
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005288 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5289 wpa_s->num_multichan_concurrent);
5290
5291 /*
5292 * It is possible that the total number of used frequencies is bigger
5293 * than the number of frequencies used for P2P, so get the system wide
5294 * number of unused frequencies.
5295 */
5296 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5297
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005298 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005299 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5300 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005301
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005302 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005303 int ret;
5304 if (go)
5305 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5306 else
5307 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5308 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005309 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005310 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5311 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005312 /*
5313 * If freq is a DFS channel and DFS is offloaded
5314 * to the driver, allow P2P GO to use it.
5315 */
5316 wpa_printf(MSG_DEBUG,
5317 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5318 freq);
5319 } else {
5320 wpa_printf(MSG_DEBUG,
5321 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5322 freq);
5323 res = -3;
5324 goto exit_free;
5325 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005326 }
5327
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005328 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005329 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005330 freq_in_use = 1;
5331 }
5332
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005333 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005334 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5335 freq);
5336 res = -2;
5337 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005338 }
5339 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5340 "requested channel (%u MHz)", freq);
5341 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005342 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005343 }
5344
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005345 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005346
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005347 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5348 enum wpa_driver_if_type iface_type;
5349
5350 if (go)
5351 iface_type = WPA_IF_P2P_GO;
5352 else
5353 iface_type = WPA_IF_P2P_CLIENT;
5354
5355 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5356 best_freq, go);
5357
5358 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5359 &max_pref_freq,
5360 pref_freq_list);
5361 if (!res && max_pref_freq > 0) {
5362 *num_pref_freq = max_pref_freq;
5363 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005364 while (i < *num_pref_freq &&
5365 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005366 pref_freq_list[i]) ||
5367 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005368 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005369 wpa_printf(MSG_DEBUG,
5370 "P2P: preferred_freq_list[%d]=%d is disallowed",
5371 i, pref_freq_list[i]);
5372 i++;
5373 }
5374 if (i != *num_pref_freq) {
5375 best_freq = pref_freq_list[i];
5376 wpa_printf(MSG_DEBUG,
5377 "P2P: Using preferred_freq_list[%d]=%d",
5378 i, best_freq);
5379 } else {
5380 wpa_printf(MSG_DEBUG,
5381 "P2P: All driver preferred frequencies are disallowed for P2P use");
5382 *num_pref_freq = 0;
5383 }
5384 } else {
5385 wpa_printf(MSG_DEBUG,
5386 "P2P: No preferred frequency list available");
5387 }
5388 }
5389
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005390 /* We have a candidate frequency to use */
5391 if (best_freq > 0) {
5392 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005393 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005394 best_freq);
5395 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005396 } else {
5397 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 -07005398 best_freq);
5399 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005400 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005401 } else if (num_unused > 0) {
5402 wpa_printf(MSG_DEBUG,
5403 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5404 *force_freq = 0;
5405 } else {
5406 wpa_printf(MSG_DEBUG,
5407 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5408 res = -2;
5409 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005410 }
5411
5412exit_ok:
5413 res = 0;
5414exit_free:
5415 os_free(freqs);
5416 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005417}
5418
5419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005420/**
5421 * wpas_p2p_connect - Request P2P Group Formation to be started
5422 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5423 * @peer_addr: Address of the peer P2P Device
5424 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5425 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005426 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427 * @join: Whether to join an existing group (as a client) instead of starting
5428 * Group Owner negotiation; @peer_addr is BSSID in that case
5429 * @auth: Whether to only authorize the connection instead of doing that and
5430 * initiating Group Owner negotiation
5431 * @go_intent: GO Intent or -1 to use default
5432 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005433 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005434 * @persistent_id: Persistent group credentials to use for forcing GO
5435 * parameters or -1 to generate new values (SSID/passphrase)
5436 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5437 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005438 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005439 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005440 * @vht_chwidth: Channel width supported by GO operating with VHT support
5441 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005442 * @group_ssid: Specific Group SSID for join or %NULL if not set
5443 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5445 * failure, -2 on failure due to channel not currently available,
5446 * -3 if forced channel is not supported
5447 */
5448int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5449 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005450 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005451 int go_intent, int freq, unsigned int vht_center_freq2,
5452 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005453 unsigned int vht_chwidth, const u8 *group_ssid,
5454 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005456 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005457 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005459 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005460 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005461 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462
5463 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5464 return -1;
5465
Dmitry Shmidt04949592012-07-19 12:16:46 -07005466 if (persistent_id >= 0) {
5467 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5468 if (ssid == NULL || ssid->disabled != 2 ||
5469 ssid->mode != WPAS_MODE_P2P_GO)
5470 return -1;
5471 }
5472
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005473 os_free(wpa_s->global->add_psk);
5474 wpa_s->global->add_psk = NULL;
5475
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005476 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005477 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005478 wpa_s->global->pending_p2ps_group_freq = 0;
5479 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005480
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 if (go_intent < 0)
5482 go_intent = wpa_s->conf->p2p_go_intent;
5483
5484 if (!auth)
5485 wpa_s->p2p_long_listen = 0;
5486
5487 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005488 wpa_s->p2p_persistent_group = !!persistent_group;
5489 wpa_s->p2p_persistent_id = persistent_id;
5490 wpa_s->p2p_go_intent = go_intent;
5491 wpa_s->p2p_connect_freq = freq;
5492 wpa_s->p2p_fallback_to_go_neg = 0;
5493 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005494 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005495 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005496 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5497 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005498
5499 if (pin)
5500 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5501 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005502 if (wps_generate_pin((unsigned int *) &ret) < 0)
5503 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005504 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5505 "%08d", ret);
5506 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5507 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5509 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005510 } else if (wps_method == WPS_P2PS) {
5511 /* Force the P2Ps default PIN to be used */
5512 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 } else
5514 wpa_s->p2p_pin[0] = '\0';
5515
Dmitry Shmidt04949592012-07-19 12:16:46 -07005516 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5518 if (auth) {
5519 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5520 "connect a running group from " MACSTR,
5521 MAC2STR(peer_addr));
5522 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5523 return ret;
5524 }
5525 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5526 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5527 iface_addr) < 0) {
5528 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5529 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5530 dev_addr);
5531 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005532 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005533 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005534 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5535 "%ld.%06ld",
5536 wpa_s->p2p_auto_started.sec,
5537 wpa_s->p2p_auto_started.usec);
5538 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005539 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005540 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005541 auto_join, freq,
5542 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543 return -1;
5544 return ret;
5545 }
5546
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005547 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005548 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005549 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005550 if (res)
5551 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005552 wpas_p2p_set_own_freq_preference(wpa_s,
5553 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005554
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005555 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5558
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005559 if (wpa_s->create_p2p_iface) {
5560 /* Prepare to add a new interface for the group */
5561 iftype = WPA_IF_P2P_GROUP;
5562 if (go_intent == 15)
5563 iftype = WPA_IF_P2P_GO;
5564 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5565 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5566 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005570 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005571 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005572 if (wpa_s->p2p_mgmt)
5573 if_addr = wpa_s->parent->own_addr;
5574 else
5575 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005576 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5577 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578
5579 if (auth) {
5580 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005581 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005582 force_freq, persistent_group, ssid,
5583 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584 return -1;
5585 return ret;
5586 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005587
5588 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5589 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005590 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005591 if (wpa_s->create_p2p_iface)
5592 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 return -1;
5594 }
5595 return ret;
5596}
5597
5598
5599/**
5600 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5601 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5602 * @freq: Frequency of the channel in MHz
5603 * @duration: Duration of the stay on the channel in milliseconds
5604 *
5605 * This callback is called when the driver indicates that it has started the
5606 * requested remain-on-channel duration.
5607 */
5608void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5609 unsigned int freq, unsigned int duration)
5610{
5611 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5612 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005613 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)",
5614 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5615 wpa_s->roc_waiting_drv_freq, freq, duration);
5616 if (wpa_s->off_channel_freq &&
5617 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005618 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5619 wpa_s->pending_listen_duration);
5620 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005621 } else {
5622 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5623 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5624 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 }
5626}
5627
5628
Jithu Jance57cea1a2014-08-20 10:22:04 -07005629int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005630{
5631 /* Limit maximum Listen state time based on driver limitation. */
5632 if (timeout > wpa_s->max_remain_on_chan)
5633 timeout = wpa_s->max_remain_on_chan;
5634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 return p2p_listen(wpa_s->global->p2p, timeout);
5636}
5637
5638
5639/**
5640 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5641 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5642 * @freq: Frequency of the channel in MHz
5643 *
5644 * This callback is called when the driver indicates that a remain-on-channel
5645 * operation has been completed, i.e., the duration on the requested channel
5646 * has timed out.
5647 */
5648void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5649 unsigned int freq)
5650{
5651 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5652 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005653 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005654 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5656 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005657 if (wpa_s->p2p_long_listen > 0)
5658 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5660 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005661 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 if (wpa_s->p2p_long_listen > 0) {
5664 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5665 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005666 } else {
5667 /*
5668 * When listen duration is over, stop listen & update p2p_state
5669 * to IDLE.
5670 */
5671 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 }
5673}
5674
5675
5676/**
5677 * wpas_p2p_group_remove - Remove a P2P group
5678 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5679 * @ifname: Network interface name of the group interface or "*" to remove all
5680 * groups
5681 * Returns: 0 on success, -1 on failure
5682 *
5683 * This function is used to remove a P2P group. This can be used to disconnect
5684 * from a group in which the local end is a P2P Client or to end a P2P Group in
5685 * case the local end is the Group Owner. If a virtual network interface was
5686 * created for this group, that interface will be removed. Otherwise, only the
5687 * configured P2P group network will be removed from the interface.
5688 */
5689int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5690{
5691 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005692 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693
5694 if (os_strcmp(ifname, "*") == 0) {
5695 struct wpa_supplicant *prev;
5696 wpa_s = global->ifaces;
5697 while (wpa_s) {
5698 prev = wpa_s;
5699 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005700 if (prev->p2p_group_interface !=
5701 NOT_P2P_GROUP_INTERFACE ||
5702 (prev->current_ssid &&
5703 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005704 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 }
5706 return 0;
5707 }
5708
5709 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5710 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5711 break;
5712 }
5713
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005714 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715}
5716
5717
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005718static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5719{
5720 unsigned int r;
5721
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005722 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5723 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5724 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5725 int res;
5726
5727 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5728 &size, pref_freq_list);
5729 if (!res && size > 0) {
5730 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005731 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005732 (!p2p_supported_freq(wpa_s->global->p2p,
5733 pref_freq_list[i]) ||
5734 wpas_p2p_disallowed_freq(wpa_s->global,
5735 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005736 wpa_printf(MSG_DEBUG,
5737 "P2P: preferred_freq_list[%d]=%d is disallowed",
5738 i, pref_freq_list[i]);
5739 i++;
5740 }
5741 if (i != size) {
5742 freq = pref_freq_list[i];
5743 wpa_printf(MSG_DEBUG,
5744 "P2P: Using preferred_freq_list[%d]=%d",
5745 i, freq);
5746 } else {
5747 wpa_printf(MSG_DEBUG,
5748 "P2P: All driver preferred frequencies are disallowed for P2P use");
5749 }
5750 } else {
5751 wpa_printf(MSG_DEBUG,
5752 "P2P: No preferred frequency list available");
5753 }
5754 }
5755
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005756 if (freq == 2) {
5757 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5758 "band");
5759 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005760 p2p_supported_freq_go(wpa_s->global->p2p,
5761 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005762 freq = wpa_s->best_24_freq;
5763 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5764 "channel: %d MHz", freq);
5765 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005766 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5767 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005768 freq = 2412 + (r % 3) * 25;
5769 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5770 "channel: %d MHz", freq);
5771 }
5772 }
5773
5774 if (freq == 5) {
5775 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5776 "band");
5777 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005778 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005779 wpa_s->best_5_freq)) {
5780 freq = wpa_s->best_5_freq;
5781 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5782 "channel: %d MHz", freq);
5783 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005784 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5785 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005786 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005787 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005788 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5789 "5 GHz channel for P2P group");
5790 return -1;
5791 }
5792 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5793 "channel: %d MHz", freq);
5794 }
5795 }
5796
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005797 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005798 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005799 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5800 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005801 /*
5802 * If freq is a DFS channel and DFS is offloaded to the
5803 * driver, allow P2P GO to use it.
5804 */
5805 wpa_printf(MSG_DEBUG, "P2P: "
5806 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5807 __func__, freq);
5808 return freq;
5809 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005810 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5811 "(%u MHz) is not supported for P2P uses",
5812 freq);
5813 return -1;
5814 }
5815
5816 return freq;
5817}
5818
5819
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005820static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5821 const struct p2p_channels *channels,
5822 int freq)
5823{
5824 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5825 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5826 freq_included(wpa_s, channels, freq))
5827 return 1;
5828 return 0;
5829}
5830
5831
5832static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5833 struct p2p_go_neg_results *params,
5834 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005835{
5836 unsigned int i, r;
5837
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005838 /* try all channels in operating class 115 */
5839 for (i = 0; i < 4; i++) {
5840 params->freq = 5180 + i * 20;
5841 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005842 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005843 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5844 goto out;
5845 }
5846
5847 /* try all channels in operating class 124 */
5848 for (i = 0; i < 4; i++) {
5849 params->freq = 5745 + i * 20;
5850 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005851 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005852 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5853 goto out;
5854 }
5855
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005856 /* try social channel class 180 channel 2 */
5857 params->freq = 58320 + 1 * 2160;
5858 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005859 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005860 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5861 goto out;
5862
5863 /* try all channels in reg. class 180 */
5864 for (i = 0; i < 4; i++) {
5865 params->freq = 58320 + i * 2160;
5866 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005867 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005868 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5869 goto out;
5870 }
5871
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005872 /* try some random selection of the social channels */
5873 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5874 return;
5875
5876 for (i = 0; i < 3; i++) {
5877 params->freq = 2412 + ((r + i) % 3) * 25;
5878 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5879 goto out;
5880 }
5881
5882 /* try all other channels in operating class 81 */
5883 for (i = 0; i < 11; i++) {
5884 params->freq = 2412 + i * 5;
5885
5886 /* skip social channels; covered in the previous loop */
5887 if (params->freq == 2412 ||
5888 params->freq == 2437 ||
5889 params->freq == 2462)
5890 continue;
5891
5892 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5893 goto out;
5894 }
5895
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005896 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005897 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005898 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005899out:
5900 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5901 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005902}
5903
5904
Roshan Pius3a1667e2018-07-03 15:17:14 -07005905static int wpas_same_band(int freq1, int freq2)
5906{
5907 enum hostapd_hw_mode mode1, mode2;
5908 u8 chan1, chan2;
5909
5910 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5911 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5912 if (mode1 == NUM_HOSTAPD_MODES)
5913 return 0;
5914 return mode1 == mode2;
5915}
5916
5917
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005918static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5919 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005920 int freq, int vht_center_freq2, int ht40,
5921 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005922 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005923{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005924 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005925 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005926 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005927 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005928 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929
5930 os_memset(params, 0, sizeof(*params));
5931 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005932 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005933 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005934 params->max_oper_chwidth = max_oper_chwidth;
5935 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005936
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005937 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5938 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005939 if (!freqs)
5940 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005941
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005942 num = get_shared_radio_freqs_data(wpa_s, freqs,
5943 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005944
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005945 if (wpa_s->current_ssid &&
5946 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5947 wpa_s->wpa_state == WPA_COMPLETED) {
5948 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5949 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005950
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005951 /*
5952 * If the frequency selection is done for an active P2P GO that
5953 * is not sharing a frequency, allow to select a new frequency
5954 * even if there are no unused frequencies as we are about to
5955 * move the P2P GO so its frequency can be re-used.
5956 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005957 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005958 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5959 freqs[i].flags == 0) {
5960 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005961 break;
5962 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005963 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005964 }
5965
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005966 /* try using the forced freq */
5967 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005968 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
5969 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005970 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005971 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005972 freq);
5973 goto fail;
5974 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005975 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5976 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005977 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5978 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005979 /*
5980 * If freq is a DFS channel and DFS is offloaded
5981 * to the driver, allow P2P GO to use it.
5982 */
5983 wpa_printf(MSG_DEBUG,
5984 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
5985 __func__, freq);
5986 } else {
5987 wpa_printf(MSG_DEBUG,
5988 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
5989 freq);
5990 goto fail;
5991 }
5992 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005993
5994 for (i = 0; i < num; i++) {
5995 if (freqs[i].freq == freq) {
5996 wpa_printf(MSG_DEBUG,
5997 "P2P: forced freq (%d MHz) is also shared",
5998 freq);
5999 params->freq = freq;
6000 goto success;
6001 }
6002 }
6003
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006004 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006005 wpa_printf(MSG_DEBUG,
6006 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6007 freq);
6008 goto fail;
6009 }
6010
6011 wpa_printf(MSG_DEBUG,
6012 "P2P: force GO freq (%d MHz) on a free channel",
6013 freq);
6014 params->freq = freq;
6015 goto success;
6016 }
6017
6018 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006019 if (num &&
6020 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006021 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6022 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6023 wpa_printf(MSG_DEBUG,
6024 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006025 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006026 params->freq = cand;
6027 goto success;
6028 }
6029
6030 /* try using one of the shared freqs */
6031 for (i = 0; i < num; i++) {
6032 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6033 freqs[i].freq)) {
6034 wpa_printf(MSG_DEBUG,
6035 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006036 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006037 params->freq = freqs[i].freq;
6038 goto success;
6039 }
6040 }
6041 }
6042
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006043 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006044 wpa_printf(MSG_DEBUG,
6045 "P2P: Cannot force GO on any of the channels we are already using");
6046 goto fail;
6047 }
6048
6049 /* try using the setting from the configuration file */
6050 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6051 wpa_s->conf->p2p_oper_channel >= 1 &&
6052 wpa_s->conf->p2p_oper_channel <= 11 &&
6053 wpas_p2p_supported_freq_go(
6054 wpa_s, channels,
6055 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6056 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6057 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6058 "frequency %d MHz", params->freq);
6059 goto success;
6060 }
6061
6062 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6063 wpa_s->conf->p2p_oper_reg_class == 116 ||
6064 wpa_s->conf->p2p_oper_reg_class == 117 ||
6065 wpa_s->conf->p2p_oper_reg_class == 124 ||
6066 wpa_s->conf->p2p_oper_reg_class == 125 ||
6067 wpa_s->conf->p2p_oper_reg_class == 126 ||
6068 wpa_s->conf->p2p_oper_reg_class == 127) &&
6069 wpas_p2p_supported_freq_go(wpa_s, channels,
6070 5000 +
6071 5 * wpa_s->conf->p2p_oper_channel)) {
6072 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6073 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6074 "frequency %d MHz", params->freq);
6075 goto success;
6076 }
6077
6078 /* Try using best channels */
6079 if (wpa_s->conf->p2p_oper_channel == 0 &&
6080 wpa_s->best_overall_freq > 0 &&
6081 wpas_p2p_supported_freq_go(wpa_s, channels,
6082 wpa_s->best_overall_freq)) {
6083 params->freq = wpa_s->best_overall_freq;
6084 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6085 "channel %d MHz", params->freq);
6086 goto success;
6087 }
6088
6089 if (wpa_s->conf->p2p_oper_channel == 0 &&
6090 wpa_s->best_24_freq > 0 &&
6091 wpas_p2p_supported_freq_go(wpa_s, channels,
6092 wpa_s->best_24_freq)) {
6093 params->freq = wpa_s->best_24_freq;
6094 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6095 "channel %d MHz", params->freq);
6096 goto success;
6097 }
6098
6099 if (wpa_s->conf->p2p_oper_channel == 0 &&
6100 wpa_s->best_5_freq > 0 &&
6101 wpas_p2p_supported_freq_go(wpa_s, channels,
6102 wpa_s->best_5_freq)) {
6103 params->freq = wpa_s->best_5_freq;
6104 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6105 "channel %d MHz", params->freq);
6106 goto success;
6107 }
6108
6109 /* try using preferred channels */
6110 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6111 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6112 params->freq = cand;
6113 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6114 "channels", params->freq);
6115 goto success;
6116 }
6117
Roshan Pius3a1667e2018-07-03 15:17:14 -07006118 /* Try using a channel that allows VHT to be used with 80 MHz */
6119 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6120 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6121 enum hostapd_hw_mode mode;
6122 struct hostapd_hw_modes *hwmode;
6123 u8 chan;
6124
6125 cand = wpa_s->p2p_group_common_freqs[i];
6126 mode = ieee80211_freq_to_chan(cand, &chan);
6127 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6128 mode);
6129 if (!hwmode ||
6130 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6131 BW80) != ALLOWED)
6132 continue;
6133 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6134 params->freq = cand;
6135 wpa_printf(MSG_DEBUG,
6136 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6137 params->freq);
6138 goto success;
6139 }
6140 }
6141 }
6142
6143 /* Try using a channel that allows HT to be used with 40 MHz on the same
6144 * band so that CSA can be used */
6145 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6146 wpa_s->p2p_group_common_freqs) {
6147 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6148 enum hostapd_hw_mode mode;
6149 struct hostapd_hw_modes *hwmode;
6150 u8 chan;
6151
6152 cand = wpa_s->p2p_group_common_freqs[i];
6153 mode = ieee80211_freq_to_chan(cand, &chan);
6154 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6155 mode);
6156 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6157 cand) ||
6158 !hwmode ||
6159 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6160 BW40MINUS) != ALLOWED &&
6161 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6162 BW40PLUS) != ALLOWED))
6163 continue;
6164 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6165 params->freq = cand;
6166 wpa_printf(MSG_DEBUG,
6167 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6168 params->freq);
6169 goto success;
6170 }
6171 }
6172 }
6173
6174 /* Try using one of the group common freqs on the same band so that CSA
6175 * can be used */
6176 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6177 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6178 cand = wpa_s->p2p_group_common_freqs[i];
6179 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6180 cand))
6181 continue;
6182 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6183 params->freq = cand;
6184 wpa_printf(MSG_DEBUG,
6185 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6186 params->freq);
6187 goto success;
6188 }
6189 }
6190 }
6191
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006192 /* Try using one of the group common freqs */
6193 if (wpa_s->p2p_group_common_freqs) {
6194 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6195 cand = wpa_s->p2p_group_common_freqs[i];
6196 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6197 params->freq = cand;
6198 wpa_printf(MSG_DEBUG,
6199 "P2P: Use freq %d MHz common with the peer",
6200 params->freq);
6201 goto success;
6202 }
6203 }
6204 }
6205
6206 /* no preference, select some channel */
6207 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6208
6209 if (params->freq == 0) {
6210 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6211 goto fail;
6212 }
6213
6214success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006215 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006216 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006217fail:
6218 os_free(freqs);
6219 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006220}
6221
6222
6223static struct wpa_supplicant *
6224wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6225 int go)
6226{
6227 struct wpa_supplicant *group_wpa_s;
6228
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006229 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006230 if (wpa_s->p2p_mgmt) {
6231 /*
6232 * We may be called on the p2p_dev interface which
6233 * cannot be used for group operations, so always use
6234 * the primary interface.
6235 */
6236 wpa_s->parent->p2pdev = wpa_s;
6237 wpa_s = wpa_s->parent;
6238 }
6239 wpa_dbg(wpa_s, MSG_DEBUG,
6240 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006241 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006242 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006243 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006244 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006245 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246
6247 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006248 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006249 wpa_msg_global(wpa_s, MSG_ERROR,
6250 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006253 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6254 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006255 wpa_msg_global(wpa_s, MSG_ERROR,
6256 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257 wpas_p2p_remove_pending_group_interface(wpa_s);
6258 return NULL;
6259 }
6260
Roshan Pius3a1667e2018-07-03 15:17:14 -07006261 if (go && wpa_s->p2p_go_do_acs) {
6262 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6263 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6264 wpa_s->p2p_go_do_acs = 0;
6265 }
6266
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006267 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6268 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006269 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270 return group_wpa_s;
6271}
6272
6273
6274/**
6275 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6276 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6277 * @persistent_group: Whether to create a persistent group
6278 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006279 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006280 * @ht40: Start GO with 40 MHz channel width
6281 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006282 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006283 * Returns: 0 on success, -1 on failure
6284 *
6285 * This function creates a new P2P group with the local end as the Group Owner,
6286 * i.e., without using Group Owner Negotiation.
6287 */
6288int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006289 int freq, int vht_center_freq2, int ht40, int vht,
6290 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291{
6292 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006293
6294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6295 return -1;
6296
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006297 os_free(wpa_s->global->add_psk);
6298 wpa_s->global->add_psk = NULL;
6299
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006300 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006301 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006302 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006303
Roshan Pius3a1667e2018-07-03 15:17:14 -07006304 if (!wpa_s->p2p_go_do_acs) {
6305 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6306 if (freq < 0)
6307 return -1;
6308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006309
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006310 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6311 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006312 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006314 p2p_go_params(wpa_s->global->p2p, &params);
6315 params.persistent_group = persistent_group;
6316
6317 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6318 if (wpa_s == NULL)
6319 return -1;
6320 wpas_start_wps_go(wpa_s, &params, 0);
6321
6322 return 0;
6323}
6324
6325
6326static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006327 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006328 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006329{
6330 struct wpa_ssid *ssid;
6331
6332 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6333 if (wpa_s == NULL)
6334 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006335 if (force_scan)
6336 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006337 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338
6339 wpa_supplicant_ap_deinit(wpa_s);
6340
6341 ssid = wpa_config_add_network(wpa_s->conf);
6342 if (ssid == NULL)
6343 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006344 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006345 wpa_config_set_network_defaults(ssid);
6346 ssid->temporary = 1;
6347 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006348 ssid->pbss = params->pbss;
6349 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6350 WPA_CIPHER_CCMP;
6351 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6353 ssid->ssid = os_malloc(params->ssid_len);
6354 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006355 wpa_config_remove_network(wpa_s->conf, ssid->id);
6356 return -1;
6357 }
6358 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6359 ssid->ssid_len = params->ssid_len;
6360 ssid->p2p_group = 1;
6361 ssid->export_keys = 1;
6362 if (params->psk_set) {
6363 os_memcpy(ssid->psk, params->psk, 32);
6364 ssid->psk_set = 1;
6365 }
6366 if (params->passphrase)
6367 ssid->passphrase = os_strdup(params->passphrase);
6368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006369 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006370 wpa_s->p2p_in_invitation = 1;
6371 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006372 wpa_s->p2p_go_group_formation_completed = 0;
6373 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006374
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006375 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006376 NULL);
6377 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6378 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006379 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006380 wpa_supplicant_select_network(wpa_s, ssid);
6381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006382 return 0;
6383}
6384
6385
6386int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6387 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006388 int force_freq, int neg_freq,
6389 int vht_center_freq2, int ht40,
6390 int vht, int max_oper_chwidth,
6391 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006392 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393{
6394 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006395 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006396
6397 if (ssid->disabled != 2 || ssid->ssid == NULL)
6398 return -1;
6399
6400 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6401 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6402 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6403 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006404 if (go == 0 &&
6405 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006406 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006407 /*
6408 * This can happen if Invitation Response frame was lost
6409 * and the peer (GO of a persistent group) tries to
6410 * invite us again. Reschedule the timeout to avoid
6411 * terminating the wait for the connection too early
6412 * since we now know that the peer is still trying to
6413 * invite us instead of having already started the GO.
6414 */
6415 wpa_printf(MSG_DEBUG,
6416 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6417 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6418 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006419 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006420 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006421 return 0;
6422 }
6423
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006424 os_free(wpa_s->global->add_psk);
6425 wpa_s->global->add_psk = NULL;
6426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006427 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006428 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429
Dmitry Shmidt04949592012-07-19 12:16:46 -07006430 wpa_s->p2p_fallback_to_go_neg = 0;
6431
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006432 if (ssid->mode == WPAS_MODE_P2P_GO) {
6433 if (force_freq > 0) {
6434 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6435 if (freq < 0)
6436 return -1;
6437 } else {
6438 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6439 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006440 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006441 freq = 0;
6442 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006443 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006444 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006445 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006446 struct os_reltime now;
6447 struct wpa_bss *bss =
6448 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006449
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006450 os_get_reltime(&now);
6451 if (bss &&
6452 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006453 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006454 freq = bss->freq;
6455 else
6456 freq = 0;
6457 }
6458
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006459 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6460 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006461 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006462 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006463 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006464
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006465 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6466 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006467 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006468
6469 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006470 params.psk_set = ssid->psk_set;
6471 if (params.psk_set)
6472 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006473 if (ssid->passphrase) {
6474 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6475 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6476 "persistent group");
6477 return -1;
6478 }
6479 os_strlcpy(params.passphrase, ssid->passphrase,
6480 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006481 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6483 params.ssid_len = ssid->ssid_len;
6484 params.persistent_group = 1;
6485
6486 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6487 if (wpa_s == NULL)
6488 return -1;
6489
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006490 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6491
Dmitry Shmidt56052862013-10-04 10:23:25 -07006492 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006493 wpas_start_wps_go(wpa_s, &params, 0);
6494
6495 return 0;
6496}
6497
6498
6499static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6500 struct wpabuf *proberesp_ies)
6501{
6502 struct wpa_supplicant *wpa_s = ctx;
6503 if (wpa_s->ap_iface) {
6504 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006505 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6506 wpabuf_free(beacon_ies);
6507 wpabuf_free(proberesp_ies);
6508 return;
6509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510 if (beacon_ies) {
6511 wpabuf_free(hapd->p2p_beacon_ie);
6512 hapd->p2p_beacon_ie = beacon_ies;
6513 }
6514 wpabuf_free(hapd->p2p_probe_resp_ie);
6515 hapd->p2p_probe_resp_ie = proberesp_ies;
6516 } else {
6517 wpabuf_free(beacon_ies);
6518 wpabuf_free(proberesp_ies);
6519 }
6520 wpa_supplicant_ap_update_beacon(wpa_s);
6521}
6522
6523
6524static void wpas_p2p_idle_update(void *ctx, int idle)
6525{
6526 struct wpa_supplicant *wpa_s = ctx;
6527 if (!wpa_s->ap_iface)
6528 return;
6529 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006530 if (idle) {
6531 if (wpa_s->global->p2p_fail_on_wps_complete &&
6532 wpa_s->p2p_in_provisioning) {
6533 wpas_p2p_grpform_fail_after_wps(wpa_s);
6534 return;
6535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006536 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006537 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006538 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6539}
6540
6541
6542struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006543 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544{
6545 struct p2p_group *group;
6546 struct p2p_group_config *cfg;
6547
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006548 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6549 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550 return NULL;
6551
6552 cfg = os_zalloc(sizeof(*cfg));
6553 if (cfg == NULL)
6554 return NULL;
6555
Dmitry Shmidt04949592012-07-19 12:16:46 -07006556 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006557 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006558 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006559 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6561 if (wpa_s->max_stations &&
6562 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6563 cfg->max_clients = wpa_s->max_stations;
6564 else
6565 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006566 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6567 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006568 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006569 cfg->cb_ctx = wpa_s;
6570 cfg->ie_update = wpas_p2p_ie_update;
6571 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006572 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6573 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574
6575 group = p2p_group_init(wpa_s->global->p2p, cfg);
6576 if (group == NULL)
6577 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006578 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006579 p2p_group_notif_formation_done(group);
6580 wpa_s->p2p_group = group;
6581 return group;
6582}
6583
6584
6585void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6586 int registrar)
6587{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006588 struct wpa_ssid *ssid = wpa_s->current_ssid;
6589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590 if (!wpa_s->p2p_in_provisioning) {
6591 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6592 "provisioning not in progress");
6593 return;
6594 }
6595
Dmitry Shmidt04949592012-07-19 12:16:46 -07006596 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6597 u8 go_dev_addr[ETH_ALEN];
6598 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6599 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6600 ssid->ssid_len);
6601 /* Clear any stored provisioning info */
6602 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6603 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006604
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006605 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006606 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006607 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006608 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6609 /*
6610 * Use a separate timeout for initial data connection to
6611 * complete to allow the group to be removed automatically if
6612 * something goes wrong in this step before the P2P group idle
6613 * timeout mechanism is taken into use.
6614 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006615 wpa_dbg(wpa_s, MSG_DEBUG,
6616 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6617 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006618 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6619 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006620 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006621 /* Complete group formation on successful data connection. */
6622 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006623 } else if (ssid) {
6624 /*
6625 * Use a separate timeout for initial data connection to
6626 * complete to allow the group to be removed automatically if
6627 * the client does not complete data connection successfully.
6628 */
6629 wpa_dbg(wpa_s, MSG_DEBUG,
6630 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6631 P2P_MAX_INITIAL_CONN_WAIT_GO);
6632 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6633 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006634 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006635 /*
6636 * Complete group formation on first successful data connection
6637 */
6638 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006639 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640 if (wpa_s->global->p2p)
6641 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006642 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006643}
6644
6645
Jouni Malinen75ecf522011-06-27 15:19:46 -07006646void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6647 struct wps_event_fail *fail)
6648{
6649 if (!wpa_s->p2p_in_provisioning) {
6650 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6651 "provisioning not in progress");
6652 return;
6653 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006654
6655 if (wpa_s->go_params) {
6656 p2p_clear_provisioning_info(
6657 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006658 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006659 }
6660
Jouni Malinen75ecf522011-06-27 15:19:46 -07006661 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006662
6663 if (wpa_s == wpa_s->global->p2p_group_formation) {
6664 /*
6665 * Allow some time for the failed WPS negotiation exchange to
6666 * complete, but remove the group since group formation cannot
6667 * succeed after provisioning failure.
6668 */
6669 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6670 wpa_s->global->p2p_fail_on_wps_complete = 1;
6671 eloop_deplete_timeout(0, 50000,
6672 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006673 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006674 }
6675}
6676
6677
6678int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6679{
6680 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6681 !wpa_s->p2p_in_provisioning)
6682 return 0;
6683
6684 wpas_p2p_grpform_fail_after_wps(wpa_s);
6685
6686 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006687}
6688
6689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006691 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006692 enum wpas_p2p_prov_disc_use use,
6693 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694{
6695 u16 config_methods;
6696
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006697 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006698 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006699 wpa_s->p2p_fallback_to_go_neg = 0;
6700 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006701 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6702 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006703 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6704 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6705
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006706 wpa_printf(MSG_DEBUG,
6707 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6708 __func__, p2ps_prov->conncap,
6709 p2ps_prov->adv_id, p2ps_prov->conncap,
6710 p2ps_prov->status, p2ps_prov->info);
6711
6712 config_methods = 0;
6713 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006714 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006715 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006716 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006717 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6718 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006719 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006720 else {
6721 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006722 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006723 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725
Dmitry Shmidt04949592012-07-19 12:16:46 -07006726 if (use == WPAS_P2P_PD_AUTO) {
6727 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6728 wpa_s->pending_pd_config_methods = config_methods;
6729 wpa_s->p2p_auto_pd = 1;
6730 wpa_s->p2p_auto_join = 0;
6731 wpa_s->pending_pd_before_join = 0;
6732 wpa_s->auto_pd_scan_retry = 0;
6733 wpas_p2p_stop_find(wpa_s);
6734 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006735 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006736 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6737 wpa_s->p2p_auto_started.sec,
6738 wpa_s->p2p_auto_started.usec);
6739 wpas_p2p_join_scan(wpa_s, NULL);
6740 return 0;
6741 }
6742
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006743 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6744 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006745 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006746 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006747
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006748 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006749 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006750 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751}
6752
6753
6754int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6755 char *end)
6756{
6757 return p2p_scan_result_text(ies, ies_len, buf, end);
6758}
6759
6760
6761static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6762{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006763 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006764 return;
6765
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006766 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006767 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006768 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6769 wpa_s, NULL);
6770 offchannel_send_action_done(wpa_s);
6771 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006773 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6774 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006775 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006776}
6777
6778
6779int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6780 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006781 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006782 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006783 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006784{
6785 wpas_p2p_clear_pending_action_tx(wpa_s);
6786 wpa_s->p2p_long_listen = 0;
6787
Dmitry Shmidt04949592012-07-19 12:16:46 -07006788 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006789 wpa_s->p2p_in_provisioning) {
6790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6791 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6792 " (P2P disabled)" : "",
6793 wpa_s->p2p_in_provisioning ?
6794 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006795 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006796 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006798 wpa_supplicant_cancel_sched_scan(wpa_s);
6799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006800 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006801 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006802 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006803}
6804
6805
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006806static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6807 struct wpa_scan_results *scan_res)
6808{
6809 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6810
6811 if (wpa_s->p2p_scan_work) {
6812 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6813 wpa_s->p2p_scan_work = NULL;
6814 radio_work_done(work);
6815 }
6816
6817 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6818 return;
6819
6820 /*
6821 * Indicate that results have been processed so that the P2P module can
6822 * continue pending tasks.
6823 */
6824 p2p_scan_res_handled(wpa_s->global->p2p);
6825}
6826
6827
6828static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006829{
6830 wpas_p2p_clear_pending_action_tx(wpa_s);
6831 wpa_s->p2p_long_listen = 0;
6832 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6833 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006834
6835 if (wpa_s->global->p2p)
6836 p2p_stop_find(wpa_s->global->p2p);
6837
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006838 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6839 wpa_printf(MSG_DEBUG,
6840 "P2P: Do not consider the scan results after stop_find");
6841 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6842 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006843}
6844
6845
6846void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6847{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006848 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006849 if (!wpa_s->global->pending_group_iface_for_p2ps)
6850 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006851}
6852
6853
6854static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6855{
6856 struct wpa_supplicant *wpa_s = eloop_ctx;
6857 wpa_s->p2p_long_listen = 0;
6858}
6859
6860
6861int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6862{
6863 int res;
6864
6865 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6866 return -1;
6867
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006868 if (wpa_s->p2p_lo_started) {
6869 wpa_printf(MSG_DEBUG,
6870 "P2P: Cannot start P2P listen, it is offloaded");
6871 return -1;
6872 }
6873
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006874 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006875 wpas_p2p_clear_pending_action_tx(wpa_s);
6876
6877 if (timeout == 0) {
6878 /*
6879 * This is a request for unlimited Listen state. However, at
6880 * least for now, this is mapped to a Listen state for one
6881 * hour.
6882 */
6883 timeout = 3600;
6884 }
6885 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6886 wpa_s->p2p_long_listen = 0;
6887
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006888 /*
6889 * Stop previous find/listen operation to avoid trying to request a new
6890 * remain-on-channel operation while the driver is still running the
6891 * previous one.
6892 */
6893 if (wpa_s->global->p2p)
6894 p2p_stop_find(wpa_s->global->p2p);
6895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006896 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6897 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6898 wpa_s->p2p_long_listen = timeout * 1000;
6899 eloop_register_timeout(timeout, 0,
6900 wpas_p2p_long_listen_timeout,
6901 wpa_s, NULL);
6902 }
6903
6904 return res;
6905}
6906
6907
6908int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6909 u8 *buf, size_t len, int p2p_group)
6910{
6911 struct wpabuf *p2p_ie;
6912 int ret;
6913
6914 if (wpa_s->global->p2p_disabled)
6915 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006916 /*
6917 * Advertize mandatory cross connection capability even on
6918 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6919 */
6920 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006921 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922 if (wpa_s->global->p2p == NULL)
6923 return -1;
6924 if (bss == NULL)
6925 return -1;
6926
6927 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6928 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6929 p2p_group, p2p_ie);
6930 wpabuf_free(p2p_ie);
6931
6932 return ret;
6933}
6934
6935
6936int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006937 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006938 const u8 *ie, size_t ie_len,
6939 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006940{
6941 if (wpa_s->global->p2p_disabled)
6942 return 0;
6943 if (wpa_s->global->p2p == NULL)
6944 return 0;
6945
Dmitry Shmidt04949592012-07-19 12:16:46 -07006946 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006947 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006948 case P2P_PREQ_NOT_P2P:
6949 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6950 ssi_signal);
6951 /* fall through */
6952 case P2P_PREQ_MALFORMED:
6953 case P2P_PREQ_NOT_LISTEN:
6954 case P2P_PREQ_NOT_PROCESSED:
6955 default: /* make gcc happy */
6956 return 0;
6957 case P2P_PREQ_PROCESSED:
6958 return 1;
6959 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006960}
6961
6962
6963void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6964 const u8 *sa, const u8 *bssid,
6965 u8 category, const u8 *data, size_t len, int freq)
6966{
6967 if (wpa_s->global->p2p_disabled)
6968 return;
6969 if (wpa_s->global->p2p == NULL)
6970 return;
6971
6972 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6973 freq);
6974}
6975
6976
6977void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6978{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006979 unsigned int bands;
6980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981 if (wpa_s->global->p2p_disabled)
6982 return;
6983 if (wpa_s->global->p2p == NULL)
6984 return;
6985
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006986 bands = wpas_get_bands(wpa_s, NULL);
6987 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988}
6989
6990
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006991static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006992{
6993 p2p_group_deinit(wpa_s->p2p_group);
6994 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006995
6996 wpa_s->ap_configured_cb = NULL;
6997 wpa_s->ap_configured_cb_ctx = NULL;
6998 wpa_s->ap_configured_cb_data = NULL;
6999 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007000}
7001
7002
7003int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7004{
7005 wpa_s->p2p_long_listen = 0;
7006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007007 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7008 return -1;
7009
7010 return p2p_reject(wpa_s->global->p2p, addr);
7011}
7012
7013
7014/* Invite to reinvoke a persistent group */
7015int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007016 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007017 int vht_center_freq2, int ht40, int vht, int max_chwidth,
7018 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007019{
7020 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007021 u8 *bssid = NULL;
7022 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007023 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007024 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007025 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007026
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007027 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007028 if (peer_addr)
7029 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7030 else
7031 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007032
Jouni Malinen31be0a42012-08-31 21:20:51 +03007033 wpa_s->p2p_persistent_go_freq = freq;
7034 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007035 wpa_s->p2p_go_vht = !!vht;
7036 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7037 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007038 if (ssid->mode == WPAS_MODE_P2P_GO) {
7039 role = P2P_INVITE_ROLE_GO;
7040 if (peer_addr == NULL) {
7041 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7042 "address in invitation command");
7043 return -1;
7044 }
7045 if (wpas_p2p_create_iface(wpa_s)) {
7046 if (wpas_p2p_add_group_interface(wpa_s,
7047 WPA_IF_P2P_GO) < 0) {
7048 wpa_printf(MSG_ERROR, "P2P: Failed to "
7049 "allocate a new interface for the "
7050 "group");
7051 return -1;
7052 }
7053 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007054 } else if (wpa_s->p2p_mgmt)
7055 bssid = wpa_s->parent->own_addr;
7056 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007057 bssid = wpa_s->own_addr;
7058 } else {
7059 role = P2P_INVITE_ROLE_CLIENT;
7060 peer_addr = ssid->bssid;
7061 }
7062 wpa_s->pending_invite_ssid_id = ssid->id;
7063
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007064 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007065 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007066 role == P2P_INVITE_ROLE_GO,
7067 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007068 if (res)
7069 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007071 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7072 return -1;
7073
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007074 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7075
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007076 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7077 no_pref_freq_given && pref_freq > 0 &&
7078 wpa_s->num_multichan_concurrent > 1 &&
7079 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7080 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7081 pref_freq);
7082 pref_freq = 0;
7083 }
7084
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007085 /*
7086 * Stop any find/listen operations before invitation and possibly
7087 * connection establishment.
7088 */
7089 wpas_p2p_stop_find_oper(wpa_s);
7090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007091 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007092 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007093 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094}
7095
7096
7097/* Invite to join an active group */
7098int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7099 const u8 *peer_addr, const u8 *go_dev_addr)
7100{
7101 struct wpa_global *global = wpa_s->global;
7102 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007103 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007104 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007105 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007106 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007107 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007108 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109
Jouni Malinen31be0a42012-08-31 21:20:51 +03007110 wpa_s->p2p_persistent_go_freq = 0;
7111 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007112 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007113 wpa_s->p2p_go_vht_center_freq2 = 0;
7114 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007116 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7117 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7118 break;
7119 }
7120 if (wpa_s == NULL) {
7121 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7122 return -1;
7123 }
7124
7125 ssid = wpa_s->current_ssid;
7126 if (ssid == NULL) {
7127 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7128 "invitation");
7129 return -1;
7130 }
7131
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007132 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007133 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007134 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007135 ssid->ssid, ssid->ssid_len);
7136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137 if (ssid->mode == WPAS_MODE_P2P_GO) {
7138 role = P2P_INVITE_ROLE_ACTIVE_GO;
7139 bssid = wpa_s->own_addr;
7140 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007141 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007142 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007143 } else {
7144 role = P2P_INVITE_ROLE_CLIENT;
7145 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7146 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7147 "invite to current group");
7148 return -1;
7149 }
7150 bssid = wpa_s->bssid;
7151 if (go_dev_addr == NULL &&
7152 !is_zero_ether_addr(wpa_s->go_dev_addr))
7153 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007154 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7155 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007156 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007157 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007159 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7160 return -1;
7161
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007162 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007163 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007164 role == P2P_INVITE_ROLE_ACTIVE_GO,
7165 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007166 if (res)
7167 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007168 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007170 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007171 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007172 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173}
7174
7175
7176void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7177{
7178 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007179 u8 go_dev_addr[ETH_ALEN];
7180 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007181 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007182 u8 ip[3 * 4];
7183 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007184
Dmitry Shmidt04949592012-07-19 12:16:46 -07007185 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7186 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007187 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007188 }
7189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007190 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007191 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007192
7193 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007194 if (!wpa_s->p2p_go_group_formation_completed &&
7195 wpa_s->global->p2p_group_formation == wpa_s) {
7196 wpa_dbg(wpa_s, MSG_DEBUG,
7197 "P2P: Marking group formation completed on client on data connection");
7198 wpa_s->p2p_go_group_formation_completed = 1;
7199 wpa_s->global->p2p_group_formation = NULL;
7200 wpa_s->p2p_in_provisioning = 0;
7201 wpa_s->p2p_in_invitation = 0;
7202 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007204 os_memset(go_dev_addr, 0, ETH_ALEN);
7205 if (ssid->bssid_set)
7206 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7207 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7208 ssid->ssid_len);
7209 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7210
7211 if (wpa_s->global->p2p_group_formation == wpa_s)
7212 wpa_s->global->p2p_group_formation = NULL;
7213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007214 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7215 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007216
7217 ip_addr[0] = '\0';
7218 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007219 int res;
7220
7221 res = os_snprintf(ip_addr, sizeof(ip_addr),
7222 " ip_addr=%u.%u.%u.%u "
7223 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7224 ip[0], ip[1], ip[2], ip[3],
7225 ip[4], ip[5], ip[6], ip[7],
7226 ip[8], ip[9], ip[10], ip[11]);
7227 if (os_snprintf_error(sizeof(ip_addr), res))
7228 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007229 }
7230
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007231 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7232 ssid->passphrase == NULL && ssid->psk_set ?
7233 ssid->psk : NULL,
7234 ssid->passphrase, go_dev_addr, persistent,
7235 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007236
7237 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007238 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7239 ssid, go_dev_addr);
7240
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007241 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007242}
7243
7244
7245int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7246 u32 interval1, u32 duration2, u32 interval2)
7247{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007248 int ret;
7249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007250 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7251 return -1;
7252
7253 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7254 wpa_s->current_ssid == NULL ||
7255 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7256 return -1;
7257
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007258 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7259 wpa_s->own_addr, wpa_s->assoc_freq,
7260 duration1, interval1, duration2, interval2);
7261 if (ret == 0)
7262 wpa_s->waiting_presence_resp = 1;
7263
7264 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007265}
7266
7267
7268int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7269 unsigned int interval)
7270{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007271 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7272 return -1;
7273
7274 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7275}
7276
7277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007278static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7279{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007280 if (wpa_s->current_ssid == NULL) {
7281 /*
7282 * current_ssid can be cleared when P2P client interface gets
7283 * disconnected, so assume this interface was used as P2P
7284 * client.
7285 */
7286 return 1;
7287 }
7288 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007289 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7290}
7291
7292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007293static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7294{
7295 struct wpa_supplicant *wpa_s = eloop_ctx;
7296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007297 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007298 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7299 "disabled");
7300 return;
7301 }
7302
Dmitry Shmidt04949592012-07-19 12:16:46 -07007303 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7304 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007305 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007306}
7307
7308
7309static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7310{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007311 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007312
Dmitry Shmidt04949592012-07-19 12:16:46 -07007313 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7314 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007316 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7317 return;
7318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007319 timeout = wpa_s->conf->p2p_group_idle;
7320 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7321 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7322 timeout = P2P_MAX_CLIENT_IDLE;
7323
7324 if (timeout == 0)
7325 return;
7326
Dmitry Shmidt04949592012-07-19 12:16:46 -07007327 if (timeout < 0) {
7328 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7329 timeout = 0; /* special client mode no-timeout */
7330 else
7331 return;
7332 }
7333
7334 if (wpa_s->p2p_in_provisioning) {
7335 /*
7336 * Use the normal group formation timeout during the
7337 * provisioning phase to avoid terminating this process too
7338 * early due to group idle timeout.
7339 */
7340 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7341 "during provisioning");
7342 return;
7343 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307344
Dmitry Shmidt04949592012-07-19 12:16:46 -07007345 if (wpa_s->show_group_started) {
7346 /*
7347 * Use the normal group formation timeout between the end of
7348 * the provisioning phase and completion of 4-way handshake to
7349 * avoid terminating this process too early due to group idle
7350 * timeout.
7351 */
7352 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7353 "while waiting for initial 4-way handshake to "
7354 "complete");
7355 return;
7356 }
7357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007358 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007359 timeout);
7360 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7361 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007362}
7363
7364
Jouni Malinen2b89da82012-08-31 22:04:41 +03007365/* Returns 1 if the interface was removed */
7366int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7367 u16 reason_code, const u8 *ie, size_t ie_len,
7368 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007369{
7370 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007371 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007372
Dmitry Shmidt04949592012-07-19 12:16:46 -07007373 if (!locally_generated)
7374 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7375 ie_len);
7376
7377 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7378 wpa_s->current_ssid &&
7379 wpa_s->current_ssid->p2p_group &&
7380 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7381 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7382 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007383 if (wpas_p2p_group_delete(wpa_s,
7384 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7385 > 0)
7386 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007387 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007388
7389 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007390}
7391
7392
7393void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007394 u16 reason_code, const u8 *ie, size_t ie_len,
7395 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007396{
7397 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7398 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007399
Dmitry Shmidt04949592012-07-19 12:16:46 -07007400 if (!locally_generated)
7401 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7402 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007403}
7404
7405
7406void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7407{
7408 struct p2p_data *p2p = wpa_s->global->p2p;
7409
7410 if (p2p == NULL)
7411 return;
7412
7413 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7414 return;
7415
7416 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7417 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7418
7419 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7420 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7421
7422 if (wpa_s->wps &&
7423 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7424 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7425
7426 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7427 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7428
7429 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7430 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7431 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7432 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7433 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7434 }
7435
7436 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7437 p2p_set_sec_dev_types(p2p,
7438 (void *) wpa_s->conf->sec_device_type,
7439 wpa_s->conf->num_sec_device_types);
7440
7441 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7442 int i;
7443 p2p_remove_wps_vendor_extensions(p2p);
7444 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7445 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7446 continue;
7447 p2p_add_wps_vendor_extension(
7448 p2p, wpa_s->conf->wps_vendor_ext[i]);
7449 }
7450 }
7451
7452 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7453 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7454 char country[3];
7455 country[0] = wpa_s->conf->country[0];
7456 country[1] = wpa_s->conf->country[1];
7457 country[2] = 0x04;
7458 p2p_set_country(p2p, country);
7459 }
7460
7461 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7462 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7463 wpa_s->conf->p2p_ssid_postfix ?
7464 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7465 0);
7466 }
7467
7468 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7469 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007470
7471 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7472 u8 reg_class, channel;
7473 int ret;
7474 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007475 u8 channel_forced;
7476
Jouni Malinen75ecf522011-06-27 15:19:46 -07007477 if (wpa_s->conf->p2p_listen_reg_class &&
7478 wpa_s->conf->p2p_listen_channel) {
7479 reg_class = wpa_s->conf->p2p_listen_reg_class;
7480 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007481 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007482 } else {
7483 reg_class = 81;
7484 /*
7485 * Pick one of the social channels randomly as the
7486 * listen channel.
7487 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007488 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7489 channel = 1;
7490 else
7491 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007492 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007493 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007494 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7495 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007496 if (ret)
7497 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7498 "failed: %d", ret);
7499 }
7500 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7501 u8 op_reg_class, op_channel, cfg_op_channel;
7502 int ret = 0;
7503 unsigned int r;
7504 if (wpa_s->conf->p2p_oper_reg_class &&
7505 wpa_s->conf->p2p_oper_channel) {
7506 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7507 op_channel = wpa_s->conf->p2p_oper_channel;
7508 cfg_op_channel = 1;
7509 } else {
7510 op_reg_class = 81;
7511 /*
7512 * Use random operation channel from (1, 6, 11)
7513 *if no other preference is indicated.
7514 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007515 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7516 op_channel = 1;
7517 else
7518 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007519 cfg_op_channel = 0;
7520 }
7521 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7522 cfg_op_channel);
7523 if (ret)
7524 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7525 "failed: %d", ret);
7526 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007527
7528 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7529 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7530 wpa_s->conf->p2p_pref_chan) < 0) {
7531 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7532 "update failed");
7533 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007534
7535 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7536 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7537 "update failed");
7538 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007539 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007540
7541 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7542 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007543}
7544
7545
7546int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7547 int duration)
7548{
7549 if (!wpa_s->ap_iface)
7550 return -1;
7551 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7552 duration);
7553}
7554
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007555
7556int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7557{
7558 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7559 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007560
7561 wpa_s->global->cross_connection = enabled;
7562 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7563
7564 if (!enabled) {
7565 struct wpa_supplicant *iface;
7566
7567 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7568 {
7569 if (iface->cross_connect_enabled == 0)
7570 continue;
7571
7572 iface->cross_connect_enabled = 0;
7573 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007574 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007575 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7576 iface->ifname,
7577 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007578 }
7579 }
7580
7581 return 0;
7582}
7583
7584
7585static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7586{
7587 struct wpa_supplicant *iface;
7588
7589 if (!uplink->global->cross_connection)
7590 return;
7591
7592 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7593 if (!iface->cross_connect_enabled)
7594 continue;
7595 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7596 0)
7597 continue;
7598 if (iface->ap_iface == NULL)
7599 continue;
7600 if (iface->cross_connect_in_use)
7601 continue;
7602
7603 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007604 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007605 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7606 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607 }
7608}
7609
7610
7611static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7612{
7613 struct wpa_supplicant *iface;
7614
7615 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7616 if (!iface->cross_connect_enabled)
7617 continue;
7618 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7619 0)
7620 continue;
7621 if (!iface->cross_connect_in_use)
7622 continue;
7623
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007624 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007625 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7626 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627 iface->cross_connect_in_use = 0;
7628 }
7629}
7630
7631
7632void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7633{
7634 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7635 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7636 wpa_s->cross_connect_disallowed)
7637 wpas_p2p_disable_cross_connect(wpa_s);
7638 else
7639 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007640 if (!wpa_s->ap_iface &&
7641 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7642 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007643}
7644
7645
7646void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7647{
7648 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007649 if (!wpa_s->ap_iface &&
7650 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7651 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007652 wpas_p2p_set_group_idle_timeout(wpa_s);
7653}
7654
7655
7656static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7657{
7658 struct wpa_supplicant *iface;
7659
7660 if (!wpa_s->global->cross_connection)
7661 return;
7662
7663 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7664 if (iface == wpa_s)
7665 continue;
7666 if (iface->drv_flags &
7667 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7668 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007669 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7670 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671 continue;
7672
7673 wpa_s->cross_connect_enabled = 1;
7674 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7675 sizeof(wpa_s->cross_connect_uplink));
7676 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7677 "%s to %s whenever uplink is available",
7678 wpa_s->ifname, wpa_s->cross_connect_uplink);
7679
7680 if (iface->ap_iface || iface->current_ssid == NULL ||
7681 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7682 iface->cross_connect_disallowed ||
7683 iface->wpa_state != WPA_COMPLETED)
7684 break;
7685
7686 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007687 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007688 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7689 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007690 break;
7691 }
7692}
7693
7694
7695int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7696{
7697 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7698 !wpa_s->p2p_in_provisioning)
7699 return 0; /* not P2P client operation */
7700
7701 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7702 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007703 if (wpa_s != wpa_s->p2pdev)
7704 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007705 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007706 return 1;
7707}
7708
7709
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007710void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7711{
7712 struct wpa_supplicant *wpa_s = eloop_ctx;
7713 wpas_p2p_notif_pbc_overlap(wpa_s);
7714}
7715
7716
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007717void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7718 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007719{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007720 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007721 struct wpa_used_freq_data *freqs = NULL;
7722 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723
7724 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7725 return;
7726
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007727 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7728 if (!freqs)
7729 return;
7730
7731 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007733 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007734 os_memset(&cli_chan, 0, sizeof(cli_chan));
7735 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007736 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7737 "channel list");
7738 return;
7739 }
7740
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007741 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007742
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007743 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007744
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007745 /*
7746 * The used frequencies map changed, so it is possible that a GO is
7747 * using a channel that is no longer valid for P2P use. It is also
7748 * possible that due to policy consideration, it would be preferable to
7749 * move it to a frequency already used by other station interfaces.
7750 */
7751 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7752
7753 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007754}
7755
7756
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007757static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7758 struct wpa_scan_results *scan_res)
7759{
7760 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7761}
7762
7763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007764int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7765{
7766 struct wpa_global *global = wpa_s->global;
7767 int found = 0;
7768 const u8 *peer;
7769
7770 if (global->p2p == NULL)
7771 return -1;
7772
7773 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7774
7775 if (wpa_s->pending_interface_name[0] &&
7776 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7777 found = 1;
7778
7779 peer = p2p_get_go_neg_peer(global->p2p);
7780 if (peer) {
7781 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7782 MACSTR, MAC2STR(peer));
7783 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007784 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007785 }
7786
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007787 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7788 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7789 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7790 found = 1;
7791 }
7792
7793 if (wpa_s->pending_pd_before_join) {
7794 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7795 wpa_s->pending_pd_before_join = 0;
7796 found = 1;
7797 }
7798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007799 wpas_p2p_stop_find(wpa_s);
7800
7801 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7802 if (wpa_s == global->p2p_group_formation &&
7803 (wpa_s->p2p_in_provisioning ||
7804 wpa_s->parent->pending_interface_type ==
7805 WPA_IF_P2P_CLIENT)) {
7806 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7807 "formation found - cancelling",
7808 wpa_s->ifname);
7809 found = 1;
7810 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007811 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007812 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007813 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007814 break;
7815 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007816 wpas_p2p_group_delete(wpa_s,
7817 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007818 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007819 } else if (wpa_s->p2p_in_invitation) {
7820 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7821 wpa_s->ifname);
7822 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007823 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007824 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007825 }
7826 }
7827
7828 if (!found) {
7829 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7830 return -1;
7831 }
7832
7833 return 0;
7834}
7835
7836
7837void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7838{
7839 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7840 return;
7841
7842 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7843 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007844 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007845}
7846
7847
7848void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7849 int freq_24, int freq_5, int freq_overall)
7850{
7851 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007852 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007853 return;
7854 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7855}
7856
7857
7858int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7859{
7860 u8 peer[ETH_ALEN];
7861 struct p2p_data *p2p = wpa_s->global->p2p;
7862
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007863 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007864 return -1;
7865
7866 if (hwaddr_aton(addr, peer))
7867 return -1;
7868
7869 return p2p_unauthorize(p2p, peer);
7870}
7871
7872
7873/**
7874 * wpas_p2p_disconnect - Disconnect from a P2P Group
7875 * @wpa_s: Pointer to wpa_supplicant data
7876 * Returns: 0 on success, -1 on failure
7877 *
7878 * This can be used to disconnect from a group in which the local end is a P2P
7879 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7880 * virtual network interface was created for this group, that interface will be
7881 * removed. Otherwise, only the configured P2P group network will be removed
7882 * from the interface.
7883 */
7884int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7885{
7886
7887 if (wpa_s == NULL)
7888 return -1;
7889
Jouni Malinen2b89da82012-08-31 22:04:41 +03007890 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7891 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007892}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007893
7894
7895int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7896{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007897 int ret;
7898
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007899 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7900 return 0;
7901
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007902 ret = p2p_in_progress(wpa_s->global->p2p);
7903 if (ret == 0) {
7904 /*
7905 * Check whether there is an ongoing WPS provisioning step (or
7906 * other parts of group formation) on another interface since
7907 * p2p_in_progress() does not report this to avoid issues for
7908 * scans during such provisioning step.
7909 */
7910 if (wpa_s->global->p2p_group_formation &&
7911 wpa_s->global->p2p_group_formation != wpa_s) {
7912 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7913 "in group formation",
7914 wpa_s->global->p2p_group_formation->ifname);
7915 ret = 1;
7916 }
7917 }
7918
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007919 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007920 struct os_reltime now;
7921 os_get_reltime(&now);
7922 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7923 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007924 /* Wait for the first client has expired */
7925 wpa_s->global->p2p_go_wait_client.sec = 0;
7926 } else {
7927 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7928 ret = 1;
7929 }
7930 }
7931
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007932 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007933}
7934
7935
7936void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7937 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007938{
7939 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7940 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007941 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007942 /**
7943 * Remove the network by scheduling the group formation
7944 * timeout to happen immediately. The teardown code
7945 * needs to be scheduled to run asynch later so that we
7946 * don't delete data from under ourselves unexpectedly.
7947 * Calling wpas_p2p_group_formation_timeout directly
7948 * causes a series of crashes in WPS failure scenarios.
7949 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007950 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7951 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007952 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007953 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007954 }
7955}
7956
7957
7958struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007959 const u8 *addr, const u8 *ssid,
7960 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007961{
7962 struct wpa_ssid *s;
7963 size_t i;
7964
7965 for (s = wpa_s->conf->ssid; s; s = s->next) {
7966 if (s->disabled != 2)
7967 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007968 if (ssid &&
7969 (ssid_len != s->ssid_len ||
7970 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7971 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007972 if (addr == NULL) {
7973 if (s->mode == WPAS_MODE_P2P_GO)
7974 return s;
7975 continue;
7976 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007977 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7978 return s; /* peer is GO in the persistent group */
7979 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7980 continue;
7981 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007982 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007983 addr, ETH_ALEN) == 0)
7984 return s; /* peer is P2P client in persistent
7985 * group */
7986 }
7987 }
7988
7989 return NULL;
7990}
7991
7992
7993void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7994 const u8 *addr)
7995{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007996 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007997 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007998 /*
7999 * This can happen if WPS provisioning step is not terminated
8000 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8001 * peer was able to connect, there is no need to time out group
8002 * formation after this, though. In addition, this is used with
8003 * the initial connection wait on the GO as a separate formation
8004 * timeout and as such, expected to be hit after the initial WPS
8005 * provisioning step.
8006 */
8007 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008008
8009 if (!wpa_s->p2p_go_group_formation_completed &&
8010 !wpa_s->group_formation_reported) {
8011 /*
8012 * GO has not yet notified group formation success since
8013 * the WPS step was not completed cleanly. Do that
8014 * notification now since the P2P Client was able to
8015 * connect and as such, must have received the
8016 * credential from the WPS step.
8017 */
8018 if (wpa_s->global->p2p)
8019 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008020 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008021 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008022 }
8023 if (!wpa_s->p2p_go_group_formation_completed) {
8024 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8025 wpa_s->p2p_go_group_formation_completed = 1;
8026 wpa_s->global->p2p_group_formation = NULL;
8027 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008028 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008029 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008030 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008031 if (addr == NULL)
8032 return;
8033 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8034}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008035
Dmitry Shmidt04949592012-07-19 12:16:46 -07008036
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008037static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8038 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008039{
8040 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008041 int ret = 0;
8042
Dmitry Shmidt04949592012-07-19 12:16:46 -07008043 if (wpa_s->global->p2p_group_formation)
8044 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008045 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008046 offchannel_send_action_done(wpa_s);
8047 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008048 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008049 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8050 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8051 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8052 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008053 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008054 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008055 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008056 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008057 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008058 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008059 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008060}
8061
8062
8063int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8064{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008065 int res;
8066
Dmitry Shmidt04949592012-07-19 12:16:46 -07008067 if (!wpa_s->p2p_fallback_to_go_neg ||
8068 wpa_s->p2p_in_provisioning <= 5)
8069 return 0;
8070
8071 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8072 return 0; /* peer operating as a GO */
8073
8074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8075 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008076 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008077 "reason=GO-not-found");
8078 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008079
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008080 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008081}
8082
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008083
8084unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8085{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008086 struct wpa_supplicant *ifs;
8087
8088 if (wpa_s->wpa_state > WPA_SCANNING) {
8089 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8090 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008091 wpa_s->conf->p2p_search_delay);
8092 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008093 }
8094
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008095 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8096 radio_list) {
8097 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8099 "delay due to concurrent operation on "
8100 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008101 wpa_s->conf->p2p_search_delay,
8102 ifs->ifname);
8103 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008104 }
8105 }
8106
8107 return 0;
8108}
8109
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008110
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008111static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8112 struct wpa_ssid *s, const u8 *addr,
8113 int iface_addr)
8114{
8115 struct psk_list_entry *psk, *tmp;
8116 int changed = 0;
8117
8118 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8119 list) {
8120 if ((iface_addr && !psk->p2p &&
8121 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8122 (!iface_addr && psk->p2p &&
8123 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8124 wpa_dbg(wpa_s, MSG_DEBUG,
8125 "P2P: Remove persistent group PSK list entry for "
8126 MACSTR " p2p=%u",
8127 MAC2STR(psk->addr), psk->p2p);
8128 dl_list_del(&psk->list);
8129 os_free(psk);
8130 changed++;
8131 }
8132 }
8133
8134 return changed;
8135}
8136
8137
8138void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8139 const u8 *p2p_dev_addr,
8140 const u8 *psk, size_t psk_len)
8141{
8142 struct wpa_ssid *ssid = wpa_s->current_ssid;
8143 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008144 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008145
8146 if (psk_len != sizeof(p->psk))
8147 return;
8148
8149 if (p2p_dev_addr) {
8150 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8151 " p2p_dev_addr=" MACSTR,
8152 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8153 if (is_zero_ether_addr(p2p_dev_addr))
8154 p2p_dev_addr = NULL;
8155 } else {
8156 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8157 MAC2STR(mac_addr));
8158 }
8159
8160 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8161 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8162 /* To be added to persistent group once created */
8163 if (wpa_s->global->add_psk == NULL) {
8164 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8165 if (wpa_s->global->add_psk == NULL)
8166 return;
8167 }
8168 p = wpa_s->global->add_psk;
8169 if (p2p_dev_addr) {
8170 p->p2p = 1;
8171 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8172 } else {
8173 p->p2p = 0;
8174 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8175 }
8176 os_memcpy(p->psk, psk, psk_len);
8177 return;
8178 }
8179
8180 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8181 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8182 return;
8183 }
8184
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008185 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008186 ssid->ssid_len);
8187 if (!persistent) {
8188 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8189 return;
8190 }
8191
8192 p = os_zalloc(sizeof(*p));
8193 if (p == NULL)
8194 return;
8195 if (p2p_dev_addr) {
8196 p->p2p = 1;
8197 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8198 } else {
8199 p->p2p = 0;
8200 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8201 }
8202 os_memcpy(p->psk, psk, psk_len);
8203
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008204 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8205 (last = dl_list_last(&persistent->psk_list,
8206 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008207 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8208 MACSTR " (p2p=%u) to make room for a new one",
8209 MAC2STR(last->addr), last->p2p);
8210 dl_list_del(&last->list);
8211 os_free(last);
8212 }
8213
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008214 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008215 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8216 p2p_dev_addr == NULL);
8217 if (p2p_dev_addr) {
8218 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8219 MACSTR, MAC2STR(p2p_dev_addr));
8220 } else {
8221 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8222 MAC2STR(mac_addr));
8223 }
8224 dl_list_add(&persistent->psk_list, &p->list);
8225
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008226 if (wpa_s->p2pdev->conf->update_config &&
8227 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008228 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008229}
8230
8231
8232static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8233 struct wpa_ssid *s, const u8 *addr,
8234 int iface_addr)
8235{
8236 int res;
8237
8238 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008239 if (res > 0 && wpa_s->conf->update_config &&
8240 wpa_config_write(wpa_s->confname, wpa_s->conf))
8241 wpa_dbg(wpa_s, MSG_DEBUG,
8242 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008243}
8244
8245
8246static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8247 const u8 *peer, int iface_addr)
8248{
8249 struct hostapd_data *hapd;
8250 struct hostapd_wpa_psk *psk, *prev, *rem;
8251 struct sta_info *sta;
8252
8253 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8254 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8255 return;
8256
8257 /* Remove per-station PSK entry */
8258 hapd = wpa_s->ap_iface->bss[0];
8259 prev = NULL;
8260 psk = hapd->conf->ssid.wpa_psk;
8261 while (psk) {
8262 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8263 (!iface_addr &&
8264 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8265 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8266 MACSTR " iface_addr=%d",
8267 MAC2STR(peer), iface_addr);
8268 if (prev)
8269 prev->next = psk->next;
8270 else
8271 hapd->conf->ssid.wpa_psk = psk->next;
8272 rem = psk;
8273 psk = psk->next;
8274 os_free(rem);
8275 } else {
8276 prev = psk;
8277 psk = psk->next;
8278 }
8279 }
8280
8281 /* Disconnect from group */
8282 if (iface_addr)
8283 sta = ap_get_sta(hapd, peer);
8284 else
8285 sta = ap_get_sta_p2p(hapd, peer);
8286 if (sta) {
8287 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8288 " (iface_addr=%d) from group",
8289 MAC2STR(peer), iface_addr);
8290 hostapd_drv_sta_deauth(hapd, sta->addr,
8291 WLAN_REASON_DEAUTH_LEAVING);
8292 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8293 }
8294}
8295
8296
8297void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8298 int iface_addr)
8299{
8300 struct wpa_ssid *s;
8301 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008302 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008303
8304 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8305
8306 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008307 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008308 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8309 continue;
8310 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008311 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8312 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008313 }
8314
8315 /* Remove from any operating group */
8316 for (w = wpa_s->global->ifaces; w; w = w->next)
8317 wpas_p2p_remove_client_go(w, peer, iface_addr);
8318}
8319
8320
8321static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8322{
8323 struct wpa_supplicant *wpa_s = eloop_ctx;
8324 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8325}
8326
8327
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008328static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8329{
8330 struct wpa_supplicant *wpa_s = eloop_ctx;
8331
8332 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8333 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8334}
8335
8336
8337int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8338 struct wpa_ssid *ssid)
8339{
8340 struct wpa_supplicant *iface;
8341
8342 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8343 if (!iface->current_ssid ||
8344 iface->current_ssid->frequency == freq ||
8345 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8346 !iface->current_ssid->p2p_group))
8347 continue;
8348
8349 /* Remove the connection with least priority */
8350 if (!wpas_is_p2p_prioritized(iface)) {
8351 /* STA connection has priority over existing
8352 * P2P connection, so remove the interface. */
8353 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8354 eloop_register_timeout(0, 0,
8355 wpas_p2p_group_freq_conflict,
8356 iface, NULL);
8357 /* If connection in progress is P2P connection, do not
8358 * proceed for the connection. */
8359 if (wpa_s == iface)
8360 return -1;
8361 else
8362 return 0;
8363 } else {
8364 /* P2P connection has priority, disable the STA network
8365 */
8366 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8367 ssid);
8368 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8369 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8370 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8371 ETH_ALEN);
8372 /* If P2P connection is in progress, continue
8373 * connecting...*/
8374 if (wpa_s == iface)
8375 return 0;
8376 else
8377 return -1;
8378 }
8379 }
8380
8381 return 0;
8382}
8383
8384
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008385int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8386{
8387 struct wpa_ssid *ssid = wpa_s->current_ssid;
8388
8389 if (ssid == NULL || !ssid->p2p_group)
8390 return 0;
8391
8392 if (wpa_s->p2p_last_4way_hs_fail &&
8393 wpa_s->p2p_last_4way_hs_fail == ssid) {
8394 u8 go_dev_addr[ETH_ALEN];
8395 struct wpa_ssid *persistent;
8396
8397 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8398 ssid->ssid,
8399 ssid->ssid_len) <= 0) {
8400 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8401 goto disconnect;
8402 }
8403
8404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8405 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008406 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008407 ssid->ssid,
8408 ssid->ssid_len);
8409 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8410 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8411 goto disconnect;
8412 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008413 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008414 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8415 persistent->id);
8416 disconnect:
8417 wpa_s->p2p_last_4way_hs_fail = NULL;
8418 /*
8419 * Remove the group from a timeout to avoid issues with caller
8420 * continuing to use the interface if this is on a P2P group
8421 * interface.
8422 */
8423 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8424 wpa_s, NULL);
8425 return 1;
8426 }
8427
8428 wpa_s->p2p_last_4way_hs_fail = ssid;
8429 return 0;
8430}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008431
8432
8433#ifdef CONFIG_WPS_NFC
8434
8435static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8436 struct wpabuf *p2p)
8437{
8438 struct wpabuf *ret;
8439 size_t wsc_len;
8440
8441 if (p2p == NULL) {
8442 wpabuf_free(wsc);
8443 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8444 return NULL;
8445 }
8446
8447 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8448 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8449 if (ret == NULL) {
8450 wpabuf_free(wsc);
8451 wpabuf_free(p2p);
8452 return NULL;
8453 }
8454
8455 wpabuf_put_be16(ret, wsc_len);
8456 if (wsc)
8457 wpabuf_put_buf(ret, wsc);
8458 wpabuf_put_be16(ret, wpabuf_len(p2p));
8459 wpabuf_put_buf(ret, p2p);
8460
8461 wpabuf_free(wsc);
8462 wpabuf_free(p2p);
8463 wpa_hexdump_buf(MSG_DEBUG,
8464 "P2P: Generated NFC connection handover message", ret);
8465
8466 if (ndef && ret) {
8467 struct wpabuf *tmp;
8468 tmp = ndef_build_p2p(ret);
8469 wpabuf_free(ret);
8470 if (tmp == NULL) {
8471 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8472 return NULL;
8473 }
8474 ret = tmp;
8475 }
8476
8477 return ret;
8478}
8479
8480
8481static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8482 struct wpa_ssid **ssid, u8 *go_dev_addr)
8483{
8484 struct wpa_supplicant *iface;
8485
8486 if (go_dev_addr)
8487 os_memset(go_dev_addr, 0, ETH_ALEN);
8488 if (ssid)
8489 *ssid = NULL;
8490 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8491 if (iface->wpa_state < WPA_ASSOCIATING ||
8492 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8493 !iface->current_ssid->p2p_group ||
8494 iface->current_ssid->mode != WPAS_MODE_INFRA)
8495 continue;
8496 if (ssid)
8497 *ssid = iface->current_ssid;
8498 if (go_dev_addr)
8499 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8500 return iface->assoc_freq;
8501 }
8502 return 0;
8503}
8504
8505
8506struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8507 int ndef)
8508{
8509 struct wpabuf *wsc, *p2p;
8510 struct wpa_ssid *ssid;
8511 u8 go_dev_addr[ETH_ALEN];
8512 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8513
8514 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8515 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8516 return NULL;
8517 }
8518
8519 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8520 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8521 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8522 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8523 return NULL;
8524 }
8525
8526 if (cli_freq == 0) {
8527 wsc = wps_build_nfc_handover_req_p2p(
8528 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8529 } else
8530 wsc = NULL;
8531 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8532 go_dev_addr, ssid ? ssid->ssid : NULL,
8533 ssid ? ssid->ssid_len : 0);
8534
8535 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8536}
8537
8538
8539struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8540 int ndef, int tag)
8541{
8542 struct wpabuf *wsc, *p2p;
8543 struct wpa_ssid *ssid;
8544 u8 go_dev_addr[ETH_ALEN];
8545 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8546
8547 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8548 return NULL;
8549
8550 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8551 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8552 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8553 return NULL;
8554
8555 if (cli_freq == 0) {
8556 wsc = wps_build_nfc_handover_sel_p2p(
8557 wpa_s->parent->wps,
8558 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8559 DEV_PW_NFC_CONNECTION_HANDOVER,
8560 wpa_s->conf->wps_nfc_dh_pubkey,
8561 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8562 } else
8563 wsc = NULL;
8564 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8565 go_dev_addr, ssid ? ssid->ssid : NULL,
8566 ssid ? ssid->ssid_len : 0);
8567
8568 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8569}
8570
8571
8572static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8573 struct p2p_nfc_params *params)
8574{
8575 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8576 "connection handover (freq=%d)",
8577 params->go_freq);
8578
8579 if (params->go_freq && params->go_ssid_len) {
8580 wpa_s->p2p_wps_method = WPS_NFC;
8581 wpa_s->pending_join_wps_method = WPS_NFC;
8582 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8583 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8584 ETH_ALEN);
8585 return wpas_p2p_join_start(wpa_s, params->go_freq,
8586 params->go_ssid,
8587 params->go_ssid_len);
8588 }
8589
8590 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8591 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008592 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008593 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8594 params->go_ssid_len ? params->go_ssid : NULL,
8595 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008596}
8597
8598
8599static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8600 struct p2p_nfc_params *params, int tag)
8601{
8602 int res, persistent;
8603 struct wpa_ssid *ssid;
8604
8605 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8606 "connection handover");
8607 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8608 ssid = wpa_s->current_ssid;
8609 if (ssid == NULL)
8610 continue;
8611 if (ssid->mode != WPAS_MODE_P2P_GO)
8612 continue;
8613 if (wpa_s->ap_iface == NULL)
8614 continue;
8615 break;
8616 }
8617 if (wpa_s == NULL) {
8618 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8619 return -1;
8620 }
8621
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008622 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008623 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008624 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008625 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8626 return -1;
8627 }
8628 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008629 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8630 wpa_s->p2pdev->p2p_oob_dev_pw,
8631 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8632 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008633 if (res)
8634 return res;
8635
8636 if (!tag) {
8637 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8638 return 0;
8639 }
8640
8641 if (!params->peer ||
8642 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8643 return 0;
8644
8645 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8646 " to join", MAC2STR(params->peer->p2p_device_addr));
8647
8648 wpa_s->global->p2p_invite_group = wpa_s;
8649 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008650 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008651 params->peer->p2p_device_addr,
8652 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008653 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008654
8655 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8656 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8657 ssid->ssid, ssid->ssid_len, ssid->frequency,
8658 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008659 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008660}
8661
8662
8663static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8664 struct p2p_nfc_params *params,
8665 int forced_freq)
8666{
8667 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8668 "connection handover");
8669 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8670 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008671 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008672 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8673 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008674}
8675
8676
8677static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8678 struct p2p_nfc_params *params,
8679 int forced_freq)
8680{
8681 int res;
8682
8683 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8684 "connection handover");
8685 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8686 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008687 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008688 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8689 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008690 if (res)
8691 return res;
8692
8693 res = wpas_p2p_listen(wpa_s, 60);
8694 if (res) {
8695 p2p_unauthorize(wpa_s->global->p2p,
8696 params->peer->p2p_device_addr);
8697 }
8698
8699 return res;
8700}
8701
8702
8703static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8704 const struct wpabuf *data,
8705 int sel, int tag, int forced_freq)
8706{
8707 const u8 *pos, *end;
8708 u16 len, id;
8709 struct p2p_nfc_params params;
8710 int res;
8711
8712 os_memset(&params, 0, sizeof(params));
8713 params.sel = sel;
8714
8715 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8716
8717 pos = wpabuf_head(data);
8718 end = pos + wpabuf_len(data);
8719
8720 if (end - pos < 2) {
8721 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8722 "attributes");
8723 return -1;
8724 }
8725 len = WPA_GET_BE16(pos);
8726 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008727 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008728 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8729 "attributes");
8730 return -1;
8731 }
8732 params.wsc_attr = pos;
8733 params.wsc_len = len;
8734 pos += len;
8735
8736 if (end - pos < 2) {
8737 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8738 "attributes");
8739 return -1;
8740 }
8741 len = WPA_GET_BE16(pos);
8742 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008743 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008744 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8745 "attributes");
8746 return -1;
8747 }
8748 params.p2p_attr = pos;
8749 params.p2p_len = len;
8750 pos += len;
8751
8752 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8753 params.wsc_attr, params.wsc_len);
8754 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8755 params.p2p_attr, params.p2p_len);
8756 if (pos < end) {
8757 wpa_hexdump(MSG_DEBUG,
8758 "P2P: Ignored extra data after P2P attributes",
8759 pos, end - pos);
8760 }
8761
8762 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8763 if (res)
8764 return res;
8765
8766 if (params.next_step == NO_ACTION)
8767 return 0;
8768
8769 if (params.next_step == BOTH_GO) {
8770 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8771 MAC2STR(params.peer->p2p_device_addr));
8772 return 0;
8773 }
8774
8775 if (params.next_step == PEER_CLIENT) {
8776 if (!is_zero_ether_addr(params.go_dev_addr)) {
8777 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8778 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8779 " ssid=\"%s\"",
8780 MAC2STR(params.peer->p2p_device_addr),
8781 params.go_freq,
8782 MAC2STR(params.go_dev_addr),
8783 wpa_ssid_txt(params.go_ssid,
8784 params.go_ssid_len));
8785 } else {
8786 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8787 "peer=" MACSTR " freq=%d",
8788 MAC2STR(params.peer->p2p_device_addr),
8789 params.go_freq);
8790 }
8791 return 0;
8792 }
8793
8794 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8795 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8796 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8797 return 0;
8798 }
8799
8800 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8801 wpa_s->p2p_oob_dev_pw = NULL;
8802
8803 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8804 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8805 "received");
8806 return -1;
8807 }
8808
8809 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8810 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8811 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8812 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8813 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8814 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8815 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8816
8817 if (tag) {
8818 if (id < 0x10) {
8819 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8820 "peer OOB Device Password Id %u", id);
8821 return -1;
8822 }
8823 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8824 "Device Password Id %u", id);
8825 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8826 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8827 params.oob_dev_pw_len -
8828 WPS_OOB_PUBKEY_HASH_LEN - 2);
8829 wpa_s->p2p_oob_dev_pw_id = id;
8830 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8831 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8832 params.oob_dev_pw_len -
8833 WPS_OOB_PUBKEY_HASH_LEN - 2);
8834 if (wpa_s->p2p_oob_dev_pw == NULL)
8835 return -1;
8836
8837 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8838 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8839 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8840 return -1;
8841 } else {
8842 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8843 "without Device Password");
8844 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8845 }
8846
8847 switch (params.next_step) {
8848 case NO_ACTION:
8849 case BOTH_GO:
8850 case PEER_CLIENT:
8851 /* already covered above */
8852 return 0;
8853 case JOIN_GROUP:
8854 return wpas_p2p_nfc_join_group(wpa_s, &params);
8855 case AUTH_JOIN:
8856 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8857 case INIT_GO_NEG:
8858 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8859 case RESP_GO_NEG:
8860 /* TODO: use own OOB Dev Pw */
8861 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8862 }
8863
8864 return -1;
8865}
8866
8867
8868int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8869 const struct wpabuf *data, int forced_freq)
8870{
8871 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8872 return -1;
8873
8874 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8875}
8876
8877
8878int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8879 const struct wpabuf *req,
8880 const struct wpabuf *sel, int forced_freq)
8881{
8882 struct wpabuf *tmp;
8883 int ret;
8884
8885 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8886 return -1;
8887
8888 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8889
8890 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8891 wpabuf_head(req), wpabuf_len(req));
8892 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8893 wpabuf_head(sel), wpabuf_len(sel));
8894 if (forced_freq)
8895 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8896 tmp = ndef_parse_p2p(init ? sel : req);
8897 if (tmp == NULL) {
8898 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8899 return -1;
8900 }
8901
8902 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8903 forced_freq);
8904 wpabuf_free(tmp);
8905
8906 return ret;
8907}
8908
8909
8910int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8911{
8912 const u8 *if_addr;
8913 int go_intent = wpa_s->conf->p2p_go_intent;
8914 struct wpa_supplicant *iface;
8915
8916 if (wpa_s->global->p2p == NULL)
8917 return -1;
8918
8919 if (!enabled) {
8920 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8921 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8922 {
8923 if (!iface->ap_iface)
8924 continue;
8925 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8926 }
8927 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8928 0, NULL);
8929 if (wpa_s->p2p_nfc_tag_enabled)
8930 wpas_p2p_remove_pending_group_interface(wpa_s);
8931 wpa_s->p2p_nfc_tag_enabled = 0;
8932 return 0;
8933 }
8934
8935 if (wpa_s->global->p2p_disabled)
8936 return -1;
8937
8938 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8939 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8940 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8941 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8942 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8943 "to allow static handover cases");
8944 return -1;
8945 }
8946
8947 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8948
8949 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8950 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8951 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8952 if (wpa_s->p2p_oob_dev_pw == NULL)
8953 return -1;
8954 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8955
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008956 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8957 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8958 /*
8959 * P2P Group Interface present and the command came on group
8960 * interface, so enable the token for the current interface.
8961 */
8962 wpa_s->create_p2p_iface = 0;
8963 } else {
8964 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8965 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008966
8967 if (wpa_s->create_p2p_iface) {
8968 enum wpa_driver_if_type iftype;
8969 /* Prepare to add a new interface for the group */
8970 iftype = WPA_IF_P2P_GROUP;
8971 if (go_intent == 15)
8972 iftype = WPA_IF_P2P_GO;
8973 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8974 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8975 "interface for the group");
8976 return -1;
8977 }
8978
8979 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008980 } else if (wpa_s->p2p_mgmt)
8981 if_addr = wpa_s->parent->own_addr;
8982 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008983 if_addr = wpa_s->own_addr;
8984
8985 wpa_s->p2p_nfc_tag_enabled = enabled;
8986
8987 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8988 struct hostapd_data *hapd;
8989 if (iface->ap_iface == NULL)
8990 continue;
8991 hapd = iface->ap_iface->bss[0];
8992 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8993 hapd->conf->wps_nfc_dh_pubkey =
8994 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8995 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8996 hapd->conf->wps_nfc_dh_privkey =
8997 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8998 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8999 hapd->conf->wps_nfc_dev_pw =
9000 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9001 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9002
9003 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9004 wpa_dbg(iface, MSG_DEBUG,
9005 "P2P: Failed to enable NFC Tag for GO");
9006 }
9007 }
9008 p2p_set_authorized_oob_dev_pw_id(
9009 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9010 if_addr);
9011
9012 return 0;
9013}
9014
9015#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009016
9017
9018static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9019 struct wpa_used_freq_data *freqs,
9020 unsigned int num)
9021{
9022 u8 curr_chan, cand, chan;
9023 unsigned int i;
9024
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009025 /*
9026 * If possible, optimize the Listen channel to be a channel that is
9027 * already used by one of the other interfaces.
9028 */
9029 if (!wpa_s->conf->p2p_optimize_listen_chan)
9030 return;
9031
9032 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9033 return;
9034
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009035 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9036 for (i = 0, cand = 0; i < num; i++) {
9037 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9038 if (curr_chan == chan) {
9039 cand = 0;
9040 break;
9041 }
9042
9043 if (chan == 1 || chan == 6 || chan == 11)
9044 cand = chan;
9045 }
9046
9047 if (cand) {
9048 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009049 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009050 cand);
9051 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9052 }
9053}
9054
9055
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009056static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009057{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009058 struct hostapd_config *conf;
9059 struct p2p_go_neg_results params;
9060 struct csa_settings csa_settings;
9061 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9062 int old_freq = current_ssid->frequency;
9063 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009064
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009065 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9066 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9067 return -1;
9068 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009069
9070 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009071 * TODO: This function may not always work correctly. For example,
9072 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009073 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009074 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009075 wpa_dbg(wpa_s, MSG_DEBUG,
9076 "P2P CSA: Failed to select new frequency for GO");
9077 return -1;
9078 }
9079
9080 if (current_ssid->frequency == params.freq) {
9081 wpa_dbg(wpa_s, MSG_DEBUG,
9082 "P2P CSA: Selected same frequency - not moving GO");
9083 return 0;
9084 }
9085
9086 conf = hostapd_config_defaults();
9087 if (!conf) {
9088 wpa_dbg(wpa_s, MSG_DEBUG,
9089 "P2P CSA: Failed to allocate default config");
9090 return -1;
9091 }
9092
9093 current_ssid->frequency = params.freq;
9094 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9095 wpa_dbg(wpa_s, MSG_DEBUG,
9096 "P2P CSA: Failed to create new GO config");
9097 ret = -1;
9098 goto out;
9099 }
9100
9101 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9102 wpa_dbg(wpa_s, MSG_DEBUG,
9103 "P2P CSA: CSA to a different band is not supported");
9104 ret = -1;
9105 goto out;
9106 }
9107
9108 os_memset(&csa_settings, 0, sizeof(csa_settings));
9109 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9110 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9111 csa_settings.freq_params.freq = params.freq;
9112 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9113 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9114 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9115
9116 if (conf->ieee80211ac) {
9117 int freq1 = 0, freq2 = 0;
9118 u8 chan, opclass;
9119
9120 if (ieee80211_freq_to_channel_ext(params.freq,
9121 conf->secondary_channel,
9122 conf->vht_oper_chwidth,
9123 &opclass, &chan) ==
9124 NUM_HOSTAPD_MODES) {
9125 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9126 ret = -1;
9127 goto out;
9128 }
9129
9130 if (conf->vht_oper_centr_freq_seg0_idx)
9131 freq1 = ieee80211_chan_to_freq(
9132 NULL, opclass,
9133 conf->vht_oper_centr_freq_seg0_idx);
9134
9135 if (conf->vht_oper_centr_freq_seg1_idx)
9136 freq2 = ieee80211_chan_to_freq(
9137 NULL, opclass,
9138 conf->vht_oper_centr_freq_seg1_idx);
9139
9140 if (freq1 < 0 || freq2 < 0) {
9141 wpa_dbg(wpa_s, MSG_DEBUG,
9142 "P2P CSA: Selected invalid VHT center freqs");
9143 ret = -1;
9144 goto out;
9145 }
9146
9147 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9148 csa_settings.freq_params.center_freq1 = freq1;
9149 csa_settings.freq_params.center_freq2 = freq2;
9150
9151 switch (conf->vht_oper_chwidth) {
9152 case VHT_CHANWIDTH_80MHZ:
9153 case VHT_CHANWIDTH_80P80MHZ:
9154 csa_settings.freq_params.bandwidth = 80;
9155 break;
9156 case VHT_CHANWIDTH_160MHZ:
9157 csa_settings.freq_params.bandwidth = 160;
9158 break;
9159 }
9160 }
9161
9162 ret = ap_switch_channel(wpa_s, &csa_settings);
9163out:
9164 current_ssid->frequency = old_freq;
9165 hostapd_config_free(conf);
9166 return ret;
9167}
9168
9169
9170static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9171{
9172 struct p2p_go_neg_results params;
9173 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9174
9175 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9176
9177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9178 current_ssid->frequency);
9179
9180 /* Stop the AP functionality */
9181 /* TODO: Should do this in a way that does not indicated to possible
9182 * P2P Clients in the group that the group is terminated. */
9183 wpa_supplicant_ap_deinit(wpa_s);
9184
9185 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009186 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009187 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9188 wpas_p2p_group_delete(wpa_s,
9189 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9190 return;
9191 }
9192 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9193 params.freq);
9194
9195 if (params.freq &&
9196 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9197 wpa_printf(MSG_DEBUG,
9198 "P2P: Selected freq (%u MHz) is not valid for P2P",
9199 params.freq);
9200 wpas_p2p_group_delete(wpa_s,
9201 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9202 return;
9203 }
9204
9205 /* Update the frequency */
9206 current_ssid->frequency = params.freq;
9207 wpa_s->connect_without_scan = current_ssid;
9208 wpa_s->reassociate = 1;
9209 wpa_s->disconnected = 0;
9210 wpa_supplicant_req_scan(wpa_s, 0, 0);
9211}
9212
9213
9214static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9215{
9216 struct wpa_supplicant *wpa_s = eloop_ctx;
9217
9218 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009219 return;
9220
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009221 wpas_p2p_go_update_common_freqs(wpa_s);
9222
9223 /* Do not move GO in the middle of a CSA */
9224 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9225 wpa_printf(MSG_DEBUG,
9226 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009227 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009228 }
9229
9230 /*
9231 * First, try a channel switch flow. If it is not supported or fails,
9232 * take down the GO and bring it up again.
9233 */
9234 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9235 wpas_p2p_move_go_no_csa(wpa_s);
9236}
9237
9238
9239static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9240{
9241 struct wpa_supplicant *wpa_s = eloop_ctx;
9242 struct wpa_used_freq_data *freqs = NULL;
9243 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009244
9245 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9246 if (!freqs)
9247 return;
9248
9249 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9250
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009251 /* Previous attempt to move a GO was not possible -- try again. */
9252 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9253 WPAS_P2P_CHANNEL_UPDATE_ANY);
9254
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009255 os_free(freqs);
9256}
9257
9258
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009259/*
9260 * Consider moving a GO from its currently used frequency:
9261 * 1. It is possible that due to regulatory consideration the frequency
9262 * can no longer be used and there is a need to evacuate the GO.
9263 * 2. It is possible that due to MCC considerations, it would be preferable
9264 * to move the GO to a channel that is currently used by some other
9265 * station interface.
9266 *
9267 * In case a frequency that became invalid is once again valid, cancel a
9268 * previously initiated GO frequency change.
9269 */
9270static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9271 struct wpa_used_freq_data *freqs,
9272 unsigned int num)
9273{
9274 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9275 unsigned int timeout;
9276 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009277 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009278
9279 wpas_p2p_go_update_common_freqs(wpa_s);
9280
9281 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009282 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009283 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009284 for (i = 0, invalid_freq = 0; i < num; i++) {
9285 if (freqs[i].freq == freq) {
9286 flags = freqs[i].flags;
9287
9288 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009289 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9290 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009291 wpa_dbg(wpa_s, MSG_DEBUG,
9292 "P2P: Freq=%d MHz no longer valid for GO",
9293 freq);
9294 invalid_freq = 1;
9295 }
9296 } else if (freqs[i].flags == 0) {
9297 /* Freq is not used by any other station interface */
9298 continue;
9299 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009300 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009301 /* Freq is not valid for P2P use cases */
9302 continue;
9303 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9304 P2P_GO_FREQ_MOVE_SCM) {
9305 policy_move = 1;
9306 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9307 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9308 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9309 policy_move = 1;
9310 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9311 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9312 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9313 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9314 policy_move = 1;
9315 } else if ((wpa_s->drv_flags &
9316 WPA_DRIVER_FLAGS_AP_CSA) &&
9317 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9318 u8 chan;
9319
9320 /*
9321 * We do not support CSA between bands, so move
9322 * GO only within the same band.
9323 */
9324 if (wpa_s->ap_iface->current_mode->mode ==
9325 ieee80211_freq_to_chan(freqs[i].freq,
9326 &chan))
9327 policy_move = 1;
9328 }
9329 }
9330 }
9331
9332 wpa_dbg(wpa_s, MSG_DEBUG,
9333 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9334 invalid_freq, policy_move, flags);
9335
9336 /*
9337 * The channel is valid, or we are going to have a policy move, so
9338 * cancel timeout.
9339 */
9340 if (!invalid_freq || policy_move) {
9341 wpa_dbg(wpa_s, MSG_DEBUG,
9342 "P2P: Cancel a GO move from freq=%d MHz", freq);
9343 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9344
9345 if (wpas_p2p_in_progress(wpa_s)) {
9346 wpa_dbg(wpa_s, MSG_DEBUG,
9347 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9348 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9349 wpa_s, NULL);
9350 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9351 wpas_p2p_reconsider_moving_go,
9352 wpa_s, NULL);
9353 return;
9354 }
9355 }
9356
9357 if (!invalid_freq && (!policy_move || flags != 0)) {
9358 wpa_dbg(wpa_s, MSG_DEBUG,
9359 "P2P: Not initiating a GO frequency change");
9360 return;
9361 }
9362
9363 /*
9364 * Do not consider moving GO if it is in the middle of a CSA. When the
9365 * CSA is finished this flow should be retriggered.
9366 */
9367 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9368 wpa_dbg(wpa_s, MSG_DEBUG,
9369 "P2P: Not initiating a GO frequency change - CSA is in progress");
9370 return;
9371 }
9372
9373 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9374 timeout = P2P_GO_FREQ_CHANGE_TIME;
9375 else
9376 timeout = 0;
9377
9378 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9379 freq, timeout);
9380 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9381 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9382}
9383
9384
9385static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9386 struct wpa_used_freq_data *freqs,
9387 unsigned int num,
9388 enum wpas_p2p_channel_update_trig trig)
9389{
9390 struct wpa_supplicant *ifs;
9391
9392 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9393 NULL);
9394
9395 /*
9396 * Travers all the radio interfaces, and for each GO interface, check
9397 * if there is a need to move the GO from the frequency it is using,
9398 * or in case the frequency is valid again, cancel the evacuation flow.
9399 */
9400 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9401 radio_list) {
9402 if (ifs->current_ssid == NULL ||
9403 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9404 continue;
9405
9406 /*
9407 * The GO was just started or completed channel switch, no need
9408 * to move it.
9409 */
9410 if (wpa_s == ifs &&
9411 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9412 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9413 wpa_dbg(wpa_s, MSG_DEBUG,
9414 "P2P: GO move - schedule re-consideration");
9415 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9416 wpas_p2p_reconsider_moving_go,
9417 wpa_s, NULL);
9418 continue;
9419 }
9420
9421 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9422 }
9423}
9424
9425
9426void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9427{
9428 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9429 return;
9430
9431 wpas_p2p_update_channel_list(wpa_s,
9432 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9433}
9434
9435
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009436void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9437{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009438 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9440 "the management interface is being removed");
9441 wpas_p2p_deinit_global(wpa_s->global);
9442 }
9443}
9444
9445
9446void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9447{
9448 if (wpa_s->ap_iface->bss)
9449 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9450 wpas_p2p_group_deinit(wpa_s);
9451}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009452
9453
9454int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9455 unsigned int period, unsigned int interval,
9456 unsigned int count)
9457{
9458 struct p2p_data *p2p = wpa_s->global->p2p;
9459 u8 *device_types;
9460 size_t dev_types_len;
9461 struct wpabuf *buf;
9462 int ret;
9463
9464 if (wpa_s->p2p_lo_started) {
9465 wpa_dbg(wpa_s, MSG_DEBUG,
9466 "P2P Listen offload is already started");
9467 return 0;
9468 }
9469
9470 if (wpa_s->global->p2p == NULL ||
9471 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9472 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9473 return -1;
9474 }
9475
9476 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9477 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9478 freq);
9479 return -1;
9480 }
9481
9482 /* Get device type */
9483 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9484 WPS_DEV_TYPE_LEN;
9485 device_types = os_malloc(dev_types_len);
9486 if (!device_types)
9487 return -1;
9488 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9489 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9490 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9491
9492 /* Get Probe Response IE(s) */
9493 buf = p2p_build_probe_resp_template(p2p, freq);
9494 if (!buf) {
9495 os_free(device_types);
9496 return -1;
9497 }
9498
9499 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9500 device_types, dev_types_len,
9501 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9502 if (ret < 0)
9503 wpa_dbg(wpa_s, MSG_DEBUG,
9504 "P2P: Failed to start P2P listen offload");
9505
9506 os_free(device_types);
9507 wpabuf_free(buf);
9508
9509 if (ret == 0) {
9510 wpa_s->p2p_lo_started = 1;
9511
9512 /* Stop current P2P listen if any */
9513 wpas_stop_listen(wpa_s);
9514 }
9515
9516 return ret;
9517}
9518
9519
9520int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9521{
9522 int ret;
9523
9524 if (!wpa_s->p2p_lo_started)
9525 return 0;
9526
9527 ret = wpa_drv_p2p_lo_stop(wpa_s);
9528 if (ret < 0)
9529 wpa_dbg(wpa_s, MSG_DEBUG,
9530 "P2P: Failed to stop P2P listen offload");
9531
9532 wpa_s->p2p_lo_started = 0;
9533 return ret;
9534}