blob: b9b0beaaf67bf9331126bf89353d32589f4247c5 [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
2078 if (force_ifname[0]) {
2079 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2080 force_ifname);
2081 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2082 sizeof(wpa_s->pending_interface_name));
2083 } else
2084 os_strlcpy(wpa_s->pending_interface_name, ifname,
2085 sizeof(wpa_s->pending_interface_name));
2086 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2087 MACSTR, wpa_s->pending_interface_name,
2088 MAC2STR(wpa_s->pending_interface_addr));
2089
2090 return 0;
2091}
2092
2093
2094static void wpas_p2p_remove_pending_group_interface(
2095 struct wpa_supplicant *wpa_s)
2096{
2097 if (!wpa_s->pending_interface_name[0] ||
2098 is_zero_ether_addr(wpa_s->pending_interface_addr))
2099 return; /* No pending virtual interface */
2100
2101 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2102 wpa_s->pending_interface_name);
2103 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2104 wpa_s->pending_interface_name);
2105 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2106 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002107 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108}
2109
2110
2111static struct wpa_supplicant *
2112wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2113{
2114 struct wpa_interface iface;
2115 struct wpa_supplicant *group_wpa_s;
2116
2117 if (!wpa_s->pending_interface_name[0]) {
2118 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2119 if (!wpas_p2p_create_iface(wpa_s))
2120 return NULL;
2121 /*
2122 * Something has forced us to remove the pending interface; try
2123 * to create a new one and hope for the best that we will get
2124 * the same local address.
2125 */
2126 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2127 WPA_IF_P2P_CLIENT) < 0)
2128 return NULL;
2129 }
2130
2131 os_memset(&iface, 0, sizeof(iface));
2132 iface.ifname = wpa_s->pending_interface_name;
2133 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002134 if (wpa_s->conf->ctrl_interface == NULL &&
2135 wpa_s->parent != wpa_s &&
2136 wpa_s->p2p_mgmt &&
2137 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2138 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2139 else
2140 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002142 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 if (group_wpa_s == NULL) {
2144 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2145 "wpa_supplicant interface");
2146 return NULL;
2147 }
2148 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2150 P2P_GROUP_INTERFACE_CLIENT;
2151 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002152 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153
2154 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2155
2156 return group_wpa_s;
2157}
2158
2159
2160static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2161 void *timeout_ctx)
2162{
2163 struct wpa_supplicant *wpa_s = eloop_ctx;
2164 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002165 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002166}
2167
2168
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002169static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2170 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002171{
2172 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002173 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002174 if (wpa_s->global->p2p)
2175 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002176 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177}
2178
2179
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002180static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2181{
2182 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2183 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002184 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002185 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002186 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002187 wpa_s->global->p2p_fail_on_wps_complete = 0;
2188}
2189
2190
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002191void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2192{
2193 if (wpa_s->global->p2p_group_formation != wpa_s)
2194 return;
2195 /* Speed up group formation timeout since this cannot succeed */
2196 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002197 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002198 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002199 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002200}
2201
2202
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002203static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204{
2205 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002206 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207
2208 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2209 wpa_drv_cancel_remain_on_channel(wpa_s);
2210 wpa_s->off_channel_freq = 0;
2211 wpa_s->roc_waiting_drv_freq = 0;
2212 }
2213
2214 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002215 wpa_msg_global(wpa_s, MSG_INFO,
2216 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2217 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002218 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 wpas_p2p_remove_pending_group_interface(wpa_s);
2220 return;
2221 }
2222
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002223 if (!res->role_go) {
2224 /* Inform driver of the operating channel of GO. */
2225 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2226 }
2227
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002228 if (wpa_s->p2p_go_ht40)
2229 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002230 if (wpa_s->p2p_go_vht)
2231 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002232 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2233 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002234
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002235 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2236 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2237 " wps_method=%s",
2238 res->role_go ? "GO" : "client", res->freq, res->ht40,
2239 MAC2STR(res->peer_device_addr),
2240 MAC2STR(res->peer_interface_addr),
2241 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243
Dmitry Shmidt04949592012-07-19 12:16:46 -07002244 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2245 struct wpa_ssid *ssid;
2246 ssid = wpa_config_get_network(wpa_s->conf,
2247 wpa_s->p2p_persistent_id);
2248 if (ssid && ssid->disabled == 2 &&
2249 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2250 size_t len = os_strlen(ssid->passphrase);
2251 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2252 "on requested persistent group");
2253 os_memcpy(res->passphrase, ssid->passphrase, len);
2254 res->passphrase[len] = '\0';
2255 }
2256 }
2257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002259 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2261 if (group_wpa_s == NULL) {
2262 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002263 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2264 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002265 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 return;
2267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2269 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002271 group_wpa_s = wpa_s->parent;
2272 wpa_s->global->p2p_group_formation = group_wpa_s;
2273 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002274 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002275 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002277 group_wpa_s->p2p_in_provisioning = 1;
2278 group_wpa_s->p2pdev = wpa_s;
2279 if (group_wpa_s != wpa_s) {
2280 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2281 sizeof(group_wpa_s->p2p_pin));
2282 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2283 }
2284 if (res->role_go) {
2285 wpas_start_wps_go(group_wpa_s, res, 1);
2286 } else {
2287 os_get_reltime(&group_wpa_s->scan_min_time);
2288 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 }
2290
2291 wpa_s->p2p_long_listen = 0;
2292 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2293
2294 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2295 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2296 (res->peer_config_timeout % 100) * 10000,
2297 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2298}
2299
2300
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002301static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2302 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303{
2304 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002305 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002306 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2307 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002309 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310}
2311
2312
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002313static void wpas_dev_found(void *ctx, const u8 *addr,
2314 const struct p2p_peer_info *info,
2315 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002317 u8 *wfd_dev_info = NULL;
2318 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002319#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002320 struct wpa_supplicant *wpa_s = ctx;
2321 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002322 char *wfd_dev_info_hex = NULL;
2323
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002324#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002325 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2326 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002327 if (wfd_dev_info_hex) {
2328 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2329 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2330 // Only used for notification, so not handling error.
2331 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2332 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002333#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002334
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002335 if (info->p2ps_instance) {
2336 char str[256];
2337 const u8 *buf = wpabuf_head(info->p2ps_instance);
2338 size_t len = wpabuf_len(info->p2ps_instance);
2339
2340 while (len) {
2341 u32 id;
2342 u16 methods;
2343 u8 str_len;
2344
2345 if (len < 4 + 2 + 1)
2346 break;
2347 id = WPA_GET_LE32(buf);
2348 buf += sizeof(u32);
2349 methods = WPA_GET_BE16(buf);
2350 buf += sizeof(u16);
2351 str_len = *buf++;
2352 if (str_len > len - 4 - 2 - 1)
2353 break;
2354 os_memcpy(str, buf, str_len);
2355 str[str_len] = '\0';
2356 buf += str_len;
2357 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2358
2359 wpa_msg_global(wpa_s, MSG_INFO,
2360 P2P_EVENT_DEVICE_FOUND MACSTR
2361 " p2p_dev_addr=" MACSTR
2362 " pri_dev_type=%s name='%s'"
2363 " config_methods=0x%x"
2364 " dev_capab=0x%x"
2365 " group_capab=0x%x"
2366 " adv_id=%x asp_svc=%s%s",
2367 MAC2STR(addr),
2368 MAC2STR(info->p2p_device_addr),
2369 wps_dev_type_bin2str(
2370 info->pri_dev_type,
2371 devtype, sizeof(devtype)),
2372 info->device_name, methods,
2373 info->dev_capab, info->group_capab,
2374 id, str,
2375 info->vendor_elems ?
2376 " vendor_elems=1" : "");
2377 }
2378 goto done;
2379 }
2380
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002381 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2382 " p2p_dev_addr=" MACSTR
2383 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002384 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002385 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2386 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2387 sizeof(devtype)),
2388 info->device_name, info->config_methods,
2389 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002390 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002391 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002392 info->vendor_elems ? " vendor_elems=1" : "",
2393 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002395done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002396 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002397#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002398
Roshan Piusfd2fd662017-01-23 13:41:57 -08002399 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2400 wfd_dev_info_len, new_device);
2401 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402}
2403
2404
2405static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2406{
2407 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002408
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002409 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2410 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2413}
2414
2415
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002416static void wpas_find_stopped(void *ctx)
2417{
2418 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002419
2420 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2421 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2422
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002423 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002424 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002425}
2426
2427
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002428struct wpas_p2p_listen_work {
2429 unsigned int freq;
2430 unsigned int duration;
2431 struct wpabuf *probe_resp_ie;
2432};
2433
2434
2435static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2436{
2437 if (lwork == NULL)
2438 return;
2439 wpabuf_free(lwork->probe_resp_ie);
2440 os_free(lwork);
2441}
2442
2443
2444static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2445{
2446 struct wpas_p2p_listen_work *lwork;
2447
2448 if (!wpa_s->p2p_listen_work)
2449 return;
2450
2451 lwork = wpa_s->p2p_listen_work->ctx;
2452 wpas_p2p_listen_work_free(lwork);
2453 radio_work_done(wpa_s->p2p_listen_work);
2454 wpa_s->p2p_listen_work = NULL;
2455}
2456
2457
2458static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2459{
2460 struct wpa_supplicant *wpa_s = work->wpa_s;
2461 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002462 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002463
2464 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002465 if (work->started) {
2466 wpa_s->p2p_listen_work = NULL;
2467 wpas_stop_listen(wpa_s);
2468 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002469 wpas_p2p_listen_work_free(lwork);
2470 return;
2471 }
2472
2473 wpa_s->p2p_listen_work = work;
2474
2475 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2476
2477 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2478 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2479 "report received Probe Request frames");
2480 wpas_p2p_listen_work_done(wpa_s);
2481 return;
2482 }
2483
2484 wpa_s->pending_listen_freq = lwork->freq;
2485 wpa_s->pending_listen_duration = lwork->duration;
2486
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002487 duration = lwork->duration;
2488#ifdef CONFIG_TESTING_OPTIONS
2489 if (wpa_s->extra_roc_dur) {
2490 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2491 duration, duration + wpa_s->extra_roc_dur);
2492 duration += wpa_s->extra_roc_dur;
2493 }
2494#endif /* CONFIG_TESTING_OPTIONS */
2495
2496 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002497 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2498 "to remain on channel (%u MHz) for Listen "
2499 "state", lwork->freq);
2500 wpas_p2p_listen_work_done(wpa_s);
2501 wpa_s->pending_listen_freq = 0;
2502 return;
2503 }
2504 wpa_s->off_channel_freq = 0;
2505 wpa_s->roc_waiting_drv_freq = lwork->freq;
2506}
2507
2508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509static int wpas_start_listen(void *ctx, unsigned int freq,
2510 unsigned int duration,
2511 const struct wpabuf *probe_resp_ie)
2512{
2513 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002514 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002516 if (wpa_s->p2p_listen_work) {
2517 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 return -1;
2519 }
2520
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002521 lwork = os_zalloc(sizeof(*lwork));
2522 if (lwork == NULL)
2523 return -1;
2524 lwork->freq = freq;
2525 lwork->duration = duration;
2526 if (probe_resp_ie) {
2527 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2528 if (lwork->probe_resp_ie == NULL) {
2529 wpas_p2p_listen_work_free(lwork);
2530 return -1;
2531 }
2532 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002534 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2535 lwork) < 0) {
2536 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 return -1;
2538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539
2540 return 0;
2541}
2542
2543
2544static void wpas_stop_listen(void *ctx)
2545{
2546 struct wpa_supplicant *wpa_s = ctx;
2547 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2548 wpa_drv_cancel_remain_on_channel(wpa_s);
2549 wpa_s->off_channel_freq = 0;
2550 wpa_s->roc_waiting_drv_freq = 0;
2551 }
2552 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002553
2554 /*
2555 * Don't cancel Probe Request RX reporting for a connected P2P Client
2556 * handling Probe Request frames.
2557 */
2558 if (!wpa_s->p2p_cli_probe)
2559 wpa_drv_probe_req_report(wpa_s, 0);
2560
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002561 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562}
2563
2564
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002565static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2566 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002567{
2568 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002569 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2570 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571}
2572
2573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2575 const u8 *peer, const char *params,
2576 unsigned int generated_pin)
2577{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002578 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2579 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580}
2581
2582
2583static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2584 const u8 *peer, const char *params)
2585{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002586 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2587 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588}
2589
2590
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002591static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2592 const u8 *dev_addr, const u8 *pri_dev_type,
2593 const char *dev_name, u16 supp_config_methods,
2594 u8 dev_capab, u8 group_capab, const u8 *group_id,
2595 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596{
2597 struct wpa_supplicant *wpa_s = ctx;
2598 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002599 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 u8 empty_dev_type[8];
2601 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002602 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002603 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002604
2605 if (group_id) {
2606 for (group = wpa_s->global->ifaces; group; group = group->next)
2607 {
2608 struct wpa_ssid *s = group->current_ssid;
2609 if (s != NULL &&
2610 s->mode == WPAS_MODE_P2P_GO &&
2611 group_id_len - ETH_ALEN == s->ssid_len &&
2612 os_memcmp(group_id + ETH_ALEN, s->ssid,
2613 s->ssid_len) == 0)
2614 break;
2615 }
2616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002617
2618 if (pri_dev_type == NULL) {
2619 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2620 pri_dev_type = empty_dev_type;
2621 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002622 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2623 " pri_dev_type=%s name='%s' config_methods=0x%x "
2624 "dev_capab=0x%x group_capab=0x%x%s%s",
2625 MAC2STR(dev_addr),
2626 wps_dev_type_bin2str(pri_dev_type, devtype,
2627 sizeof(devtype)),
2628 dev_name, supp_config_methods, dev_capab, group_capab,
2629 group ? " group=" : "",
2630 group ? group->ifname : "");
2631 if (os_snprintf_error(sizeof(params), res))
2632 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002636 if (wps_generate_pin(&generated_pin) < 0) {
2637 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2638 wpas_notify_p2p_provision_discovery(
2639 wpa_s, peer, 0 /* response */,
2640 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2641 return;
2642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 wpas_prov_disc_local_display(wpa_s, peer, params,
2644 generated_pin);
2645 } else if (config_methods & WPS_CONFIG_KEYPAD)
2646 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2647 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002648 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2649 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002650
2651 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2652 P2P_PROV_DISC_SUCCESS,
2653 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654}
2655
2656
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002657static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658{
2659 struct wpa_supplicant *wpa_s = ctx;
2660 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002661 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002663 if (wpa_s->pending_pd_before_join &&
2664 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2665 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2666 wpa_s->pending_pd_before_join = 0;
2667 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2668 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002669 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002670 return;
2671 }
2672
Dmitry Shmidt04949592012-07-19 12:16:46 -07002673 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002674 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2675 int res;
2676
2677 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2678 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2679 if (os_snprintf_error(sizeof(params), res))
2680 params[sizeof(params) - 1] = '\0';
2681 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002682 params[0] = '\0';
2683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002685 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002687 if (wps_generate_pin(&generated_pin) < 0) {
2688 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2689 wpas_notify_p2p_provision_discovery(
2690 wpa_s, peer, 0 /* response */,
2691 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2692 return;
2693 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002694 wpas_prov_disc_local_display(wpa_s, peer, params,
2695 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002697 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2698 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699
Jouni Malinen75ecf522011-06-27 15:19:46 -07002700 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2701 P2P_PROV_DISC_SUCCESS,
2702 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002703}
2704
2705
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002706static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002707 enum p2p_prov_disc_status status,
2708 u32 adv_id, const u8 *adv_mac,
2709 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002710{
2711 struct wpa_supplicant *wpa_s = ctx;
2712
Dmitry Shmidt04949592012-07-19 12:16:46 -07002713 if (wpa_s->p2p_fallback_to_go_neg) {
2714 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2715 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002716 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002717 P2P_EVENT_FALLBACK_TO_GO_NEG
2718 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002719 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2720 return;
2721 }
2722
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002723 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002724 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002725 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2726 "join-existing-group operation (no ACK for PD "
2727 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002728 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002729 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002730 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002731
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002732 if (adv_id && adv_mac && deferred_session_resp) {
2733 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2734 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2735 " deferred_session_resp='%s'",
2736 MAC2STR(peer), status, adv_id,
2737 deferred_session_resp);
2738 } else if (adv_id && adv_mac) {
2739 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2740 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2741 MAC2STR(peer), status, adv_id);
2742 } else {
2743 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2744 " p2p_dev_addr=" MACSTR " status=%d",
2745 MAC2STR(peer), status);
2746 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002747
Jouni Malinen75ecf522011-06-27 15:19:46 -07002748 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2749 status, 0, 0);
2750}
2751
2752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002753static int freq_included(struct wpa_supplicant *wpa_s,
2754 const struct p2p_channels *channels,
2755 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002756{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002757 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2758 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2759 return 1;
2760 return 0;
2761}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002762
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002763
2764static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2765{
2766 unsigned int num = P2P_MAX_CHANNELS;
2767 int *common_freqs;
2768 int ret;
2769
2770 p2p_go_dump_common_freqs(wpa_s);
2771 common_freqs = os_calloc(num, sizeof(int));
2772 if (!common_freqs)
2773 return;
2774
2775 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2776 if (ret < 0) {
2777 wpa_dbg(wpa_s, MSG_DEBUG,
2778 "P2P: Failed to get group common freqs");
2779 os_free(common_freqs);
2780 return;
2781 }
2782
2783 os_free(wpa_s->p2p_group_common_freqs);
2784 wpa_s->p2p_group_common_freqs = common_freqs;
2785 wpa_s->p2p_group_common_freqs_num = num;
2786 p2p_go_dump_common_freqs(wpa_s);
2787}
2788
2789
2790/*
2791 * Check if the given frequency is one of the possible operating frequencies
2792 * set after the completion of the GO Negotiation.
2793 */
2794static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2795{
2796 unsigned int i;
2797
2798 p2p_go_dump_common_freqs(wpa_s);
2799
2800 /* assume no restrictions */
2801 if (!wpa_s->p2p_group_common_freqs_num)
2802 return 1;
2803
2804 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2805 if (wpa_s->p2p_group_common_freqs[i] == freq)
2806 return 1;
2807 }
2808 return 0;
2809}
2810
2811
2812static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2813 struct sta_info *sta, void *ctx)
2814{
2815 int *ecsa_support = ctx;
2816
2817 *ecsa_support &= sta->ecsa_supported;
2818
2819 return 0;
2820}
2821
2822
2823/* Check if all the peers support eCSA */
2824static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2825{
2826 int ecsa_support = 1;
2827
2828 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2829 &ecsa_support);
2830
2831 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002832}
2833
2834
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002835/**
2836 * Pick the best frequency to use from all the currently used frequencies.
2837 */
2838static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2839 struct wpa_used_freq_data *freqs,
2840 unsigned int num)
2841{
2842 unsigned int i, c;
2843
2844 /* find a candidate freq that is supported by P2P */
2845 for (c = 0; c < num; c++)
2846 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2847 break;
2848
2849 if (c == num)
2850 return 0;
2851
2852 /* once we have a candidate, try to find a 'better' one */
2853 for (i = c + 1; i < num; i++) {
2854 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2855 continue;
2856
2857 /*
2858 * 1. Infrastructure station interfaces have higher preference.
2859 * 2. P2P Clients have higher preference.
2860 * 3. All others.
2861 */
2862 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2863 c = i;
2864 break;
2865 }
2866
2867 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2868 c = i;
2869 }
2870 return freqs[c].freq;
2871}
2872
2873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2875 const u8 *go_dev_addr, const u8 *ssid,
2876 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002877 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002878 const struct p2p_channels *channels,
2879 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880{
2881 struct wpa_supplicant *wpa_s = ctx;
2882 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002883 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002885 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886
2887 if (!persistent_group) {
2888 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002889 " to join an active group (SSID: %s)",
2890 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2892 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2893 == 0 ||
2894 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2895 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2896 "authorized invitation");
2897 goto accept_inv;
2898 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002899
2900#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002901 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2902 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002903 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002904 wpa_s->p2p_wps_method = WPS_NFC;
2905 wpa_s->pending_join_wps_method = WPS_NFC;
2906 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002907 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002908 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002909 bssid, ETH_ALEN);
2910 goto accept_inv;
2911 }
2912#endif /* CONFIG_WPS_NFC */
2913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 /*
2915 * Do not accept the invitation automatically; notify user and
2916 * request approval.
2917 */
2918 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2919 }
2920
2921 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2922 if (grp) {
2923 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2924 "running persistent group");
2925 if (*go)
2926 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2927 goto accept_inv;
2928 }
2929
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002930 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2931 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2932 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2933 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002934 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002935 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002936 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2937
2938 for (s = wpa_s->conf->ssid; s; s = s->next) {
2939 if (s->disabled == 2 &&
2940 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2941 s->ssid_len == ssid_len &&
2942 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2943 break;
2944 }
2945
2946 if (!s) {
2947 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2948 " requested reinvocation of an unknown group",
2949 MAC2STR(sa));
2950 return P2P_SC_FAIL_UNKNOWN_GROUP;
2951 }
2952
2953 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2954 *go = 1;
2955 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2956 wpa_printf(MSG_DEBUG, "P2P: The only available "
2957 "interface is already in use - reject "
2958 "invitation");
2959 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2960 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002961 if (wpa_s->p2p_mgmt)
2962 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2963 ETH_ALEN);
2964 else
2965 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 } else if (s->mode == WPAS_MODE_P2P_GO) {
2967 *go = 1;
2968 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2969 {
2970 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2971 "interface address for the group");
2972 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2973 }
2974 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2975 ETH_ALEN);
2976 }
2977
2978accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002979 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2980
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002981 best_freq = 0;
2982 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2983 sizeof(struct wpa_used_freq_data));
2984 if (freqs) {
2985 int num_channels = wpa_s->num_multichan_concurrent;
2986 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2987 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2988 os_free(freqs);
2989 }
2990
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002991 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002992 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002993 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002994 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002995
2996 if (wpa_s->num_multichan_concurrent < 2 ||
2997 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2998 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 -07002999 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 }
3002
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003003 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3004 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003005 if (*go == 0) {
3006 /* We are the client */
3007 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3008 "running a GO but we are capable of MCC, "
3009 "figure out the best channel to use");
3010 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003011 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003012 /* We are the GO, and *force_freq is not in the
3013 * intersection */
3014 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3015 "in intersection but we are capable of MCC, "
3016 "figure out the best channel to use",
3017 *force_freq);
3018 *force_freq = 0;
3019 }
3020 }
3021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 return P2P_SC_SUCCESS;
3023}
3024
3025
3026static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3027 const u8 *ssid, size_t ssid_len,
3028 const u8 *go_dev_addr, u8 status,
3029 int op_freq)
3030{
3031 struct wpa_supplicant *wpa_s = ctx;
3032 struct wpa_ssid *s;
3033
3034 for (s = wpa_s->conf->ssid; s; s = s->next) {
3035 if (s->disabled == 2 &&
3036 s->ssid_len == ssid_len &&
3037 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3038 break;
3039 }
3040
3041 if (status == P2P_SC_SUCCESS) {
3042 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003043 " was accepted; op_freq=%d MHz, SSID=%s",
3044 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003046 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003047 if (go) {
3048 wpa_msg_global(wpa_s, MSG_INFO,
3049 P2P_EVENT_INVITATION_ACCEPTED
3050 "sa=" MACSTR
3051 " persistent=%d freq=%d",
3052 MAC2STR(sa), s->id, op_freq);
3053 } else {
3054 wpa_msg_global(wpa_s, MSG_INFO,
3055 P2P_EVENT_INVITATION_ACCEPTED
3056 "sa=" MACSTR
3057 " persistent=%d",
3058 MAC2STR(sa), s->id);
3059 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003060 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003061 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003062 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3063 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003065 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003066 wpa_msg_global(wpa_s, MSG_INFO,
3067 P2P_EVENT_INVITATION_ACCEPTED
3068 "sa=" MACSTR " go_dev_addr=" MACSTR
3069 " bssid=" MACSTR " unknown-network",
3070 MAC2STR(sa), MAC2STR(go_dev_addr),
3071 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003073 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003074 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 }
3076 return;
3077 }
3078
3079 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3080 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3081 " was rejected (status %u)", MAC2STR(sa), status);
3082 return;
3083 }
3084
3085 if (!s) {
3086 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003087 wpa_msg_global(wpa_s, MSG_INFO,
3088 P2P_EVENT_INVITATION_RECEIVED
3089 "sa=" MACSTR " go_dev_addr=" MACSTR
3090 " bssid=" MACSTR " unknown-network",
3091 MAC2STR(sa), MAC2STR(go_dev_addr),
3092 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003094 wpa_msg_global(wpa_s, MSG_INFO,
3095 P2P_EVENT_INVITATION_RECEIVED
3096 "sa=" MACSTR " go_dev_addr=" MACSTR
3097 " unknown-network",
3098 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003100 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3101 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 return;
3103 }
3104
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003105 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003106 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3107 "sa=" MACSTR " persistent=%d freq=%d",
3108 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003109 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003110 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3111 "sa=" MACSTR " persistent=%d",
3112 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003113 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003114 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3115 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116}
3117
3118
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003119static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3120 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003121 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003122{
3123 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003124 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003125
3126 if (ssid == NULL)
3127 return;
3128
3129 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003130 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003131 ETH_ALEN) == 0)
3132 break;
3133 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003134 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003135 if (ssid->mode != WPAS_MODE_P2P_GO &&
3136 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3137 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3138 "due to invitation result", ssid->id);
3139 wpas_notify_network_removed(wpa_s, ssid);
3140 wpa_config_remove_network(wpa_s->conf, ssid->id);
3141 return;
3142 }
3143 return; /* Peer not found in client list */
3144 }
3145
3146 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003147 "group %d client list%s",
3148 MAC2STR(peer), ssid->id,
3149 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003150 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3151 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3152 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003153 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003154 if (p2p_wpa_s->conf->update_config &&
3155 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003156 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003157}
3158
3159
3160static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3161 const u8 *peer)
3162{
3163 struct wpa_ssid *ssid;
3164
3165 wpa_s = wpa_s->global->p2p_invite_group;
3166 if (wpa_s == NULL)
3167 return; /* No known invitation group */
3168 ssid = wpa_s->current_ssid;
3169 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3170 !ssid->p2p_persistent_group)
3171 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003172 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003173 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003174 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003175}
3176
3177
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003178static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003179 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003180 const u8 *peer, int neg_freq,
3181 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003182{
3183 struct wpa_supplicant *wpa_s = ctx;
3184 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003185 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186
3187 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003188 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3189 "status=%d " MACSTR,
3190 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003192 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3193 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003194 }
3195 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3196
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003197 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3198 status, MAC2STR(peer));
3199 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003200 struct wpa_supplicant *group_if =
3201 wpa_s->global->p2p_invite_group;
3202
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003203 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3204 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003205
3206 /*
3207 * Invitation to an active group. If this is successful and we
3208 * are the GO, set the client wait to postpone some concurrent
3209 * operations and to allow provisioning and connection to happen
3210 * more quickly.
3211 */
3212 if (status == P2P_SC_SUCCESS &&
3213 group_if && group_if->current_ssid &&
3214 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3215 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3216#ifdef CONFIG_TESTING_OPTIONS
3217 if (group_if->p2p_go_csa_on_inv) {
3218 wpa_printf(MSG_DEBUG,
3219 "Testing: force P2P GO CSA after invitation");
3220 eloop_cancel_timeout(
3221 wpas_p2p_reconsider_moving_go,
3222 wpa_s, NULL);
3223 eloop_register_timeout(
3224 0, 50000,
3225 wpas_p2p_reconsider_moving_go,
3226 wpa_s, NULL);
3227 }
3228#endif /* CONFIG_TESTING_OPTIONS */
3229 }
3230 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003231 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003233 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3234 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3235 "invitation exchange to indicate readiness for "
3236 "re-invocation");
3237 }
3238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003240 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3241 ssid = wpa_config_get_network(
3242 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003243 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 wpas_p2p_remove_pending_group_interface(wpa_s);
3246 return;
3247 }
3248
3249 ssid = wpa_config_get_network(wpa_s->conf,
3250 wpa_s->pending_invite_ssid_id);
3251 if (ssid == NULL) {
3252 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3253 "data matching with invitation");
3254 return;
3255 }
3256
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003257 /*
3258 * The peer could have missed our ctrl::ack frame for Invitation
3259 * Response and continue retransmitting the frame. To reduce the
3260 * likelihood of the peer not getting successful TX status for the
3261 * Invitation Response frame, wait a short time here before starting
3262 * the persistent group so that we will remain on the current channel to
3263 * acknowledge any possible retransmission from the peer.
3264 */
3265 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3266 "starting persistent group");
3267 os_sleep(0, 50000);
3268
Dmitry Shmidt15907092014-03-25 10:42:57 -07003269 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003270 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003271 freq = neg_freq;
3272 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003273 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003274 freq = peer_oper_freq;
3275 else
3276 freq = 0;
3277
3278 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3279 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003281 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003282 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003283 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003284 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003285 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003286 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003287 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003288 ssid->mode == WPAS_MODE_P2P_GO ?
3289 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003290 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291}
3292
3293
Dmitry Shmidt04949592012-07-19 12:16:46 -07003294static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3295 unsigned int freq)
3296{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003297 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3298 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003299 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003300}
3301
3302
3303static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3304{
3305 reg->channel[reg->channels] = chan;
3306 reg->channels++;
3307}
3308
3309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003311 struct p2p_channels *chan,
3312 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313{
3314 int i, cla = 0;
3315
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003316 wpa_s->global->p2p_24ghz_social_channels = 1;
3317
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003318 os_memset(cli_chan, 0, sizeof(*cli_chan));
3319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3321 "band");
3322
3323 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3324 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003325 chan->reg_class[cla].channels = 0;
3326 for (i = 0; i < 11; i++) {
3327 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3328 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3329 }
3330 if (chan->reg_class[cla].channels)
3331 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332
3333 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3334 "band");
3335
3336 /* Operating class 115 - 5 GHz, channels 36-48 */
3337 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003338 chan->reg_class[cla].channels = 0;
3339 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3340 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3341 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3342 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3343 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3344 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3345 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3346 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3347 if (chan->reg_class[cla].channels)
3348 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349
3350 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3351 "band");
3352
3353 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3354 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003355 chan->reg_class[cla].channels = 0;
3356 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3357 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3358 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3359 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3360 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3361 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3362 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3363 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3364 if (chan->reg_class[cla].channels)
3365 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366
3367 chan->reg_classes = cla;
3368 return 0;
3369}
3370
3371
Dmitry Shmidt04949592012-07-19 12:16:46 -07003372static int has_channel(struct wpa_global *global,
3373 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374{
3375 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003376 unsigned int freq;
3377
3378 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3379 chan * 5;
3380 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003381 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003382
3383 for (i = 0; i < mode->num_channels; i++) {
3384 if (mode->channels[i].chan == chan) {
3385 if (flags)
3386 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003387 if (mode->channels[i].flag &
3388 (HOSTAPD_CHAN_DISABLED |
3389 HOSTAPD_CHAN_RADAR))
3390 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003391 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3392 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003393 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 }
3395 }
3396
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003397 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398}
3399
3400
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003401static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3402 struct hostapd_hw_modes *mode,
3403 u8 channel)
3404{
3405 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003406 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003407
3408 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3409 return 0;
3410
3411 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3412 /*
3413 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3414 * so the center channel is 6 channels away from the start/end.
3415 */
3416 if (channel >= center_channels[i] - 6 &&
3417 channel <= center_channels[i] + 6)
3418 return center_channels[i];
3419
3420 return 0;
3421}
3422
3423
3424static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3425 struct hostapd_hw_modes *mode,
3426 u8 channel, u8 bw)
3427{
3428 u8 center_chan;
3429 int i, flags;
3430 enum chan_allowed res, ret = ALLOWED;
3431
3432 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3433 if (!center_chan)
3434 return NOT_ALLOWED;
3435 if (center_chan >= 58 && center_chan <= 138)
3436 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3437
3438 /* check all the channels are available */
3439 for (i = 0; i < 4; i++) {
3440 int adj_chan = center_chan - 6 + i * 4;
3441
3442 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3443 if (res == NOT_ALLOWED)
3444 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003445 if (res == NO_IR)
3446 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003447
3448 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3449 return NOT_ALLOWED;
3450 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3451 return NOT_ALLOWED;
3452 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3453 return NOT_ALLOWED;
3454 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3455 return NOT_ALLOWED;
3456 }
3457
3458 return ret;
3459}
3460
3461
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003462static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3463 struct hostapd_hw_modes *mode,
3464 u8 channel)
3465{
3466 u8 center_channels[] = { 50, 114 };
3467 unsigned int i;
3468
3469 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3470 return 0;
3471
3472 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3473 /*
3474 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3475 * so the center channel is 14 channels away from the start/end.
3476 */
3477 if (channel >= center_channels[i] - 14 &&
3478 channel <= center_channels[i] + 14)
3479 return center_channels[i];
3480
3481 return 0;
3482}
3483
3484
3485static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3486 struct hostapd_hw_modes *mode,
3487 u8 channel, u8 bw)
3488{
3489 u8 center_chan;
3490 int i, flags;
3491 enum chan_allowed res, ret = ALLOWED;
3492
3493 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3494 if (!center_chan)
3495 return NOT_ALLOWED;
3496 /* VHT 160 MHz uses DFS channels in most countries. */
3497
3498 /* Check all the channels are available */
3499 for (i = 0; i < 8; i++) {
3500 int adj_chan = center_chan - 14 + i * 4;
3501
3502 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3503 if (res == NOT_ALLOWED)
3504 return NOT_ALLOWED;
3505
3506 if (res == NO_IR)
3507 ret = NO_IR;
3508
3509 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3510 return NOT_ALLOWED;
3511 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3512 return NOT_ALLOWED;
3513 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3514 return NOT_ALLOWED;
3515 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3516 return NOT_ALLOWED;
3517 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3518 return NOT_ALLOWED;
3519 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3520 return NOT_ALLOWED;
3521 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3522 return NOT_ALLOWED;
3523 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3524 return NOT_ALLOWED;
3525 }
3526
3527 return ret;
3528}
3529
3530
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003531static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3532 struct hostapd_hw_modes *mode,
3533 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003534{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003535 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003536 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003537
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003538 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3539 if (bw == BW40MINUS) {
3540 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3541 return NOT_ALLOWED;
3542 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3543 } else if (bw == BW40PLUS) {
3544 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3545 return NOT_ALLOWED;
3546 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3547 } else if (bw == BW80) {
3548 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003549 } else if (bw == BW160) {
3550 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003551 }
3552
3553 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3554 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003555 if (res == NO_IR || res2 == NO_IR)
3556 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003557 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003558}
3559
3560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003562 struct p2p_channels *chan,
3563 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003564{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003565 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003566 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003568 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3570 "of all supported channels; assume dualband "
3571 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003572 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003573 }
3574
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003575 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003577 for (op = 0; global_op_class[op].op_class; op++) {
3578 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003580 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003582 if (o->p2p == NO_P2P_SUPP)
3583 continue;
3584
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003585 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586 if (mode == NULL)
3587 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003588 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3589 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003591 enum chan_allowed res;
3592 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3593 if (res == ALLOWED) {
3594 if (reg == NULL) {
3595 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3596 o->op_class);
3597 reg = &chan->reg_class[cla];
3598 cla++;
3599 reg->reg_class = o->op_class;
3600 }
3601 reg->channel[reg->channels] = ch;
3602 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003603 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003604 wpa_s->conf->p2p_add_cli_chan) {
3605 if (cli_reg == NULL) {
3606 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3607 o->op_class);
3608 cli_reg = &cli_chan->reg_class[cli_cla];
3609 cli_cla++;
3610 cli_reg->reg_class = o->op_class;
3611 }
3612 cli_reg->channel[cli_reg->channels] = ch;
3613 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615 }
3616 if (reg) {
3617 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3618 reg->channel, reg->channels);
3619 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003620 if (cli_reg) {
3621 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3622 cli_reg->channel, cli_reg->channels);
3623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003624 }
3625
3626 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003627 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 return 0;
3630}
3631
3632
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003633int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3634 struct hostapd_hw_modes *mode, u8 channel)
3635{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003636 int op;
3637 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003638
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003639 for (op = 0; global_op_class[op].op_class; op++) {
3640 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003641 u8 ch;
3642
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003643 if (o->p2p == NO_P2P_SUPP)
3644 continue;
3645
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003646 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3647 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003648 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3649 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003650 continue;
3651 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003652 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003653 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003654 }
3655 }
3656 return 0;
3657}
3658
3659
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003660int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3661 struct hostapd_hw_modes *mode, u8 channel)
3662{
3663 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3664 return 0;
3665
3666 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3667}
3668
3669
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003670int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3671 struct hostapd_hw_modes *mode, u8 channel)
3672{
3673 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3674 return 0;
3675 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3676}
3677
3678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3680 size_t buf_len)
3681{
3682 struct wpa_supplicant *wpa_s = ctx;
3683
3684 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3685 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3686 break;
3687 }
3688 if (wpa_s == NULL)
3689 return -1;
3690
3691 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3692}
3693
3694
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003695struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3696 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003697{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003698 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3699 struct wpa_ssid *s = wpa_s->current_ssid;
3700 if (s == NULL)
3701 continue;
3702 if (s->mode != WPAS_MODE_P2P_GO &&
3703 s->mode != WPAS_MODE_AP &&
3704 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3705 continue;
3706 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003707 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003708 continue;
3709 return wpa_s;
3710 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003711
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003712 return NULL;
3713
3714}
3715
3716
3717struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3718 const u8 *peer_dev_addr)
3719{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003720 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3721 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003722 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003723 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003724 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3725 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003726 }
3727
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003728 return NULL;
3729}
3730
3731
3732static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3733{
3734 struct wpa_supplicant *wpa_s = ctx;
3735
3736 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003737}
3738
3739
Dmitry Shmidt18463232014-01-24 12:29:41 -08003740static int wpas_is_concurrent_session_active(void *ctx)
3741{
3742 struct wpa_supplicant *wpa_s = ctx;
3743 struct wpa_supplicant *ifs;
3744
3745 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3746 if (ifs == wpa_s)
3747 continue;
3748 if (ifs->wpa_state > WPA_ASSOCIATED)
3749 return 1;
3750 }
3751 return 0;
3752}
3753
3754
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003755static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3756{
3757 struct wpa_supplicant *wpa_s = ctx;
3758 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3759}
3760
3761
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003762int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3763 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003764{
3765 struct wpa_interface iface;
3766 struct wpa_supplicant *p2pdev_wpa_s;
3767 char ifname[100];
3768 char force_name[100];
3769 int ret;
3770
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003771 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3772 wpa_s->ifname);
3773 if (os_snprintf_error(sizeof(ifname), ret))
3774 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003775 force_name[0] = '\0';
3776 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3777 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3778 force_name, wpa_s->pending_interface_addr, NULL);
3779 if (ret < 0) {
3780 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3781 return ret;
3782 }
3783 os_strlcpy(wpa_s->pending_interface_name, ifname,
3784 sizeof(wpa_s->pending_interface_name));
3785
3786 os_memset(&iface, 0, sizeof(iface));
3787 iface.p2p_mgmt = 1;
3788 iface.ifname = wpa_s->pending_interface_name;
3789 iface.driver = wpa_s->driver->name;
3790 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003791
3792 /*
3793 * If a P2P Device configuration file was given, use it as the interface
3794 * configuration file (instead of using parent's configuration file.
3795 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003796 if (conf_p2p_dev) {
3797 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003798 iface.ctrl_interface = NULL;
3799 } else {
3800 iface.confname = wpa_s->confname;
3801 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3802 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003803
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003804 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003805 if (!p2pdev_wpa_s) {
3806 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3807 return -1;
3808 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003809
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003810 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003811 wpa_s->pending_interface_name[0] = '\0';
3812 return 0;
3813}
3814
3815
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003816static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3817 const u8 *noa, size_t noa_len)
3818{
3819 struct wpa_supplicant *wpa_s, *intf = ctx;
3820 char hex[100];
3821
3822 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3823 if (wpa_s->waiting_presence_resp)
3824 break;
3825 }
3826 if (!wpa_s) {
3827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3828 return;
3829 }
3830 wpa_s->waiting_presence_resp = 0;
3831
3832 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3833 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3834 " status=%u noa=%s", MAC2STR(src), status, hex);
3835}
3836
3837
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003838static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3839 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003840 u8 *ret_ssid, size_t *ret_ssid_len,
3841 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003842{
3843 struct wpa_supplicant *wpa_s = ctx;
3844 struct wpa_ssid *s;
3845
3846 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3847 if (s) {
3848 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3849 *ret_ssid_len = s->ssid_len;
3850 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003851
3852 if (s->mode != WPAS_MODE_P2P_GO) {
3853 os_memset(intended_iface_addr, 0, ETH_ALEN);
3854 } else if (wpas_p2p_create_iface(wpa_s)) {
3855 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3856 return 0;
3857
3858 os_memcpy(intended_iface_addr,
3859 wpa_s->pending_interface_addr, ETH_ALEN);
3860 } else {
3861 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3862 ETH_ALEN);
3863 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003864 return 1;
3865 }
3866
3867 return 0;
3868}
3869
3870
3871static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003872 u8 *ssid, size_t *ssid_len, int *group_iface,
3873 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003874{
3875 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003876 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003877 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003878
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003879 /*
3880 * group_iface will be set to 1 only if a dedicated interface for P2P
3881 * role is required. First, we try to reuse an active GO. However,
3882 * if it is not present, we will try to reactivate an existing
3883 * persistent group and set group_iface to 1, so the caller will know
3884 * that the pending interface should be used.
3885 */
3886 *group_iface = 0;
3887
3888 if (freq)
3889 *freq = 0;
3890
3891 go = wpas_p2p_get_go_group(wpa_s);
3892 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003893 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003894 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003895 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003896 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3897 else
3898 return 0;
3899 } else {
3900 s = go->current_ssid;
3901 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3902 if (freq)
3903 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003904 }
3905
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003906 os_memcpy(ssid, s->ssid, s->ssid_len);
3907 *ssid_len = s->ssid_len;
3908
3909 return 1;
3910}
3911
3912
3913static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3914 const u8 *ssid, size_t ssid_len)
3915{
3916 struct wpa_supplicant *wpa_s = ctx;
3917 struct wpa_ssid *s;
3918 int save_config = 0;
3919 size_t i;
3920
3921 /* Start with our first choice of Persistent Groups */
3922 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3923 if (go && ssid && ssid_len &&
3924 s->ssid_len == ssid_len &&
3925 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3926 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3927 break;
3928
3929 /* Remove stale persistent group */
3930 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3931 wpa_config_remove_network(wpa_s->conf, s->id);
3932 save_config = 1;
3933 continue;
3934 }
3935
3936 for (i = 0; i < s->num_p2p_clients; i++) {
3937 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3938 peer, ETH_ALEN) != 0)
3939 continue;
3940
3941 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3942 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3943 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3944 break;
3945 }
3946 s->num_p2p_clients--;
3947 save_config = 1;
3948 }
3949
3950 if (save_config)
3951 p2p_config_write(wpa_s);
3952
3953 /* Return TRUE if valid SSID remains */
3954 return s != NULL;
3955}
3956
3957
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003958static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3959 const u8 *feat_cap, size_t feat_cap_len)
3960{
3961 static const char pref[] = " feature_cap=";
3962 int ret;
3963
3964 buf[0] = '\0';
3965
3966 /*
3967 * We expect a feature capability to contain at least one byte to be
3968 * reported. The string buffer provided by the caller function is
3969 * expected to be big enough to contain all bytes of the attribute for
3970 * known specifications. This function truncates the reported bytes if
3971 * the feature capability data exceeds the string buffer size.
3972 */
3973 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3974 return;
3975
3976 os_memcpy(buf, pref, sizeof(pref));
3977 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3978 buf_len - sizeof(pref) + 1,
3979 feat_cap, feat_cap_len);
3980
3981 if (ret != (2 * (int) feat_cap_len))
3982 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3983}
3984
3985
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003986static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3987 const u8 *adv_mac, const u8 *ses_mac,
3988 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3989 u8 conncap, int passwd_id,
3990 const u8 *persist_ssid,
3991 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003992 int prov_start, const char *session_info,
3993 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003994 unsigned int freq,
3995 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003996{
3997 struct wpa_supplicant *wpa_s = ctx;
3998 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003999 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004000 int save_config = 0;
4001 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004002 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004003
4004 if (!dev)
4005 return;
4006
4007 os_memset(mac, 0, ETH_ALEN);
4008 if (!adv_mac)
4009 adv_mac = mac;
4010 if (!ses_mac)
4011 ses_mac = mac;
4012 if (!grp_mac)
4013 grp_mac = mac;
4014
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004015 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4016 feat_cap, feat_cap_len);
4017
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004018 if (prov_start) {
4019 if (session_info == NULL) {
4020 wpa_msg_global(wpa_s, MSG_INFO,
4021 P2P_EVENT_P2PS_PROVISION_START MACSTR
4022 " adv_id=%x conncap=%x"
4023 " adv_mac=" MACSTR
4024 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004025 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004026 MAC2STR(dev), adv_id, conncap,
4027 MAC2STR(adv_mac),
4028 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004029 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004030 } else {
4031 wpa_msg_global(wpa_s, MSG_INFO,
4032 P2P_EVENT_P2PS_PROVISION_START MACSTR
4033 " adv_id=%x conncap=%x"
4034 " adv_mac=" MACSTR
4035 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004036 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004037 MAC2STR(dev), adv_id, conncap,
4038 MAC2STR(adv_mac),
4039 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004040 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004041 }
4042 return;
4043 }
4044
4045 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4046 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4047
4048 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4049 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4050 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4051
4052 if (persistent_go && !persistent_go->num_p2p_clients) {
4053 /* remove empty persistent GO */
4054 wpa_config_remove_network(wpa_s->conf,
4055 persistent_go->id);
4056 }
4057
4058 wpa_msg_global(wpa_s, MSG_INFO,
4059 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4060 " status=%d"
4061 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004062 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004063 MAC2STR(dev), status,
4064 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004065 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004066 return;
4067 }
4068
4069 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004070 if (persist_ssid && persist_ssid_size)
4071 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4072 persist_ssid_size);
4073
4074 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4075 is_zero_ether_addr(grp_mac)) {
4076 wpa_dbg(wpa_s, MSG_ERROR,
4077 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4078 return;
4079 }
4080
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004081 for (;;) {
4082 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4083 if (!stale)
4084 break;
4085
4086 if (s && s->ssid_len == stale->ssid_len &&
4087 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4088 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4089 break;
4090
4091 /* Remove stale persistent group */
4092 if (stale->mode != WPAS_MODE_P2P_GO ||
4093 stale->num_p2p_clients <= 1) {
4094 wpa_config_remove_network(wpa_s->conf, stale->id);
4095 } else {
4096 size_t i;
4097
4098 for (i = 0; i < stale->num_p2p_clients; i++) {
4099 if (os_memcmp(stale->p2p_client_list +
4100 i * ETH_ALEN,
4101 dev, ETH_ALEN) == 0) {
4102 os_memmove(stale->p2p_client_list +
4103 i * ETH_ALEN,
4104 stale->p2p_client_list +
4105 (i + 1) * ETH_ALEN,
4106 (stale->num_p2p_clients -
4107 i - 1) * ETH_ALEN);
4108 break;
4109 }
4110 }
4111 stale->num_p2p_clients--;
4112 }
4113 save_config = 1;
4114 }
4115
4116 if (save_config)
4117 p2p_config_write(wpa_s);
4118
4119 if (s) {
4120 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4121 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4122
4123 if (persistent_go && s != persistent_go &&
4124 !persistent_go->num_p2p_clients) {
4125 /* remove empty persistent GO */
4126 wpa_config_remove_network(wpa_s->conf,
4127 persistent_go->id);
4128 /* Save config */
4129 }
4130
4131 wpa_msg_global(wpa_s, MSG_INFO,
4132 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4133 " status=%d"
4134 " adv_id=%x adv_mac=" MACSTR
4135 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004136 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004137 MAC2STR(dev), status,
4138 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004139 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004140 return;
4141 }
4142
4143 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004144 /*
4145 * We need to copy the interface name. Simply saving a
4146 * pointer isn't enough, since if we use pending_interface_name
4147 * it will be overwritten when the group is added.
4148 */
4149 char go_ifname[100];
4150
4151 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004152 if (!go_wpa_s) {
4153 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004154 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004155
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004156 if (!wpas_p2p_create_iface(wpa_s))
4157 os_memcpy(go_ifname, wpa_s->ifname,
4158 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004159 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004160 os_memcpy(go_ifname,
4161 wpa_s->pending_interface_name,
4162 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004163
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004164 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004165 wpas_p2ps_prov_complete(
4166 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4167 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004168 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004169 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4170 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004171 return;
4172 }
4173
4174 /* If PD Resp complete, start up the GO */
4175 if (response_done && persistent_go) {
4176 wpas_p2p_group_add_persistent(
4177 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004178 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004179 persistent_go->mode ==
4180 WPAS_MODE_P2P_GO ?
4181 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004182 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004183 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004184 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004185 }
4186
4187 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004188 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4189 ETH_ALEN);
4190 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004191 }
4192 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004193 os_memcpy(go_ifname, go_wpa_s->ifname,
4194 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004195
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004196 if (is_zero_ether_addr(grp_mac)) {
4197 wpa_dbg(go_wpa_s, MSG_DEBUG,
4198 "P2P: Setting PIN-1 for ANY");
4199 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4200 "12345670", NULL, 0,
4201 0);
4202 } else {
4203 wpa_dbg(go_wpa_s, MSG_DEBUG,
4204 "P2P: Setting PIN-1 for " MACSTR,
4205 MAC2STR(grp_mac));
4206 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4207 "12345670", NULL, 0,
4208 0);
4209 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004210
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004211 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4212 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004213 }
4214
4215 wpa_msg_global(wpa_s, MSG_INFO,
4216 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4217 " status=%d conncap=%x"
4218 " adv_id=%x adv_mac=" MACSTR
4219 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004220 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004221 MAC2STR(dev), status, conncap,
4222 adv_id, MAC2STR(adv_mac),
4223 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004224 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004225 return;
4226 }
4227
4228 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4229 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4230
4231 if (persistent_go && !persistent_go->num_p2p_clients) {
4232 /* remove empty persistent GO */
4233 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4234 }
4235
4236 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004237 char ssid_hex[32 * 2 + 1];
4238
4239 if (group_ssid)
4240 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4241 group_ssid, group_ssid_len);
4242 else
4243 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004244 wpa_msg_global(wpa_s, MSG_INFO,
4245 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4246 " status=%d conncap=%x"
4247 " adv_id=%x adv_mac=" MACSTR
4248 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004249 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004250 MAC2STR(dev), status, conncap,
4251 adv_id, MAC2STR(adv_mac),
4252 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004253 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4254 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004255 } else {
4256 wpa_msg_global(wpa_s, MSG_INFO,
4257 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4258 " status=%d conncap=%x"
4259 " adv_id=%x adv_mac=" MACSTR
4260 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004261 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004262 MAC2STR(dev), status, conncap,
4263 adv_id, MAC2STR(adv_mac),
4264 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004265 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004266 }
4267}
4268
4269
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004270static int _wpas_p2p_in_progress(void *ctx)
4271{
4272 struct wpa_supplicant *wpa_s = ctx;
4273 return wpas_p2p_in_progress(wpa_s);
4274}
4275
4276
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004277static int wpas_prov_disc_resp_cb(void *ctx)
4278{
4279 struct wpa_supplicant *wpa_s = ctx;
4280 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004281 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004282
4283 if (!wpa_s->global->pending_p2ps_group)
4284 return 0;
4285
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004286 freq = wpa_s->global->pending_p2ps_group_freq;
4287 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004288 wpa_s->global->pending_p2ps_group = 0;
4289
4290 if (wpas_p2p_get_go_group(wpa_s))
4291 return 0;
4292 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4293
4294 if (persistent_go) {
4295 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004296 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004297 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004298 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004299 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004300 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004301 }
4302
4303 return 1;
4304}
4305
4306
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004307static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4308 unsigned int *len,
4309 unsigned int *freq_list)
4310{
4311 struct wpa_supplicant *wpa_s = ctx;
4312
4313 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4314 WPA_IF_P2P_CLIENT, len, freq_list);
4315}
4316
4317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318/**
4319 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4320 * @global: Pointer to global data from wpa_supplicant_init()
4321 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4322 * Returns: 0 on success, -1 on failure
4323 */
4324int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4325{
4326 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327 int i;
4328
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004329 if (wpa_s->conf->p2p_disabled)
4330 return 0;
4331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4333 return 0;
4334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335 if (global->p2p)
4336 return 0;
4337
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004338 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004340 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 p2p.p2p_scan = wpas_p2p_scan;
4342 p2p.send_action = wpas_send_action;
4343 p2p.send_action_done = wpas_send_action_done;
4344 p2p.go_neg_completed = wpas_go_neg_completed;
4345 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4346 p2p.dev_found = wpas_dev_found;
4347 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004348 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349 p2p.start_listen = wpas_start_listen;
4350 p2p.stop_listen = wpas_stop_listen;
4351 p2p.send_probe_resp = wpas_send_probe_resp;
4352 p2p.sd_request = wpas_sd_request;
4353 p2p.sd_response = wpas_sd_response;
4354 p2p.prov_disc_req = wpas_prov_disc_req;
4355 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004356 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 p2p.invitation_process = wpas_invitation_process;
4358 p2p.invitation_received = wpas_invitation_received;
4359 p2p.invitation_result = wpas_invitation_result;
4360 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004361 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004362 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004363 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004364 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004365 p2p.get_persistent_group = wpas_get_persistent_group;
4366 p2p.get_go_info = wpas_get_go_info;
4367 p2p.remove_stale_groups = wpas_remove_stale_groups;
4368 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4369 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4370 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004371 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372
4373 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004374 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375 p2p.dev_name = wpa_s->conf->device_name;
4376 p2p.manufacturer = wpa_s->conf->manufacturer;
4377 p2p.model_name = wpa_s->conf->model_name;
4378 p2p.model_number = wpa_s->conf->model_number;
4379 p2p.serial_number = wpa_s->conf->serial_number;
4380 if (wpa_s->wps) {
4381 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4382 p2p.config_methods = wpa_s->wps->config_methods;
4383 }
4384
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004385 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4386 wpa_printf(MSG_ERROR,
4387 "P2P: Failed to configure supported channel list");
4388 return -1;
4389 }
4390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 if (wpa_s->conf->p2p_listen_reg_class &&
4392 wpa_s->conf->p2p_listen_channel) {
4393 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4394 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004395 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 /*
4398 * Pick one of the social channels randomly as the listen
4399 * channel.
4400 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004401 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4402 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004403 wpa_printf(MSG_INFO,
4404 "P2P: No social channels supported by the driver - do not enable P2P");
4405 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004406 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004407 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004408 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004409 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4410 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411
4412 if (wpa_s->conf->p2p_oper_reg_class &&
4413 wpa_s->conf->p2p_oper_channel) {
4414 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4415 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4416 p2p.cfg_op_channel = 1;
4417 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4418 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4419
4420 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004421 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004422 * Use random operation channel from 2.4 GHz band social
4423 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4424 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004426 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4427 &p2p.op_channel) != 0) {
4428 wpa_printf(MSG_ERROR,
4429 "P2P: Failed to select random social channel as operation channel");
4430 return -1;
4431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 p2p.cfg_op_channel = 0;
4433 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4434 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4435 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004436
4437 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4438 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4439 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4440 }
4441
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004442 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4443 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4444 p2p.country[2] = 0x04;
4445 } else
4446 os_memcpy(p2p.country, "XX\x04", 3);
4447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4449 WPS_DEV_TYPE_LEN);
4450
4451 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4452 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4453 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4454
4455 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4456 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4457
4458 p2p.max_peers = 100;
4459
4460 if (wpa_s->conf->p2p_ssid_postfix) {
4461 p2p.ssid_postfix_len =
4462 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4463 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4464 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4465 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4466 p2p.ssid_postfix_len);
4467 }
4468
4469 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4470
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004471 p2p.max_listen = wpa_s->max_remain_on_chan;
4472
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004473 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4474 wpa_s->conf->p2p_passphrase_len <= 63)
4475 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4476 else
4477 p2p.passphrase_len = 8;
4478
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479 global->p2p = p2p_init(&p2p);
4480 if (global->p2p == NULL)
4481 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004482 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483
4484 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4485 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4486 continue;
4487 p2p_add_wps_vendor_extension(
4488 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4489 }
4490
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004491 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493 return 0;
4494}
4495
4496
4497/**
4498 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4499 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4500 *
4501 * This function deinitialize per-interface P2P data.
4502 */
4503void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4504{
4505 if (wpa_s->driver && wpa_s->drv_priv)
4506 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004507
4508 if (wpa_s->go_params) {
4509 /* Clear any stored provisioning info */
4510 p2p_clear_provisioning_info(
4511 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004512 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004513 }
4514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 os_free(wpa_s->go_params);
4516 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004517 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004518 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4519 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4520 wpa_s->p2p_long_listen = 0;
4521 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4522 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4523 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004524 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004525 wpas_p2p_listen_work_done(wpa_s);
4526 if (wpa_s->p2p_send_action_work) {
4527 os_free(wpa_s->p2p_send_action_work->ctx);
4528 radio_work_done(wpa_s->p2p_send_action_work);
4529 wpa_s->p2p_send_action_work = NULL;
4530 }
4531 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004533 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4534 wpa_s->p2p_oob_dev_pw = NULL;
4535
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004536 os_free(wpa_s->p2p_group_common_freqs);
4537 wpa_s->p2p_group_common_freqs = NULL;
4538 wpa_s->p2p_group_common_freqs_num = 0;
4539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540 /* TODO: remove group interface from the driver if this wpa_s instance
4541 * is on top of a P2P group interface */
4542}
4543
4544
4545/**
4546 * wpas_p2p_deinit_global - Deinitialize global P2P module
4547 * @global: Pointer to global data from wpa_supplicant_init()
4548 *
4549 * This function deinitializes the global (per device) P2P module.
4550 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004551static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552{
4553 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004555 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004556
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004557 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004558
4559 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4561 wpa_s = wpa_s->next;
4562 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 tmp = global->ifaces;
4564 while (tmp &&
4565 (tmp == wpa_s ||
4566 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4567 tmp = tmp->next;
4568 }
4569 if (tmp == NULL)
4570 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004571 /* Disconnect from the P2P group and deinit the interface */
4572 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 }
4574
4575 /*
4576 * Deinit GO data on any possibly remaining interface (if main
4577 * interface is used as GO).
4578 */
4579 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4580 if (wpa_s->ap_iface)
4581 wpas_p2p_group_deinit(wpa_s);
4582 }
4583
4584 p2p_deinit(global->p2p);
4585 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004586 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587}
4588
4589
4590static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4591{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004592 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004593 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594 if (wpa_s->drv_flags &
4595 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4596 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4597 return 1; /* P2P group requires a new interface in every case
4598 */
4599 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4600 return 0; /* driver does not support concurrent operations */
4601 if (wpa_s->global->ifaces->next)
4602 return 1; /* more that one interface already in use */
4603 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4604 return 1; /* this interface is already in use */
4605 return 0;
4606}
4607
4608
4609static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4610 const u8 *peer_addr,
4611 enum p2p_wps_method wps_method,
4612 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004613 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004614 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004616 if (persistent_group && wpa_s->conf->persistent_reconnect)
4617 persistent_group = 2;
4618
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004619 /*
4620 * Increase GO config timeout if HT40 is used since it takes some time
4621 * to scan channels for coex purposes before the BSS can be started.
4622 */
4623 p2p_set_config_timeout(wpa_s->global->p2p,
4624 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004626 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4627 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004628 persistent_group, ssid ? ssid->ssid : NULL,
4629 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004630 wpa_s->p2p_pd_before_go_neg, pref_freq,
4631 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4632 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633}
4634
4635
4636static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4637 const u8 *peer_addr,
4638 enum p2p_wps_method wps_method,
4639 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004640 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004641 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004643 if (persistent_group && wpa_s->conf->persistent_reconnect)
4644 persistent_group = 2;
4645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4647 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004648 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004649 ssid ? ssid->ssid_len : 0, pref_freq,
4650 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4651 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652}
4653
4654
4655static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4656{
4657 wpa_s->p2p_join_scan_count++;
4658 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4659 wpa_s->p2p_join_scan_count);
4660 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4661 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4662 " for join operationg - stop join attempt",
4663 MAC2STR(wpa_s->pending_join_iface_addr));
4664 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665 if (wpa_s->p2p_auto_pd) {
4666 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004667 wpa_msg_global(wpa_s, MSG_INFO,
4668 P2P_EVENT_PROV_DISC_FAILURE
4669 " p2p_dev_addr=" MACSTR " status=N/A",
4670 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004671 return;
4672 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004673 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004674 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004675 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004676 }
4677}
4678
4679
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4681{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004682 int res;
4683 unsigned int num, i;
4684 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004685
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004686 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4687 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004688 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004689 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004690
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004691 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4692 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004693 if (!freqs)
4694 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004695
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004696 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4697 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004698
4699 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004700 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004701 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4702 freq);
4703 res = 0;
4704 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004705 }
4706 }
4707
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004708 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004709 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004710
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004711exit_free:
4712 os_free(freqs);
4713 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004714}
4715
4716
4717static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4718 const u8 *peer_dev_addr)
4719{
4720 struct wpa_bss *bss;
4721 int updated;
4722
4723 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4724 if (bss == NULL)
4725 return -1;
4726 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4727 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4728 "last scan");
4729 return 0;
4730 }
4731
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004732 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4733 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004734 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4735 "%ld.%06ld (%supdated in last scan)",
4736 bss->last_update.sec, bss->last_update.usec,
4737 updated ? "": "not ");
4738
4739 return updated;
4740}
4741
4742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4744 struct wpa_scan_results *scan_res)
4745{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004746 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004747 int freq;
4748 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4751
4752 if (wpa_s->global->p2p_disabled)
4753 return;
4754
Dmitry Shmidt04949592012-07-19 12:16:46 -07004755 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4756 scan_res ? (int) scan_res->num : -1,
4757 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758
4759 if (scan_res)
4760 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4761
Dmitry Shmidt04949592012-07-19 12:16:46 -07004762 if (wpa_s->p2p_auto_pd) {
4763 int join = wpas_p2p_peer_go(wpa_s,
4764 wpa_s->pending_join_dev_addr);
4765 if (join == 0 &&
4766 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4767 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004768 bss = wpa_bss_get_bssid_latest(
4769 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004770 if (bss) {
4771 freq = bss->freq;
4772 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4773 "the peer " MACSTR " at %d MHz",
4774 wpa_s->auto_pd_scan_retry,
4775 MAC2STR(wpa_s->
4776 pending_join_dev_addr),
4777 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004778 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004779 return;
4780 }
4781 }
4782
4783 if (join < 0)
4784 join = 0;
4785
4786 wpa_s->p2p_auto_pd = 0;
4787 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4788 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4789 MAC2STR(wpa_s->pending_join_dev_addr), join);
4790 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004791 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004792 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004793 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004794 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004795 wpa_msg_global(wpa_s, MSG_INFO,
4796 P2P_EVENT_PROV_DISC_FAILURE
4797 " p2p_dev_addr=" MACSTR " status=N/A",
4798 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004799 }
4800 return;
4801 }
4802
4803 if (wpa_s->p2p_auto_join) {
4804 int join = wpas_p2p_peer_go(wpa_s,
4805 wpa_s->pending_join_dev_addr);
4806 if (join < 0) {
4807 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4808 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004809 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004810 P2P_EVENT_FALLBACK_TO_GO_NEG
4811 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4813 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4814 wpa_s->p2p_persistent_group, 0, 0, 0,
4815 wpa_s->p2p_go_intent,
4816 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004817 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004818 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004819 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004820 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004821 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004822 wpa_s->p2p_go_max_oper_chwidth,
4823 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004824 return;
4825 }
4826
4827 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4828 "try to join the group", join ? "" :
4829 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004830 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004831 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004832 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004833 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004834 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004835 }
4836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004837 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4838 wpa_s->pending_join_iface_addr);
4839 if (freq < 0 &&
4840 p2p_get_interface_addr(wpa_s->global->p2p,
4841 wpa_s->pending_join_dev_addr,
4842 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004843 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4844 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4846 "address for join from " MACSTR " to " MACSTR
4847 " based on newly discovered P2P peer entry",
4848 MAC2STR(wpa_s->pending_join_iface_addr),
4849 MAC2STR(iface_addr));
4850 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4851 ETH_ALEN);
4852
4853 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4854 wpa_s->pending_join_iface_addr);
4855 }
4856 if (freq >= 0) {
4857 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4858 "from P2P peer table: %d MHz", freq);
4859 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004860 if (wpa_s->p2p_join_ssid_len) {
4861 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4862 MACSTR " and SSID %s",
4863 MAC2STR(wpa_s->pending_join_iface_addr),
4864 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4865 wpa_s->p2p_join_ssid_len));
4866 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4867 wpa_s->p2p_join_ssid,
4868 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004869 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004870 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4871 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4872 bss = wpa_bss_get_bssid_latest(wpa_s,
4873 wpa_s->pending_join_iface_addr);
4874 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004875 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004876 u8 dev_addr[ETH_ALEN];
4877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 freq = bss->freq;
4879 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004880 "from BSS table: %d MHz (SSID %s)", freq,
4881 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004882 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4883 dev_addr) == 0 &&
4884 os_memcmp(wpa_s->pending_join_dev_addr,
4885 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4886 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4887 ETH_ALEN) != 0) {
4888 wpa_printf(MSG_DEBUG,
4889 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4890 MAC2STR(dev_addr),
4891 MAC2STR(wpa_s->pending_join_dev_addr));
4892 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4893 ETH_ALEN);
4894 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895 }
4896 if (freq > 0) {
4897 u16 method;
4898
Dmitry Shmidt04949592012-07-19 12:16:46 -07004899 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004900 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004901 P2P_EVENT_GROUP_FORMATION_FAILURE
4902 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004903 wpas_notify_p2p_group_formation_failure(
4904 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004905 return;
4906 }
4907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4909 "prior to joining an existing group (GO " MACSTR
4910 " freq=%u MHz)",
4911 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4912 wpa_s->pending_pd_before_join = 1;
4913
4914 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 case WPS_PIN_DISPLAY:
4916 method = WPS_CONFIG_KEYPAD;
4917 break;
4918 case WPS_PIN_KEYPAD:
4919 method = WPS_CONFIG_DISPLAY;
4920 break;
4921 case WPS_PBC:
4922 method = WPS_CONFIG_PUSHBUTTON;
4923 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004924 case WPS_P2PS:
4925 method = WPS_CONFIG_P2PS;
4926 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004927 default:
4928 method = 0;
4929 break;
4930 }
4931
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004932 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4933 wpa_s->pending_join_dev_addr) ==
4934 method)) {
4935 /*
4936 * We have already performed provision discovery for
4937 * joining the group. Proceed directly to join
4938 * operation without duplicated provision discovery. */
4939 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4940 "with " MACSTR " already done - proceed to "
4941 "join",
4942 MAC2STR(wpa_s->pending_join_dev_addr));
4943 wpa_s->pending_pd_before_join = 0;
4944 goto start;
4945 }
4946
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004948 wpa_s->pending_join_dev_addr,
4949 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004950 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4952 "Discovery Request before joining an "
4953 "existing group");
4954 wpa_s->pending_pd_before_join = 0;
4955 goto start;
4956 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957 return;
4958 }
4959
4960 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4961 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4962 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4963 wpas_p2p_check_join_scan_limit(wpa_s);
4964 return;
4965
4966start:
4967 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004968 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
4969 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970}
4971
4972
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004973static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4974 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976 int ret;
4977 struct wpa_driver_scan_params params;
4978 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004979 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004980 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004981 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004982
4983 os_memset(&params, 0, sizeof(params));
4984
4985 /* P2P Wildcard SSID */
4986 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004987 if (ssid && ssid_len) {
4988 params.ssids[0].ssid = ssid;
4989 params.ssids[0].ssid_len = ssid_len;
4990 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4991 wpa_s->p2p_join_ssid_len = ssid_len;
4992 } else {
4993 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4994 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4995 wpa_s->p2p_join_ssid_len = 0;
4996 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997
4998 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004999 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5000 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5001 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 if (wps_ie == NULL) {
5003 wpas_p2p_scan_res_join(wpa_s, NULL);
5004 return;
5005 }
5006
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005007 if (!freq) {
5008 int oper_freq;
5009 /*
5010 * If freq is not provided, check the operating freq of the GO
5011 * and use a single channel scan on if possible.
5012 */
5013 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5014 wpa_s->pending_join_iface_addr);
5015 if (oper_freq > 0)
5016 freq = oper_freq;
5017 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005018 if (freq > 0) {
5019 freqs[0] = freq;
5020 params.freqs = freqs;
5021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005023 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5024 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5025 if (ies == NULL) {
5026 wpabuf_free(wps_ie);
5027 wpas_p2p_scan_res_join(wpa_s, NULL);
5028 return;
5029 }
5030 wpabuf_put_buf(ies, wps_ie);
5031 wpabuf_free(wps_ie);
5032
5033 bands = wpas_get_bands(wpa_s, freqs);
5034 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5035
5036 params.p2p_probe = 1;
5037 params.extra_ies = wpabuf_head(ies);
5038 params.extra_ies_len = wpabuf_len(ies);
5039
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005040 if (wpa_s->clear_driver_scan_cache) {
5041 wpa_printf(MSG_DEBUG,
5042 "Request driver to clear scan cache due to local BSS flush");
5043 params.only_new_results = 1;
5044 }
5045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005046 /*
5047 * Run a scan to update BSS table and start Provision Discovery once
5048 * the new scan results become available.
5049 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005050 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005051 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005052 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005053 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005054 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005055 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005056 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057
5058 wpabuf_free(ies);
5059
5060 if (ret) {
5061 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5062 "try again later");
5063 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5064 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5065 wpas_p2p_check_join_scan_limit(wpa_s);
5066 }
5067}
5068
5069
Dmitry Shmidt04949592012-07-19 12:16:46 -07005070static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5071{
5072 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005073 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005074}
5075
5076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005077static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005078 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005079 int auto_join, int op_freq,
5080 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005081{
5082 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005083 MACSTR " dev " MACSTR " op_freq=%d)%s",
5084 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005085 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005086 if (ssid && ssid_len) {
5087 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5088 wpa_ssid_txt(ssid, ssid_len));
5089 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090
Dmitry Shmidt04949592012-07-19 12:16:46 -07005091 wpa_s->p2p_auto_pd = 0;
5092 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5094 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5095 wpa_s->pending_join_wps_method = wps_method;
5096
5097 /* Make sure we are not running find during connection establishment */
5098 wpas_p2p_stop_find(wpa_s);
5099
5100 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005101 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 return 0;
5103}
5104
5105
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005106static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5107 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108{
5109 struct wpa_supplicant *group;
5110 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005111 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005112
5113 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5114 if (group == NULL)
5115 return -1;
5116 if (group != wpa_s) {
5117 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5118 sizeof(group->p2p_pin));
5119 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005120 } else {
5121 /*
5122 * Need to mark the current interface for p2p_group_formation
5123 * when a separate group interface is not used. This is needed
5124 * to allow p2p_cancel stop a pending p2p_connect-join.
5125 * wpas_p2p_init_group_interface() addresses this for the case
5126 * where a separate group interface is used.
5127 */
5128 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005129 }
5130
5131 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133
5134 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005135 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5137 ETH_ALEN);
5138 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005139 if (freq && ssid && ssid_len) {
5140 res.freq = freq;
5141 res.ssid_len = ssid_len;
5142 os_memcpy(res.ssid, ssid, ssid_len);
5143 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005144 if (ssid && ssid_len) {
5145 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5146 ssid, ssid_len);
5147 } else {
5148 bss = wpa_bss_get_bssid_latest(
5149 wpa_s, wpa_s->pending_join_iface_addr);
5150 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005151 if (bss) {
5152 res.freq = bss->freq;
5153 res.ssid_len = bss->ssid_len;
5154 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5155 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5156 bss->freq,
5157 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005158 } else if (ssid && ssid_len) {
5159 res.ssid_len = ssid_len;
5160 os_memcpy(res.ssid, ssid, ssid_len);
5161 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5162 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005163 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005164 }
5165
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005166 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5167 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5168 "starting client");
5169 wpa_drv_cancel_remain_on_channel(wpa_s);
5170 wpa_s->off_channel_freq = 0;
5171 wpa_s->roc_waiting_drv_freq = 0;
5172 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 wpas_start_wps_enrollee(group, &res);
5174
5175 /*
5176 * Allow a longer timeout for join-a-running-group than normal 15
5177 * second group formation timeout since the GO may not have authorized
5178 * our connection yet.
5179 */
5180 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5181 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5182 wpa_s, NULL);
5183
5184 return 0;
5185}
5186
5187
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005188static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005189 int *force_freq, int *pref_freq, int go,
5190 unsigned int *pref_freq_list,
5191 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005192{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005193 struct wpa_used_freq_data *freqs;
5194 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005195 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5196
5197 max_pref_freq = *num_pref_freq;
5198 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005199
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005200 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5201 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005202 if (!freqs)
5203 return -1;
5204
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005205 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5206 wpa_s->num_multichan_concurrent);
5207
5208 /*
5209 * It is possible that the total number of used frequencies is bigger
5210 * than the number of frequencies used for P2P, so get the system wide
5211 * number of unused frequencies.
5212 */
5213 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5214
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005215 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005216 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5217 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005218
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005219 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005220 int ret;
5221 if (go)
5222 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5223 else
5224 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5225 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005226 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005227 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5228 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005229 /*
5230 * If freq is a DFS channel and DFS is offloaded
5231 * to the driver, allow P2P GO to use it.
5232 */
5233 wpa_printf(MSG_DEBUG,
5234 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5235 freq);
5236 } else {
5237 wpa_printf(MSG_DEBUG,
5238 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5239 freq);
5240 res = -3;
5241 goto exit_free;
5242 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005243 }
5244
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005245 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005246 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005247 freq_in_use = 1;
5248 }
5249
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005250 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005251 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5252 freq);
5253 res = -2;
5254 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005255 }
5256 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5257 "requested channel (%u MHz)", freq);
5258 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005259 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005260 }
5261
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005262 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005263
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005264 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5265 enum wpa_driver_if_type iface_type;
5266
5267 if (go)
5268 iface_type = WPA_IF_P2P_GO;
5269 else
5270 iface_type = WPA_IF_P2P_CLIENT;
5271
5272 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5273 best_freq, go);
5274
5275 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5276 &max_pref_freq,
5277 pref_freq_list);
5278 if (!res && max_pref_freq > 0) {
5279 *num_pref_freq = max_pref_freq;
5280 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005281 while (i < *num_pref_freq &&
5282 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005283 pref_freq_list[i]) ||
5284 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005285 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005286 wpa_printf(MSG_DEBUG,
5287 "P2P: preferred_freq_list[%d]=%d is disallowed",
5288 i, pref_freq_list[i]);
5289 i++;
5290 }
5291 if (i != *num_pref_freq) {
5292 best_freq = pref_freq_list[i];
5293 wpa_printf(MSG_DEBUG,
5294 "P2P: Using preferred_freq_list[%d]=%d",
5295 i, best_freq);
5296 } else {
5297 wpa_printf(MSG_DEBUG,
5298 "P2P: All driver preferred frequencies are disallowed for P2P use");
5299 *num_pref_freq = 0;
5300 }
5301 } else {
5302 wpa_printf(MSG_DEBUG,
5303 "P2P: No preferred frequency list available");
5304 }
5305 }
5306
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005307 /* We have a candidate frequency to use */
5308 if (best_freq > 0) {
5309 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005310 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005311 best_freq);
5312 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005313 } else {
5314 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 -07005315 best_freq);
5316 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005317 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005318 } else if (num_unused > 0) {
5319 wpa_printf(MSG_DEBUG,
5320 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5321 *force_freq = 0;
5322 } else {
5323 wpa_printf(MSG_DEBUG,
5324 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5325 res = -2;
5326 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005327 }
5328
5329exit_ok:
5330 res = 0;
5331exit_free:
5332 os_free(freqs);
5333 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005334}
5335
5336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337/**
5338 * wpas_p2p_connect - Request P2P Group Formation to be started
5339 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5340 * @peer_addr: Address of the peer P2P Device
5341 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5342 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005343 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005344 * @join: Whether to join an existing group (as a client) instead of starting
5345 * Group Owner negotiation; @peer_addr is BSSID in that case
5346 * @auth: Whether to only authorize the connection instead of doing that and
5347 * initiating Group Owner negotiation
5348 * @go_intent: GO Intent or -1 to use default
5349 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005350 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005351 * @persistent_id: Persistent group credentials to use for forcing GO
5352 * parameters or -1 to generate new values (SSID/passphrase)
5353 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5354 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005355 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005356 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005357 * @vht_chwidth: Channel width supported by GO operating with VHT support
5358 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005359 * @group_ssid: Specific Group SSID for join or %NULL if not set
5360 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5362 * failure, -2 on failure due to channel not currently available,
5363 * -3 if forced channel is not supported
5364 */
5365int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5366 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005367 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005368 int go_intent, int freq, unsigned int vht_center_freq2,
5369 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005370 unsigned int vht_chwidth, const u8 *group_ssid,
5371 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005373 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005374 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005376 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005377 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005378 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379
5380 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5381 return -1;
5382
Dmitry Shmidt04949592012-07-19 12:16:46 -07005383 if (persistent_id >= 0) {
5384 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5385 if (ssid == NULL || ssid->disabled != 2 ||
5386 ssid->mode != WPAS_MODE_P2P_GO)
5387 return -1;
5388 }
5389
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005390 os_free(wpa_s->global->add_psk);
5391 wpa_s->global->add_psk = NULL;
5392
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005393 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005394 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005395 wpa_s->global->pending_p2ps_group_freq = 0;
5396 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 if (go_intent < 0)
5399 go_intent = wpa_s->conf->p2p_go_intent;
5400
5401 if (!auth)
5402 wpa_s->p2p_long_listen = 0;
5403
5404 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005405 wpa_s->p2p_persistent_group = !!persistent_group;
5406 wpa_s->p2p_persistent_id = persistent_id;
5407 wpa_s->p2p_go_intent = go_intent;
5408 wpa_s->p2p_connect_freq = freq;
5409 wpa_s->p2p_fallback_to_go_neg = 0;
5410 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005411 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005412 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005413 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5414 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005415
5416 if (pin)
5417 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5418 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005419 if (wps_generate_pin((unsigned int *) &ret) < 0)
5420 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005421 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5422 "%08d", ret);
5423 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5424 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5426 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005427 } else if (wps_method == WPS_P2PS) {
5428 /* Force the P2Ps default PIN to be used */
5429 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430 } else
5431 wpa_s->p2p_pin[0] = '\0';
5432
Dmitry Shmidt04949592012-07-19 12:16:46 -07005433 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5435 if (auth) {
5436 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5437 "connect a running group from " MACSTR,
5438 MAC2STR(peer_addr));
5439 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5440 return ret;
5441 }
5442 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5443 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5444 iface_addr) < 0) {
5445 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5446 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5447 dev_addr);
5448 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005449 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005450 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005451 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5452 "%ld.%06ld",
5453 wpa_s->p2p_auto_started.sec,
5454 wpa_s->p2p_auto_started.usec);
5455 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005456 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005457 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005458 auto_join, freq,
5459 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 return -1;
5461 return ret;
5462 }
5463
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005464 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005465 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005466 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005467 if (res)
5468 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005469 wpas_p2p_set_own_freq_preference(wpa_s,
5470 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005472 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5475
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005476 if (wpa_s->create_p2p_iface) {
5477 /* Prepare to add a new interface for the group */
5478 iftype = WPA_IF_P2P_GROUP;
5479 if (go_intent == 15)
5480 iftype = WPA_IF_P2P_GO;
5481 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5482 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5483 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005485 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005487 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005488 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005489 if (wpa_s->p2p_mgmt)
5490 if_addr = wpa_s->parent->own_addr;
5491 else
5492 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005493 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5494 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495
5496 if (auth) {
5497 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005498 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005499 force_freq, persistent_group, ssid,
5500 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 return -1;
5502 return ret;
5503 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005504
5505 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5506 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005507 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005508 if (wpa_s->create_p2p_iface)
5509 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 return -1;
5511 }
5512 return ret;
5513}
5514
5515
5516/**
5517 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5518 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5519 * @freq: Frequency of the channel in MHz
5520 * @duration: Duration of the stay on the channel in milliseconds
5521 *
5522 * This callback is called when the driver indicates that it has started the
5523 * requested remain-on-channel duration.
5524 */
5525void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5526 unsigned int freq, unsigned int duration)
5527{
5528 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5529 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005530 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)",
5531 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5532 wpa_s->roc_waiting_drv_freq, freq, duration);
5533 if (wpa_s->off_channel_freq &&
5534 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005535 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5536 wpa_s->pending_listen_duration);
5537 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005538 } else {
5539 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5540 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5541 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 }
5543}
5544
5545
Jithu Jance57cea1a2014-08-20 10:22:04 -07005546int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547{
5548 /* Limit maximum Listen state time based on driver limitation. */
5549 if (timeout > wpa_s->max_remain_on_chan)
5550 timeout = wpa_s->max_remain_on_chan;
5551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005552 return p2p_listen(wpa_s->global->p2p, timeout);
5553}
5554
5555
5556/**
5557 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5558 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5559 * @freq: Frequency of the channel in MHz
5560 *
5561 * This callback is called when the driver indicates that a remain-on-channel
5562 * operation has been completed, i.e., the duration on the requested channel
5563 * has timed out.
5564 */
5565void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5566 unsigned int freq)
5567{
5568 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5569 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005570 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005571 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5573 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005574 if (wpa_s->p2p_long_listen > 0)
5575 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5577 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005578 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 if (wpa_s->p2p_long_listen > 0) {
5581 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5582 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005583 } else {
5584 /*
5585 * When listen duration is over, stop listen & update p2p_state
5586 * to IDLE.
5587 */
5588 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 }
5590}
5591
5592
5593/**
5594 * wpas_p2p_group_remove - Remove a P2P group
5595 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5596 * @ifname: Network interface name of the group interface or "*" to remove all
5597 * groups
5598 * Returns: 0 on success, -1 on failure
5599 *
5600 * This function is used to remove a P2P group. This can be used to disconnect
5601 * from a group in which the local end is a P2P Client or to end a P2P Group in
5602 * case the local end is the Group Owner. If a virtual network interface was
5603 * created for this group, that interface will be removed. Otherwise, only the
5604 * configured P2P group network will be removed from the interface.
5605 */
5606int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5607{
5608 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005609 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005610
5611 if (os_strcmp(ifname, "*") == 0) {
5612 struct wpa_supplicant *prev;
5613 wpa_s = global->ifaces;
5614 while (wpa_s) {
5615 prev = wpa_s;
5616 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005617 if (prev->p2p_group_interface !=
5618 NOT_P2P_GROUP_INTERFACE ||
5619 (prev->current_ssid &&
5620 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005621 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 }
5623 return 0;
5624 }
5625
5626 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5627 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5628 break;
5629 }
5630
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005631 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632}
5633
5634
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005635static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5636{
5637 unsigned int r;
5638
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005639 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5640 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5641 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5642 int res;
5643
5644 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5645 &size, pref_freq_list);
5646 if (!res && size > 0) {
5647 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005648 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005649 (!p2p_supported_freq(wpa_s->global->p2p,
5650 pref_freq_list[i]) ||
5651 wpas_p2p_disallowed_freq(wpa_s->global,
5652 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005653 wpa_printf(MSG_DEBUG,
5654 "P2P: preferred_freq_list[%d]=%d is disallowed",
5655 i, pref_freq_list[i]);
5656 i++;
5657 }
5658 if (i != size) {
5659 freq = pref_freq_list[i];
5660 wpa_printf(MSG_DEBUG,
5661 "P2P: Using preferred_freq_list[%d]=%d",
5662 i, freq);
5663 } else {
5664 wpa_printf(MSG_DEBUG,
5665 "P2P: All driver preferred frequencies are disallowed for P2P use");
5666 }
5667 } else {
5668 wpa_printf(MSG_DEBUG,
5669 "P2P: No preferred frequency list available");
5670 }
5671 }
5672
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005673 if (freq == 2) {
5674 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5675 "band");
5676 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005677 p2p_supported_freq_go(wpa_s->global->p2p,
5678 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005679 freq = wpa_s->best_24_freq;
5680 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5681 "channel: %d MHz", freq);
5682 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005683 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5684 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005685 freq = 2412 + (r % 3) * 25;
5686 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5687 "channel: %d MHz", freq);
5688 }
5689 }
5690
5691 if (freq == 5) {
5692 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5693 "band");
5694 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005695 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005696 wpa_s->best_5_freq)) {
5697 freq = wpa_s->best_5_freq;
5698 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5699 "channel: %d MHz", freq);
5700 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005701 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5702 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005703 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005704 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005705 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5706 "5 GHz channel for P2P group");
5707 return -1;
5708 }
5709 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5710 "channel: %d MHz", freq);
5711 }
5712 }
5713
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005714 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005715 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005716 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5717 wpa_s->hw.num_modes)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005718 /*
5719 * If freq is a DFS channel and DFS is offloaded to the
5720 * driver, allow P2P GO to use it.
5721 */
5722 wpa_printf(MSG_DEBUG, "P2P: "
5723 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5724 __func__, freq);
5725 return freq;
5726 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005727 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5728 "(%u MHz) is not supported for P2P uses",
5729 freq);
5730 return -1;
5731 }
5732
5733 return freq;
5734}
5735
5736
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005737static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5738 const struct p2p_channels *channels,
5739 int freq)
5740{
5741 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5742 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5743 freq_included(wpa_s, channels, freq))
5744 return 1;
5745 return 0;
5746}
5747
5748
5749static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5750 struct p2p_go_neg_results *params,
5751 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005752{
5753 unsigned int i, r;
5754
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005755 /* try all channels in operating class 115 */
5756 for (i = 0; i < 4; i++) {
5757 params->freq = 5180 + i * 20;
5758 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005759 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005760 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5761 goto out;
5762 }
5763
5764 /* try all channels in operating class 124 */
5765 for (i = 0; i < 4; i++) {
5766 params->freq = 5745 + i * 20;
5767 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005768 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005769 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5770 goto out;
5771 }
5772
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005773 /* try social channel class 180 channel 2 */
5774 params->freq = 58320 + 1 * 2160;
5775 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005776 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005777 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5778 goto out;
5779
5780 /* try all channels in reg. class 180 */
5781 for (i = 0; i < 4; i++) {
5782 params->freq = 58320 + i * 2160;
5783 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005784 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005785 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5786 goto out;
5787 }
5788
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005789 /* try some random selection of the social channels */
5790 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5791 return;
5792
5793 for (i = 0; i < 3; i++) {
5794 params->freq = 2412 + ((r + i) % 3) * 25;
5795 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5796 goto out;
5797 }
5798
5799 /* try all other channels in operating class 81 */
5800 for (i = 0; i < 11; i++) {
5801 params->freq = 2412 + i * 5;
5802
5803 /* skip social channels; covered in the previous loop */
5804 if (params->freq == 2412 ||
5805 params->freq == 2437 ||
5806 params->freq == 2462)
5807 continue;
5808
5809 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5810 goto out;
5811 }
5812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005813 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005814 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005815 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005816out:
5817 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5818 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005819}
5820
5821
Roshan Pius3a1667e2018-07-03 15:17:14 -07005822static int wpas_same_band(int freq1, int freq2)
5823{
5824 enum hostapd_hw_mode mode1, mode2;
5825 u8 chan1, chan2;
5826
5827 mode1 = ieee80211_freq_to_chan(freq1, &chan1);
5828 mode2 = ieee80211_freq_to_chan(freq2, &chan2);
5829 if (mode1 == NUM_HOSTAPD_MODES)
5830 return 0;
5831 return mode1 == mode2;
5832}
5833
5834
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005835static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5836 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005837 int freq, int vht_center_freq2, int ht40,
5838 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005839 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005841 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005842 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005843 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005844 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005845 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005846
5847 os_memset(params, 0, sizeof(*params));
5848 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005849 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005850 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005851 params->max_oper_chwidth = max_oper_chwidth;
5852 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005854 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5855 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005856 if (!freqs)
5857 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005858
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005859 num = get_shared_radio_freqs_data(wpa_s, freqs,
5860 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005861
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005862 if (wpa_s->current_ssid &&
5863 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5864 wpa_s->wpa_state == WPA_COMPLETED) {
5865 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5866 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005867
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005868 /*
5869 * If the frequency selection is done for an active P2P GO that
5870 * is not sharing a frequency, allow to select a new frequency
5871 * even if there are no unused frequencies as we are about to
5872 * move the P2P GO so its frequency can be re-used.
5873 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005874 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005875 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5876 freqs[i].flags == 0) {
5877 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005878 break;
5879 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005880 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005881 }
5882
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005883 /* try using the forced freq */
5884 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005885 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
5886 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005887 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005888 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005889 freq);
5890 goto fail;
5891 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005892 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5893 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07005894 ieee80211_is_dfs(freq, wpa_s->hw.modes,
5895 wpa_s->hw.num_modes)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005896 /*
5897 * If freq is a DFS channel and DFS is offloaded
5898 * to the driver, allow P2P GO to use it.
5899 */
5900 wpa_printf(MSG_DEBUG,
5901 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
5902 __func__, freq);
5903 } else {
5904 wpa_printf(MSG_DEBUG,
5905 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
5906 freq);
5907 goto fail;
5908 }
5909 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005910
5911 for (i = 0; i < num; i++) {
5912 if (freqs[i].freq == freq) {
5913 wpa_printf(MSG_DEBUG,
5914 "P2P: forced freq (%d MHz) is also shared",
5915 freq);
5916 params->freq = freq;
5917 goto success;
5918 }
5919 }
5920
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005921 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005922 wpa_printf(MSG_DEBUG,
5923 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5924 freq);
5925 goto fail;
5926 }
5927
5928 wpa_printf(MSG_DEBUG,
5929 "P2P: force GO freq (%d MHz) on a free channel",
5930 freq);
5931 params->freq = freq;
5932 goto success;
5933 }
5934
5935 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005936 if (num &&
5937 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005938 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5939 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5940 wpa_printf(MSG_DEBUG,
5941 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005942 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005943 params->freq = cand;
5944 goto success;
5945 }
5946
5947 /* try using one of the shared freqs */
5948 for (i = 0; i < num; i++) {
5949 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5950 freqs[i].freq)) {
5951 wpa_printf(MSG_DEBUG,
5952 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005953 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005954 params->freq = freqs[i].freq;
5955 goto success;
5956 }
5957 }
5958 }
5959
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005960 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005961 wpa_printf(MSG_DEBUG,
5962 "P2P: Cannot force GO on any of the channels we are already using");
5963 goto fail;
5964 }
5965
5966 /* try using the setting from the configuration file */
5967 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5968 wpa_s->conf->p2p_oper_channel >= 1 &&
5969 wpa_s->conf->p2p_oper_channel <= 11 &&
5970 wpas_p2p_supported_freq_go(
5971 wpa_s, channels,
5972 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5973 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5974 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5975 "frequency %d MHz", params->freq);
5976 goto success;
5977 }
5978
5979 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5980 wpa_s->conf->p2p_oper_reg_class == 116 ||
5981 wpa_s->conf->p2p_oper_reg_class == 117 ||
5982 wpa_s->conf->p2p_oper_reg_class == 124 ||
5983 wpa_s->conf->p2p_oper_reg_class == 125 ||
5984 wpa_s->conf->p2p_oper_reg_class == 126 ||
5985 wpa_s->conf->p2p_oper_reg_class == 127) &&
5986 wpas_p2p_supported_freq_go(wpa_s, channels,
5987 5000 +
5988 5 * wpa_s->conf->p2p_oper_channel)) {
5989 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5990 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5991 "frequency %d MHz", params->freq);
5992 goto success;
5993 }
5994
5995 /* Try using best channels */
5996 if (wpa_s->conf->p2p_oper_channel == 0 &&
5997 wpa_s->best_overall_freq > 0 &&
5998 wpas_p2p_supported_freq_go(wpa_s, channels,
5999 wpa_s->best_overall_freq)) {
6000 params->freq = wpa_s->best_overall_freq;
6001 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6002 "channel %d MHz", params->freq);
6003 goto success;
6004 }
6005
6006 if (wpa_s->conf->p2p_oper_channel == 0 &&
6007 wpa_s->best_24_freq > 0 &&
6008 wpas_p2p_supported_freq_go(wpa_s, channels,
6009 wpa_s->best_24_freq)) {
6010 params->freq = wpa_s->best_24_freq;
6011 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6012 "channel %d MHz", params->freq);
6013 goto success;
6014 }
6015
6016 if (wpa_s->conf->p2p_oper_channel == 0 &&
6017 wpa_s->best_5_freq > 0 &&
6018 wpas_p2p_supported_freq_go(wpa_s, channels,
6019 wpa_s->best_5_freq)) {
6020 params->freq = wpa_s->best_5_freq;
6021 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6022 "channel %d MHz", params->freq);
6023 goto success;
6024 }
6025
6026 /* try using preferred channels */
6027 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6028 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6029 params->freq = cand;
6030 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6031 "channels", params->freq);
6032 goto success;
6033 }
6034
Roshan Pius3a1667e2018-07-03 15:17:14 -07006035 /* Try using a channel that allows VHT to be used with 80 MHz */
6036 if (wpa_s->hw.modes && wpa_s->p2p_group_common_freqs) {
6037 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6038 enum hostapd_hw_mode mode;
6039 struct hostapd_hw_modes *hwmode;
6040 u8 chan;
6041
6042 cand = wpa_s->p2p_group_common_freqs[i];
6043 mode = ieee80211_freq_to_chan(cand, &chan);
6044 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6045 mode);
6046 if (!hwmode ||
6047 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6048 BW80) != ALLOWED)
6049 continue;
6050 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6051 params->freq = cand;
6052 wpa_printf(MSG_DEBUG,
6053 "P2P: Use freq %d MHz common with the peer and allowing VHT80",
6054 params->freq);
6055 goto success;
6056 }
6057 }
6058 }
6059
6060 /* Try using a channel that allows HT to be used with 40 MHz on the same
6061 * band so that CSA can be used */
6062 if (wpa_s->current_ssid && wpa_s->hw.modes &&
6063 wpa_s->p2p_group_common_freqs) {
6064 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6065 enum hostapd_hw_mode mode;
6066 struct hostapd_hw_modes *hwmode;
6067 u8 chan;
6068
6069 cand = wpa_s->p2p_group_common_freqs[i];
6070 mode = ieee80211_freq_to_chan(cand, &chan);
6071 hwmode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes,
6072 mode);
6073 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6074 cand) ||
6075 !hwmode ||
6076 (wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6077 BW40MINUS) != ALLOWED &&
6078 wpas_p2p_verify_channel(wpa_s, hwmode, chan,
6079 BW40PLUS) != ALLOWED))
6080 continue;
6081 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6082 params->freq = cand;
6083 wpa_printf(MSG_DEBUG,
6084 "P2P: Use freq %d MHz common with the peer, allowing HT40, and maintaining same band",
6085 params->freq);
6086 goto success;
6087 }
6088 }
6089 }
6090
6091 /* Try using one of the group common freqs on the same band so that CSA
6092 * can be used */
6093 if (wpa_s->current_ssid && wpa_s->p2p_group_common_freqs) {
6094 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6095 cand = wpa_s->p2p_group_common_freqs[i];
6096 if (!wpas_same_band(wpa_s->current_ssid->frequency,
6097 cand))
6098 continue;
6099 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6100 params->freq = cand;
6101 wpa_printf(MSG_DEBUG,
6102 "P2P: Use freq %d MHz common with the peer and maintaining same band",
6103 params->freq);
6104 goto success;
6105 }
6106 }
6107 }
6108
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006109 /* Try using one of the group common freqs */
6110 if (wpa_s->p2p_group_common_freqs) {
6111 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6112 cand = wpa_s->p2p_group_common_freqs[i];
6113 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6114 params->freq = cand;
6115 wpa_printf(MSG_DEBUG,
6116 "P2P: Use freq %d MHz common with the peer",
6117 params->freq);
6118 goto success;
6119 }
6120 }
6121 }
6122
6123 /* no preference, select some channel */
6124 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6125
6126 if (params->freq == 0) {
6127 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6128 goto fail;
6129 }
6130
6131success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006132 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006133 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006134fail:
6135 os_free(freqs);
6136 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006137}
6138
6139
6140static struct wpa_supplicant *
6141wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6142 int go)
6143{
6144 struct wpa_supplicant *group_wpa_s;
6145
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006146 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006147 if (wpa_s->p2p_mgmt) {
6148 /*
6149 * We may be called on the p2p_dev interface which
6150 * cannot be used for group operations, so always use
6151 * the primary interface.
6152 */
6153 wpa_s->parent->p2pdev = wpa_s;
6154 wpa_s = wpa_s->parent;
6155 }
6156 wpa_dbg(wpa_s, MSG_DEBUG,
6157 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006158 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006159 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006160 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006162 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163
6164 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006165 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006166 wpa_msg_global(wpa_s, MSG_ERROR,
6167 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006168 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006169 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6171 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006172 wpa_msg_global(wpa_s, MSG_ERROR,
6173 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174 wpas_p2p_remove_pending_group_interface(wpa_s);
6175 return NULL;
6176 }
6177
Roshan Pius3a1667e2018-07-03 15:17:14 -07006178 if (go && wpa_s->p2p_go_do_acs) {
6179 group_wpa_s->p2p_go_do_acs = wpa_s->p2p_go_do_acs;
6180 group_wpa_s->p2p_go_acs_band = wpa_s->p2p_go_acs_band;
6181 wpa_s->p2p_go_do_acs = 0;
6182 }
6183
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006184 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6185 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006186 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187 return group_wpa_s;
6188}
6189
6190
6191/**
6192 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6193 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6194 * @persistent_group: Whether to create a persistent group
6195 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006196 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006197 * @ht40: Start GO with 40 MHz channel width
6198 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006199 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 * Returns: 0 on success, -1 on failure
6201 *
6202 * This function creates a new P2P group with the local end as the Group Owner,
6203 * i.e., without using Group Owner Negotiation.
6204 */
6205int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006206 int freq, int vht_center_freq2, int ht40, int vht,
6207 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006208{
6209 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210
6211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6212 return -1;
6213
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006214 os_free(wpa_s->global->add_psk);
6215 wpa_s->global->add_psk = NULL;
6216
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006217 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006219 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006220
Roshan Pius3a1667e2018-07-03 15:17:14 -07006221 if (!wpa_s->p2p_go_do_acs) {
6222 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6223 if (freq < 0)
6224 return -1;
6225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006226
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006227 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6228 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006229 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006231 p2p_go_params(wpa_s->global->p2p, &params);
6232 params.persistent_group = persistent_group;
6233
6234 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6235 if (wpa_s == NULL)
6236 return -1;
6237 wpas_start_wps_go(wpa_s, &params, 0);
6238
6239 return 0;
6240}
6241
6242
6243static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006244 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006245 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246{
6247 struct wpa_ssid *ssid;
6248
6249 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6250 if (wpa_s == NULL)
6251 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006252 if (force_scan)
6253 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006254 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255
6256 wpa_supplicant_ap_deinit(wpa_s);
6257
6258 ssid = wpa_config_add_network(wpa_s->conf);
6259 if (ssid == NULL)
6260 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006261 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006262 wpa_config_set_network_defaults(ssid);
6263 ssid->temporary = 1;
6264 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006265 ssid->pbss = params->pbss;
6266 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6267 WPA_CIPHER_CCMP;
6268 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6270 ssid->ssid = os_malloc(params->ssid_len);
6271 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 wpa_config_remove_network(wpa_s->conf, ssid->id);
6273 return -1;
6274 }
6275 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6276 ssid->ssid_len = params->ssid_len;
6277 ssid->p2p_group = 1;
6278 ssid->export_keys = 1;
6279 if (params->psk_set) {
6280 os_memcpy(ssid->psk, params->psk, 32);
6281 ssid->psk_set = 1;
6282 }
6283 if (params->passphrase)
6284 ssid->passphrase = os_strdup(params->passphrase);
6285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006286 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006287 wpa_s->p2p_in_invitation = 1;
6288 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006289 wpa_s->p2p_go_group_formation_completed = 0;
6290 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006292 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006293 NULL);
6294 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6295 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006296 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006297 wpa_supplicant_select_network(wpa_s, ssid);
6298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006299 return 0;
6300}
6301
6302
6303int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6304 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006305 int force_freq, int neg_freq,
6306 int vht_center_freq2, int ht40,
6307 int vht, int max_oper_chwidth,
6308 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006309 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006310{
6311 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006312 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006313
6314 if (ssid->disabled != 2 || ssid->ssid == NULL)
6315 return -1;
6316
6317 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6318 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6319 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6320 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006321 if (go == 0 &&
6322 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006323 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006324 /*
6325 * This can happen if Invitation Response frame was lost
6326 * and the peer (GO of a persistent group) tries to
6327 * invite us again. Reschedule the timeout to avoid
6328 * terminating the wait for the connection too early
6329 * since we now know that the peer is still trying to
6330 * invite us instead of having already started the GO.
6331 */
6332 wpa_printf(MSG_DEBUG,
6333 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6334 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6335 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006336 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006337 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338 return 0;
6339 }
6340
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006341 os_free(wpa_s->global->add_psk);
6342 wpa_s->global->add_psk = NULL;
6343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006344 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006345 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006346
Dmitry Shmidt04949592012-07-19 12:16:46 -07006347 wpa_s->p2p_fallback_to_go_neg = 0;
6348
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006349 if (ssid->mode == WPAS_MODE_P2P_GO) {
6350 if (force_freq > 0) {
6351 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6352 if (freq < 0)
6353 return -1;
6354 } else {
6355 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6356 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006357 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006358 freq = 0;
6359 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006360 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006361 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006362 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006363 struct os_reltime now;
6364 struct wpa_bss *bss =
6365 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006366
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006367 os_get_reltime(&now);
6368 if (bss &&
6369 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006370 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006371 freq = bss->freq;
6372 else
6373 freq = 0;
6374 }
6375
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006376 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6377 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006378 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006379 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006380 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006381
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006382 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6383 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006384 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006385
6386 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006387 params.psk_set = ssid->psk_set;
6388 if (params.psk_set)
6389 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006390 if (ssid->passphrase) {
6391 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6392 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6393 "persistent group");
6394 return -1;
6395 }
6396 os_strlcpy(params.passphrase, ssid->passphrase,
6397 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006399 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6400 params.ssid_len = ssid->ssid_len;
6401 params.persistent_group = 1;
6402
6403 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6404 if (wpa_s == NULL)
6405 return -1;
6406
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006407 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6408
Dmitry Shmidt56052862013-10-04 10:23:25 -07006409 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006410 wpas_start_wps_go(wpa_s, &params, 0);
6411
6412 return 0;
6413}
6414
6415
6416static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6417 struct wpabuf *proberesp_ies)
6418{
6419 struct wpa_supplicant *wpa_s = ctx;
6420 if (wpa_s->ap_iface) {
6421 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006422 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6423 wpabuf_free(beacon_ies);
6424 wpabuf_free(proberesp_ies);
6425 return;
6426 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006427 if (beacon_ies) {
6428 wpabuf_free(hapd->p2p_beacon_ie);
6429 hapd->p2p_beacon_ie = beacon_ies;
6430 }
6431 wpabuf_free(hapd->p2p_probe_resp_ie);
6432 hapd->p2p_probe_resp_ie = proberesp_ies;
6433 } else {
6434 wpabuf_free(beacon_ies);
6435 wpabuf_free(proberesp_ies);
6436 }
6437 wpa_supplicant_ap_update_beacon(wpa_s);
6438}
6439
6440
6441static void wpas_p2p_idle_update(void *ctx, int idle)
6442{
6443 struct wpa_supplicant *wpa_s = ctx;
6444 if (!wpa_s->ap_iface)
6445 return;
6446 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006447 if (idle) {
6448 if (wpa_s->global->p2p_fail_on_wps_complete &&
6449 wpa_s->p2p_in_provisioning) {
6450 wpas_p2p_grpform_fail_after_wps(wpa_s);
6451 return;
6452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006453 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006454 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006455 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6456}
6457
6458
6459struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006460 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006461{
6462 struct p2p_group *group;
6463 struct p2p_group_config *cfg;
6464
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006465 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6466 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467 return NULL;
6468
6469 cfg = os_zalloc(sizeof(*cfg));
6470 if (cfg == NULL)
6471 return NULL;
6472
Dmitry Shmidt04949592012-07-19 12:16:46 -07006473 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006474 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006475 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006476 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006477 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6478 if (wpa_s->max_stations &&
6479 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6480 cfg->max_clients = wpa_s->max_stations;
6481 else
6482 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006483 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6484 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006485 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006486 cfg->cb_ctx = wpa_s;
6487 cfg->ie_update = wpas_p2p_ie_update;
6488 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006489 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6490 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491
6492 group = p2p_group_init(wpa_s->global->p2p, cfg);
6493 if (group == NULL)
6494 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006495 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006496 p2p_group_notif_formation_done(group);
6497 wpa_s->p2p_group = group;
6498 return group;
6499}
6500
6501
6502void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6503 int registrar)
6504{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006505 struct wpa_ssid *ssid = wpa_s->current_ssid;
6506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 if (!wpa_s->p2p_in_provisioning) {
6508 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6509 "provisioning not in progress");
6510 return;
6511 }
6512
Dmitry Shmidt04949592012-07-19 12:16:46 -07006513 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6514 u8 go_dev_addr[ETH_ALEN];
6515 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6516 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6517 ssid->ssid_len);
6518 /* Clear any stored provisioning info */
6519 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6520 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006521
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006522 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006524 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006525 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6526 /*
6527 * Use a separate timeout for initial data connection to
6528 * complete to allow the group to be removed automatically if
6529 * something goes wrong in this step before the P2P group idle
6530 * timeout mechanism is taken into use.
6531 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006532 wpa_dbg(wpa_s, MSG_DEBUG,
6533 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6534 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006535 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6536 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006537 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006538 /* Complete group formation on successful data connection. */
6539 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006540 } else if (ssid) {
6541 /*
6542 * Use a separate timeout for initial data connection to
6543 * complete to allow the group to be removed automatically if
6544 * the client does not complete data connection successfully.
6545 */
6546 wpa_dbg(wpa_s, MSG_DEBUG,
6547 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6548 P2P_MAX_INITIAL_CONN_WAIT_GO);
6549 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6550 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006551 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006552 /*
6553 * Complete group formation on first successful data connection
6554 */
6555 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006556 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006557 if (wpa_s->global->p2p)
6558 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006559 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560}
6561
6562
Jouni Malinen75ecf522011-06-27 15:19:46 -07006563void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6564 struct wps_event_fail *fail)
6565{
6566 if (!wpa_s->p2p_in_provisioning) {
6567 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6568 "provisioning not in progress");
6569 return;
6570 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006571
6572 if (wpa_s->go_params) {
6573 p2p_clear_provisioning_info(
6574 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006575 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006576 }
6577
Jouni Malinen75ecf522011-06-27 15:19:46 -07006578 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006579
6580 if (wpa_s == wpa_s->global->p2p_group_formation) {
6581 /*
6582 * Allow some time for the failed WPS negotiation exchange to
6583 * complete, but remove the group since group formation cannot
6584 * succeed after provisioning failure.
6585 */
6586 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6587 wpa_s->global->p2p_fail_on_wps_complete = 1;
6588 eloop_deplete_timeout(0, 50000,
6589 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006590 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006591 }
6592}
6593
6594
6595int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6596{
6597 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6598 !wpa_s->p2p_in_provisioning)
6599 return 0;
6600
6601 wpas_p2p_grpform_fail_after_wps(wpa_s);
6602
6603 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006604}
6605
6606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006608 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006609 enum wpas_p2p_prov_disc_use use,
6610 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611{
6612 u16 config_methods;
6613
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006614 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006615 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006616 wpa_s->p2p_fallback_to_go_neg = 0;
6617 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006618 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6619 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006620 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6621 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6622
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006623 wpa_printf(MSG_DEBUG,
6624 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6625 __func__, p2ps_prov->conncap,
6626 p2ps_prov->adv_id, p2ps_prov->conncap,
6627 p2ps_prov->status, p2ps_prov->info);
6628
6629 config_methods = 0;
6630 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006631 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006632 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006634 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6635 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006636 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006637 else {
6638 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006639 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006641 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006642
Dmitry Shmidt04949592012-07-19 12:16:46 -07006643 if (use == WPAS_P2P_PD_AUTO) {
6644 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6645 wpa_s->pending_pd_config_methods = config_methods;
6646 wpa_s->p2p_auto_pd = 1;
6647 wpa_s->p2p_auto_join = 0;
6648 wpa_s->pending_pd_before_join = 0;
6649 wpa_s->auto_pd_scan_retry = 0;
6650 wpas_p2p_stop_find(wpa_s);
6651 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006652 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006653 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6654 wpa_s->p2p_auto_started.sec,
6655 wpa_s->p2p_auto_started.usec);
6656 wpas_p2p_join_scan(wpa_s, NULL);
6657 return 0;
6658 }
6659
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006660 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6661 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006662 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006663 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006665 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006666 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006667 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006668}
6669
6670
6671int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6672 char *end)
6673{
6674 return p2p_scan_result_text(ies, ies_len, buf, end);
6675}
6676
6677
6678static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6679{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006680 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006681 return;
6682
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006683 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006684 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006685 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6686 wpa_s, NULL);
6687 offchannel_send_action_done(wpa_s);
6688 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6691 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006692 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006693}
6694
6695
6696int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6697 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006698 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006699 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006700 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006701{
6702 wpas_p2p_clear_pending_action_tx(wpa_s);
6703 wpa_s->p2p_long_listen = 0;
6704
Dmitry Shmidt04949592012-07-19 12:16:46 -07006705 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006706 wpa_s->p2p_in_provisioning) {
6707 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6708 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6709 " (P2P disabled)" : "",
6710 wpa_s->p2p_in_provisioning ?
6711 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006712 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006714
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006715 wpa_supplicant_cancel_sched_scan(wpa_s);
6716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006717 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006718 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006719 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006720}
6721
6722
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006723static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6724 struct wpa_scan_results *scan_res)
6725{
6726 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6727
6728 if (wpa_s->p2p_scan_work) {
6729 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6730 wpa_s->p2p_scan_work = NULL;
6731 radio_work_done(work);
6732 }
6733
6734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6735 return;
6736
6737 /*
6738 * Indicate that results have been processed so that the P2P module can
6739 * continue pending tasks.
6740 */
6741 p2p_scan_res_handled(wpa_s->global->p2p);
6742}
6743
6744
6745static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006746{
6747 wpas_p2p_clear_pending_action_tx(wpa_s);
6748 wpa_s->p2p_long_listen = 0;
6749 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6750 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006751
6752 if (wpa_s->global->p2p)
6753 p2p_stop_find(wpa_s->global->p2p);
6754
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006755 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6756 wpa_printf(MSG_DEBUG,
6757 "P2P: Do not consider the scan results after stop_find");
6758 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6759 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006760}
6761
6762
6763void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6764{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006765 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006766 if (!wpa_s->global->pending_group_iface_for_p2ps)
6767 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006768}
6769
6770
6771static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6772{
6773 struct wpa_supplicant *wpa_s = eloop_ctx;
6774 wpa_s->p2p_long_listen = 0;
6775}
6776
6777
6778int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6779{
6780 int res;
6781
6782 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6783 return -1;
6784
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006785 if (wpa_s->p2p_lo_started) {
6786 wpa_printf(MSG_DEBUG,
6787 "P2P: Cannot start P2P listen, it is offloaded");
6788 return -1;
6789 }
6790
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006791 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006792 wpas_p2p_clear_pending_action_tx(wpa_s);
6793
6794 if (timeout == 0) {
6795 /*
6796 * This is a request for unlimited Listen state. However, at
6797 * least for now, this is mapped to a Listen state for one
6798 * hour.
6799 */
6800 timeout = 3600;
6801 }
6802 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6803 wpa_s->p2p_long_listen = 0;
6804
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006805 /*
6806 * Stop previous find/listen operation to avoid trying to request a new
6807 * remain-on-channel operation while the driver is still running the
6808 * previous one.
6809 */
6810 if (wpa_s->global->p2p)
6811 p2p_stop_find(wpa_s->global->p2p);
6812
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006813 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6814 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6815 wpa_s->p2p_long_listen = timeout * 1000;
6816 eloop_register_timeout(timeout, 0,
6817 wpas_p2p_long_listen_timeout,
6818 wpa_s, NULL);
6819 }
6820
6821 return res;
6822}
6823
6824
6825int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6826 u8 *buf, size_t len, int p2p_group)
6827{
6828 struct wpabuf *p2p_ie;
6829 int ret;
6830
6831 if (wpa_s->global->p2p_disabled)
6832 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006833 /*
6834 * Advertize mandatory cross connection capability even on
6835 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6836 */
6837 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006838 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006839 if (wpa_s->global->p2p == NULL)
6840 return -1;
6841 if (bss == NULL)
6842 return -1;
6843
6844 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6845 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6846 p2p_group, p2p_ie);
6847 wpabuf_free(p2p_ie);
6848
6849 return ret;
6850}
6851
6852
6853int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006854 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006855 const u8 *ie, size_t ie_len,
6856 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006857{
6858 if (wpa_s->global->p2p_disabled)
6859 return 0;
6860 if (wpa_s->global->p2p == NULL)
6861 return 0;
6862
Dmitry Shmidt04949592012-07-19 12:16:46 -07006863 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006864 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006865 case P2P_PREQ_NOT_P2P:
6866 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6867 ssi_signal);
6868 /* fall through */
6869 case P2P_PREQ_MALFORMED:
6870 case P2P_PREQ_NOT_LISTEN:
6871 case P2P_PREQ_NOT_PROCESSED:
6872 default: /* make gcc happy */
6873 return 0;
6874 case P2P_PREQ_PROCESSED:
6875 return 1;
6876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006877}
6878
6879
6880void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6881 const u8 *sa, const u8 *bssid,
6882 u8 category, const u8 *data, size_t len, int freq)
6883{
6884 if (wpa_s->global->p2p_disabled)
6885 return;
6886 if (wpa_s->global->p2p == NULL)
6887 return;
6888
6889 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6890 freq);
6891}
6892
6893
6894void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6895{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006896 unsigned int bands;
6897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006898 if (wpa_s->global->p2p_disabled)
6899 return;
6900 if (wpa_s->global->p2p == NULL)
6901 return;
6902
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006903 bands = wpas_get_bands(wpa_s, NULL);
6904 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905}
6906
6907
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006908static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006909{
6910 p2p_group_deinit(wpa_s->p2p_group);
6911 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006912
6913 wpa_s->ap_configured_cb = NULL;
6914 wpa_s->ap_configured_cb_ctx = NULL;
6915 wpa_s->ap_configured_cb_data = NULL;
6916 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006917}
6918
6919
6920int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6921{
6922 wpa_s->p2p_long_listen = 0;
6923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6925 return -1;
6926
6927 return p2p_reject(wpa_s->global->p2p, addr);
6928}
6929
6930
6931/* Invite to reinvoke a persistent group */
6932int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006933 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006934 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6935 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936{
6937 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006938 u8 *bssid = NULL;
6939 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006940 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006941 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006942 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006943
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006944 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006945 if (peer_addr)
6946 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6947 else
6948 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006949
Jouni Malinen31be0a42012-08-31 21:20:51 +03006950 wpa_s->p2p_persistent_go_freq = freq;
6951 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006952 wpa_s->p2p_go_vht = !!vht;
6953 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
6954 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955 if (ssid->mode == WPAS_MODE_P2P_GO) {
6956 role = P2P_INVITE_ROLE_GO;
6957 if (peer_addr == NULL) {
6958 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6959 "address in invitation command");
6960 return -1;
6961 }
6962 if (wpas_p2p_create_iface(wpa_s)) {
6963 if (wpas_p2p_add_group_interface(wpa_s,
6964 WPA_IF_P2P_GO) < 0) {
6965 wpa_printf(MSG_ERROR, "P2P: Failed to "
6966 "allocate a new interface for the "
6967 "group");
6968 return -1;
6969 }
6970 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006971 } else if (wpa_s->p2p_mgmt)
6972 bssid = wpa_s->parent->own_addr;
6973 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974 bssid = wpa_s->own_addr;
6975 } else {
6976 role = P2P_INVITE_ROLE_CLIENT;
6977 peer_addr = ssid->bssid;
6978 }
6979 wpa_s->pending_invite_ssid_id = ssid->id;
6980
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006981 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006982 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006983 role == P2P_INVITE_ROLE_GO,
6984 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006985 if (res)
6986 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6989 return -1;
6990
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006991 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6992
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006993 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6994 no_pref_freq_given && pref_freq > 0 &&
6995 wpa_s->num_multichan_concurrent > 1 &&
6996 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6997 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6998 pref_freq);
6999 pref_freq = 0;
7000 }
7001
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007002 /*
7003 * Stop any find/listen operations before invitation and possibly
7004 * connection establishment.
7005 */
7006 wpas_p2p_stop_find_oper(wpa_s);
7007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007009 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007010 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007011}
7012
7013
7014/* Invite to join an active group */
7015int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7016 const u8 *peer_addr, const u8 *go_dev_addr)
7017{
7018 struct wpa_global *global = wpa_s->global;
7019 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007020 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007021 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007022 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007023 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007024 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007025 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007026
Jouni Malinen31be0a42012-08-31 21:20:51 +03007027 wpa_s->p2p_persistent_go_freq = 0;
7028 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007029 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007030 wpa_s->p2p_go_vht_center_freq2 = 0;
7031 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007032
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7034 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7035 break;
7036 }
7037 if (wpa_s == NULL) {
7038 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7039 return -1;
7040 }
7041
7042 ssid = wpa_s->current_ssid;
7043 if (ssid == NULL) {
7044 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7045 "invitation");
7046 return -1;
7047 }
7048
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007049 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007050 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007051 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007052 ssid->ssid, ssid->ssid_len);
7053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007054 if (ssid->mode == WPAS_MODE_P2P_GO) {
7055 role = P2P_INVITE_ROLE_ACTIVE_GO;
7056 bssid = wpa_s->own_addr;
7057 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007058 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007059 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007060 } else {
7061 role = P2P_INVITE_ROLE_CLIENT;
7062 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7063 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7064 "invite to current group");
7065 return -1;
7066 }
7067 bssid = wpa_s->bssid;
7068 if (go_dev_addr == NULL &&
7069 !is_zero_ether_addr(wpa_s->go_dev_addr))
7070 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007071 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7072 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007073 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007074 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7077 return -1;
7078
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007079 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007080 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007081 role == P2P_INVITE_ROLE_ACTIVE_GO,
7082 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007083 if (res)
7084 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007085 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007087 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007088 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007089 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007090}
7091
7092
7093void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7094{
7095 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007096 u8 go_dev_addr[ETH_ALEN];
7097 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007098 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007099 u8 ip[3 * 4];
7100 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007101
Dmitry Shmidt04949592012-07-19 12:16:46 -07007102 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7103 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007104 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007105 }
7106
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007108 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007109
7110 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007111 if (!wpa_s->p2p_go_group_formation_completed &&
7112 wpa_s->global->p2p_group_formation == wpa_s) {
7113 wpa_dbg(wpa_s, MSG_DEBUG,
7114 "P2P: Marking group formation completed on client on data connection");
7115 wpa_s->p2p_go_group_formation_completed = 1;
7116 wpa_s->global->p2p_group_formation = NULL;
7117 wpa_s->p2p_in_provisioning = 0;
7118 wpa_s->p2p_in_invitation = 0;
7119 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007121 os_memset(go_dev_addr, 0, ETH_ALEN);
7122 if (ssid->bssid_set)
7123 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7124 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7125 ssid->ssid_len);
7126 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7127
7128 if (wpa_s->global->p2p_group_formation == wpa_s)
7129 wpa_s->global->p2p_group_formation = NULL;
7130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007131 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7132 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007133
7134 ip_addr[0] = '\0';
7135 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007136 int res;
7137
7138 res = os_snprintf(ip_addr, sizeof(ip_addr),
7139 " ip_addr=%u.%u.%u.%u "
7140 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7141 ip[0], ip[1], ip[2], ip[3],
7142 ip[4], ip[5], ip[6], ip[7],
7143 ip[8], ip[9], ip[10], ip[11]);
7144 if (os_snprintf_error(sizeof(ip_addr), res))
7145 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007146 }
7147
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007148 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7149 ssid->passphrase == NULL && ssid->psk_set ?
7150 ssid->psk : NULL,
7151 ssid->passphrase, go_dev_addr, persistent,
7152 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007153
7154 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007155 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7156 ssid, go_dev_addr);
7157
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007158 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007159}
7160
7161
7162int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7163 u32 interval1, u32 duration2, u32 interval2)
7164{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007165 int ret;
7166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7168 return -1;
7169
7170 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7171 wpa_s->current_ssid == NULL ||
7172 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7173 return -1;
7174
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007175 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7176 wpa_s->own_addr, wpa_s->assoc_freq,
7177 duration1, interval1, duration2, interval2);
7178 if (ret == 0)
7179 wpa_s->waiting_presence_resp = 1;
7180
7181 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007182}
7183
7184
7185int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7186 unsigned int interval)
7187{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007188 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7189 return -1;
7190
7191 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7192}
7193
7194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007195static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7196{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007197 if (wpa_s->current_ssid == NULL) {
7198 /*
7199 * current_ssid can be cleared when P2P client interface gets
7200 * disconnected, so assume this interface was used as P2P
7201 * client.
7202 */
7203 return 1;
7204 }
7205 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007206 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7207}
7208
7209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007210static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7211{
7212 struct wpa_supplicant *wpa_s = eloop_ctx;
7213
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007214 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007215 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7216 "disabled");
7217 return;
7218 }
7219
Dmitry Shmidt04949592012-07-19 12:16:46 -07007220 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7221 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007222 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007223}
7224
7225
7226static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7227{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007228 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007229
Dmitry Shmidt04949592012-07-19 12:16:46 -07007230 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7231 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007233 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7234 return;
7235
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007236 timeout = wpa_s->conf->p2p_group_idle;
7237 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7238 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7239 timeout = P2P_MAX_CLIENT_IDLE;
7240
7241 if (timeout == 0)
7242 return;
7243
Dmitry Shmidt04949592012-07-19 12:16:46 -07007244 if (timeout < 0) {
7245 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7246 timeout = 0; /* special client mode no-timeout */
7247 else
7248 return;
7249 }
7250
7251 if (wpa_s->p2p_in_provisioning) {
7252 /*
7253 * Use the normal group formation timeout during the
7254 * provisioning phase to avoid terminating this process too
7255 * early due to group idle timeout.
7256 */
7257 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7258 "during provisioning");
7259 return;
7260 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307261
Dmitry Shmidt04949592012-07-19 12:16:46 -07007262 if (wpa_s->show_group_started) {
7263 /*
7264 * Use the normal group formation timeout between the end of
7265 * the provisioning phase and completion of 4-way handshake to
7266 * avoid terminating this process too early due to group idle
7267 * timeout.
7268 */
7269 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7270 "while waiting for initial 4-way handshake to "
7271 "complete");
7272 return;
7273 }
7274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007275 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007276 timeout);
7277 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7278 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007279}
7280
7281
Jouni Malinen2b89da82012-08-31 22:04:41 +03007282/* Returns 1 if the interface was removed */
7283int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7284 u16 reason_code, const u8 *ie, size_t ie_len,
7285 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007286{
7287 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007288 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007289
Dmitry Shmidt04949592012-07-19 12:16:46 -07007290 if (!locally_generated)
7291 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7292 ie_len);
7293
7294 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7295 wpa_s->current_ssid &&
7296 wpa_s->current_ssid->p2p_group &&
7297 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7298 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7299 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007300 if (wpas_p2p_group_delete(wpa_s,
7301 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7302 > 0)
7303 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007304 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007305
7306 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007307}
7308
7309
7310void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007311 u16 reason_code, const u8 *ie, size_t ie_len,
7312 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007313{
7314 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7315 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007316
Dmitry Shmidt04949592012-07-19 12:16:46 -07007317 if (!locally_generated)
7318 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7319 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007320}
7321
7322
7323void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7324{
7325 struct p2p_data *p2p = wpa_s->global->p2p;
7326
7327 if (p2p == NULL)
7328 return;
7329
7330 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7331 return;
7332
7333 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7334 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7335
7336 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7337 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7338
7339 if (wpa_s->wps &&
7340 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7341 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7342
7343 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7344 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7345
7346 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7347 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7348 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7349 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7350 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7351 }
7352
7353 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7354 p2p_set_sec_dev_types(p2p,
7355 (void *) wpa_s->conf->sec_device_type,
7356 wpa_s->conf->num_sec_device_types);
7357
7358 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7359 int i;
7360 p2p_remove_wps_vendor_extensions(p2p);
7361 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7362 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7363 continue;
7364 p2p_add_wps_vendor_extension(
7365 p2p, wpa_s->conf->wps_vendor_ext[i]);
7366 }
7367 }
7368
7369 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7370 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7371 char country[3];
7372 country[0] = wpa_s->conf->country[0];
7373 country[1] = wpa_s->conf->country[1];
7374 country[2] = 0x04;
7375 p2p_set_country(p2p, country);
7376 }
7377
7378 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7379 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7380 wpa_s->conf->p2p_ssid_postfix ?
7381 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7382 0);
7383 }
7384
7385 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7386 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007387
7388 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7389 u8 reg_class, channel;
7390 int ret;
7391 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007392 u8 channel_forced;
7393
Jouni Malinen75ecf522011-06-27 15:19:46 -07007394 if (wpa_s->conf->p2p_listen_reg_class &&
7395 wpa_s->conf->p2p_listen_channel) {
7396 reg_class = wpa_s->conf->p2p_listen_reg_class;
7397 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007398 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007399 } else {
7400 reg_class = 81;
7401 /*
7402 * Pick one of the social channels randomly as the
7403 * listen channel.
7404 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007405 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7406 channel = 1;
7407 else
7408 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007409 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007410 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007411 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7412 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007413 if (ret)
7414 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7415 "failed: %d", ret);
7416 }
7417 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7418 u8 op_reg_class, op_channel, cfg_op_channel;
7419 int ret = 0;
7420 unsigned int r;
7421 if (wpa_s->conf->p2p_oper_reg_class &&
7422 wpa_s->conf->p2p_oper_channel) {
7423 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7424 op_channel = wpa_s->conf->p2p_oper_channel;
7425 cfg_op_channel = 1;
7426 } else {
7427 op_reg_class = 81;
7428 /*
7429 * Use random operation channel from (1, 6, 11)
7430 *if no other preference is indicated.
7431 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007432 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7433 op_channel = 1;
7434 else
7435 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007436 cfg_op_channel = 0;
7437 }
7438 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7439 cfg_op_channel);
7440 if (ret)
7441 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7442 "failed: %d", ret);
7443 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007444
7445 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7446 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7447 wpa_s->conf->p2p_pref_chan) < 0) {
7448 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7449 "update failed");
7450 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007451
7452 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7453 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7454 "update failed");
7455 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007456 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007457
7458 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7459 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007460}
7461
7462
7463int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7464 int duration)
7465{
7466 if (!wpa_s->ap_iface)
7467 return -1;
7468 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7469 duration);
7470}
7471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007472
7473int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7474{
7475 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7476 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007477
7478 wpa_s->global->cross_connection = enabled;
7479 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7480
7481 if (!enabled) {
7482 struct wpa_supplicant *iface;
7483
7484 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7485 {
7486 if (iface->cross_connect_enabled == 0)
7487 continue;
7488
7489 iface->cross_connect_enabled = 0;
7490 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007491 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007492 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7493 iface->ifname,
7494 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007495 }
7496 }
7497
7498 return 0;
7499}
7500
7501
7502static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7503{
7504 struct wpa_supplicant *iface;
7505
7506 if (!uplink->global->cross_connection)
7507 return;
7508
7509 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7510 if (!iface->cross_connect_enabled)
7511 continue;
7512 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7513 0)
7514 continue;
7515 if (iface->ap_iface == NULL)
7516 continue;
7517 if (iface->cross_connect_in_use)
7518 continue;
7519
7520 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007521 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007522 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7523 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524 }
7525}
7526
7527
7528static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7529{
7530 struct wpa_supplicant *iface;
7531
7532 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7533 if (!iface->cross_connect_enabled)
7534 continue;
7535 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7536 0)
7537 continue;
7538 if (!iface->cross_connect_in_use)
7539 continue;
7540
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007541 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007542 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7543 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007544 iface->cross_connect_in_use = 0;
7545 }
7546}
7547
7548
7549void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7550{
7551 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7552 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7553 wpa_s->cross_connect_disallowed)
7554 wpas_p2p_disable_cross_connect(wpa_s);
7555 else
7556 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007557 if (!wpa_s->ap_iface &&
7558 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7559 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007560}
7561
7562
7563void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7564{
7565 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007566 if (!wpa_s->ap_iface &&
7567 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7568 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007569 wpas_p2p_set_group_idle_timeout(wpa_s);
7570}
7571
7572
7573static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7574{
7575 struct wpa_supplicant *iface;
7576
7577 if (!wpa_s->global->cross_connection)
7578 return;
7579
7580 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7581 if (iface == wpa_s)
7582 continue;
7583 if (iface->drv_flags &
7584 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7585 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007586 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7587 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007588 continue;
7589
7590 wpa_s->cross_connect_enabled = 1;
7591 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7592 sizeof(wpa_s->cross_connect_uplink));
7593 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7594 "%s to %s whenever uplink is available",
7595 wpa_s->ifname, wpa_s->cross_connect_uplink);
7596
7597 if (iface->ap_iface || iface->current_ssid == NULL ||
7598 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7599 iface->cross_connect_disallowed ||
7600 iface->wpa_state != WPA_COMPLETED)
7601 break;
7602
7603 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007604 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007605 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7606 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607 break;
7608 }
7609}
7610
7611
7612int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7613{
7614 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7615 !wpa_s->p2p_in_provisioning)
7616 return 0; /* not P2P client operation */
7617
7618 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7619 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007620 if (wpa_s != wpa_s->p2pdev)
7621 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007622 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007623 return 1;
7624}
7625
7626
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007627void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7628{
7629 struct wpa_supplicant *wpa_s = eloop_ctx;
7630 wpas_p2p_notif_pbc_overlap(wpa_s);
7631}
7632
7633
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007634void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7635 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007636{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007637 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007638 struct wpa_used_freq_data *freqs = NULL;
7639 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007640
7641 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7642 return;
7643
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007644 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7645 if (!freqs)
7646 return;
7647
7648 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007650 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007651 os_memset(&cli_chan, 0, sizeof(cli_chan));
7652 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007653 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7654 "channel list");
7655 return;
7656 }
7657
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007658 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007659
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007660 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007661
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007662 /*
7663 * The used frequencies map changed, so it is possible that a GO is
7664 * using a channel that is no longer valid for P2P use. It is also
7665 * possible that due to policy consideration, it would be preferable to
7666 * move it to a frequency already used by other station interfaces.
7667 */
7668 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7669
7670 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007671}
7672
7673
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007674static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7675 struct wpa_scan_results *scan_res)
7676{
7677 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7678}
7679
7680
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007681int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7682{
7683 struct wpa_global *global = wpa_s->global;
7684 int found = 0;
7685 const u8 *peer;
7686
7687 if (global->p2p == NULL)
7688 return -1;
7689
7690 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7691
7692 if (wpa_s->pending_interface_name[0] &&
7693 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7694 found = 1;
7695
7696 peer = p2p_get_go_neg_peer(global->p2p);
7697 if (peer) {
7698 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7699 MACSTR, MAC2STR(peer));
7700 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007701 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007702 }
7703
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007704 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7705 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7706 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7707 found = 1;
7708 }
7709
7710 if (wpa_s->pending_pd_before_join) {
7711 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7712 wpa_s->pending_pd_before_join = 0;
7713 found = 1;
7714 }
7715
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007716 wpas_p2p_stop_find(wpa_s);
7717
7718 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7719 if (wpa_s == global->p2p_group_formation &&
7720 (wpa_s->p2p_in_provisioning ||
7721 wpa_s->parent->pending_interface_type ==
7722 WPA_IF_P2P_CLIENT)) {
7723 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7724 "formation found - cancelling",
7725 wpa_s->ifname);
7726 found = 1;
7727 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007728 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007729 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007730 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007731 break;
7732 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007733 wpas_p2p_group_delete(wpa_s,
7734 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007735 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007736 } else if (wpa_s->p2p_in_invitation) {
7737 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7738 wpa_s->ifname);
7739 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007740 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007741 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007742 }
7743 }
7744
7745 if (!found) {
7746 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7747 return -1;
7748 }
7749
7750 return 0;
7751}
7752
7753
7754void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7755{
7756 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7757 return;
7758
7759 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7760 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007761 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007762}
7763
7764
7765void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7766 int freq_24, int freq_5, int freq_overall)
7767{
7768 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007769 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007770 return;
7771 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7772}
7773
7774
7775int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7776{
7777 u8 peer[ETH_ALEN];
7778 struct p2p_data *p2p = wpa_s->global->p2p;
7779
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007780 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007781 return -1;
7782
7783 if (hwaddr_aton(addr, peer))
7784 return -1;
7785
7786 return p2p_unauthorize(p2p, peer);
7787}
7788
7789
7790/**
7791 * wpas_p2p_disconnect - Disconnect from a P2P Group
7792 * @wpa_s: Pointer to wpa_supplicant data
7793 * Returns: 0 on success, -1 on failure
7794 *
7795 * This can be used to disconnect from a group in which the local end is a P2P
7796 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7797 * virtual network interface was created for this group, that interface will be
7798 * removed. Otherwise, only the configured P2P group network will be removed
7799 * from the interface.
7800 */
7801int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7802{
7803
7804 if (wpa_s == NULL)
7805 return -1;
7806
Jouni Malinen2b89da82012-08-31 22:04:41 +03007807 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7808 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007809}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007810
7811
7812int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7813{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007814 int ret;
7815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007816 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7817 return 0;
7818
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007819 ret = p2p_in_progress(wpa_s->global->p2p);
7820 if (ret == 0) {
7821 /*
7822 * Check whether there is an ongoing WPS provisioning step (or
7823 * other parts of group formation) on another interface since
7824 * p2p_in_progress() does not report this to avoid issues for
7825 * scans during such provisioning step.
7826 */
7827 if (wpa_s->global->p2p_group_formation &&
7828 wpa_s->global->p2p_group_formation != wpa_s) {
7829 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7830 "in group formation",
7831 wpa_s->global->p2p_group_formation->ifname);
7832 ret = 1;
7833 }
7834 }
7835
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007836 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007837 struct os_reltime now;
7838 os_get_reltime(&now);
7839 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7840 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007841 /* Wait for the first client has expired */
7842 wpa_s->global->p2p_go_wait_client.sec = 0;
7843 } else {
7844 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7845 ret = 1;
7846 }
7847 }
7848
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007849 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007850}
7851
7852
7853void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7854 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007855{
7856 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7857 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007858 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007859 /**
7860 * Remove the network by scheduling the group formation
7861 * timeout to happen immediately. The teardown code
7862 * needs to be scheduled to run asynch later so that we
7863 * don't delete data from under ourselves unexpectedly.
7864 * Calling wpas_p2p_group_formation_timeout directly
7865 * causes a series of crashes in WPS failure scenarios.
7866 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007867 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7868 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007869 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007870 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007871 }
7872}
7873
7874
7875struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007876 const u8 *addr, const u8 *ssid,
7877 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007878{
7879 struct wpa_ssid *s;
7880 size_t i;
7881
7882 for (s = wpa_s->conf->ssid; s; s = s->next) {
7883 if (s->disabled != 2)
7884 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007885 if (ssid &&
7886 (ssid_len != s->ssid_len ||
7887 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7888 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007889 if (addr == NULL) {
7890 if (s->mode == WPAS_MODE_P2P_GO)
7891 return s;
7892 continue;
7893 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007894 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7895 return s; /* peer is GO in the persistent group */
7896 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7897 continue;
7898 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007899 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007900 addr, ETH_ALEN) == 0)
7901 return s; /* peer is P2P client in persistent
7902 * group */
7903 }
7904 }
7905
7906 return NULL;
7907}
7908
7909
7910void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7911 const u8 *addr)
7912{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007913 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007914 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007915 /*
7916 * This can happen if WPS provisioning step is not terminated
7917 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7918 * peer was able to connect, there is no need to time out group
7919 * formation after this, though. In addition, this is used with
7920 * the initial connection wait on the GO as a separate formation
7921 * timeout and as such, expected to be hit after the initial WPS
7922 * provisioning step.
7923 */
7924 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007925
7926 if (!wpa_s->p2p_go_group_formation_completed &&
7927 !wpa_s->group_formation_reported) {
7928 /*
7929 * GO has not yet notified group formation success since
7930 * the WPS step was not completed cleanly. Do that
7931 * notification now since the P2P Client was able to
7932 * connect and as such, must have received the
7933 * credential from the WPS step.
7934 */
7935 if (wpa_s->global->p2p)
7936 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007937 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007938 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007939 }
7940 if (!wpa_s->p2p_go_group_formation_completed) {
7941 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7942 wpa_s->p2p_go_group_formation_completed = 1;
7943 wpa_s->global->p2p_group_formation = NULL;
7944 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007945 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007946 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007947 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007948 if (addr == NULL)
7949 return;
7950 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7951}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007952
Dmitry Shmidt04949592012-07-19 12:16:46 -07007953
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007954static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7955 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007956{
7957 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007958 int ret = 0;
7959
Dmitry Shmidt04949592012-07-19 12:16:46 -07007960 if (wpa_s->global->p2p_group_formation)
7961 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007962 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007963 offchannel_send_action_done(wpa_s);
7964 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007965 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007966 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7967 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7968 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7969 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007970 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007971 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007972 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007973 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007974 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007975 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007976 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007977}
7978
7979
7980int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7981{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007982 int res;
7983
Dmitry Shmidt04949592012-07-19 12:16:46 -07007984 if (!wpa_s->p2p_fallback_to_go_neg ||
7985 wpa_s->p2p_in_provisioning <= 5)
7986 return 0;
7987
7988 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7989 return 0; /* peer operating as a GO */
7990
7991 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7992 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007993 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007994 "reason=GO-not-found");
7995 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007996
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007997 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007998}
7999
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008000
8001unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8002{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008003 struct wpa_supplicant *ifs;
8004
8005 if (wpa_s->wpa_state > WPA_SCANNING) {
8006 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8007 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008008 wpa_s->conf->p2p_search_delay);
8009 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008010 }
8011
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008012 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8013 radio_list) {
8014 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008015 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8016 "delay due to concurrent operation on "
8017 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008018 wpa_s->conf->p2p_search_delay,
8019 ifs->ifname);
8020 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008021 }
8022 }
8023
8024 return 0;
8025}
8026
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008027
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008028static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8029 struct wpa_ssid *s, const u8 *addr,
8030 int iface_addr)
8031{
8032 struct psk_list_entry *psk, *tmp;
8033 int changed = 0;
8034
8035 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8036 list) {
8037 if ((iface_addr && !psk->p2p &&
8038 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8039 (!iface_addr && psk->p2p &&
8040 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8041 wpa_dbg(wpa_s, MSG_DEBUG,
8042 "P2P: Remove persistent group PSK list entry for "
8043 MACSTR " p2p=%u",
8044 MAC2STR(psk->addr), psk->p2p);
8045 dl_list_del(&psk->list);
8046 os_free(psk);
8047 changed++;
8048 }
8049 }
8050
8051 return changed;
8052}
8053
8054
8055void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8056 const u8 *p2p_dev_addr,
8057 const u8 *psk, size_t psk_len)
8058{
8059 struct wpa_ssid *ssid = wpa_s->current_ssid;
8060 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008061 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008062
8063 if (psk_len != sizeof(p->psk))
8064 return;
8065
8066 if (p2p_dev_addr) {
8067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8068 " p2p_dev_addr=" MACSTR,
8069 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8070 if (is_zero_ether_addr(p2p_dev_addr))
8071 p2p_dev_addr = NULL;
8072 } else {
8073 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8074 MAC2STR(mac_addr));
8075 }
8076
8077 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8078 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8079 /* To be added to persistent group once created */
8080 if (wpa_s->global->add_psk == NULL) {
8081 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8082 if (wpa_s->global->add_psk == NULL)
8083 return;
8084 }
8085 p = wpa_s->global->add_psk;
8086 if (p2p_dev_addr) {
8087 p->p2p = 1;
8088 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8089 } else {
8090 p->p2p = 0;
8091 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8092 }
8093 os_memcpy(p->psk, psk, psk_len);
8094 return;
8095 }
8096
8097 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8099 return;
8100 }
8101
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008102 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008103 ssid->ssid_len);
8104 if (!persistent) {
8105 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8106 return;
8107 }
8108
8109 p = os_zalloc(sizeof(*p));
8110 if (p == NULL)
8111 return;
8112 if (p2p_dev_addr) {
8113 p->p2p = 1;
8114 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8115 } else {
8116 p->p2p = 0;
8117 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8118 }
8119 os_memcpy(p->psk, psk, psk_len);
8120
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008121 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8122 (last = dl_list_last(&persistent->psk_list,
8123 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008124 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8125 MACSTR " (p2p=%u) to make room for a new one",
8126 MAC2STR(last->addr), last->p2p);
8127 dl_list_del(&last->list);
8128 os_free(last);
8129 }
8130
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008131 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008132 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8133 p2p_dev_addr == NULL);
8134 if (p2p_dev_addr) {
8135 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8136 MACSTR, MAC2STR(p2p_dev_addr));
8137 } else {
8138 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8139 MAC2STR(mac_addr));
8140 }
8141 dl_list_add(&persistent->psk_list, &p->list);
8142
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008143 if (wpa_s->p2pdev->conf->update_config &&
8144 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008145 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008146}
8147
8148
8149static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8150 struct wpa_ssid *s, const u8 *addr,
8151 int iface_addr)
8152{
8153 int res;
8154
8155 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008156 if (res > 0 && wpa_s->conf->update_config &&
8157 wpa_config_write(wpa_s->confname, wpa_s->conf))
8158 wpa_dbg(wpa_s, MSG_DEBUG,
8159 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008160}
8161
8162
8163static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8164 const u8 *peer, int iface_addr)
8165{
8166 struct hostapd_data *hapd;
8167 struct hostapd_wpa_psk *psk, *prev, *rem;
8168 struct sta_info *sta;
8169
8170 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8171 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8172 return;
8173
8174 /* Remove per-station PSK entry */
8175 hapd = wpa_s->ap_iface->bss[0];
8176 prev = NULL;
8177 psk = hapd->conf->ssid.wpa_psk;
8178 while (psk) {
8179 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8180 (!iface_addr &&
8181 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8183 MACSTR " iface_addr=%d",
8184 MAC2STR(peer), iface_addr);
8185 if (prev)
8186 prev->next = psk->next;
8187 else
8188 hapd->conf->ssid.wpa_psk = psk->next;
8189 rem = psk;
8190 psk = psk->next;
8191 os_free(rem);
8192 } else {
8193 prev = psk;
8194 psk = psk->next;
8195 }
8196 }
8197
8198 /* Disconnect from group */
8199 if (iface_addr)
8200 sta = ap_get_sta(hapd, peer);
8201 else
8202 sta = ap_get_sta_p2p(hapd, peer);
8203 if (sta) {
8204 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8205 " (iface_addr=%d) from group",
8206 MAC2STR(peer), iface_addr);
8207 hostapd_drv_sta_deauth(hapd, sta->addr,
8208 WLAN_REASON_DEAUTH_LEAVING);
8209 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8210 }
8211}
8212
8213
8214void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8215 int iface_addr)
8216{
8217 struct wpa_ssid *s;
8218 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008219 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008220
8221 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8222
8223 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008224 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008225 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8226 continue;
8227 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008228 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8229 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008230 }
8231
8232 /* Remove from any operating group */
8233 for (w = wpa_s->global->ifaces; w; w = w->next)
8234 wpas_p2p_remove_client_go(w, peer, iface_addr);
8235}
8236
8237
8238static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8239{
8240 struct wpa_supplicant *wpa_s = eloop_ctx;
8241 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8242}
8243
8244
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008245static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8246{
8247 struct wpa_supplicant *wpa_s = eloop_ctx;
8248
8249 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8250 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8251}
8252
8253
8254int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8255 struct wpa_ssid *ssid)
8256{
8257 struct wpa_supplicant *iface;
8258
8259 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8260 if (!iface->current_ssid ||
8261 iface->current_ssid->frequency == freq ||
8262 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8263 !iface->current_ssid->p2p_group))
8264 continue;
8265
8266 /* Remove the connection with least priority */
8267 if (!wpas_is_p2p_prioritized(iface)) {
8268 /* STA connection has priority over existing
8269 * P2P connection, so remove the interface. */
8270 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8271 eloop_register_timeout(0, 0,
8272 wpas_p2p_group_freq_conflict,
8273 iface, NULL);
8274 /* If connection in progress is P2P connection, do not
8275 * proceed for the connection. */
8276 if (wpa_s == iface)
8277 return -1;
8278 else
8279 return 0;
8280 } else {
8281 /* P2P connection has priority, disable the STA network
8282 */
8283 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8284 ssid);
8285 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8286 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8287 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8288 ETH_ALEN);
8289 /* If P2P connection is in progress, continue
8290 * connecting...*/
8291 if (wpa_s == iface)
8292 return 0;
8293 else
8294 return -1;
8295 }
8296 }
8297
8298 return 0;
8299}
8300
8301
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008302int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8303{
8304 struct wpa_ssid *ssid = wpa_s->current_ssid;
8305
8306 if (ssid == NULL || !ssid->p2p_group)
8307 return 0;
8308
8309 if (wpa_s->p2p_last_4way_hs_fail &&
8310 wpa_s->p2p_last_4way_hs_fail == ssid) {
8311 u8 go_dev_addr[ETH_ALEN];
8312 struct wpa_ssid *persistent;
8313
8314 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8315 ssid->ssid,
8316 ssid->ssid_len) <= 0) {
8317 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8318 goto disconnect;
8319 }
8320
8321 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8322 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008323 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008324 ssid->ssid,
8325 ssid->ssid_len);
8326 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8327 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8328 goto disconnect;
8329 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008330 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008331 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8332 persistent->id);
8333 disconnect:
8334 wpa_s->p2p_last_4way_hs_fail = NULL;
8335 /*
8336 * Remove the group from a timeout to avoid issues with caller
8337 * continuing to use the interface if this is on a P2P group
8338 * interface.
8339 */
8340 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8341 wpa_s, NULL);
8342 return 1;
8343 }
8344
8345 wpa_s->p2p_last_4way_hs_fail = ssid;
8346 return 0;
8347}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008348
8349
8350#ifdef CONFIG_WPS_NFC
8351
8352static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8353 struct wpabuf *p2p)
8354{
8355 struct wpabuf *ret;
8356 size_t wsc_len;
8357
8358 if (p2p == NULL) {
8359 wpabuf_free(wsc);
8360 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8361 return NULL;
8362 }
8363
8364 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8365 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8366 if (ret == NULL) {
8367 wpabuf_free(wsc);
8368 wpabuf_free(p2p);
8369 return NULL;
8370 }
8371
8372 wpabuf_put_be16(ret, wsc_len);
8373 if (wsc)
8374 wpabuf_put_buf(ret, wsc);
8375 wpabuf_put_be16(ret, wpabuf_len(p2p));
8376 wpabuf_put_buf(ret, p2p);
8377
8378 wpabuf_free(wsc);
8379 wpabuf_free(p2p);
8380 wpa_hexdump_buf(MSG_DEBUG,
8381 "P2P: Generated NFC connection handover message", ret);
8382
8383 if (ndef && ret) {
8384 struct wpabuf *tmp;
8385 tmp = ndef_build_p2p(ret);
8386 wpabuf_free(ret);
8387 if (tmp == NULL) {
8388 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8389 return NULL;
8390 }
8391 ret = tmp;
8392 }
8393
8394 return ret;
8395}
8396
8397
8398static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8399 struct wpa_ssid **ssid, u8 *go_dev_addr)
8400{
8401 struct wpa_supplicant *iface;
8402
8403 if (go_dev_addr)
8404 os_memset(go_dev_addr, 0, ETH_ALEN);
8405 if (ssid)
8406 *ssid = NULL;
8407 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8408 if (iface->wpa_state < WPA_ASSOCIATING ||
8409 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8410 !iface->current_ssid->p2p_group ||
8411 iface->current_ssid->mode != WPAS_MODE_INFRA)
8412 continue;
8413 if (ssid)
8414 *ssid = iface->current_ssid;
8415 if (go_dev_addr)
8416 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8417 return iface->assoc_freq;
8418 }
8419 return 0;
8420}
8421
8422
8423struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8424 int ndef)
8425{
8426 struct wpabuf *wsc, *p2p;
8427 struct wpa_ssid *ssid;
8428 u8 go_dev_addr[ETH_ALEN];
8429 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8430
8431 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8432 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8433 return NULL;
8434 }
8435
8436 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8437 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8438 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8440 return NULL;
8441 }
8442
8443 if (cli_freq == 0) {
8444 wsc = wps_build_nfc_handover_req_p2p(
8445 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8446 } else
8447 wsc = NULL;
8448 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8449 go_dev_addr, ssid ? ssid->ssid : NULL,
8450 ssid ? ssid->ssid_len : 0);
8451
8452 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8453}
8454
8455
8456struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8457 int ndef, int tag)
8458{
8459 struct wpabuf *wsc, *p2p;
8460 struct wpa_ssid *ssid;
8461 u8 go_dev_addr[ETH_ALEN];
8462 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8463
8464 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8465 return NULL;
8466
8467 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8468 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8469 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8470 return NULL;
8471
8472 if (cli_freq == 0) {
8473 wsc = wps_build_nfc_handover_sel_p2p(
8474 wpa_s->parent->wps,
8475 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8476 DEV_PW_NFC_CONNECTION_HANDOVER,
8477 wpa_s->conf->wps_nfc_dh_pubkey,
8478 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8479 } else
8480 wsc = NULL;
8481 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8482 go_dev_addr, ssid ? ssid->ssid : NULL,
8483 ssid ? ssid->ssid_len : 0);
8484
8485 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8486}
8487
8488
8489static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8490 struct p2p_nfc_params *params)
8491{
8492 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8493 "connection handover (freq=%d)",
8494 params->go_freq);
8495
8496 if (params->go_freq && params->go_ssid_len) {
8497 wpa_s->p2p_wps_method = WPS_NFC;
8498 wpa_s->pending_join_wps_method = WPS_NFC;
8499 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8500 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8501 ETH_ALEN);
8502 return wpas_p2p_join_start(wpa_s, params->go_freq,
8503 params->go_ssid,
8504 params->go_ssid_len);
8505 }
8506
8507 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8508 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008509 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008510 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8511 params->go_ssid_len ? params->go_ssid : NULL,
8512 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008513}
8514
8515
8516static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8517 struct p2p_nfc_params *params, int tag)
8518{
8519 int res, persistent;
8520 struct wpa_ssid *ssid;
8521
8522 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8523 "connection handover");
8524 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8525 ssid = wpa_s->current_ssid;
8526 if (ssid == NULL)
8527 continue;
8528 if (ssid->mode != WPAS_MODE_P2P_GO)
8529 continue;
8530 if (wpa_s->ap_iface == NULL)
8531 continue;
8532 break;
8533 }
8534 if (wpa_s == NULL) {
8535 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8536 return -1;
8537 }
8538
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008539 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008540 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008541 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008542 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8543 return -1;
8544 }
8545 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008546 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8547 wpa_s->p2pdev->p2p_oob_dev_pw,
8548 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8549 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008550 if (res)
8551 return res;
8552
8553 if (!tag) {
8554 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8555 return 0;
8556 }
8557
8558 if (!params->peer ||
8559 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8560 return 0;
8561
8562 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8563 " to join", MAC2STR(params->peer->p2p_device_addr));
8564
8565 wpa_s->global->p2p_invite_group = wpa_s;
8566 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008567 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008568 params->peer->p2p_device_addr,
8569 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008570 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008571
8572 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8573 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8574 ssid->ssid, ssid->ssid_len, ssid->frequency,
8575 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008576 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008577}
8578
8579
8580static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8581 struct p2p_nfc_params *params,
8582 int forced_freq)
8583{
8584 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8585 "connection handover");
8586 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8587 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008588 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008589 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8590 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008591}
8592
8593
8594static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8595 struct p2p_nfc_params *params,
8596 int forced_freq)
8597{
8598 int res;
8599
8600 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8601 "connection handover");
8602 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8603 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008604 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008605 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8606 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008607 if (res)
8608 return res;
8609
8610 res = wpas_p2p_listen(wpa_s, 60);
8611 if (res) {
8612 p2p_unauthorize(wpa_s->global->p2p,
8613 params->peer->p2p_device_addr);
8614 }
8615
8616 return res;
8617}
8618
8619
8620static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8621 const struct wpabuf *data,
8622 int sel, int tag, int forced_freq)
8623{
8624 const u8 *pos, *end;
8625 u16 len, id;
8626 struct p2p_nfc_params params;
8627 int res;
8628
8629 os_memset(&params, 0, sizeof(params));
8630 params.sel = sel;
8631
8632 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8633
8634 pos = wpabuf_head(data);
8635 end = pos + wpabuf_len(data);
8636
8637 if (end - pos < 2) {
8638 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8639 "attributes");
8640 return -1;
8641 }
8642 len = WPA_GET_BE16(pos);
8643 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008644 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008645 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8646 "attributes");
8647 return -1;
8648 }
8649 params.wsc_attr = pos;
8650 params.wsc_len = len;
8651 pos += len;
8652
8653 if (end - pos < 2) {
8654 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8655 "attributes");
8656 return -1;
8657 }
8658 len = WPA_GET_BE16(pos);
8659 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008660 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008661 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8662 "attributes");
8663 return -1;
8664 }
8665 params.p2p_attr = pos;
8666 params.p2p_len = len;
8667 pos += len;
8668
8669 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8670 params.wsc_attr, params.wsc_len);
8671 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8672 params.p2p_attr, params.p2p_len);
8673 if (pos < end) {
8674 wpa_hexdump(MSG_DEBUG,
8675 "P2P: Ignored extra data after P2P attributes",
8676 pos, end - pos);
8677 }
8678
8679 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8680 if (res)
8681 return res;
8682
8683 if (params.next_step == NO_ACTION)
8684 return 0;
8685
8686 if (params.next_step == BOTH_GO) {
8687 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8688 MAC2STR(params.peer->p2p_device_addr));
8689 return 0;
8690 }
8691
8692 if (params.next_step == PEER_CLIENT) {
8693 if (!is_zero_ether_addr(params.go_dev_addr)) {
8694 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8695 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8696 " ssid=\"%s\"",
8697 MAC2STR(params.peer->p2p_device_addr),
8698 params.go_freq,
8699 MAC2STR(params.go_dev_addr),
8700 wpa_ssid_txt(params.go_ssid,
8701 params.go_ssid_len));
8702 } else {
8703 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8704 "peer=" MACSTR " freq=%d",
8705 MAC2STR(params.peer->p2p_device_addr),
8706 params.go_freq);
8707 }
8708 return 0;
8709 }
8710
8711 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8712 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8713 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8714 return 0;
8715 }
8716
8717 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8718 wpa_s->p2p_oob_dev_pw = NULL;
8719
8720 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8721 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8722 "received");
8723 return -1;
8724 }
8725
8726 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8727 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8728 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8729 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8730 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8731 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8732 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8733
8734 if (tag) {
8735 if (id < 0x10) {
8736 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8737 "peer OOB Device Password Id %u", id);
8738 return -1;
8739 }
8740 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8741 "Device Password Id %u", id);
8742 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8743 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8744 params.oob_dev_pw_len -
8745 WPS_OOB_PUBKEY_HASH_LEN - 2);
8746 wpa_s->p2p_oob_dev_pw_id = id;
8747 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8748 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8749 params.oob_dev_pw_len -
8750 WPS_OOB_PUBKEY_HASH_LEN - 2);
8751 if (wpa_s->p2p_oob_dev_pw == NULL)
8752 return -1;
8753
8754 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8755 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8756 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8757 return -1;
8758 } else {
8759 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8760 "without Device Password");
8761 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8762 }
8763
8764 switch (params.next_step) {
8765 case NO_ACTION:
8766 case BOTH_GO:
8767 case PEER_CLIENT:
8768 /* already covered above */
8769 return 0;
8770 case JOIN_GROUP:
8771 return wpas_p2p_nfc_join_group(wpa_s, &params);
8772 case AUTH_JOIN:
8773 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8774 case INIT_GO_NEG:
8775 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8776 case RESP_GO_NEG:
8777 /* TODO: use own OOB Dev Pw */
8778 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8779 }
8780
8781 return -1;
8782}
8783
8784
8785int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8786 const struct wpabuf *data, int forced_freq)
8787{
8788 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8789 return -1;
8790
8791 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8792}
8793
8794
8795int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8796 const struct wpabuf *req,
8797 const struct wpabuf *sel, int forced_freq)
8798{
8799 struct wpabuf *tmp;
8800 int ret;
8801
8802 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8803 return -1;
8804
8805 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8806
8807 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8808 wpabuf_head(req), wpabuf_len(req));
8809 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8810 wpabuf_head(sel), wpabuf_len(sel));
8811 if (forced_freq)
8812 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8813 tmp = ndef_parse_p2p(init ? sel : req);
8814 if (tmp == NULL) {
8815 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8816 return -1;
8817 }
8818
8819 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8820 forced_freq);
8821 wpabuf_free(tmp);
8822
8823 return ret;
8824}
8825
8826
8827int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8828{
8829 const u8 *if_addr;
8830 int go_intent = wpa_s->conf->p2p_go_intent;
8831 struct wpa_supplicant *iface;
8832
8833 if (wpa_s->global->p2p == NULL)
8834 return -1;
8835
8836 if (!enabled) {
8837 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8838 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8839 {
8840 if (!iface->ap_iface)
8841 continue;
8842 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8843 }
8844 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8845 0, NULL);
8846 if (wpa_s->p2p_nfc_tag_enabled)
8847 wpas_p2p_remove_pending_group_interface(wpa_s);
8848 wpa_s->p2p_nfc_tag_enabled = 0;
8849 return 0;
8850 }
8851
8852 if (wpa_s->global->p2p_disabled)
8853 return -1;
8854
8855 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8856 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8857 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8858 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8859 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8860 "to allow static handover cases");
8861 return -1;
8862 }
8863
8864 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8865
8866 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8867 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8868 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8869 if (wpa_s->p2p_oob_dev_pw == NULL)
8870 return -1;
8871 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8872
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008873 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8874 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8875 /*
8876 * P2P Group Interface present and the command came on group
8877 * interface, so enable the token for the current interface.
8878 */
8879 wpa_s->create_p2p_iface = 0;
8880 } else {
8881 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8882 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008883
8884 if (wpa_s->create_p2p_iface) {
8885 enum wpa_driver_if_type iftype;
8886 /* Prepare to add a new interface for the group */
8887 iftype = WPA_IF_P2P_GROUP;
8888 if (go_intent == 15)
8889 iftype = WPA_IF_P2P_GO;
8890 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8891 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8892 "interface for the group");
8893 return -1;
8894 }
8895
8896 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008897 } else if (wpa_s->p2p_mgmt)
8898 if_addr = wpa_s->parent->own_addr;
8899 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008900 if_addr = wpa_s->own_addr;
8901
8902 wpa_s->p2p_nfc_tag_enabled = enabled;
8903
8904 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8905 struct hostapd_data *hapd;
8906 if (iface->ap_iface == NULL)
8907 continue;
8908 hapd = iface->ap_iface->bss[0];
8909 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8910 hapd->conf->wps_nfc_dh_pubkey =
8911 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8912 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8913 hapd->conf->wps_nfc_dh_privkey =
8914 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8915 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8916 hapd->conf->wps_nfc_dev_pw =
8917 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8918 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8919
8920 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8921 wpa_dbg(iface, MSG_DEBUG,
8922 "P2P: Failed to enable NFC Tag for GO");
8923 }
8924 }
8925 p2p_set_authorized_oob_dev_pw_id(
8926 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8927 if_addr);
8928
8929 return 0;
8930}
8931
8932#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008933
8934
8935static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8936 struct wpa_used_freq_data *freqs,
8937 unsigned int num)
8938{
8939 u8 curr_chan, cand, chan;
8940 unsigned int i;
8941
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008942 /*
8943 * If possible, optimize the Listen channel to be a channel that is
8944 * already used by one of the other interfaces.
8945 */
8946 if (!wpa_s->conf->p2p_optimize_listen_chan)
8947 return;
8948
8949 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8950 return;
8951
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008952 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8953 for (i = 0, cand = 0; i < num; i++) {
8954 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8955 if (curr_chan == chan) {
8956 cand = 0;
8957 break;
8958 }
8959
8960 if (chan == 1 || chan == 6 || chan == 11)
8961 cand = chan;
8962 }
8963
8964 if (cand) {
8965 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008966 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008967 cand);
8968 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8969 }
8970}
8971
8972
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008973static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008974{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008975 struct hostapd_config *conf;
8976 struct p2p_go_neg_results params;
8977 struct csa_settings csa_settings;
8978 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8979 int old_freq = current_ssid->frequency;
8980 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008981
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008982 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8983 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8984 return -1;
8985 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008986
8987 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008988 * TODO: This function may not always work correctly. For example,
8989 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008990 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008991 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008992 wpa_dbg(wpa_s, MSG_DEBUG,
8993 "P2P CSA: Failed to select new frequency for GO");
8994 return -1;
8995 }
8996
8997 if (current_ssid->frequency == params.freq) {
8998 wpa_dbg(wpa_s, MSG_DEBUG,
8999 "P2P CSA: Selected same frequency - not moving GO");
9000 return 0;
9001 }
9002
9003 conf = hostapd_config_defaults();
9004 if (!conf) {
9005 wpa_dbg(wpa_s, MSG_DEBUG,
9006 "P2P CSA: Failed to allocate default config");
9007 return -1;
9008 }
9009
9010 current_ssid->frequency = params.freq;
9011 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
9012 wpa_dbg(wpa_s, MSG_DEBUG,
9013 "P2P CSA: Failed to create new GO config");
9014 ret = -1;
9015 goto out;
9016 }
9017
9018 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
9019 wpa_dbg(wpa_s, MSG_DEBUG,
9020 "P2P CSA: CSA to a different band is not supported");
9021 ret = -1;
9022 goto out;
9023 }
9024
9025 os_memset(&csa_settings, 0, sizeof(csa_settings));
9026 csa_settings.cs_count = P2P_GO_CSA_COUNT;
9027 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
9028 csa_settings.freq_params.freq = params.freq;
9029 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
9030 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
9031 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
9032
9033 if (conf->ieee80211ac) {
9034 int freq1 = 0, freq2 = 0;
9035 u8 chan, opclass;
9036
9037 if (ieee80211_freq_to_channel_ext(params.freq,
9038 conf->secondary_channel,
9039 conf->vht_oper_chwidth,
9040 &opclass, &chan) ==
9041 NUM_HOSTAPD_MODES) {
9042 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
9043 ret = -1;
9044 goto out;
9045 }
9046
9047 if (conf->vht_oper_centr_freq_seg0_idx)
9048 freq1 = ieee80211_chan_to_freq(
9049 NULL, opclass,
9050 conf->vht_oper_centr_freq_seg0_idx);
9051
9052 if (conf->vht_oper_centr_freq_seg1_idx)
9053 freq2 = ieee80211_chan_to_freq(
9054 NULL, opclass,
9055 conf->vht_oper_centr_freq_seg1_idx);
9056
9057 if (freq1 < 0 || freq2 < 0) {
9058 wpa_dbg(wpa_s, MSG_DEBUG,
9059 "P2P CSA: Selected invalid VHT center freqs");
9060 ret = -1;
9061 goto out;
9062 }
9063
9064 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
9065 csa_settings.freq_params.center_freq1 = freq1;
9066 csa_settings.freq_params.center_freq2 = freq2;
9067
9068 switch (conf->vht_oper_chwidth) {
9069 case VHT_CHANWIDTH_80MHZ:
9070 case VHT_CHANWIDTH_80P80MHZ:
9071 csa_settings.freq_params.bandwidth = 80;
9072 break;
9073 case VHT_CHANWIDTH_160MHZ:
9074 csa_settings.freq_params.bandwidth = 160;
9075 break;
9076 }
9077 }
9078
9079 ret = ap_switch_channel(wpa_s, &csa_settings);
9080out:
9081 current_ssid->frequency = old_freq;
9082 hostapd_config_free(conf);
9083 return ret;
9084}
9085
9086
9087static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
9088{
9089 struct p2p_go_neg_results params;
9090 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
9091
9092 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
9093
9094 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
9095 current_ssid->frequency);
9096
9097 /* Stop the AP functionality */
9098 /* TODO: Should do this in a way that does not indicated to possible
9099 * P2P Clients in the group that the group is terminated. */
9100 wpa_supplicant_ap_deinit(wpa_s);
9101
9102 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08009103 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009104 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
9105 wpas_p2p_group_delete(wpa_s,
9106 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9107 return;
9108 }
9109 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9110 params.freq);
9111
9112 if (params.freq &&
9113 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9114 wpa_printf(MSG_DEBUG,
9115 "P2P: Selected freq (%u MHz) is not valid for P2P",
9116 params.freq);
9117 wpas_p2p_group_delete(wpa_s,
9118 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9119 return;
9120 }
9121
9122 /* Update the frequency */
9123 current_ssid->frequency = params.freq;
9124 wpa_s->connect_without_scan = current_ssid;
9125 wpa_s->reassociate = 1;
9126 wpa_s->disconnected = 0;
9127 wpa_supplicant_req_scan(wpa_s, 0, 0);
9128}
9129
9130
9131static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9132{
9133 struct wpa_supplicant *wpa_s = eloop_ctx;
9134
9135 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009136 return;
9137
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009138 wpas_p2p_go_update_common_freqs(wpa_s);
9139
9140 /* Do not move GO in the middle of a CSA */
9141 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9142 wpa_printf(MSG_DEBUG,
9143 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009144 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009145 }
9146
9147 /*
9148 * First, try a channel switch flow. If it is not supported or fails,
9149 * take down the GO and bring it up again.
9150 */
9151 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9152 wpas_p2p_move_go_no_csa(wpa_s);
9153}
9154
9155
9156static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9157{
9158 struct wpa_supplicant *wpa_s = eloop_ctx;
9159 struct wpa_used_freq_data *freqs = NULL;
9160 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009161
9162 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9163 if (!freqs)
9164 return;
9165
9166 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9167
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009168 /* Previous attempt to move a GO was not possible -- try again. */
9169 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9170 WPAS_P2P_CHANNEL_UPDATE_ANY);
9171
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009172 os_free(freqs);
9173}
9174
9175
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009176/*
9177 * Consider moving a GO from its currently used frequency:
9178 * 1. It is possible that due to regulatory consideration the frequency
9179 * can no longer be used and there is a need to evacuate the GO.
9180 * 2. It is possible that due to MCC considerations, it would be preferable
9181 * to move the GO to a channel that is currently used by some other
9182 * station interface.
9183 *
9184 * In case a frequency that became invalid is once again valid, cancel a
9185 * previously initiated GO frequency change.
9186 */
9187static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9188 struct wpa_used_freq_data *freqs,
9189 unsigned int num)
9190{
9191 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9192 unsigned int timeout;
9193 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009194 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009195
9196 wpas_p2p_go_update_common_freqs(wpa_s);
9197
9198 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009199 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07009200 ieee80211_is_dfs(freq, wpa_s->hw.modes, wpa_s->hw.num_modes);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009201 for (i = 0, invalid_freq = 0; i < num; i++) {
9202 if (freqs[i].freq == freq) {
9203 flags = freqs[i].flags;
9204
9205 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009206 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9207 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009208 wpa_dbg(wpa_s, MSG_DEBUG,
9209 "P2P: Freq=%d MHz no longer valid for GO",
9210 freq);
9211 invalid_freq = 1;
9212 }
9213 } else if (freqs[i].flags == 0) {
9214 /* Freq is not used by any other station interface */
9215 continue;
9216 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009217 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009218 /* Freq is not valid for P2P use cases */
9219 continue;
9220 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9221 P2P_GO_FREQ_MOVE_SCM) {
9222 policy_move = 1;
9223 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9224 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9225 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9226 policy_move = 1;
9227 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9228 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9229 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9230 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9231 policy_move = 1;
9232 } else if ((wpa_s->drv_flags &
9233 WPA_DRIVER_FLAGS_AP_CSA) &&
9234 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9235 u8 chan;
9236
9237 /*
9238 * We do not support CSA between bands, so move
9239 * GO only within the same band.
9240 */
9241 if (wpa_s->ap_iface->current_mode->mode ==
9242 ieee80211_freq_to_chan(freqs[i].freq,
9243 &chan))
9244 policy_move = 1;
9245 }
9246 }
9247 }
9248
9249 wpa_dbg(wpa_s, MSG_DEBUG,
9250 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9251 invalid_freq, policy_move, flags);
9252
9253 /*
9254 * The channel is valid, or we are going to have a policy move, so
9255 * cancel timeout.
9256 */
9257 if (!invalid_freq || policy_move) {
9258 wpa_dbg(wpa_s, MSG_DEBUG,
9259 "P2P: Cancel a GO move from freq=%d MHz", freq);
9260 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9261
9262 if (wpas_p2p_in_progress(wpa_s)) {
9263 wpa_dbg(wpa_s, MSG_DEBUG,
9264 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9265 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9266 wpa_s, NULL);
9267 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9268 wpas_p2p_reconsider_moving_go,
9269 wpa_s, NULL);
9270 return;
9271 }
9272 }
9273
9274 if (!invalid_freq && (!policy_move || flags != 0)) {
9275 wpa_dbg(wpa_s, MSG_DEBUG,
9276 "P2P: Not initiating a GO frequency change");
9277 return;
9278 }
9279
9280 /*
9281 * Do not consider moving GO if it is in the middle of a CSA. When the
9282 * CSA is finished this flow should be retriggered.
9283 */
9284 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9285 wpa_dbg(wpa_s, MSG_DEBUG,
9286 "P2P: Not initiating a GO frequency change - CSA is in progress");
9287 return;
9288 }
9289
9290 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9291 timeout = P2P_GO_FREQ_CHANGE_TIME;
9292 else
9293 timeout = 0;
9294
9295 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9296 freq, timeout);
9297 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9298 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9299}
9300
9301
9302static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9303 struct wpa_used_freq_data *freqs,
9304 unsigned int num,
9305 enum wpas_p2p_channel_update_trig trig)
9306{
9307 struct wpa_supplicant *ifs;
9308
9309 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9310 NULL);
9311
9312 /*
9313 * Travers all the radio interfaces, and for each GO interface, check
9314 * if there is a need to move the GO from the frequency it is using,
9315 * or in case the frequency is valid again, cancel the evacuation flow.
9316 */
9317 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9318 radio_list) {
9319 if (ifs->current_ssid == NULL ||
9320 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9321 continue;
9322
9323 /*
9324 * The GO was just started or completed channel switch, no need
9325 * to move it.
9326 */
9327 if (wpa_s == ifs &&
9328 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9329 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9330 wpa_dbg(wpa_s, MSG_DEBUG,
9331 "P2P: GO move - schedule re-consideration");
9332 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9333 wpas_p2p_reconsider_moving_go,
9334 wpa_s, NULL);
9335 continue;
9336 }
9337
9338 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9339 }
9340}
9341
9342
9343void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9344{
9345 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9346 return;
9347
9348 wpas_p2p_update_channel_list(wpa_s,
9349 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9350}
9351
9352
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009353void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9354{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009355 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9357 "the management interface is being removed");
9358 wpas_p2p_deinit_global(wpa_s->global);
9359 }
9360}
9361
9362
9363void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9364{
9365 if (wpa_s->ap_iface->bss)
9366 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9367 wpas_p2p_group_deinit(wpa_s);
9368}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009369
9370
9371int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9372 unsigned int period, unsigned int interval,
9373 unsigned int count)
9374{
9375 struct p2p_data *p2p = wpa_s->global->p2p;
9376 u8 *device_types;
9377 size_t dev_types_len;
9378 struct wpabuf *buf;
9379 int ret;
9380
9381 if (wpa_s->p2p_lo_started) {
9382 wpa_dbg(wpa_s, MSG_DEBUG,
9383 "P2P Listen offload is already started");
9384 return 0;
9385 }
9386
9387 if (wpa_s->global->p2p == NULL ||
9388 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9389 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9390 return -1;
9391 }
9392
9393 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9394 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9395 freq);
9396 return -1;
9397 }
9398
9399 /* Get device type */
9400 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9401 WPS_DEV_TYPE_LEN;
9402 device_types = os_malloc(dev_types_len);
9403 if (!device_types)
9404 return -1;
9405 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9406 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9407 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9408
9409 /* Get Probe Response IE(s) */
9410 buf = p2p_build_probe_resp_template(p2p, freq);
9411 if (!buf) {
9412 os_free(device_types);
9413 return -1;
9414 }
9415
9416 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9417 device_types, dev_types_len,
9418 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9419 if (ret < 0)
9420 wpa_dbg(wpa_s, MSG_DEBUG,
9421 "P2P: Failed to start P2P listen offload");
9422
9423 os_free(device_types);
9424 wpabuf_free(buf);
9425
9426 if (ret == 0) {
9427 wpa_s->p2p_lo_started = 1;
9428
9429 /* Stop current P2P listen if any */
9430 wpas_stop_listen(wpa_s);
9431 }
9432
9433 return ret;
9434}
9435
9436
9437int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9438{
9439 int ret;
9440
9441 if (!wpa_s->p2p_lo_started)
9442 return 0;
9443
9444 ret = wpa_drv_p2p_lo_stop(wpa_s);
9445 if (ret < 0)
9446 wpa_dbg(wpa_s, MSG_DEBUG,
9447 "P2P: Failed to stop P2P listen offload");
9448
9449 wpa_s->p2p_lo_started = 0;
9450 return ret;
9451}