blob: 3d3296f9524d3d6faa494e4ed3be22e20b215ef3 [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
4361 // store generated MAC address.
4362 if (wpa_s->conf->p2p_device_persistent_mac_addr)
4363 os_free(wpa_s->conf->p2p_device_persistent_mac_addr);
4364 size_t mac_addr_str_len = sizeof("00:00:00:00:00:00");
4365 wpa_s->conf->p2p_device_persistent_mac_addr =
4366 os_zalloc(mac_addr_str_len + 1);
4367 os_snprintf(wpa_s->conf->p2p_device_persistent_mac_addr,
4368 mac_addr_str_len, MACSTR, MAC2STR(addr));
4369 } else {
4370 // If there are existing saved groups, restore last MAC address.
4371 // if there is no last used MAC address, the last one is factory MAC.
4372 if (!wpa_s->conf->p2p_device_persistent_mac_addr)
4373 return 0;
4374
4375 if (hwaddr_aton(wpa_s->conf->p2p_device_persistent_mac_addr, addr) < 0)
4376 return -EINVAL;
4377 wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
4378 }
4379
4380 if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
4381 wpa_msg(wpa_s, MSG_INFO,
4382 "Failed to set random MAC address");
4383 return -EINVAL;
4384 }
4385
4386 if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
4387 wpa_msg(wpa_s, MSG_INFO,
4388 "Could not update MAC address information");
4389 return -EINVAL;
4390 }
4391
4392 wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
4393 MAC2STR(addr));
4394
4395 return 0;
4396}
4397
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399/**
4400 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4401 * @global: Pointer to global data from wpa_supplicant_init()
4402 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4403 * Returns: 0 on success, -1 on failure
4404 */
4405int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4406{
4407 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408 int i;
4409
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004410 if (wpa_s->conf->p2p_disabled)
4411 return 0;
4412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4414 return 0;
4415
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004416 if (global->p2p)
4417 return 0;
4418
Jimmy Chenf887c7b2018-11-13 15:19:57 +08004419 if (wpas_p2p_mac_setup(wpa_s) < 0) {
4420 wpa_msg(wpa_s, MSG_ERROR,
4421 "Failed to initialize P2P random MAC address.");
4422 return -1;
4423 }
4424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004427 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 p2p.p2p_scan = wpas_p2p_scan;
4429 p2p.send_action = wpas_send_action;
4430 p2p.send_action_done = wpas_send_action_done;
4431 p2p.go_neg_completed = wpas_go_neg_completed;
4432 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4433 p2p.dev_found = wpas_dev_found;
4434 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004435 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004436 p2p.start_listen = wpas_start_listen;
4437 p2p.stop_listen = wpas_stop_listen;
4438 p2p.send_probe_resp = wpas_send_probe_resp;
4439 p2p.sd_request = wpas_sd_request;
4440 p2p.sd_response = wpas_sd_response;
4441 p2p.prov_disc_req = wpas_prov_disc_req;
4442 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004443 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 p2p.invitation_process = wpas_invitation_process;
4445 p2p.invitation_received = wpas_invitation_received;
4446 p2p.invitation_result = wpas_invitation_result;
4447 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004448 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004449 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004450 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004451 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004452 p2p.get_persistent_group = wpas_get_persistent_group;
4453 p2p.get_go_info = wpas_get_go_info;
4454 p2p.remove_stale_groups = wpas_remove_stale_groups;
4455 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4456 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4457 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004458 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459
4460 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004461 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 p2p.dev_name = wpa_s->conf->device_name;
4463 p2p.manufacturer = wpa_s->conf->manufacturer;
4464 p2p.model_name = wpa_s->conf->model_name;
4465 p2p.model_number = wpa_s->conf->model_number;
4466 p2p.serial_number = wpa_s->conf->serial_number;
4467 if (wpa_s->wps) {
4468 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4469 p2p.config_methods = wpa_s->wps->config_methods;
4470 }
4471
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004472 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4473 wpa_printf(MSG_ERROR,
4474 "P2P: Failed to configure supported channel list");
4475 return -1;
4476 }
4477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 if (wpa_s->conf->p2p_listen_reg_class &&
4479 wpa_s->conf->p2p_listen_channel) {
4480 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4481 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004482 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484 /*
4485 * Pick one of the social channels randomly as the listen
4486 * channel.
4487 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004488 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4489 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004490 wpa_printf(MSG_INFO,
4491 "P2P: No social channels supported by the driver - do not enable P2P");
4492 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004493 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004494 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004496 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4497 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498
4499 if (wpa_s->conf->p2p_oper_reg_class &&
4500 wpa_s->conf->p2p_oper_channel) {
4501 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4502 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4503 p2p.cfg_op_channel = 1;
4504 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4505 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4506
4507 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004509 * Use random operation channel from 2.4 GHz band social
4510 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4511 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004513 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4514 &p2p.op_channel) != 0) {
4515 wpa_printf(MSG_ERROR,
4516 "P2P: Failed to select random social channel as operation channel");
4517 return -1;
4518 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 p2p.cfg_op_channel = 0;
4520 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4521 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4522 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004523
4524 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4525 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4526 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4527 }
4528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4530 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4531 p2p.country[2] = 0x04;
4532 } else
4533 os_memcpy(p2p.country, "XX\x04", 3);
4534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4536 WPS_DEV_TYPE_LEN);
4537
4538 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4539 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4540 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4541
4542 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4543 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4544
4545 p2p.max_peers = 100;
4546
4547 if (wpa_s->conf->p2p_ssid_postfix) {
4548 p2p.ssid_postfix_len =
4549 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4550 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4551 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4552 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4553 p2p.ssid_postfix_len);
4554 }
4555
4556 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4557
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004558 p2p.max_listen = wpa_s->max_remain_on_chan;
4559
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004560 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4561 wpa_s->conf->p2p_passphrase_len <= 63)
4562 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4563 else
4564 p2p.passphrase_len = 8;
4565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 global->p2p = p2p_init(&p2p);
4567 if (global->p2p == NULL)
4568 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004569 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004570
4571 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4572 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4573 continue;
4574 p2p_add_wps_vendor_extension(
4575 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4576 }
4577
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004578 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580 return 0;
4581}
4582
4583
4584/**
4585 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4586 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4587 *
4588 * This function deinitialize per-interface P2P data.
4589 */
4590void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4591{
4592 if (wpa_s->driver && wpa_s->drv_priv)
4593 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004594
4595 if (wpa_s->go_params) {
4596 /* Clear any stored provisioning info */
4597 p2p_clear_provisioning_info(
4598 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004599 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004600 }
4601
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 os_free(wpa_s->go_params);
4603 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004604 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4606 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4607 wpa_s->p2p_long_listen = 0;
4608 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4609 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4610 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004611 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004612 wpas_p2p_listen_work_done(wpa_s);
4613 if (wpa_s->p2p_send_action_work) {
4614 os_free(wpa_s->p2p_send_action_work->ctx);
4615 radio_work_done(wpa_s->p2p_send_action_work);
4616 wpa_s->p2p_send_action_work = NULL;
4617 }
4618 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004620 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4621 wpa_s->p2p_oob_dev_pw = NULL;
4622
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004623 os_free(wpa_s->p2p_group_common_freqs);
4624 wpa_s->p2p_group_common_freqs = NULL;
4625 wpa_s->p2p_group_common_freqs_num = 0;
4626
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627 /* TODO: remove group interface from the driver if this wpa_s instance
4628 * is on top of a P2P group interface */
4629}
4630
4631
4632/**
4633 * wpas_p2p_deinit_global - Deinitialize global P2P module
4634 * @global: Pointer to global data from wpa_supplicant_init()
4635 *
4636 * This function deinitializes the global (per device) P2P module.
4637 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004638static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639{
4640 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004642 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004643
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004644 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645
4646 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004647 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4648 wpa_s = wpa_s->next;
4649 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650 tmp = global->ifaces;
4651 while (tmp &&
4652 (tmp == wpa_s ||
4653 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4654 tmp = tmp->next;
4655 }
4656 if (tmp == NULL)
4657 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004658 /* Disconnect from the P2P group and deinit the interface */
4659 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660 }
4661
4662 /*
4663 * Deinit GO data on any possibly remaining interface (if main
4664 * interface is used as GO).
4665 */
4666 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4667 if (wpa_s->ap_iface)
4668 wpas_p2p_group_deinit(wpa_s);
4669 }
4670
4671 p2p_deinit(global->p2p);
4672 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004673 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674}
4675
4676
4677static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4678{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004679 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004680 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004681 if (wpa_s->drv_flags &
4682 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4683 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4684 return 1; /* P2P group requires a new interface in every case
4685 */
4686 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4687 return 0; /* driver does not support concurrent operations */
4688 if (wpa_s->global->ifaces->next)
4689 return 1; /* more that one interface already in use */
4690 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4691 return 1; /* this interface is already in use */
4692 return 0;
4693}
4694
4695
4696static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4697 const u8 *peer_addr,
4698 enum p2p_wps_method wps_method,
4699 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004701 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004703 if (persistent_group && wpa_s->conf->persistent_reconnect)
4704 persistent_group = 2;
4705
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004706 /*
4707 * Increase GO config timeout if HT40 is used since it takes some time
4708 * to scan channels for coex purposes before the BSS can be started.
4709 */
4710 p2p_set_config_timeout(wpa_s->global->p2p,
4711 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004713 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4714 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004715 persistent_group, ssid ? ssid->ssid : NULL,
4716 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004717 wpa_s->p2p_pd_before_go_neg, pref_freq,
4718 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4719 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004720}
4721
4722
4723static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4724 const u8 *peer_addr,
4725 enum p2p_wps_method wps_method,
4726 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004727 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004728 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004730 if (persistent_group && wpa_s->conf->persistent_reconnect)
4731 persistent_group = 2;
4732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4734 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004736 ssid ? ssid->ssid_len : 0, pref_freq,
4737 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4738 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739}
4740
4741
4742static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4743{
4744 wpa_s->p2p_join_scan_count++;
4745 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4746 wpa_s->p2p_join_scan_count);
4747 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4748 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4749 " for join operationg - stop join attempt",
4750 MAC2STR(wpa_s->pending_join_iface_addr));
4751 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004752 if (wpa_s->p2p_auto_pd) {
4753 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004754 wpa_msg_global(wpa_s, MSG_INFO,
4755 P2P_EVENT_PROV_DISC_FAILURE
4756 " p2p_dev_addr=" MACSTR " status=N/A",
4757 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004758 return;
4759 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004760 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004761 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004762 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004763 }
4764}
4765
4766
Dmitry Shmidt04949592012-07-19 12:16:46 -07004767static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4768{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004769 int res;
4770 unsigned int num, i;
4771 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004772
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004773 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4774 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004775 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004776 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004777
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004778 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4779 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004780 if (!freqs)
4781 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004782
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004783 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4784 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004785
4786 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004787 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004788 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4789 freq);
4790 res = 0;
4791 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004792 }
4793 }
4794
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004795 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004796 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004797
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004798exit_free:
4799 os_free(freqs);
4800 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004801}
4802
4803
4804static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4805 const u8 *peer_dev_addr)
4806{
4807 struct wpa_bss *bss;
4808 int updated;
4809
4810 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4811 if (bss == NULL)
4812 return -1;
4813 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4814 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4815 "last scan");
4816 return 0;
4817 }
4818
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004819 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4820 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004821 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4822 "%ld.%06ld (%supdated in last scan)",
4823 bss->last_update.sec, bss->last_update.usec,
4824 updated ? "": "not ");
4825
4826 return updated;
4827}
4828
4829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4831 struct wpa_scan_results *scan_res)
4832{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004833 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 int freq;
4835 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4838
4839 if (wpa_s->global->p2p_disabled)
4840 return;
4841
Dmitry Shmidt04949592012-07-19 12:16:46 -07004842 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4843 scan_res ? (int) scan_res->num : -1,
4844 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845
4846 if (scan_res)
4847 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4848
Dmitry Shmidt04949592012-07-19 12:16:46 -07004849 if (wpa_s->p2p_auto_pd) {
4850 int join = wpas_p2p_peer_go(wpa_s,
4851 wpa_s->pending_join_dev_addr);
4852 if (join == 0 &&
4853 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4854 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004855 bss = wpa_bss_get_bssid_latest(
4856 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004857 if (bss) {
4858 freq = bss->freq;
4859 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4860 "the peer " MACSTR " at %d MHz",
4861 wpa_s->auto_pd_scan_retry,
4862 MAC2STR(wpa_s->
4863 pending_join_dev_addr),
4864 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004865 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004866 return;
4867 }
4868 }
4869
4870 if (join < 0)
4871 join = 0;
4872
4873 wpa_s->p2p_auto_pd = 0;
4874 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4875 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4876 MAC2STR(wpa_s->pending_join_dev_addr), join);
4877 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004878 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004879 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004880 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004881 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004882 wpa_msg_global(wpa_s, MSG_INFO,
4883 P2P_EVENT_PROV_DISC_FAILURE
4884 " p2p_dev_addr=" MACSTR " status=N/A",
4885 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004886 }
4887 return;
4888 }
4889
4890 if (wpa_s->p2p_auto_join) {
4891 int join = wpas_p2p_peer_go(wpa_s,
4892 wpa_s->pending_join_dev_addr);
4893 if (join < 0) {
4894 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4895 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004896 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004897 P2P_EVENT_FALLBACK_TO_GO_NEG
4898 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4900 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4901 wpa_s->p2p_persistent_group, 0, 0, 0,
4902 wpa_s->p2p_go_intent,
4903 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004904 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004905 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004906 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004907 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004908 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004909 wpa_s->p2p_go_max_oper_chwidth,
4910 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004911 return;
4912 }
4913
4914 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4915 "try to join the group", join ? "" :
4916 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004917 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004918 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004919 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004920 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004921 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004922 }
4923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4925 wpa_s->pending_join_iface_addr);
4926 if (freq < 0 &&
4927 p2p_get_interface_addr(wpa_s->global->p2p,
4928 wpa_s->pending_join_dev_addr,
4929 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004930 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4931 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4933 "address for join from " MACSTR " to " MACSTR
4934 " based on newly discovered P2P peer entry",
4935 MAC2STR(wpa_s->pending_join_iface_addr),
4936 MAC2STR(iface_addr));
4937 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4938 ETH_ALEN);
4939
4940 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4941 wpa_s->pending_join_iface_addr);
4942 }
4943 if (freq >= 0) {
4944 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4945 "from P2P peer table: %d MHz", freq);
4946 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004947 if (wpa_s->p2p_join_ssid_len) {
4948 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4949 MACSTR " and SSID %s",
4950 MAC2STR(wpa_s->pending_join_iface_addr),
4951 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4952 wpa_s->p2p_join_ssid_len));
4953 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4954 wpa_s->p2p_join_ssid,
4955 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004956 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004957 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4958 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4959 bss = wpa_bss_get_bssid_latest(wpa_s,
4960 wpa_s->pending_join_iface_addr);
4961 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004963 u8 dev_addr[ETH_ALEN];
4964
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965 freq = bss->freq;
4966 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004967 "from BSS table: %d MHz (SSID %s)", freq,
4968 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004969 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4970 dev_addr) == 0 &&
4971 os_memcmp(wpa_s->pending_join_dev_addr,
4972 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4973 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4974 ETH_ALEN) != 0) {
4975 wpa_printf(MSG_DEBUG,
4976 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4977 MAC2STR(dev_addr),
4978 MAC2STR(wpa_s->pending_join_dev_addr));
4979 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4980 ETH_ALEN);
4981 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982 }
4983 if (freq > 0) {
4984 u16 method;
4985
Dmitry Shmidt04949592012-07-19 12:16:46 -07004986 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004987 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004988 P2P_EVENT_GROUP_FORMATION_FAILURE
4989 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004990 wpas_notify_p2p_group_formation_failure(
4991 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004992 return;
4993 }
4994
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4996 "prior to joining an existing group (GO " MACSTR
4997 " freq=%u MHz)",
4998 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4999 wpa_s->pending_pd_before_join = 1;
5000
5001 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 case WPS_PIN_DISPLAY:
5003 method = WPS_CONFIG_KEYPAD;
5004 break;
5005 case WPS_PIN_KEYPAD:
5006 method = WPS_CONFIG_DISPLAY;
5007 break;
5008 case WPS_PBC:
5009 method = WPS_CONFIG_PUSHBUTTON;
5010 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005011 case WPS_P2PS:
5012 method = WPS_CONFIG_P2PS;
5013 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 default:
5015 method = 0;
5016 break;
5017 }
5018
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005019 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5020 wpa_s->pending_join_dev_addr) ==
5021 method)) {
5022 /*
5023 * We have already performed provision discovery for
5024 * joining the group. Proceed directly to join
5025 * operation without duplicated provision discovery. */
5026 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5027 "with " MACSTR " already done - proceed to "
5028 "join",
5029 MAC2STR(wpa_s->pending_join_dev_addr));
5030 wpa_s->pending_pd_before_join = 0;
5031 goto start;
5032 }
5033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005035 wpa_s->pending_join_dev_addr,
5036 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005037 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005038 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5039 "Discovery Request before joining an "
5040 "existing group");
5041 wpa_s->pending_pd_before_join = 0;
5042 goto start;
5043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044 return;
5045 }
5046
5047 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5048 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5049 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5050 wpas_p2p_check_join_scan_limit(wpa_s);
5051 return;
5052
5053start:
5054 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005055 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
5056 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057}
5058
5059
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005060static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5061 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005063 int ret;
5064 struct wpa_driver_scan_params params;
5065 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005066 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005067 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005068 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005069
5070 os_memset(&params, 0, sizeof(params));
5071
5072 /* P2P Wildcard SSID */
5073 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005074 if (ssid && ssid_len) {
5075 params.ssids[0].ssid = ssid;
5076 params.ssids[0].ssid_len = ssid_len;
5077 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5078 wpa_s->p2p_join_ssid_len = ssid_len;
5079 } else {
5080 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5081 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5082 wpa_s->p2p_join_ssid_len = 0;
5083 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084
5085 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005086 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5087 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5088 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005089 if (wps_ie == NULL) {
5090 wpas_p2p_scan_res_join(wpa_s, NULL);
5091 return;
5092 }
5093
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005094 if (!freq) {
5095 int oper_freq;
5096 /*
5097 * If freq is not provided, check the operating freq of the GO
5098 * and use a single channel scan on if possible.
5099 */
5100 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5101 wpa_s->pending_join_iface_addr);
5102 if (oper_freq > 0)
5103 freq = oper_freq;
5104 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005105 if (freq > 0) {
5106 freqs[0] = freq;
5107 params.freqs = freqs;
5108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005110 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5111 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5112 if (ies == NULL) {
5113 wpabuf_free(wps_ie);
5114 wpas_p2p_scan_res_join(wpa_s, NULL);
5115 return;
5116 }
5117 wpabuf_put_buf(ies, wps_ie);
5118 wpabuf_free(wps_ie);
5119
5120 bands = wpas_get_bands(wpa_s, freqs);
5121 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5122
5123 params.p2p_probe = 1;
5124 params.extra_ies = wpabuf_head(ies);
5125 params.extra_ies_len = wpabuf_len(ies);
5126
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005127 if (wpa_s->clear_driver_scan_cache) {
5128 wpa_printf(MSG_DEBUG,
5129 "Request driver to clear scan cache due to local BSS flush");
5130 params.only_new_results = 1;
5131 }
5132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133 /*
5134 * Run a scan to update BSS table and start Provision Discovery once
5135 * the new scan results become available.
5136 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005137 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005138 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005139 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005140 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005141 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005142 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005143 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005144
5145 wpabuf_free(ies);
5146
5147 if (ret) {
5148 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5149 "try again later");
5150 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5151 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5152 wpas_p2p_check_join_scan_limit(wpa_s);
5153 }
5154}
5155
5156
Dmitry Shmidt04949592012-07-19 12:16:46 -07005157static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5158{
5159 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005160 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005161}
5162
5163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005164static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005165 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005166 int auto_join, int op_freq,
5167 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005168{
5169 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005170 MACSTR " dev " MACSTR " op_freq=%d)%s",
5171 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005172 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005173 if (ssid && ssid_len) {
5174 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5175 wpa_ssid_txt(ssid, ssid_len));
5176 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005177
Dmitry Shmidt04949592012-07-19 12:16:46 -07005178 wpa_s->p2p_auto_pd = 0;
5179 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005180 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5181 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5182 wpa_s->pending_join_wps_method = wps_method;
5183
5184 /* Make sure we are not running find during connection establishment */
5185 wpas_p2p_stop_find(wpa_s);
5186
5187 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005188 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 return 0;
5190}
5191
5192
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005193static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5194 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195{
5196 struct wpa_supplicant *group;
5197 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005198 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005199
5200 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5201 if (group == NULL)
5202 return -1;
5203 if (group != wpa_s) {
5204 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5205 sizeof(group->p2p_pin));
5206 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005207 } else {
5208 /*
5209 * Need to mark the current interface for p2p_group_formation
5210 * when a separate group interface is not used. This is needed
5211 * to allow p2p_cancel stop a pending p2p_connect-join.
5212 * wpas_p2p_init_group_interface() addresses this for the case
5213 * where a separate group interface is used.
5214 */
5215 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005216 }
5217
5218 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005219 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220
5221 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005222 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5224 ETH_ALEN);
5225 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005226 if (freq && ssid && ssid_len) {
5227 res.freq = freq;
5228 res.ssid_len = ssid_len;
5229 os_memcpy(res.ssid, ssid, ssid_len);
5230 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005231 if (ssid && ssid_len) {
5232 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5233 ssid, ssid_len);
5234 } else {
5235 bss = wpa_bss_get_bssid_latest(
5236 wpa_s, wpa_s->pending_join_iface_addr);
5237 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005238 if (bss) {
5239 res.freq = bss->freq;
5240 res.ssid_len = bss->ssid_len;
5241 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5242 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5243 bss->freq,
5244 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005245 } else if (ssid && ssid_len) {
5246 res.ssid_len = ssid_len;
5247 os_memcpy(res.ssid, ssid, ssid_len);
5248 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5249 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005250 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005251 }
5252
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005253 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5254 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5255 "starting client");
5256 wpa_drv_cancel_remain_on_channel(wpa_s);
5257 wpa_s->off_channel_freq = 0;
5258 wpa_s->roc_waiting_drv_freq = 0;
5259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005260 wpas_start_wps_enrollee(group, &res);
5261
5262 /*
5263 * Allow a longer timeout for join-a-running-group than normal 15
5264 * second group formation timeout since the GO may not have authorized
5265 * our connection yet.
5266 */
5267 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5268 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5269 wpa_s, NULL);
5270
5271 return 0;
5272}
5273
5274
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005275static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005276 int *force_freq, int *pref_freq, int go,
5277 unsigned int *pref_freq_list,
5278 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005279{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005280 struct wpa_used_freq_data *freqs;
5281 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005282 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5283
5284 max_pref_freq = *num_pref_freq;
5285 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005286
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005287 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5288 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005289 if (!freqs)
5290 return -1;
5291
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005292 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5293 wpa_s->num_multichan_concurrent);
5294
5295 /*
5296 * It is possible that the total number of used frequencies is bigger
5297 * than the number of frequencies used for P2P, so get the system wide
5298 * number of unused frequencies.
5299 */
5300 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5301
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005302 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005303 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5304 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005305
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005306 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005307 int ret;
5308 if (go)
5309 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5310 else
5311 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5312 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005313 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005314 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5315 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005316 /*
5317 * If freq is a DFS channel and DFS is offloaded
5318 * to the driver, allow P2P GO to use it.
5319 */
5320 wpa_printf(MSG_DEBUG,
5321 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5322 freq);
5323 } else {
5324 wpa_printf(MSG_DEBUG,
5325 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5326 freq);
5327 res = -3;
5328 goto exit_free;
5329 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005330 }
5331
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005332 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005333 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005334 freq_in_use = 1;
5335 }
5336
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005337 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005338 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5339 freq);
5340 res = -2;
5341 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005342 }
5343 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5344 "requested channel (%u MHz)", freq);
5345 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005346 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005347 }
5348
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005349 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005350
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005351 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5352 enum wpa_driver_if_type iface_type;
5353
5354 if (go)
5355 iface_type = WPA_IF_P2P_GO;
5356 else
5357 iface_type = WPA_IF_P2P_CLIENT;
5358
5359 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5360 best_freq, go);
5361
5362 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5363 &max_pref_freq,
5364 pref_freq_list);
5365 if (!res && max_pref_freq > 0) {
5366 *num_pref_freq = max_pref_freq;
5367 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005368 while (i < *num_pref_freq &&
5369 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005370 pref_freq_list[i]) ||
5371 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005372 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005373 wpa_printf(MSG_DEBUG,
5374 "P2P: preferred_freq_list[%d]=%d is disallowed",
5375 i, pref_freq_list[i]);
5376 i++;
5377 }
5378 if (i != *num_pref_freq) {
5379 best_freq = pref_freq_list[i];
5380 wpa_printf(MSG_DEBUG,
5381 "P2P: Using preferred_freq_list[%d]=%d",
5382 i, best_freq);
5383 } else {
5384 wpa_printf(MSG_DEBUG,
5385 "P2P: All driver preferred frequencies are disallowed for P2P use");
5386 *num_pref_freq = 0;
5387 }
5388 } else {
5389 wpa_printf(MSG_DEBUG,
5390 "P2P: No preferred frequency list available");
5391 }
5392 }
5393
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005394 /* We have a candidate frequency to use */
5395 if (best_freq > 0) {
5396 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005397 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005398 best_freq);
5399 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005400 } else {
5401 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 -07005402 best_freq);
5403 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005404 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005405 } else if (num_unused > 0) {
5406 wpa_printf(MSG_DEBUG,
5407 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5408 *force_freq = 0;
5409 } else {
5410 wpa_printf(MSG_DEBUG,
5411 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5412 res = -2;
5413 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005414 }
5415
5416exit_ok:
5417 res = 0;
5418exit_free:
5419 os_free(freqs);
5420 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005421}
5422
5423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005424/**
5425 * wpas_p2p_connect - Request P2P Group Formation to be started
5426 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5427 * @peer_addr: Address of the peer P2P Device
5428 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5429 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005430 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 * @join: Whether to join an existing group (as a client) instead of starting
5432 * Group Owner negotiation; @peer_addr is BSSID in that case
5433 * @auth: Whether to only authorize the connection instead of doing that and
5434 * initiating Group Owner negotiation
5435 * @go_intent: GO Intent or -1 to use default
5436 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005437 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005438 * @persistent_id: Persistent group credentials to use for forcing GO
5439 * parameters or -1 to generate new values (SSID/passphrase)
5440 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5441 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005442 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005443 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005444 * @vht_chwidth: Channel width supported by GO operating with VHT support
5445 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005446 * @group_ssid: Specific Group SSID for join or %NULL if not set
5447 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005448 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5449 * failure, -2 on failure due to channel not currently available,
5450 * -3 if forced channel is not supported
5451 */
5452int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5453 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005454 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005455 int go_intent, int freq, unsigned int vht_center_freq2,
5456 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005457 unsigned int vht_chwidth, const u8 *group_ssid,
5458 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005460 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005461 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005463 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005464 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005465 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466
5467 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5468 return -1;
5469
Dmitry Shmidt04949592012-07-19 12:16:46 -07005470 if (persistent_id >= 0) {
5471 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5472 if (ssid == NULL || ssid->disabled != 2 ||
5473 ssid->mode != WPAS_MODE_P2P_GO)
5474 return -1;
5475 }
5476
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005477 os_free(wpa_s->global->add_psk);
5478 wpa_s->global->add_psk = NULL;
5479
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005480 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005481 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005482 wpa_s->global->pending_p2ps_group_freq = 0;
5483 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005484
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485 if (go_intent < 0)
5486 go_intent = wpa_s->conf->p2p_go_intent;
5487
5488 if (!auth)
5489 wpa_s->p2p_long_listen = 0;
5490
5491 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005492 wpa_s->p2p_persistent_group = !!persistent_group;
5493 wpa_s->p2p_persistent_id = persistent_id;
5494 wpa_s->p2p_go_intent = go_intent;
5495 wpa_s->p2p_connect_freq = freq;
5496 wpa_s->p2p_fallback_to_go_neg = 0;
5497 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005498 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005499 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005500 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5501 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502
5503 if (pin)
5504 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5505 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005506 if (wps_generate_pin((unsigned int *) &ret) < 0)
5507 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005508 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5509 "%08d", ret);
5510 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5511 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005512 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5513 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005514 } else if (wps_method == WPS_P2PS) {
5515 /* Force the P2Ps default PIN to be used */
5516 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517 } else
5518 wpa_s->p2p_pin[0] = '\0';
5519
Dmitry Shmidt04949592012-07-19 12:16:46 -07005520 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5522 if (auth) {
5523 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5524 "connect a running group from " MACSTR,
5525 MAC2STR(peer_addr));
5526 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5527 return ret;
5528 }
5529 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5530 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5531 iface_addr) < 0) {
5532 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5533 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5534 dev_addr);
5535 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005536 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005537 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5539 "%ld.%06ld",
5540 wpa_s->p2p_auto_started.sec,
5541 wpa_s->p2p_auto_started.usec);
5542 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005543 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005544 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005545 auto_join, freq,
5546 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 return -1;
5548 return ret;
5549 }
5550
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005551 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005552 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005553 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005554 if (res)
5555 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005556 wpas_p2p_set_own_freq_preference(wpa_s,
5557 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005558
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005559 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005561 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005563 if (wpa_s->create_p2p_iface) {
5564 /* Prepare to add a new interface for the group */
5565 iftype = WPA_IF_P2P_GROUP;
5566 if (go_intent == 15)
5567 iftype = WPA_IF_P2P_GO;
5568 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5569 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5570 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005572 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005574 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005575 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005576 if (wpa_s->p2p_mgmt)
5577 if_addr = wpa_s->parent->own_addr;
5578 else
5579 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005580 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005582
5583 if (auth) {
5584 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005585 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005586 force_freq, persistent_group, ssid,
5587 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 return -1;
5589 return ret;
5590 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005591
5592 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5593 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005594 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005595 if (wpa_s->create_p2p_iface)
5596 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005597 return -1;
5598 }
5599 return ret;
5600}
5601
5602
5603/**
5604 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5605 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5606 * @freq: Frequency of the channel in MHz
5607 * @duration: Duration of the stay on the channel in milliseconds
5608 *
5609 * This callback is called when the driver indicates that it has started the
5610 * requested remain-on-channel duration.
5611 */
5612void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5613 unsigned int freq, unsigned int duration)
5614{
5615 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5616 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005617 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)",
5618 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5619 wpa_s->roc_waiting_drv_freq, freq, duration);
5620 if (wpa_s->off_channel_freq &&
5621 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5623 wpa_s->pending_listen_duration);
5624 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005625 } else {
5626 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5627 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5628 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 }
5630}
5631
5632
Jithu Jance57cea1a2014-08-20 10:22:04 -07005633int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005634{
5635 /* Limit maximum Listen state time based on driver limitation. */
5636 if (timeout > wpa_s->max_remain_on_chan)
5637 timeout = wpa_s->max_remain_on_chan;
5638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005639 return p2p_listen(wpa_s->global->p2p, timeout);
5640}
5641
5642
5643/**
5644 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5645 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5646 * @freq: Frequency of the channel in MHz
5647 *
5648 * This callback is called when the driver indicates that a remain-on-channel
5649 * operation has been completed, i.e., the duration on the requested channel
5650 * has timed out.
5651 */
5652void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5653 unsigned int freq)
5654{
5655 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5656 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005657 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005658 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005659 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5660 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005661 if (wpa_s->p2p_long_listen > 0)
5662 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5664 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005665 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 if (wpa_s->p2p_long_listen > 0) {
5668 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5669 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005670 } else {
5671 /*
5672 * When listen duration is over, stop listen & update p2p_state
5673 * to IDLE.
5674 */
5675 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676 }
5677}
5678
5679
5680/**
5681 * wpas_p2p_group_remove - Remove a P2P group
5682 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5683 * @ifname: Network interface name of the group interface or "*" to remove all
5684 * groups
5685 * Returns: 0 on success, -1 on failure
5686 *
5687 * This function is used to remove a P2P group. This can be used to disconnect
5688 * from a group in which the local end is a P2P Client or to end a P2P Group in
5689 * case the local end is the Group Owner. If a virtual network interface was
5690 * created for this group, that interface will be removed. Otherwise, only the
5691 * configured P2P group network will be removed from the interface.
5692 */
5693int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5694{
5695 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005696 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005697
5698 if (os_strcmp(ifname, "*") == 0) {
5699 struct wpa_supplicant *prev;
5700 wpa_s = global->ifaces;
5701 while (wpa_s) {
5702 prev = wpa_s;
5703 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005704 if (prev->p2p_group_interface !=
5705 NOT_P2P_GROUP_INTERFACE ||
5706 (prev->current_ssid &&
5707 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005708 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005709 }
5710 return 0;
5711 }
5712
5713 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5714 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5715 break;
5716 }
5717
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005718 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005719}
5720
5721
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005722static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5723{
5724 unsigned int r;
5725
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005726 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5727 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5728 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5729 int res;
5730
5731 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5732 &size, pref_freq_list);
5733 if (!res && size > 0) {
5734 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005735 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005736 (!p2p_supported_freq(wpa_s->global->p2p,
5737 pref_freq_list[i]) ||
5738 wpas_p2p_disallowed_freq(wpa_s->global,
5739 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005740 wpa_printf(MSG_DEBUG,
5741 "P2P: preferred_freq_list[%d]=%d is disallowed",
5742 i, pref_freq_list[i]);
5743 i++;
5744 }
5745 if (i != size) {
5746 freq = pref_freq_list[i];
5747 wpa_printf(MSG_DEBUG,
5748 "P2P: Using preferred_freq_list[%d]=%d",
5749 i, freq);
5750 } else {
5751 wpa_printf(MSG_DEBUG,
5752 "P2P: All driver preferred frequencies are disallowed for P2P use");
5753 }
5754 } else {
5755 wpa_printf(MSG_DEBUG,
5756 "P2P: No preferred frequency list available");
5757 }
5758 }
5759
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005760 if (freq == 2) {
5761 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5762 "band");
5763 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005764 p2p_supported_freq_go(wpa_s->global->p2p,
5765 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005766 freq = wpa_s->best_24_freq;
5767 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5768 "channel: %d MHz", freq);
5769 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005770 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5771 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005772 freq = 2412 + (r % 3) * 25;
5773 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5774 "channel: %d MHz", freq);
5775 }
5776 }
5777
5778 if (freq == 5) {
5779 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5780 "band");
5781 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005782 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005783 wpa_s->best_5_freq)) {
5784 freq = wpa_s->best_5_freq;
5785 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5786 "channel: %d MHz", freq);
5787 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005788 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5789 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005790 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005791 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005792 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5793 "5 GHz channel for P2P group");
5794 return -1;
5795 }
5796 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5797 "channel: %d MHz", freq);
5798 }
5799 }
5800
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005801 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005802 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005803 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5804 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005805 /*
5806 * If freq is a DFS channel and DFS is offloaded to the
5807 * driver, allow P2P GO to use it.
5808 */
5809 wpa_printf(MSG_DEBUG, "P2P: "
5810 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5811 __func__, freq);
5812 return freq;
5813 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005814 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5815 "(%u MHz) is not supported for P2P uses",
5816 freq);
5817 return -1;
5818 }
5819
5820 return freq;
5821}
5822
5823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005824static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5825 const struct p2p_channels *channels,
5826 int freq)
5827{
5828 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5829 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5830 freq_included(wpa_s, channels, freq))
5831 return 1;
5832 return 0;
5833}
5834
5835
5836static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5837 struct p2p_go_neg_results *params,
5838 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005839{
5840 unsigned int i, r;
5841
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005842 /* try all channels in operating class 115 */
5843 for (i = 0; i < 4; i++) {
5844 params->freq = 5180 + i * 20;
5845 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005846 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005847 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5848 goto out;
5849 }
5850
5851 /* try all channels in operating class 124 */
5852 for (i = 0; i < 4; i++) {
5853 params->freq = 5745 + i * 20;
5854 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005855 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005856 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5857 goto out;
5858 }
5859
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005860 /* try social channel class 180 channel 2 */
5861 params->freq = 58320 + 1 * 2160;
5862 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005863 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005864 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5865 goto out;
5866
5867 /* try all channels in reg. class 180 */
5868 for (i = 0; i < 4; i++) {
5869 params->freq = 58320 + i * 2160;
5870 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005871 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005872 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5873 goto out;
5874 }
5875
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005876 /* try some random selection of the social channels */
5877 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5878 return;
5879
5880 for (i = 0; i < 3; i++) {
5881 params->freq = 2412 + ((r + i) % 3) * 25;
5882 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5883 goto out;
5884 }
5885
5886 /* try all other channels in operating class 81 */
5887 for (i = 0; i < 11; i++) {
5888 params->freq = 2412 + i * 5;
5889
5890 /* skip social channels; covered in the previous loop */
5891 if (params->freq == 2412 ||
5892 params->freq == 2437 ||
5893 params->freq == 2462)
5894 continue;
5895
5896 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5897 goto out;
5898 }
5899
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005900 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005901 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005902 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005903out:
5904 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5905 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005906}
5907
5908
Roshan Pius3a1667e2018-07-03 15:17:14 -07005909static int wpas_same_band(int freq1, int freq2)
5910{
5911 enum hostapd_hw_mode mode1, mode2;
5912 u8 chan1, chan2;
5913
5914 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5915 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5916 if (mode1 == NUM_HOSTAPD_MODES)
5917 return 0;
5918 return mode1 == mode2;
5919}
5920
5921
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005922static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5923 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005924 int freq, int vht_center_freq2, int ht40,
5925 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005926 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005927{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005928 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005929 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005930 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005931 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005932 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933
5934 os_memset(params, 0, sizeof(*params));
5935 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005936 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005937 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005938 params->max_oper_chwidth = max_oper_chwidth;
5939 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005940
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005941 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5942 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005943 if (!freqs)
5944 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005945
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005946 num = get_shared_radio_freqs_data(wpa_s, freqs,
5947 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005948
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005949 if (wpa_s->current_ssid &&
5950 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5951 wpa_s->wpa_state == WPA_COMPLETED) {
5952 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5953 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005954
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005955 /*
5956 * If the frequency selection is done for an active P2P GO that
5957 * is not sharing a frequency, allow to select a new frequency
5958 * even if there are no unused frequencies as we are about to
5959 * move the P2P GO so its frequency can be re-used.
5960 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005961 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005962 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5963 freqs[i].flags == 0) {
5964 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005965 break;
5966 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005967 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005968 }
5969
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005970 /* try using the forced freq */
5971 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005972 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
5973 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005974 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005975 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005976 freq);
5977 goto fail;
5978 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005979 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5980 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005981 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5982 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005983 /*
5984 * If freq is a DFS channel and DFS is offloaded
5985 * to the driver, allow P2P GO to use it.
5986 */
5987 wpa_printf(MSG_DEBUG,
5988 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
5989 __func__, freq);
5990 } else {
5991 wpa_printf(MSG_DEBUG,
5992 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
5993 freq);
5994 goto fail;
5995 }
5996 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005997
5998 for (i = 0; i < num; i++) {
5999 if (freqs[i].freq == freq) {
6000 wpa_printf(MSG_DEBUG,
6001 "P2P: forced freq (%d MHz) is also shared",
6002 freq);
6003 params->freq = freq;
6004 goto success;
6005 }
6006 }
6007
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006008 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006009 wpa_printf(MSG_DEBUG,
6010 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
6011 freq);
6012 goto fail;
6013 }
6014
6015 wpa_printf(MSG_DEBUG,
6016 "P2P: force GO freq (%d MHz) on a free channel",
6017 freq);
6018 params->freq = freq;
6019 goto success;
6020 }
6021
6022 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006023 if (num &&
6024 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006025 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6026 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6027 wpa_printf(MSG_DEBUG,
6028 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006029 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006030 params->freq = cand;
6031 goto success;
6032 }
6033
6034 /* try using one of the shared freqs */
6035 for (i = 0; i < num; i++) {
6036 if (wpas_p2p_supported_freq_go(wpa_s, channels,
6037 freqs[i].freq)) {
6038 wpa_printf(MSG_DEBUG,
6039 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006040 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006041 params->freq = freqs[i].freq;
6042 goto success;
6043 }
6044 }
6045 }
6046
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08006047 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006048 wpa_printf(MSG_DEBUG,
6049 "P2P: Cannot force GO on any of the channels we are already using");
6050 goto fail;
6051 }
6052
6053 /* try using the setting from the configuration file */
6054 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6055 wpa_s->conf->p2p_oper_channel >= 1 &&
6056 wpa_s->conf->p2p_oper_channel <= 11 &&
6057 wpas_p2p_supported_freq_go(
6058 wpa_s, channels,
6059 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
6060 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6061 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6062 "frequency %d MHz", params->freq);
6063 goto success;
6064 }
6065
6066 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6067 wpa_s->conf->p2p_oper_reg_class == 116 ||
6068 wpa_s->conf->p2p_oper_reg_class == 117 ||
6069 wpa_s->conf->p2p_oper_reg_class == 124 ||
6070 wpa_s->conf->p2p_oper_reg_class == 125 ||
6071 wpa_s->conf->p2p_oper_reg_class == 126 ||
6072 wpa_s->conf->p2p_oper_reg_class == 127) &&
6073 wpas_p2p_supported_freq_go(wpa_s, channels,
6074 5000 +
6075 5 * wpa_s->conf->p2p_oper_channel)) {
6076 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6077 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6078 "frequency %d MHz", params->freq);
6079 goto success;
6080 }
6081
6082 /* Try using best channels */
6083 if (wpa_s->conf->p2p_oper_channel == 0 &&
6084 wpa_s->best_overall_freq > 0 &&
6085 wpas_p2p_supported_freq_go(wpa_s, channels,
6086 wpa_s->best_overall_freq)) {
6087 params->freq = wpa_s->best_overall_freq;
6088 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6089 "channel %d MHz", params->freq);
6090 goto success;
6091 }
6092
6093 if (wpa_s->conf->p2p_oper_channel == 0 &&
6094 wpa_s->best_24_freq > 0 &&
6095 wpas_p2p_supported_freq_go(wpa_s, channels,
6096 wpa_s->best_24_freq)) {
6097 params->freq = wpa_s->best_24_freq;
6098 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6099 "channel %d MHz", params->freq);
6100 goto success;
6101 }
6102
6103 if (wpa_s->conf->p2p_oper_channel == 0 &&
6104 wpa_s->best_5_freq > 0 &&
6105 wpas_p2p_supported_freq_go(wpa_s, channels,
6106 wpa_s->best_5_freq)) {
6107 params->freq = wpa_s->best_5_freq;
6108 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6109 "channel %d MHz", params->freq);
6110 goto success;
6111 }
6112
6113 /* try using preferred channels */
6114 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6115 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6116 params->freq = cand;
6117 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6118 "channels", params->freq);
6119 goto success;
6120 }
6121
Roshan Pius3a1667e2018-07-03 15:17:14 -07006122 /* Try using a channel that allows VHT to be used with 80 MHz */
6123 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6124 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6125 enum hostapd_hw_mode mode;
6126 struct hostapd_hw_modes *hwmode;
6127 u8 chan;
6128
6129 cand = wpa_s->p2p_group_common_freqs[i];
6130 mode = ieee80211_freq_to_chan(cand, &chan);
6131 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6132 mode);
6133 if (!hwmode ||
6134 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6135 BW80) != ALLOWED)
6136 continue;
6137 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6138 params->freq = cand;
6139 wpa_printf(MSG_DEBUG,
6140 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6141 params->freq);
6142 goto success;
6143 }
6144 }
6145 }
6146
6147 /* Try using a channel that allows HT to be used with 40 MHz on the same
6148 * band so that CSA can be used */
6149 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6150 wpa_s->p2p_group_common_freqs) {
6151 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6152 enum hostapd_hw_mode mode;
6153 struct hostapd_hw_modes *hwmode;
6154 u8 chan;
6155
6156 cand = wpa_s->p2p_group_common_freqs[i];
6157 mode = ieee80211_freq_to_chan(cand, &chan);
6158 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6159 mode);
6160 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6161 cand) ||
6162 !hwmode ||
6163 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6164 BW40MINUS) != ALLOWED &&
6165 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6166 BW40PLUS) != ALLOWED))
6167 continue;
6168 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6169 params->freq = cand;
6170 wpa_printf(MSG_DEBUG,
6171 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6172 params->freq);
6173 goto success;
6174 }
6175 }
6176 }
6177
6178 /* Try using one of the group common freqs on the same band so that CSA
6179 * can be used */
6180 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6181 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6182 cand = wpa_s->p2p_group_common_freqs[i];
6183 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6184 cand))
6185 continue;
6186 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6187 params->freq = cand;
6188 wpa_printf(MSG_DEBUG,
6189 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6190 params->freq);
6191 goto success;
6192 }
6193 }
6194 }
6195
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006196 /* Try using one of the group common freqs */
6197 if (wpa_s->p2p_group_common_freqs) {
6198 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6199 cand = wpa_s->p2p_group_common_freqs[i];
6200 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6201 params->freq = cand;
6202 wpa_printf(MSG_DEBUG,
6203 "P2P: Use freq %d MHz common with the peer",
6204 params->freq);
6205 goto success;
6206 }
6207 }
6208 }
6209
6210 /* no preference, select some channel */
6211 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6212
6213 if (params->freq == 0) {
6214 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6215 goto fail;
6216 }
6217
6218success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006219 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006220 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006221fail:
6222 os_free(freqs);
6223 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006224}
6225
6226
6227static struct wpa_supplicant *
6228wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6229 int go)
6230{
6231 struct wpa_supplicant *group_wpa_s;
6232
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006233 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006234 if (wpa_s->p2p_mgmt) {
6235 /*
6236 * We may be called on the p2p_dev interface which
6237 * cannot be used for group operations, so always use
6238 * the primary interface.
6239 */
6240 wpa_s->parent->p2pdev = wpa_s;
6241 wpa_s = wpa_s->parent;
6242 }
6243 wpa_dbg(wpa_s, MSG_DEBUG,
6244 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006245 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006246 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006247 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006249 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006250
6251 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006252 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006253 wpa_msg_global(wpa_s, MSG_ERROR,
6254 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006256 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006257 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6258 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006259 wpa_msg_global(wpa_s, MSG_ERROR,
6260 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006261 wpas_p2p_remove_pending_group_interface(wpa_s);
6262 return NULL;
6263 }
6264
Roshan Pius3a1667e2018-07-03 15:17:14 -07006265 if (go && wpa_s->p2p_go_do_acs) {
6266 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6267 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6268 wpa_s->p2p_go_do_acs = 0;
6269 }
6270
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006271 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6272 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006273 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006274 return group_wpa_s;
6275}
6276
6277
6278/**
6279 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6280 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6281 * @persistent_group: Whether to create a persistent group
6282 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006283 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006284 * @ht40: Start GO with 40 MHz channel width
6285 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006286 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006287 * Returns: 0 on success, -1 on failure
6288 *
6289 * This function creates a new P2P group with the local end as the Group Owner,
6290 * i.e., without using Group Owner Negotiation.
6291 */
6292int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006293 int freq, int vht_center_freq2, int ht40, int vht,
6294 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006295{
6296 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006297
6298 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6299 return -1;
6300
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006301 os_free(wpa_s->global->add_psk);
6302 wpa_s->global->add_psk = NULL;
6303
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006304 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006305 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006306 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006307
Roshan Pius3a1667e2018-07-03 15:17:14 -07006308 if (!wpa_s->p2p_go_do_acs) {
6309 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6310 if (freq < 0)
6311 return -1;
6312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006314 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6315 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006316 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318 p2p_go_params(wpa_s->global->p2p, &params);
6319 params.persistent_group = persistent_group;
6320
6321 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6322 if (wpa_s == NULL)
6323 return -1;
6324 wpas_start_wps_go(wpa_s, &params, 0);
6325
6326 return 0;
6327}
6328
6329
6330static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006331 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006332 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006333{
6334 struct wpa_ssid *ssid;
6335
6336 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6337 if (wpa_s == NULL)
6338 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006339 if (force_scan)
6340 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006341 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006342
6343 wpa_supplicant_ap_deinit(wpa_s);
6344
6345 ssid = wpa_config_add_network(wpa_s->conf);
6346 if (ssid == NULL)
6347 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006348 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349 wpa_config_set_network_defaults(ssid);
6350 ssid->temporary = 1;
6351 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006352 ssid->pbss = params->pbss;
6353 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6354 WPA_CIPHER_CCMP;
6355 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006356 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6357 ssid->ssid = os_malloc(params->ssid_len);
6358 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359 wpa_config_remove_network(wpa_s->conf, ssid->id);
6360 return -1;
6361 }
6362 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6363 ssid->ssid_len = params->ssid_len;
6364 ssid->p2p_group = 1;
6365 ssid->export_keys = 1;
6366 if (params->psk_set) {
6367 os_memcpy(ssid->psk, params->psk, 32);
6368 ssid->psk_set = 1;
6369 }
6370 if (params->passphrase)
6371 ssid->passphrase = os_strdup(params->passphrase);
6372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006373 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006374 wpa_s->p2p_in_invitation = 1;
6375 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006376 wpa_s->p2p_go_group_formation_completed = 0;
6377 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006378
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006379 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006380 NULL);
6381 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6382 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006383 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006384 wpa_supplicant_select_network(wpa_s, ssid);
6385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006386 return 0;
6387}
6388
6389
6390int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6391 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006392 int force_freq, int neg_freq,
6393 int vht_center_freq2, int ht40,
6394 int vht, int max_oper_chwidth,
6395 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006396 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006397{
6398 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006399 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400
6401 if (ssid->disabled != 2 || ssid->ssid == NULL)
6402 return -1;
6403
6404 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6405 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6406 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6407 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006408 if (go == 0 &&
6409 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006410 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006411 /*
6412 * This can happen if Invitation Response frame was lost
6413 * and the peer (GO of a persistent group) tries to
6414 * invite us again. Reschedule the timeout to avoid
6415 * terminating the wait for the connection too early
6416 * since we now know that the peer is still trying to
6417 * invite us instead of having already started the GO.
6418 */
6419 wpa_printf(MSG_DEBUG,
6420 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6421 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6422 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006423 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006424 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006425 return 0;
6426 }
6427
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006428 os_free(wpa_s->global->add_psk);
6429 wpa_s->global->add_psk = NULL;
6430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006432 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006433
Dmitry Shmidt04949592012-07-19 12:16:46 -07006434 wpa_s->p2p_fallback_to_go_neg = 0;
6435
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006436 if (ssid->mode == WPAS_MODE_P2P_GO) {
6437 if (force_freq > 0) {
6438 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6439 if (freq < 0)
6440 return -1;
6441 } else {
6442 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6443 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006444 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006445 freq = 0;
6446 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006447 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006448 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006449 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006450 struct os_reltime now;
6451 struct wpa_bss *bss =
6452 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006453
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006454 os_get_reltime(&now);
6455 if (bss &&
6456 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006457 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006458 freq = bss->freq;
6459 else
6460 freq = 0;
6461 }
6462
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006463 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6464 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006465 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006466 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006467 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006468
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006469 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6470 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006471 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006472
6473 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006474 params.psk_set = ssid->psk_set;
6475 if (params.psk_set)
6476 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006477 if (ssid->passphrase) {
6478 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6479 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6480 "persistent group");
6481 return -1;
6482 }
6483 os_strlcpy(params.passphrase, ssid->passphrase,
6484 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006485 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006486 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6487 params.ssid_len = ssid->ssid_len;
6488 params.persistent_group = 1;
6489
6490 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6491 if (wpa_s == NULL)
6492 return -1;
6493
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006494 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6495
Dmitry Shmidt56052862013-10-04 10:23:25 -07006496 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006497 wpas_start_wps_go(wpa_s, &params, 0);
6498
6499 return 0;
6500}
6501
6502
6503static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6504 struct wpabuf *proberesp_ies)
6505{
6506 struct wpa_supplicant *wpa_s = ctx;
6507 if (wpa_s->ap_iface) {
6508 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006509 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6510 wpabuf_free(beacon_ies);
6511 wpabuf_free(proberesp_ies);
6512 return;
6513 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514 if (beacon_ies) {
6515 wpabuf_free(hapd->p2p_beacon_ie);
6516 hapd->p2p_beacon_ie = beacon_ies;
6517 }
6518 wpabuf_free(hapd->p2p_probe_resp_ie);
6519 hapd->p2p_probe_resp_ie = proberesp_ies;
6520 } else {
6521 wpabuf_free(beacon_ies);
6522 wpabuf_free(proberesp_ies);
6523 }
6524 wpa_supplicant_ap_update_beacon(wpa_s);
6525}
6526
6527
6528static void wpas_p2p_idle_update(void *ctx, int idle)
6529{
6530 struct wpa_supplicant *wpa_s = ctx;
6531 if (!wpa_s->ap_iface)
6532 return;
6533 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006534 if (idle) {
6535 if (wpa_s->global->p2p_fail_on_wps_complete &&
6536 wpa_s->p2p_in_provisioning) {
6537 wpas_p2p_grpform_fail_after_wps(wpa_s);
6538 return;
6539 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006540 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006541 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6543}
6544
6545
6546struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006547 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548{
6549 struct p2p_group *group;
6550 struct p2p_group_config *cfg;
6551
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006552 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6553 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554 return NULL;
6555
6556 cfg = os_zalloc(sizeof(*cfg));
6557 if (cfg == NULL)
6558 return NULL;
6559
Dmitry Shmidt04949592012-07-19 12:16:46 -07006560 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006561 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006562 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006563 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006564 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6565 if (wpa_s->max_stations &&
6566 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6567 cfg->max_clients = wpa_s->max_stations;
6568 else
6569 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006570 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6571 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006572 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573 cfg->cb_ctx = wpa_s;
6574 cfg->ie_update = wpas_p2p_ie_update;
6575 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006576 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6577 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578
6579 group = p2p_group_init(wpa_s->global->p2p, cfg);
6580 if (group == NULL)
6581 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006582 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583 p2p_group_notif_formation_done(group);
6584 wpa_s->p2p_group = group;
6585 return group;
6586}
6587
6588
6589void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6590 int registrar)
6591{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006592 struct wpa_ssid *ssid = wpa_s->current_ssid;
6593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006594 if (!wpa_s->p2p_in_provisioning) {
6595 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6596 "provisioning not in progress");
6597 return;
6598 }
6599
Dmitry Shmidt04949592012-07-19 12:16:46 -07006600 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6601 u8 go_dev_addr[ETH_ALEN];
6602 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6603 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6604 ssid->ssid_len);
6605 /* Clear any stored provisioning info */
6606 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6607 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006608
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006609 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006610 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006611 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006612 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6613 /*
6614 * Use a separate timeout for initial data connection to
6615 * complete to allow the group to be removed automatically if
6616 * something goes wrong in this step before the P2P group idle
6617 * timeout mechanism is taken into use.
6618 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006619 wpa_dbg(wpa_s, MSG_DEBUG,
6620 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6621 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006622 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6623 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006624 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006625 /* Complete group formation on successful data connection. */
6626 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006627 } else if (ssid) {
6628 /*
6629 * Use a separate timeout for initial data connection to
6630 * complete to allow the group to be removed automatically if
6631 * the client does not complete data connection successfully.
6632 */
6633 wpa_dbg(wpa_s, MSG_DEBUG,
6634 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6635 P2P_MAX_INITIAL_CONN_WAIT_GO);
6636 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6637 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006638 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006639 /*
6640 * Complete group formation on first successful data connection
6641 */
6642 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006644 if (wpa_s->global->p2p)
6645 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006646 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006647}
6648
6649
Jouni Malinen75ecf522011-06-27 15:19:46 -07006650void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6651 struct wps_event_fail *fail)
6652{
6653 if (!wpa_s->p2p_in_provisioning) {
6654 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6655 "provisioning not in progress");
6656 return;
6657 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006658
6659 if (wpa_s->go_params) {
6660 p2p_clear_provisioning_info(
6661 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006662 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006663 }
6664
Jouni Malinen75ecf522011-06-27 15:19:46 -07006665 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006666
6667 if (wpa_s == wpa_s->global->p2p_group_formation) {
6668 /*
6669 * Allow some time for the failed WPS negotiation exchange to
6670 * complete, but remove the group since group formation cannot
6671 * succeed after provisioning failure.
6672 */
6673 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6674 wpa_s->global->p2p_fail_on_wps_complete = 1;
6675 eloop_deplete_timeout(0, 50000,
6676 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006677 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006678 }
6679}
6680
6681
6682int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6683{
6684 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6685 !wpa_s->p2p_in_provisioning)
6686 return 0;
6687
6688 wpas_p2p_grpform_fail_after_wps(wpa_s);
6689
6690 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006691}
6692
6693
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006695 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006696 enum wpas_p2p_prov_disc_use use,
6697 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006698{
6699 u16 config_methods;
6700
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006701 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006702 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006703 wpa_s->p2p_fallback_to_go_neg = 0;
6704 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006705 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6706 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006707 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6708 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6709
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006710 wpa_printf(MSG_DEBUG,
6711 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6712 __func__, p2ps_prov->conncap,
6713 p2ps_prov->adv_id, p2ps_prov->conncap,
6714 p2ps_prov->status, p2ps_prov->info);
6715
6716 config_methods = 0;
6717 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006718 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006719 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006720 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006721 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6722 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006723 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006724 else {
6725 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006726 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006727 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006728 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006729
Dmitry Shmidt04949592012-07-19 12:16:46 -07006730 if (use == WPAS_P2P_PD_AUTO) {
6731 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6732 wpa_s->pending_pd_config_methods = config_methods;
6733 wpa_s->p2p_auto_pd = 1;
6734 wpa_s->p2p_auto_join = 0;
6735 wpa_s->pending_pd_before_join = 0;
6736 wpa_s->auto_pd_scan_retry = 0;
6737 wpas_p2p_stop_find(wpa_s);
6738 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006739 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006740 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6741 wpa_s->p2p_auto_started.sec,
6742 wpa_s->p2p_auto_started.usec);
6743 wpas_p2p_join_scan(wpa_s, NULL);
6744 return 0;
6745 }
6746
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006747 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6748 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006749 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006750 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006752 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006753 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006754 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006755}
6756
6757
6758int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6759 char *end)
6760{
6761 return p2p_scan_result_text(ies, ies_len, buf, end);
6762}
6763
6764
6765static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6766{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006767 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006768 return;
6769
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006770 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006771 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006772 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6773 wpa_s, NULL);
6774 offchannel_send_action_done(wpa_s);
6775 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006777 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6778 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006779 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006780}
6781
6782
6783int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6784 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006785 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006786 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006787 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006788{
6789 wpas_p2p_clear_pending_action_tx(wpa_s);
6790 wpa_s->p2p_long_listen = 0;
6791
Dmitry Shmidt04949592012-07-19 12:16:46 -07006792 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006793 wpa_s->p2p_in_provisioning) {
6794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6795 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6796 " (P2P disabled)" : "",
6797 wpa_s->p2p_in_provisioning ?
6798 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006800 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006802 wpa_supplicant_cancel_sched_scan(wpa_s);
6803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006804 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006805 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006806 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006807}
6808
6809
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006810static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6811 struct wpa_scan_results *scan_res)
6812{
6813 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6814
6815 if (wpa_s->p2p_scan_work) {
6816 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6817 wpa_s->p2p_scan_work = NULL;
6818 radio_work_done(work);
6819 }
6820
6821 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6822 return;
6823
6824 /*
6825 * Indicate that results have been processed so that the P2P module can
6826 * continue pending tasks.
6827 */
6828 p2p_scan_res_handled(wpa_s->global->p2p);
6829}
6830
6831
6832static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006833{
6834 wpas_p2p_clear_pending_action_tx(wpa_s);
6835 wpa_s->p2p_long_listen = 0;
6836 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6837 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006838
6839 if (wpa_s->global->p2p)
6840 p2p_stop_find(wpa_s->global->p2p);
6841
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006842 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6843 wpa_printf(MSG_DEBUG,
6844 "P2P: Do not consider the scan results after stop_find");
6845 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6846 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006847}
6848
6849
6850void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6851{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006852 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006853 if (!wpa_s->global->pending_group_iface_for_p2ps)
6854 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855}
6856
6857
6858static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6859{
6860 struct wpa_supplicant *wpa_s = eloop_ctx;
6861 wpa_s->p2p_long_listen = 0;
6862}
6863
6864
6865int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6866{
6867 int res;
6868
6869 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6870 return -1;
6871
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006872 if (wpa_s->p2p_lo_started) {
6873 wpa_printf(MSG_DEBUG,
6874 "P2P: Cannot start P2P listen, it is offloaded");
6875 return -1;
6876 }
6877
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006878 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006879 wpas_p2p_clear_pending_action_tx(wpa_s);
6880
6881 if (timeout == 0) {
6882 /*
6883 * This is a request for unlimited Listen state. However, at
6884 * least for now, this is mapped to a Listen state for one
6885 * hour.
6886 */
6887 timeout = 3600;
6888 }
6889 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6890 wpa_s->p2p_long_listen = 0;
6891
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006892 /*
6893 * Stop previous find/listen operation to avoid trying to request a new
6894 * remain-on-channel operation while the driver is still running the
6895 * previous one.
6896 */
6897 if (wpa_s->global->p2p)
6898 p2p_stop_find(wpa_s->global->p2p);
6899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6901 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6902 wpa_s->p2p_long_listen = timeout * 1000;
6903 eloop_register_timeout(timeout, 0,
6904 wpas_p2p_long_listen_timeout,
6905 wpa_s, NULL);
6906 }
6907
6908 return res;
6909}
6910
6911
6912int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6913 u8 *buf, size_t len, int p2p_group)
6914{
6915 struct wpabuf *p2p_ie;
6916 int ret;
6917
6918 if (wpa_s->global->p2p_disabled)
6919 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006920 /*
6921 * Advertize mandatory cross connection capability even on
6922 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6923 */
6924 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006925 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926 if (wpa_s->global->p2p == NULL)
6927 return -1;
6928 if (bss == NULL)
6929 return -1;
6930
6931 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6932 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6933 p2p_group, p2p_ie);
6934 wpabuf_free(p2p_ie);
6935
6936 return ret;
6937}
6938
6939
6940int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006941 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006942 const u8 *ie, size_t ie_len,
6943 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006944{
6945 if (wpa_s->global->p2p_disabled)
6946 return 0;
6947 if (wpa_s->global->p2p == NULL)
6948 return 0;
6949
Dmitry Shmidt04949592012-07-19 12:16:46 -07006950 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006951 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006952 case P2P_PREQ_NOT_P2P:
6953 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6954 ssi_signal);
6955 /* fall through */
6956 case P2P_PREQ_MALFORMED:
6957 case P2P_PREQ_NOT_LISTEN:
6958 case P2P_PREQ_NOT_PROCESSED:
6959 default: /* make gcc happy */
6960 return 0;
6961 case P2P_PREQ_PROCESSED:
6962 return 1;
6963 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006964}
6965
6966
6967void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6968 const u8 *sa, const u8 *bssid,
6969 u8 category, const u8 *data, size_t len, int freq)
6970{
6971 if (wpa_s->global->p2p_disabled)
6972 return;
6973 if (wpa_s->global->p2p == NULL)
6974 return;
6975
6976 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6977 freq);
6978}
6979
6980
6981void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6982{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006983 unsigned int bands;
6984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006985 if (wpa_s->global->p2p_disabled)
6986 return;
6987 if (wpa_s->global->p2p == NULL)
6988 return;
6989
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006990 bands = wpas_get_bands(wpa_s, NULL);
6991 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006992}
6993
6994
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006995static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006996{
6997 p2p_group_deinit(wpa_s->p2p_group);
6998 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006999
7000 wpa_s->ap_configured_cb = NULL;
7001 wpa_s->ap_configured_cb_ctx = NULL;
7002 wpa_s->ap_configured_cb_data = NULL;
7003 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004}
7005
7006
7007int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7008{
7009 wpa_s->p2p_long_listen = 0;
7010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7012 return -1;
7013
7014 return p2p_reject(wpa_s->global->p2p, addr);
7015}
7016
7017
7018/* Invite to reinvoke a persistent group */
7019int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007020 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007021 int vht_center_freq2, int ht40, int vht, int max_chwidth,
7022 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007023{
7024 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007025 u8 *bssid = NULL;
7026 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007027 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007028 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007029 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007030
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007031 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007032 if (peer_addr)
7033 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7034 else
7035 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007036
Jouni Malinen31be0a42012-08-31 21:20:51 +03007037 wpa_s->p2p_persistent_go_freq = freq;
7038 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007039 wpa_s->p2p_go_vht = !!vht;
7040 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
7041 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007042 if (ssid->mode == WPAS_MODE_P2P_GO) {
7043 role = P2P_INVITE_ROLE_GO;
7044 if (peer_addr == NULL) {
7045 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7046 "address in invitation command");
7047 return -1;
7048 }
7049 if (wpas_p2p_create_iface(wpa_s)) {
7050 if (wpas_p2p_add_group_interface(wpa_s,
7051 WPA_IF_P2P_GO) < 0) {
7052 wpa_printf(MSG_ERROR, "P2P: Failed to "
7053 "allocate a new interface for the "
7054 "group");
7055 return -1;
7056 }
7057 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007058 } else if (wpa_s->p2p_mgmt)
7059 bssid = wpa_s->parent->own_addr;
7060 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007061 bssid = wpa_s->own_addr;
7062 } else {
7063 role = P2P_INVITE_ROLE_CLIENT;
7064 peer_addr = ssid->bssid;
7065 }
7066 wpa_s->pending_invite_ssid_id = ssid->id;
7067
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007068 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007069 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007070 role == P2P_INVITE_ROLE_GO,
7071 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007072 if (res)
7073 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7076 return -1;
7077
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08007078 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
7079
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007080 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7081 no_pref_freq_given && pref_freq > 0 &&
7082 wpa_s->num_multichan_concurrent > 1 &&
7083 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7084 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7085 pref_freq);
7086 pref_freq = 0;
7087 }
7088
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007089 /*
7090 * Stop any find/listen operations before invitation and possibly
7091 * connection establishment.
7092 */
7093 wpas_p2p_stop_find_oper(wpa_s);
7094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007095 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007096 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007097 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007098}
7099
7100
7101/* Invite to join an active group */
7102int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7103 const u8 *peer_addr, const u8 *go_dev_addr)
7104{
7105 struct wpa_global *global = wpa_s->global;
7106 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007107 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007108 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007109 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007110 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007111 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007112 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007113
Jouni Malinen31be0a42012-08-31 21:20:51 +03007114 wpa_s->p2p_persistent_go_freq = 0;
7115 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007116 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007117 wpa_s->p2p_go_vht_center_freq2 = 0;
7118 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7121 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7122 break;
7123 }
7124 if (wpa_s == NULL) {
7125 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7126 return -1;
7127 }
7128
7129 ssid = wpa_s->current_ssid;
7130 if (ssid == NULL) {
7131 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7132 "invitation");
7133 return -1;
7134 }
7135
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007136 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007137 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007138 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007139 ssid->ssid, ssid->ssid_len);
7140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141 if (ssid->mode == WPAS_MODE_P2P_GO) {
7142 role = P2P_INVITE_ROLE_ACTIVE_GO;
7143 bssid = wpa_s->own_addr;
7144 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007145 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007146 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147 } else {
7148 role = P2P_INVITE_ROLE_CLIENT;
7149 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7150 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7151 "invite to current group");
7152 return -1;
7153 }
7154 bssid = wpa_s->bssid;
7155 if (go_dev_addr == NULL &&
7156 !is_zero_ether_addr(wpa_s->go_dev_addr))
7157 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007158 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7159 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007160 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007161 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007163 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7164 return -1;
7165
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007166 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007167 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007168 role == P2P_INVITE_ROLE_ACTIVE_GO,
7169 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007170 if (res)
7171 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007172 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007174 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007175 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007176 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177}
7178
7179
7180void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7181{
7182 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007183 u8 go_dev_addr[ETH_ALEN];
7184 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007185 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007186 u8 ip[3 * 4];
7187 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188
Dmitry Shmidt04949592012-07-19 12:16:46 -07007189 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7190 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007191 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007192 }
7193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007194 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007195 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007196
7197 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007198 if (!wpa_s->p2p_go_group_formation_completed &&
7199 wpa_s->global->p2p_group_formation == wpa_s) {
7200 wpa_dbg(wpa_s, MSG_DEBUG,
7201 "P2P: Marking group formation completed on client on data connection");
7202 wpa_s->p2p_go_group_formation_completed = 1;
7203 wpa_s->global->p2p_group_formation = NULL;
7204 wpa_s->p2p_in_provisioning = 0;
7205 wpa_s->p2p_in_invitation = 0;
7206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007208 os_memset(go_dev_addr, 0, ETH_ALEN);
7209 if (ssid->bssid_set)
7210 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7211 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7212 ssid->ssid_len);
7213 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7214
7215 if (wpa_s->global->p2p_group_formation == wpa_s)
7216 wpa_s->global->p2p_group_formation = NULL;
7217
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007218 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7219 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007220
7221 ip_addr[0] = '\0';
7222 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007223 int res;
7224
7225 res = os_snprintf(ip_addr, sizeof(ip_addr),
7226 " ip_addr=%u.%u.%u.%u "
7227 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7228 ip[0], ip[1], ip[2], ip[3],
7229 ip[4], ip[5], ip[6], ip[7],
7230 ip[8], ip[9], ip[10], ip[11]);
7231 if (os_snprintf_error(sizeof(ip_addr), res))
7232 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007233 }
7234
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007235 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7236 ssid->passphrase == NULL && ssid->psk_set ?
7237 ssid->psk : NULL,
7238 ssid->passphrase, go_dev_addr, persistent,
7239 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007240
7241 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007242 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7243 ssid, go_dev_addr);
7244
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007245 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007246}
7247
7248
7249int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7250 u32 interval1, u32 duration2, u32 interval2)
7251{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007252 int ret;
7253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007254 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7255 return -1;
7256
7257 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7258 wpa_s->current_ssid == NULL ||
7259 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7260 return -1;
7261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007262 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7263 wpa_s->own_addr, wpa_s->assoc_freq,
7264 duration1, interval1, duration2, interval2);
7265 if (ret == 0)
7266 wpa_s->waiting_presence_resp = 1;
7267
7268 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007269}
7270
7271
7272int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7273 unsigned int interval)
7274{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7276 return -1;
7277
7278 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7279}
7280
7281
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007282static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7283{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007284 if (wpa_s->current_ssid == NULL) {
7285 /*
7286 * current_ssid can be cleared when P2P client interface gets
7287 * disconnected, so assume this interface was used as P2P
7288 * client.
7289 */
7290 return 1;
7291 }
7292 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007293 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7294}
7295
7296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007297static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7298{
7299 struct wpa_supplicant *wpa_s = eloop_ctx;
7300
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007301 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007302 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7303 "disabled");
7304 return;
7305 }
7306
Dmitry Shmidt04949592012-07-19 12:16:46 -07007307 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7308 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007309 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007310}
7311
7312
7313static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7314{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007315 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007316
Dmitry Shmidt04949592012-07-19 12:16:46 -07007317 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7318 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7321 return;
7322
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007323 timeout = wpa_s->conf->p2p_group_idle;
7324 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7325 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7326 timeout = P2P_MAX_CLIENT_IDLE;
7327
7328 if (timeout == 0)
7329 return;
7330
Dmitry Shmidt04949592012-07-19 12:16:46 -07007331 if (timeout < 0) {
7332 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7333 timeout = 0; /* special client mode no-timeout */
7334 else
7335 return;
7336 }
7337
7338 if (wpa_s->p2p_in_provisioning) {
7339 /*
7340 * Use the normal group formation timeout during the
7341 * provisioning phase to avoid terminating this process too
7342 * early due to group idle timeout.
7343 */
7344 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7345 "during provisioning");
7346 return;
7347 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307348
Dmitry Shmidt04949592012-07-19 12:16:46 -07007349 if (wpa_s->show_group_started) {
7350 /*
7351 * Use the normal group formation timeout between the end of
7352 * the provisioning phase and completion of 4-way handshake to
7353 * avoid terminating this process too early due to group idle
7354 * timeout.
7355 */
7356 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7357 "while waiting for initial 4-way handshake to "
7358 "complete");
7359 return;
7360 }
7361
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007362 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007363 timeout);
7364 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7365 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007366}
7367
7368
Jouni Malinen2b89da82012-08-31 22:04:41 +03007369/* Returns 1 if the interface was removed */
7370int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7371 u16 reason_code, const u8 *ie, size_t ie_len,
7372 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007373{
7374 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007375 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007376
Dmitry Shmidt04949592012-07-19 12:16:46 -07007377 if (!locally_generated)
7378 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7379 ie_len);
7380
7381 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7382 wpa_s->current_ssid &&
7383 wpa_s->current_ssid->p2p_group &&
7384 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7385 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7386 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007387 if (wpas_p2p_group_delete(wpa_s,
7388 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7389 > 0)
7390 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007391 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007392
7393 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007394}
7395
7396
7397void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007398 u16 reason_code, const u8 *ie, size_t ie_len,
7399 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007400{
7401 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7402 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007403
Dmitry Shmidt04949592012-07-19 12:16:46 -07007404 if (!locally_generated)
7405 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7406 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007407}
7408
7409
7410void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7411{
7412 struct p2p_data *p2p = wpa_s->global->p2p;
7413
7414 if (p2p == NULL)
7415 return;
7416
7417 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7418 return;
7419
7420 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7421 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7422
7423 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7424 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7425
7426 if (wpa_s->wps &&
7427 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7428 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7429
7430 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7431 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7432
7433 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7434 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7435 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7436 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7437 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7438 }
7439
7440 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7441 p2p_set_sec_dev_types(p2p,
7442 (void *) wpa_s->conf->sec_device_type,
7443 wpa_s->conf->num_sec_device_types);
7444
7445 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7446 int i;
7447 p2p_remove_wps_vendor_extensions(p2p);
7448 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7449 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7450 continue;
7451 p2p_add_wps_vendor_extension(
7452 p2p, wpa_s->conf->wps_vendor_ext[i]);
7453 }
7454 }
7455
7456 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7457 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7458 char country[3];
7459 country[0] = wpa_s->conf->country[0];
7460 country[1] = wpa_s->conf->country[1];
7461 country[2] = 0x04;
7462 p2p_set_country(p2p, country);
7463 }
7464
7465 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7466 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7467 wpa_s->conf->p2p_ssid_postfix ?
7468 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7469 0);
7470 }
7471
7472 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7473 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007474
7475 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7476 u8 reg_class, channel;
7477 int ret;
7478 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007479 u8 channel_forced;
7480
Jouni Malinen75ecf522011-06-27 15:19:46 -07007481 if (wpa_s->conf->p2p_listen_reg_class &&
7482 wpa_s->conf->p2p_listen_channel) {
7483 reg_class = wpa_s->conf->p2p_listen_reg_class;
7484 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007485 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007486 } else {
7487 reg_class = 81;
7488 /*
7489 * Pick one of the social channels randomly as the
7490 * listen channel.
7491 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007492 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7493 channel = 1;
7494 else
7495 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007496 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007497 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007498 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7499 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007500 if (ret)
7501 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7502 "failed: %d", ret);
7503 }
7504 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7505 u8 op_reg_class, op_channel, cfg_op_channel;
7506 int ret = 0;
7507 unsigned int r;
7508 if (wpa_s->conf->p2p_oper_reg_class &&
7509 wpa_s->conf->p2p_oper_channel) {
7510 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7511 op_channel = wpa_s->conf->p2p_oper_channel;
7512 cfg_op_channel = 1;
7513 } else {
7514 op_reg_class = 81;
7515 /*
7516 * Use random operation channel from (1, 6, 11)
7517 *if no other preference is indicated.
7518 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007519 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7520 op_channel = 1;
7521 else
7522 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007523 cfg_op_channel = 0;
7524 }
7525 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7526 cfg_op_channel);
7527 if (ret)
7528 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7529 "failed: %d", ret);
7530 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007531
7532 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7533 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7534 wpa_s->conf->p2p_pref_chan) < 0) {
7535 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7536 "update failed");
7537 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007538
7539 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7540 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7541 "update failed");
7542 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007543 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007544
7545 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7546 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007547}
7548
7549
7550int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7551 int duration)
7552{
7553 if (!wpa_s->ap_iface)
7554 return -1;
7555 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7556 duration);
7557}
7558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007559
7560int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7561{
7562 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7563 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007564
7565 wpa_s->global->cross_connection = enabled;
7566 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7567
7568 if (!enabled) {
7569 struct wpa_supplicant *iface;
7570
7571 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7572 {
7573 if (iface->cross_connect_enabled == 0)
7574 continue;
7575
7576 iface->cross_connect_enabled = 0;
7577 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007578 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007579 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7580 iface->ifname,
7581 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007582 }
7583 }
7584
7585 return 0;
7586}
7587
7588
7589static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7590{
7591 struct wpa_supplicant *iface;
7592
7593 if (!uplink->global->cross_connection)
7594 return;
7595
7596 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7597 if (!iface->cross_connect_enabled)
7598 continue;
7599 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7600 0)
7601 continue;
7602 if (iface->ap_iface == NULL)
7603 continue;
7604 if (iface->cross_connect_in_use)
7605 continue;
7606
7607 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007608 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007609 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7610 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007611 }
7612}
7613
7614
7615static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7616{
7617 struct wpa_supplicant *iface;
7618
7619 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7620 if (!iface->cross_connect_enabled)
7621 continue;
7622 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7623 0)
7624 continue;
7625 if (!iface->cross_connect_in_use)
7626 continue;
7627
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007628 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007629 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7630 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631 iface->cross_connect_in_use = 0;
7632 }
7633}
7634
7635
7636void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7637{
7638 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7639 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7640 wpa_s->cross_connect_disallowed)
7641 wpas_p2p_disable_cross_connect(wpa_s);
7642 else
7643 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007644 if (!wpa_s->ap_iface &&
7645 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7646 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007647}
7648
7649
7650void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7651{
7652 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007653 if (!wpa_s->ap_iface &&
7654 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7655 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007656 wpas_p2p_set_group_idle_timeout(wpa_s);
7657}
7658
7659
7660static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7661{
7662 struct wpa_supplicant *iface;
7663
7664 if (!wpa_s->global->cross_connection)
7665 return;
7666
7667 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7668 if (iface == wpa_s)
7669 continue;
7670 if (iface->drv_flags &
7671 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7672 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007673 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7674 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007675 continue;
7676
7677 wpa_s->cross_connect_enabled = 1;
7678 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7679 sizeof(wpa_s->cross_connect_uplink));
7680 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7681 "%s to %s whenever uplink is available",
7682 wpa_s->ifname, wpa_s->cross_connect_uplink);
7683
7684 if (iface->ap_iface || iface->current_ssid == NULL ||
7685 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7686 iface->cross_connect_disallowed ||
7687 iface->wpa_state != WPA_COMPLETED)
7688 break;
7689
7690 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007691 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007692 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7693 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007694 break;
7695 }
7696}
7697
7698
7699int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7700{
7701 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7702 !wpa_s->p2p_in_provisioning)
7703 return 0; /* not P2P client operation */
7704
7705 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7706 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007707 if (wpa_s != wpa_s->p2pdev)
7708 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007709 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007710 return 1;
7711}
7712
7713
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007714void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7715{
7716 struct wpa_supplicant *wpa_s = eloop_ctx;
7717 wpas_p2p_notif_pbc_overlap(wpa_s);
7718}
7719
7720
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007721void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7722 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007723{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007724 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007725 struct wpa_used_freq_data *freqs = NULL;
7726 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007727
7728 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7729 return;
7730
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007731 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7732 if (!freqs)
7733 return;
7734
7735 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007737 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007738 os_memset(&cli_chan, 0, sizeof(cli_chan));
7739 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7741 "channel list");
7742 return;
7743 }
7744
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007745 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007746
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007747 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007748
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007749 /*
7750 * The used frequencies map changed, so it is possible that a GO is
7751 * using a channel that is no longer valid for P2P use. It is also
7752 * possible that due to policy consideration, it would be preferable to
7753 * move it to a frequency already used by other station interfaces.
7754 */
7755 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7756
7757 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007758}
7759
7760
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007761static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7762 struct wpa_scan_results *scan_res)
7763{
7764 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7765}
7766
7767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007768int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7769{
7770 struct wpa_global *global = wpa_s->global;
7771 int found = 0;
7772 const u8 *peer;
7773
7774 if (global->p2p == NULL)
7775 return -1;
7776
7777 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7778
7779 if (wpa_s->pending_interface_name[0] &&
7780 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7781 found = 1;
7782
7783 peer = p2p_get_go_neg_peer(global->p2p);
7784 if (peer) {
7785 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7786 MACSTR, MAC2STR(peer));
7787 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007788 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789 }
7790
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007791 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7792 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7793 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7794 found = 1;
7795 }
7796
7797 if (wpa_s->pending_pd_before_join) {
7798 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7799 wpa_s->pending_pd_before_join = 0;
7800 found = 1;
7801 }
7802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007803 wpas_p2p_stop_find(wpa_s);
7804
7805 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7806 if (wpa_s == global->p2p_group_formation &&
7807 (wpa_s->p2p_in_provisioning ||
7808 wpa_s->parent->pending_interface_type ==
7809 WPA_IF_P2P_CLIENT)) {
7810 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7811 "formation found - cancelling",
7812 wpa_s->ifname);
7813 found = 1;
7814 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007815 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007816 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007817 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007818 break;
7819 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007820 wpas_p2p_group_delete(wpa_s,
7821 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007822 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007823 } else if (wpa_s->p2p_in_invitation) {
7824 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7825 wpa_s->ifname);
7826 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007827 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007828 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007829 }
7830 }
7831
7832 if (!found) {
7833 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7834 return -1;
7835 }
7836
7837 return 0;
7838}
7839
7840
7841void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7842{
7843 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7844 return;
7845
7846 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7847 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007848 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007849}
7850
7851
7852void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7853 int freq_24, int freq_5, int freq_overall)
7854{
7855 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007856 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007857 return;
7858 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7859}
7860
7861
7862int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7863{
7864 u8 peer[ETH_ALEN];
7865 struct p2p_data *p2p = wpa_s->global->p2p;
7866
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007867 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007868 return -1;
7869
7870 if (hwaddr_aton(addr, peer))
7871 return -1;
7872
7873 return p2p_unauthorize(p2p, peer);
7874}
7875
7876
7877/**
7878 * wpas_p2p_disconnect - Disconnect from a P2P Group
7879 * @wpa_s: Pointer to wpa_supplicant data
7880 * Returns: 0 on success, -1 on failure
7881 *
7882 * This can be used to disconnect from a group in which the local end is a P2P
7883 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7884 * virtual network interface was created for this group, that interface will be
7885 * removed. Otherwise, only the configured P2P group network will be removed
7886 * from the interface.
7887 */
7888int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7889{
7890
7891 if (wpa_s == NULL)
7892 return -1;
7893
Jouni Malinen2b89da82012-08-31 22:04:41 +03007894 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7895 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007896}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007897
7898
7899int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7900{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007901 int ret;
7902
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007903 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7904 return 0;
7905
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007906 ret = p2p_in_progress(wpa_s->global->p2p);
7907 if (ret == 0) {
7908 /*
7909 * Check whether there is an ongoing WPS provisioning step (or
7910 * other parts of group formation) on another interface since
7911 * p2p_in_progress() does not report this to avoid issues for
7912 * scans during such provisioning step.
7913 */
7914 if (wpa_s->global->p2p_group_formation &&
7915 wpa_s->global->p2p_group_formation != wpa_s) {
7916 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7917 "in group formation",
7918 wpa_s->global->p2p_group_formation->ifname);
7919 ret = 1;
7920 }
7921 }
7922
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007923 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007924 struct os_reltime now;
7925 os_get_reltime(&now);
7926 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7927 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007928 /* Wait for the first client has expired */
7929 wpa_s->global->p2p_go_wait_client.sec = 0;
7930 } else {
7931 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7932 ret = 1;
7933 }
7934 }
7935
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007936 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007937}
7938
7939
7940void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7941 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007942{
7943 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7944 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007945 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007946 /**
7947 * Remove the network by scheduling the group formation
7948 * timeout to happen immediately. The teardown code
7949 * needs to be scheduled to run asynch later so that we
7950 * don't delete data from under ourselves unexpectedly.
7951 * Calling wpas_p2p_group_formation_timeout directly
7952 * causes a series of crashes in WPS failure scenarios.
7953 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007954 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7955 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007956 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007957 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007958 }
7959}
7960
7961
7962struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007963 const u8 *addr, const u8 *ssid,
7964 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007965{
7966 struct wpa_ssid *s;
7967 size_t i;
7968
7969 for (s = wpa_s->conf->ssid; s; s = s->next) {
7970 if (s->disabled != 2)
7971 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007972 if (ssid &&
7973 (ssid_len != s->ssid_len ||
7974 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7975 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007976 if (addr == NULL) {
7977 if (s->mode == WPAS_MODE_P2P_GO)
7978 return s;
7979 continue;
7980 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007981 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7982 return s; /* peer is GO in the persistent group */
7983 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7984 continue;
7985 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007986 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007987 addr, ETH_ALEN) == 0)
7988 return s; /* peer is P2P client in persistent
7989 * group */
7990 }
7991 }
7992
7993 return NULL;
7994}
7995
7996
7997void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7998 const u8 *addr)
7999{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008000 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008001 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07008002 /*
8003 * This can happen if WPS provisioning step is not terminated
8004 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8005 * peer was able to connect, there is no need to time out group
8006 * formation after this, though. In addition, this is used with
8007 * the initial connection wait on the GO as a separate formation
8008 * timeout and as such, expected to be hit after the initial WPS
8009 * provisioning step.
8010 */
8011 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008012
8013 if (!wpa_s->p2p_go_group_formation_completed &&
8014 !wpa_s->group_formation_reported) {
8015 /*
8016 * GO has not yet notified group formation success since
8017 * the WPS step was not completed cleanly. Do that
8018 * notification now since the P2P Client was able to
8019 * connect and as such, must have received the
8020 * credential from the WPS step.
8021 */
8022 if (wpa_s->global->p2p)
8023 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008024 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008025 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008026 }
8027 if (!wpa_s->p2p_go_group_formation_completed) {
8028 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8029 wpa_s->p2p_go_group_formation_completed = 1;
8030 wpa_s->global->p2p_group_formation = NULL;
8031 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008032 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008033 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008034 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008035 if (addr == NULL)
8036 return;
8037 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8038}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008039
Dmitry Shmidt04949592012-07-19 12:16:46 -07008040
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008041static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8042 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008043{
8044 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008045 int ret = 0;
8046
Dmitry Shmidt04949592012-07-19 12:16:46 -07008047 if (wpa_s->global->p2p_group_formation)
8048 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07008049 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008050 offchannel_send_action_done(wpa_s);
8051 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008052 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008053 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8054 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8055 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8056 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008057 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07008058 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008059 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008060 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008061 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008062 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008063 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008064}
8065
8066
8067int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8068{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008069 int res;
8070
Dmitry Shmidt04949592012-07-19 12:16:46 -07008071 if (!wpa_s->p2p_fallback_to_go_neg ||
8072 wpa_s->p2p_in_provisioning <= 5)
8073 return 0;
8074
8075 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8076 return 0; /* peer operating as a GO */
8077
8078 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8079 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008080 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008081 "reason=GO-not-found");
8082 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008083
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008084 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008085}
8086
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008087
8088unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8089{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008090 struct wpa_supplicant *ifs;
8091
8092 if (wpa_s->wpa_state > WPA_SCANNING) {
8093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8094 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008095 wpa_s->conf->p2p_search_delay);
8096 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008097 }
8098
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008099 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8100 radio_list) {
8101 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8103 "delay due to concurrent operation on "
8104 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008105 wpa_s->conf->p2p_search_delay,
8106 ifs->ifname);
8107 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008108 }
8109 }
8110
8111 return 0;
8112}
8113
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008114
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008115static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8116 struct wpa_ssid *s, const u8 *addr,
8117 int iface_addr)
8118{
8119 struct psk_list_entry *psk, *tmp;
8120 int changed = 0;
8121
8122 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8123 list) {
8124 if ((iface_addr && !psk->p2p &&
8125 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8126 (!iface_addr && psk->p2p &&
8127 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8128 wpa_dbg(wpa_s, MSG_DEBUG,
8129 "P2P: Remove persistent group PSK list entry for "
8130 MACSTR " p2p=%u",
8131 MAC2STR(psk->addr), psk->p2p);
8132 dl_list_del(&psk->list);
8133 os_free(psk);
8134 changed++;
8135 }
8136 }
8137
8138 return changed;
8139}
8140
8141
8142void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8143 const u8 *p2p_dev_addr,
8144 const u8 *psk, size_t psk_len)
8145{
8146 struct wpa_ssid *ssid = wpa_s->current_ssid;
8147 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008148 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008149
8150 if (psk_len != sizeof(p->psk))
8151 return;
8152
8153 if (p2p_dev_addr) {
8154 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8155 " p2p_dev_addr=" MACSTR,
8156 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8157 if (is_zero_ether_addr(p2p_dev_addr))
8158 p2p_dev_addr = NULL;
8159 } else {
8160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8161 MAC2STR(mac_addr));
8162 }
8163
8164 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8165 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8166 /* To be added to persistent group once created */
8167 if (wpa_s->global->add_psk == NULL) {
8168 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8169 if (wpa_s->global->add_psk == NULL)
8170 return;
8171 }
8172 p = wpa_s->global->add_psk;
8173 if (p2p_dev_addr) {
8174 p->p2p = 1;
8175 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8176 } else {
8177 p->p2p = 0;
8178 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8179 }
8180 os_memcpy(p->psk, psk, psk_len);
8181 return;
8182 }
8183
8184 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8186 return;
8187 }
8188
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008189 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008190 ssid->ssid_len);
8191 if (!persistent) {
8192 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8193 return;
8194 }
8195
8196 p = os_zalloc(sizeof(*p));
8197 if (p == NULL)
8198 return;
8199 if (p2p_dev_addr) {
8200 p->p2p = 1;
8201 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8202 } else {
8203 p->p2p = 0;
8204 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8205 }
8206 os_memcpy(p->psk, psk, psk_len);
8207
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008208 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8209 (last = dl_list_last(&persistent->psk_list,
8210 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008211 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8212 MACSTR " (p2p=%u) to make room for a new one",
8213 MAC2STR(last->addr), last->p2p);
8214 dl_list_del(&last->list);
8215 os_free(last);
8216 }
8217
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008218 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008219 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8220 p2p_dev_addr == NULL);
8221 if (p2p_dev_addr) {
8222 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8223 MACSTR, MAC2STR(p2p_dev_addr));
8224 } else {
8225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8226 MAC2STR(mac_addr));
8227 }
8228 dl_list_add(&persistent->psk_list, &p->list);
8229
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008230 if (wpa_s->p2pdev->conf->update_config &&
8231 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008232 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008233}
8234
8235
8236static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8237 struct wpa_ssid *s, const u8 *addr,
8238 int iface_addr)
8239{
8240 int res;
8241
8242 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008243 if (res > 0 && wpa_s->conf->update_config &&
8244 wpa_config_write(wpa_s->confname, wpa_s->conf))
8245 wpa_dbg(wpa_s, MSG_DEBUG,
8246 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008247}
8248
8249
8250static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8251 const u8 *peer, int iface_addr)
8252{
8253 struct hostapd_data *hapd;
8254 struct hostapd_wpa_psk *psk, *prev, *rem;
8255 struct sta_info *sta;
8256
8257 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8258 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8259 return;
8260
8261 /* Remove per-station PSK entry */
8262 hapd = wpa_s->ap_iface->bss[0];
8263 prev = NULL;
8264 psk = hapd->conf->ssid.wpa_psk;
8265 while (psk) {
8266 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8267 (!iface_addr &&
8268 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8270 MACSTR " iface_addr=%d",
8271 MAC2STR(peer), iface_addr);
8272 if (prev)
8273 prev->next = psk->next;
8274 else
8275 hapd->conf->ssid.wpa_psk = psk->next;
8276 rem = psk;
8277 psk = psk->next;
8278 os_free(rem);
8279 } else {
8280 prev = psk;
8281 psk = psk->next;
8282 }
8283 }
8284
8285 /* Disconnect from group */
8286 if (iface_addr)
8287 sta = ap_get_sta(hapd, peer);
8288 else
8289 sta = ap_get_sta_p2p(hapd, peer);
8290 if (sta) {
8291 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8292 " (iface_addr=%d) from group",
8293 MAC2STR(peer), iface_addr);
8294 hostapd_drv_sta_deauth(hapd, sta->addr,
8295 WLAN_REASON_DEAUTH_LEAVING);
8296 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8297 }
8298}
8299
8300
8301void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8302 int iface_addr)
8303{
8304 struct wpa_ssid *s;
8305 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008306 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008307
8308 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8309
8310 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008311 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008312 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8313 continue;
8314 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008315 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8316 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008317 }
8318
8319 /* Remove from any operating group */
8320 for (w = wpa_s->global->ifaces; w; w = w->next)
8321 wpas_p2p_remove_client_go(w, peer, iface_addr);
8322}
8323
8324
8325static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8326{
8327 struct wpa_supplicant *wpa_s = eloop_ctx;
8328 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8329}
8330
8331
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008332static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8333{
8334 struct wpa_supplicant *wpa_s = eloop_ctx;
8335
8336 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8337 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8338}
8339
8340
8341int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8342 struct wpa_ssid *ssid)
8343{
8344 struct wpa_supplicant *iface;
8345
8346 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8347 if (!iface->current_ssid ||
8348 iface->current_ssid->frequency == freq ||
8349 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8350 !iface->current_ssid->p2p_group))
8351 continue;
8352
8353 /* Remove the connection with least priority */
8354 if (!wpas_is_p2p_prioritized(iface)) {
8355 /* STA connection has priority over existing
8356 * P2P connection, so remove the interface. */
8357 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8358 eloop_register_timeout(0, 0,
8359 wpas_p2p_group_freq_conflict,
8360 iface, NULL);
8361 /* If connection in progress is P2P connection, do not
8362 * proceed for the connection. */
8363 if (wpa_s == iface)
8364 return -1;
8365 else
8366 return 0;
8367 } else {
8368 /* P2P connection has priority, disable the STA network
8369 */
8370 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8371 ssid);
8372 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8373 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8374 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8375 ETH_ALEN);
8376 /* If P2P connection is in progress, continue
8377 * connecting...*/
8378 if (wpa_s == iface)
8379 return 0;
8380 else
8381 return -1;
8382 }
8383 }
8384
8385 return 0;
8386}
8387
8388
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008389int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8390{
8391 struct wpa_ssid *ssid = wpa_s->current_ssid;
8392
8393 if (ssid == NULL || !ssid->p2p_group)
8394 return 0;
8395
8396 if (wpa_s->p2p_last_4way_hs_fail &&
8397 wpa_s->p2p_last_4way_hs_fail == ssid) {
8398 u8 go_dev_addr[ETH_ALEN];
8399 struct wpa_ssid *persistent;
8400
8401 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8402 ssid->ssid,
8403 ssid->ssid_len) <= 0) {
8404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8405 goto disconnect;
8406 }
8407
8408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8409 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008410 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008411 ssid->ssid,
8412 ssid->ssid_len);
8413 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8414 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8415 goto disconnect;
8416 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008417 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008418 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8419 persistent->id);
8420 disconnect:
8421 wpa_s->p2p_last_4way_hs_fail = NULL;
8422 /*
8423 * Remove the group from a timeout to avoid issues with caller
8424 * continuing to use the interface if this is on a P2P group
8425 * interface.
8426 */
8427 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8428 wpa_s, NULL);
8429 return 1;
8430 }
8431
8432 wpa_s->p2p_last_4way_hs_fail = ssid;
8433 return 0;
8434}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008435
8436
8437#ifdef CONFIG_WPS_NFC
8438
8439static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8440 struct wpabuf *p2p)
8441{
8442 struct wpabuf *ret;
8443 size_t wsc_len;
8444
8445 if (p2p == NULL) {
8446 wpabuf_free(wsc);
8447 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8448 return NULL;
8449 }
8450
8451 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8452 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8453 if (ret == NULL) {
8454 wpabuf_free(wsc);
8455 wpabuf_free(p2p);
8456 return NULL;
8457 }
8458
8459 wpabuf_put_be16(ret, wsc_len);
8460 if (wsc)
8461 wpabuf_put_buf(ret, wsc);
8462 wpabuf_put_be16(ret, wpabuf_len(p2p));
8463 wpabuf_put_buf(ret, p2p);
8464
8465 wpabuf_free(wsc);
8466 wpabuf_free(p2p);
8467 wpa_hexdump_buf(MSG_DEBUG,
8468 "P2P: Generated NFC connection handover message", ret);
8469
8470 if (ndef && ret) {
8471 struct wpabuf *tmp;
8472 tmp = ndef_build_p2p(ret);
8473 wpabuf_free(ret);
8474 if (tmp == NULL) {
8475 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8476 return NULL;
8477 }
8478 ret = tmp;
8479 }
8480
8481 return ret;
8482}
8483
8484
8485static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8486 struct wpa_ssid **ssid, u8 *go_dev_addr)
8487{
8488 struct wpa_supplicant *iface;
8489
8490 if (go_dev_addr)
8491 os_memset(go_dev_addr, 0, ETH_ALEN);
8492 if (ssid)
8493 *ssid = NULL;
8494 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8495 if (iface->wpa_state < WPA_ASSOCIATING ||
8496 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8497 !iface->current_ssid->p2p_group ||
8498 iface->current_ssid->mode != WPAS_MODE_INFRA)
8499 continue;
8500 if (ssid)
8501 *ssid = iface->current_ssid;
8502 if (go_dev_addr)
8503 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8504 return iface->assoc_freq;
8505 }
8506 return 0;
8507}
8508
8509
8510struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8511 int ndef)
8512{
8513 struct wpabuf *wsc, *p2p;
8514 struct wpa_ssid *ssid;
8515 u8 go_dev_addr[ETH_ALEN];
8516 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8517
8518 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8519 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8520 return NULL;
8521 }
8522
8523 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8524 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8525 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8526 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8527 return NULL;
8528 }
8529
8530 if (cli_freq == 0) {
8531 wsc = wps_build_nfc_handover_req_p2p(
8532 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8533 } else
8534 wsc = NULL;
8535 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8536 go_dev_addr, ssid ? ssid->ssid : NULL,
8537 ssid ? ssid->ssid_len : 0);
8538
8539 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8540}
8541
8542
8543struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8544 int ndef, int tag)
8545{
8546 struct wpabuf *wsc, *p2p;
8547 struct wpa_ssid *ssid;
8548 u8 go_dev_addr[ETH_ALEN];
8549 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8550
8551 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8552 return NULL;
8553
8554 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8555 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8556 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8557 return NULL;
8558
8559 if (cli_freq == 0) {
8560 wsc = wps_build_nfc_handover_sel_p2p(
8561 wpa_s->parent->wps,
8562 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8563 DEV_PW_NFC_CONNECTION_HANDOVER,
8564 wpa_s->conf->wps_nfc_dh_pubkey,
8565 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8566 } else
8567 wsc = NULL;
8568 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8569 go_dev_addr, ssid ? ssid->ssid : NULL,
8570 ssid ? ssid->ssid_len : 0);
8571
8572 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8573}
8574
8575
8576static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8577 struct p2p_nfc_params *params)
8578{
8579 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8580 "connection handover (freq=%d)",
8581 params->go_freq);
8582
8583 if (params->go_freq && params->go_ssid_len) {
8584 wpa_s->p2p_wps_method = WPS_NFC;
8585 wpa_s->pending_join_wps_method = WPS_NFC;
8586 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8587 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8588 ETH_ALEN);
8589 return wpas_p2p_join_start(wpa_s, params->go_freq,
8590 params->go_ssid,
8591 params->go_ssid_len);
8592 }
8593
8594 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8595 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008596 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008597 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8598 params->go_ssid_len ? params->go_ssid : NULL,
8599 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008600}
8601
8602
8603static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8604 struct p2p_nfc_params *params, int tag)
8605{
8606 int res, persistent;
8607 struct wpa_ssid *ssid;
8608
8609 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8610 "connection handover");
8611 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8612 ssid = wpa_s->current_ssid;
8613 if (ssid == NULL)
8614 continue;
8615 if (ssid->mode != WPAS_MODE_P2P_GO)
8616 continue;
8617 if (wpa_s->ap_iface == NULL)
8618 continue;
8619 break;
8620 }
8621 if (wpa_s == NULL) {
8622 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8623 return -1;
8624 }
8625
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008626 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008627 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008628 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008629 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8630 return -1;
8631 }
8632 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008633 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8634 wpa_s->p2pdev->p2p_oob_dev_pw,
8635 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8636 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008637 if (res)
8638 return res;
8639
8640 if (!tag) {
8641 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8642 return 0;
8643 }
8644
8645 if (!params->peer ||
8646 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8647 return 0;
8648
8649 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8650 " to join", MAC2STR(params->peer->p2p_device_addr));
8651
8652 wpa_s->global->p2p_invite_group = wpa_s;
8653 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008654 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008655 params->peer->p2p_device_addr,
8656 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008657 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008658
8659 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8660 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8661 ssid->ssid, ssid->ssid_len, ssid->frequency,
8662 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008663 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008664}
8665
8666
8667static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8668 struct p2p_nfc_params *params,
8669 int forced_freq)
8670{
8671 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8672 "connection handover");
8673 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8674 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008675 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008676 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8677 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008678}
8679
8680
8681static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8682 struct p2p_nfc_params *params,
8683 int forced_freq)
8684{
8685 int res;
8686
8687 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8688 "connection handover");
8689 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8690 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008691 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008692 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8693 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008694 if (res)
8695 return res;
8696
8697 res = wpas_p2p_listen(wpa_s, 60);
8698 if (res) {
8699 p2p_unauthorize(wpa_s->global->p2p,
8700 params->peer->p2p_device_addr);
8701 }
8702
8703 return res;
8704}
8705
8706
8707static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8708 const struct wpabuf *data,
8709 int sel, int tag, int forced_freq)
8710{
8711 const u8 *pos, *end;
8712 u16 len, id;
8713 struct p2p_nfc_params params;
8714 int res;
8715
8716 os_memset(&params, 0, sizeof(params));
8717 params.sel = sel;
8718
8719 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8720
8721 pos = wpabuf_head(data);
8722 end = pos + wpabuf_len(data);
8723
8724 if (end - pos < 2) {
8725 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8726 "attributes");
8727 return -1;
8728 }
8729 len = WPA_GET_BE16(pos);
8730 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008731 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008732 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8733 "attributes");
8734 return -1;
8735 }
8736 params.wsc_attr = pos;
8737 params.wsc_len = len;
8738 pos += len;
8739
8740 if (end - pos < 2) {
8741 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8742 "attributes");
8743 return -1;
8744 }
8745 len = WPA_GET_BE16(pos);
8746 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008747 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008748 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8749 "attributes");
8750 return -1;
8751 }
8752 params.p2p_attr = pos;
8753 params.p2p_len = len;
8754 pos += len;
8755
8756 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8757 params.wsc_attr, params.wsc_len);
8758 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8759 params.p2p_attr, params.p2p_len);
8760 if (pos < end) {
8761 wpa_hexdump(MSG_DEBUG,
8762 "P2P: Ignored extra data after P2P attributes",
8763 pos, end - pos);
8764 }
8765
8766 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8767 if (res)
8768 return res;
8769
8770 if (params.next_step == NO_ACTION)
8771 return 0;
8772
8773 if (params.next_step == BOTH_GO) {
8774 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8775 MAC2STR(params.peer->p2p_device_addr));
8776 return 0;
8777 }
8778
8779 if (params.next_step == PEER_CLIENT) {
8780 if (!is_zero_ether_addr(params.go_dev_addr)) {
8781 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8782 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8783 " ssid=\"%s\"",
8784 MAC2STR(params.peer->p2p_device_addr),
8785 params.go_freq,
8786 MAC2STR(params.go_dev_addr),
8787 wpa_ssid_txt(params.go_ssid,
8788 params.go_ssid_len));
8789 } else {
8790 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8791 "peer=" MACSTR " freq=%d",
8792 MAC2STR(params.peer->p2p_device_addr),
8793 params.go_freq);
8794 }
8795 return 0;
8796 }
8797
8798 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8799 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8800 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8801 return 0;
8802 }
8803
8804 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8805 wpa_s->p2p_oob_dev_pw = NULL;
8806
8807 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8808 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8809 "received");
8810 return -1;
8811 }
8812
8813 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8814 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8815 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8816 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8817 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8818 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8819 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8820
8821 if (tag) {
8822 if (id < 0x10) {
8823 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8824 "peer OOB Device Password Id %u", id);
8825 return -1;
8826 }
8827 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8828 "Device Password Id %u", id);
8829 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8830 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8831 params.oob_dev_pw_len -
8832 WPS_OOB_PUBKEY_HASH_LEN - 2);
8833 wpa_s->p2p_oob_dev_pw_id = id;
8834 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8835 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8836 params.oob_dev_pw_len -
8837 WPS_OOB_PUBKEY_HASH_LEN - 2);
8838 if (wpa_s->p2p_oob_dev_pw == NULL)
8839 return -1;
8840
8841 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8842 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8843 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8844 return -1;
8845 } else {
8846 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8847 "without Device Password");
8848 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8849 }
8850
8851 switch (params.next_step) {
8852 case NO_ACTION:
8853 case BOTH_GO:
8854 case PEER_CLIENT:
8855 /* already covered above */
8856 return 0;
8857 case JOIN_GROUP:
8858 return wpas_p2p_nfc_join_group(wpa_s, &params);
8859 case AUTH_JOIN:
8860 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8861 case INIT_GO_NEG:
8862 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8863 case RESP_GO_NEG:
8864 /* TODO: use own OOB Dev Pw */
8865 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8866 }
8867
8868 return -1;
8869}
8870
8871
8872int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8873 const struct wpabuf *data, int forced_freq)
8874{
8875 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8876 return -1;
8877
8878 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8879}
8880
8881
8882int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8883 const struct wpabuf *req,
8884 const struct wpabuf *sel, int forced_freq)
8885{
8886 struct wpabuf *tmp;
8887 int ret;
8888
8889 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8890 return -1;
8891
8892 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8893
8894 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8895 wpabuf_head(req), wpabuf_len(req));
8896 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8897 wpabuf_head(sel), wpabuf_len(sel));
8898 if (forced_freq)
8899 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8900 tmp = ndef_parse_p2p(init ? sel : req);
8901 if (tmp == NULL) {
8902 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8903 return -1;
8904 }
8905
8906 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8907 forced_freq);
8908 wpabuf_free(tmp);
8909
8910 return ret;
8911}
8912
8913
8914int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8915{
8916 const u8 *if_addr;
8917 int go_intent = wpa_s->conf->p2p_go_intent;
8918 struct wpa_supplicant *iface;
8919
8920 if (wpa_s->global->p2p == NULL)
8921 return -1;
8922
8923 if (!enabled) {
8924 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8925 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8926 {
8927 if (!iface->ap_iface)
8928 continue;
8929 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8930 }
8931 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8932 0, NULL);
8933 if (wpa_s->p2p_nfc_tag_enabled)
8934 wpas_p2p_remove_pending_group_interface(wpa_s);
8935 wpa_s->p2p_nfc_tag_enabled = 0;
8936 return 0;
8937 }
8938
8939 if (wpa_s->global->p2p_disabled)
8940 return -1;
8941
8942 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8943 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8944 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8945 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8946 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8947 "to allow static handover cases");
8948 return -1;
8949 }
8950
8951 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8952
8953 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8954 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8955 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8956 if (wpa_s->p2p_oob_dev_pw == NULL)
8957 return -1;
8958 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8959
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008960 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8961 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8962 /*
8963 * P2P Group Interface present and the command came on group
8964 * interface, so enable the token for the current interface.
8965 */
8966 wpa_s->create_p2p_iface = 0;
8967 } else {
8968 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8969 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008970
8971 if (wpa_s->create_p2p_iface) {
8972 enum wpa_driver_if_type iftype;
8973 /* Prepare to add a new interface for the group */
8974 iftype = WPA_IF_P2P_GROUP;
8975 if (go_intent == 15)
8976 iftype = WPA_IF_P2P_GO;
8977 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8978 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8979 "interface for the group");
8980 return -1;
8981 }
8982
8983 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008984 } else if (wpa_s->p2p_mgmt)
8985 if_addr = wpa_s->parent->own_addr;
8986 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008987 if_addr = wpa_s->own_addr;
8988
8989 wpa_s->p2p_nfc_tag_enabled = enabled;
8990
8991 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8992 struct hostapd_data *hapd;
8993 if (iface->ap_iface == NULL)
8994 continue;
8995 hapd = iface->ap_iface->bss[0];
8996 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8997 hapd->conf->wps_nfc_dh_pubkey =
8998 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8999 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9000 hapd->conf->wps_nfc_dh_privkey =
9001 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9002 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9003 hapd->conf->wps_nfc_dev_pw =
9004 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9005 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9006
9007 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9008 wpa_dbg(iface, MSG_DEBUG,
9009 "P2P: Failed to enable NFC Tag for GO");
9010 }
9011 }
9012 p2p_set_authorized_oob_dev_pw_id(
9013 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9014 if_addr);
9015
9016 return 0;
9017}
9018
9019#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009020
9021
9022static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9023 struct wpa_used_freq_data *freqs,
9024 unsigned int num)
9025{
9026 u8 curr_chan, cand, chan;
9027 unsigned int i;
9028
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009029 /*
9030 * If possible, optimize the Listen channel to be a channel that is
9031 * already used by one of the other interfaces.
9032 */
9033 if (!wpa_s->conf->p2p_optimize_listen_chan)
9034 return;
9035
9036 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9037 return;
9038
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009039 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9040 for (i = 0, cand = 0; i < num; i++) {
9041 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9042 if (curr_chan == chan) {
9043 cand = 0;
9044 break;
9045 }
9046
9047 if (chan == 1 || chan == 6 || chan == 11)
9048 cand = chan;
9049 }
9050
9051 if (cand) {
9052 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009053 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009054 cand);
9055 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9056 }
9057}
9058
9059
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009060static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009061{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009062 struct hostapd_config *conf;
9063 struct p2p_go_neg_results params;
9064 struct csa_settings csa_settings;
9065 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9066 int old_freq = current_ssid->frequency;
9067 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009068
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009069 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
9070 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
9071 return -1;
9072 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009073
9074 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009075 * TODO: This function may not always work correctly. For example,
9076 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009077 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009078 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009079 wpa_dbg(wpa_s, MSG_DEBUG,
9080 "P2P CSA: Failed to select new frequency for GO");
9081 return -1;
9082 }
9083
9084 if (current_ssid->frequency == params.freq) {
9085 wpa_dbg(wpa_s, MSG_DEBUG,
9086 "P2P CSA: Selected same frequency - not moving GO");
9087 return 0;
9088 }
9089
9090 conf = hostapd_config_defaults();
9091 if (!conf) {
9092 wpa_dbg(wpa_s, MSG_DEBUG,
9093 "P2P CSA: Failed to allocate default config");
9094 return -1;
9095 }
9096
9097 current_ssid->frequency = params.freq;
9098 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9099 wpa_dbg(wpa_s, MSG_DEBUG,
9100 "P2P CSA: Failed to create new GO config");
9101 ret = -1;
9102 goto out;
9103 }
9104
9105 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9106 wpa_dbg(wpa_s, MSG_DEBUG,
9107 "P2P CSA: CSA to a different band is not supported");
9108 ret = -1;
9109 goto out;
9110 }
9111
9112 os_memset(&csa_settings, 0, sizeof(csa_settings));
9113 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9114 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9115 csa_settings.freq_params.freq = params.freq;
9116 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9117 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9118 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9119
9120 if (conf->ieee80211ac) {
9121 int freq1 = 0, freq2 = 0;
9122 u8 chan, opclass;
9123
9124 if (ieee80211_freq_to_channel_ext(params.freq,
9125 conf->secondary_channel,
9126 conf->vht_oper_chwidth,
9127 &opclass, &chan) ==
9128 NUM_HOSTAPD_MODES) {
9129 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9130 ret = -1;
9131 goto out;
9132 }
9133
9134 if (conf->vht_oper_centr_freq_seg0_idx)
9135 freq1 = ieee80211_chan_to_freq(
9136 NULL, opclass,
9137 conf->vht_oper_centr_freq_seg0_idx);
9138
9139 if (conf->vht_oper_centr_freq_seg1_idx)
9140 freq2 = ieee80211_chan_to_freq(
9141 NULL, opclass,
9142 conf->vht_oper_centr_freq_seg1_idx);
9143
9144 if (freq1 < 0 || freq2 < 0) {
9145 wpa_dbg(wpa_s, MSG_DEBUG,
9146 "P2P CSA: Selected invalid VHT center freqs");
9147 ret = -1;
9148 goto out;
9149 }
9150
9151 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9152 csa_settings.freq_params.center_freq1 = freq1;
9153 csa_settings.freq_params.center_freq2 = freq2;
9154
9155 switch (conf->vht_oper_chwidth) {
9156 case VHT_CHANWIDTH_80MHZ:
9157 case VHT_CHANWIDTH_80P80MHZ:
9158 csa_settings.freq_params.bandwidth = 80;
9159 break;
9160 case VHT_CHANWIDTH_160MHZ:
9161 csa_settings.freq_params.bandwidth = 160;
9162 break;
9163 }
9164 }
9165
9166 ret = ap_switch_channel(wpa_s, &csa_settings);
9167out:
9168 current_ssid->frequency = old_freq;
9169 hostapd_config_free(conf);
9170 return ret;
9171}
9172
9173
9174static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9175{
9176 struct p2p_go_neg_results params;
9177 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9178
9179 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9180
9181 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9182 current_ssid->frequency);
9183
9184 /* Stop the AP functionality */
9185 /* TODO: Should do this in a way that does not indicated to possible
9186 * P2P Clients in the group that the group is terminated. */
9187 wpa_supplicant_ap_deinit(wpa_s);
9188
9189 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009190 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009191 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9192 wpas_p2p_group_delete(wpa_s,
9193 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9194 return;
9195 }
9196 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9197 params.freq);
9198
9199 if (params.freq &&
9200 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9201 wpa_printf(MSG_DEBUG,
9202 "P2P: Selected freq (%u MHz) is not valid for P2P",
9203 params.freq);
9204 wpas_p2p_group_delete(wpa_s,
9205 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9206 return;
9207 }
9208
9209 /* Update the frequency */
9210 current_ssid->frequency = params.freq;
9211 wpa_s->connect_without_scan = current_ssid;
9212 wpa_s->reassociate = 1;
9213 wpa_s->disconnected = 0;
9214 wpa_supplicant_req_scan(wpa_s, 0, 0);
9215}
9216
9217
9218static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9219{
9220 struct wpa_supplicant *wpa_s = eloop_ctx;
9221
9222 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009223 return;
9224
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009225 wpas_p2p_go_update_common_freqs(wpa_s);
9226
9227 /* Do not move GO in the middle of a CSA */
9228 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9229 wpa_printf(MSG_DEBUG,
9230 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009231 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009232 }
9233
9234 /*
9235 * First, try a channel switch flow. If it is not supported or fails,
9236 * take down the GO and bring it up again.
9237 */
9238 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9239 wpas_p2p_move_go_no_csa(wpa_s);
9240}
9241
9242
9243static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9244{
9245 struct wpa_supplicant *wpa_s = eloop_ctx;
9246 struct wpa_used_freq_data *freqs = NULL;
9247 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009248
9249 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9250 if (!freqs)
9251 return;
9252
9253 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9254
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009255 /* Previous attempt to move a GO was not possible -- try again. */
9256 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9257 WPAS_P2P_CHANNEL_UPDATE_ANY);
9258
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009259 os_free(freqs);
9260}
9261
9262
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009263/*
9264 * Consider moving a GO from its currently used frequency:
9265 * 1. It is possible that due to regulatory consideration the frequency
9266 * can no longer be used and there is a need to evacuate the GO.
9267 * 2. It is possible that due to MCC considerations, it would be preferable
9268 * to move the GO to a channel that is currently used by some other
9269 * station interface.
9270 *
9271 * In case a frequency that became invalid is once again valid, cancel a
9272 * previously initiated GO frequency change.
9273 */
9274static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9275 struct wpa_used_freq_data *freqs,
9276 unsigned int num)
9277{
9278 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9279 unsigned int timeout;
9280 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009281 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009282
9283 wpas_p2p_go_update_common_freqs(wpa_s);
9284
9285 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009286 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009287 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009288 for (i = 0, invalid_freq = 0; i < num; i++) {
9289 if (freqs[i].freq == freq) {
9290 flags = freqs[i].flags;
9291
9292 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009293 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9294 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009295 wpa_dbg(wpa_s, MSG_DEBUG,
9296 "P2P: Freq=%d MHz no longer valid for GO",
9297 freq);
9298 invalid_freq = 1;
9299 }
9300 } else if (freqs[i].flags == 0) {
9301 /* Freq is not used by any other station interface */
9302 continue;
9303 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009304 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009305 /* Freq is not valid for P2P use cases */
9306 continue;
9307 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9308 P2P_GO_FREQ_MOVE_SCM) {
9309 policy_move = 1;
9310 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9311 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9312 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9313 policy_move = 1;
9314 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9315 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9316 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9317 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9318 policy_move = 1;
9319 } else if ((wpa_s->drv_flags &
9320 WPA_DRIVER_FLAGS_AP_CSA) &&
9321 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9322 u8 chan;
9323
9324 /*
9325 * We do not support CSA between bands, so move
9326 * GO only within the same band.
9327 */
9328 if (wpa_s->ap_iface->current_mode->mode ==
9329 ieee80211_freq_to_chan(freqs[i].freq,
9330 &chan))
9331 policy_move = 1;
9332 }
9333 }
9334 }
9335
9336 wpa_dbg(wpa_s, MSG_DEBUG,
9337 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9338 invalid_freq, policy_move, flags);
9339
9340 /*
9341 * The channel is valid, or we are going to have a policy move, so
9342 * cancel timeout.
9343 */
9344 if (!invalid_freq || policy_move) {
9345 wpa_dbg(wpa_s, MSG_DEBUG,
9346 "P2P: Cancel a GO move from freq=%d MHz", freq);
9347 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9348
9349 if (wpas_p2p_in_progress(wpa_s)) {
9350 wpa_dbg(wpa_s, MSG_DEBUG,
9351 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9352 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9353 wpa_s, NULL);
9354 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9355 wpas_p2p_reconsider_moving_go,
9356 wpa_s, NULL);
9357 return;
9358 }
9359 }
9360
9361 if (!invalid_freq && (!policy_move || flags != 0)) {
9362 wpa_dbg(wpa_s, MSG_DEBUG,
9363 "P2P: Not initiating a GO frequency change");
9364 return;
9365 }
9366
9367 /*
9368 * Do not consider moving GO if it is in the middle of a CSA. When the
9369 * CSA is finished this flow should be retriggered.
9370 */
9371 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9372 wpa_dbg(wpa_s, MSG_DEBUG,
9373 "P2P: Not initiating a GO frequency change - CSA is in progress");
9374 return;
9375 }
9376
9377 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9378 timeout = P2P_GO_FREQ_CHANGE_TIME;
9379 else
9380 timeout = 0;
9381
9382 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9383 freq, timeout);
9384 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9385 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9386}
9387
9388
9389static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9390 struct wpa_used_freq_data *freqs,
9391 unsigned int num,
9392 enum wpas_p2p_channel_update_trig trig)
9393{
9394 struct wpa_supplicant *ifs;
9395
9396 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9397 NULL);
9398
9399 /*
9400 * Travers all the radio interfaces, and for each GO interface, check
9401 * if there is a need to move the GO from the frequency it is using,
9402 * or in case the frequency is valid again, cancel the evacuation flow.
9403 */
9404 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9405 radio_list) {
9406 if (ifs->current_ssid == NULL ||
9407 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9408 continue;
9409
9410 /*
9411 * The GO was just started or completed channel switch, no need
9412 * to move it.
9413 */
9414 if (wpa_s == ifs &&
9415 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9416 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9417 wpa_dbg(wpa_s, MSG_DEBUG,
9418 "P2P: GO move - schedule re-consideration");
9419 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9420 wpas_p2p_reconsider_moving_go,
9421 wpa_s, NULL);
9422 continue;
9423 }
9424
9425 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9426 }
9427}
9428
9429
9430void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9431{
9432 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9433 return;
9434
9435 wpas_p2p_update_channel_list(wpa_s,
9436 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9437}
9438
9439
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009440void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9441{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009442 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9443 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9444 "the management interface is being removed");
9445 wpas_p2p_deinit_global(wpa_s->global);
9446 }
9447}
9448
9449
9450void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9451{
9452 if (wpa_s->ap_iface->bss)
9453 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9454 wpas_p2p_group_deinit(wpa_s);
9455}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009456
9457
9458int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9459 unsigned int period, unsigned int interval,
9460 unsigned int count)
9461{
9462 struct p2p_data *p2p = wpa_s->global->p2p;
9463 u8 *device_types;
9464 size_t dev_types_len;
9465 struct wpabuf *buf;
9466 int ret;
9467
9468 if (wpa_s->p2p_lo_started) {
9469 wpa_dbg(wpa_s, MSG_DEBUG,
9470 "P2P Listen offload is already started");
9471 return 0;
9472 }
9473
9474 if (wpa_s->global->p2p == NULL ||
9475 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9476 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9477 return -1;
9478 }
9479
9480 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9481 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9482 freq);
9483 return -1;
9484 }
9485
9486 /* Get device type */
9487 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9488 WPS_DEV_TYPE_LEN;
9489 device_types = os_malloc(dev_types_len);
9490 if (!device_types)
9491 return -1;
9492 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9493 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9494 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9495
9496 /* Get Probe Response IE(s) */
9497 buf = p2p_build_probe_resp_template(p2p, freq);
9498 if (!buf) {
9499 os_free(device_types);
9500 return -1;
9501 }
9502
9503 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9504 device_types, dev_types_len,
9505 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9506 if (ret < 0)
9507 wpa_dbg(wpa_s, MSG_DEBUG,
9508 "P2P: Failed to start P2P listen offload");
9509
9510 os_free(device_types);
9511 wpabuf_free(buf);
9512
9513 if (ret == 0) {
9514 wpa_s->p2p_lo_started = 1;
9515
9516 /* Stop current P2P listen if any */
9517 wpas_stop_listen(wpa_s);
9518 }
9519
9520 return ret;
9521}
9522
9523
9524int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9525{
9526 int ret;
9527
9528 if (!wpa_s->p2p_lo_started)
9529 return 0;
9530
9531 ret = wpa_drv_p2p_lo_stop(wpa_s);
9532 if (ret < 0)
9533 wpa_dbg(wpa_s, MSG_DEBUG,
9534 "P2P: Failed to stop P2P listen offload");
9535
9536 wpa_s->p2p_lo_started = 0;
9537 return ret;
9538}