blob: 0eccd52faccb0048a4ab54993c08dc42962b0bc8 [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 }
751
752 default:
753 return P2PS_SETUP_NONE;
754 }
755 }
756
757 /*
758 * For now, we only will support ownership of one group, and being a
759 * client of one group. Therefore, if we have either an existing GO
760 * group, or an existing client group, we will not do a new GO
761 * negotiation, but rather try to re-use the existing groups.
762 */
763 switch (incoming) {
764 case P2PS_SETUP_NONE:
765 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800766 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800767 conncap = P2PS_SETUP_GROUP_OWNER;
768 else if (!owned_members)
769 conncap = P2PS_SETUP_NEW;
770 else if (incoming == P2PS_SETUP_NONE)
771 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
772 else
773 conncap = P2PS_SETUP_CLIENT;
774 break;
775
776 case P2PS_SETUP_CLIENT:
777 conncap = P2PS_SETUP_GROUP_OWNER;
778 break;
779
780 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800781 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800782 conncap = P2PS_SETUP_CLIENT;
783 break;
784
785 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
786 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800787 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800788 conncap = P2PS_SETUP_GROUP_OWNER;
789 else {
790 u8 r;
791
792 if (os_get_random(&r, sizeof(r)) < 0 ||
793 (r & 1))
794 conncap = P2PS_SETUP_CLIENT;
795 else
796 conncap = P2PS_SETUP_GROUP_OWNER;
797 }
798 break;
799
800 default:
801 return P2PS_SETUP_NONE;
802 }
803
804grp_owner:
805 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
806 (!incoming && (conncap & P2PS_SETUP_NEW))) {
807 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
808 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800809
810 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800811 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800812 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800813 wpa_s->p2p_mgmt ?
814 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800815 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800816 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800817 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800818 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800819 wpa_printf(MSG_ERROR,
820 "P2P: Failed to allocate a new interface for the group");
821 return P2PS_SETUP_NONE;
822 }
823 wpa_s->global->pending_group_iface_for_p2ps = 1;
824 p2p_set_intended_addr(wpa_s->global->p2p,
825 wpa_s->pending_interface_addr);
826 }
827 }
828
829 return conncap;
830}
831
832
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700833static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
834 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835{
836 struct wpa_ssid *ssid;
837 char *gtype;
838 const char *reason;
839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 ssid = wpa_s->current_ssid;
841 if (ssid == NULL) {
842 /*
843 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700844 * pending P2P group interface waiting for provisioning or a
845 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846 */
847 ssid = wpa_s->conf->ssid;
848 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700849 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 break;
851 ssid = ssid->next;
852 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300853 if (ssid == NULL &&
854 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
855 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700856 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
857 "not found");
858 return -1;
859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 }
861 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
862 gtype = "GO";
863 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
864 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
865 wpa_s->reassociate = 0;
866 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 gtype = "client";
868 } else
869 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700870
871 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
872 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
873
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800874 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700875 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800876 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
877 wpa_s, NULL)) {
878 wpa_printf(MSG_DEBUG,
879 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
880 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
881 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
882 wpa_s, NULL);
883 }
884 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 if (wpa_s->cross_connect_in_use) {
887 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800888 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700889 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
890 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700892 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 case P2P_GROUP_REMOVAL_REQUESTED:
894 reason = " reason=REQUESTED";
895 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700896 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
897 reason = " reason=FORMATION_FAILED";
898 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
900 reason = " reason=IDLE";
901 break;
902 case P2P_GROUP_REMOVAL_UNAVAILABLE:
903 reason = " reason=UNAVAILABLE";
904 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700905 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
906 reason = " reason=GO_ENDING_SESSION";
907 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700908 case P2P_GROUP_REMOVAL_PSK_FAILURE:
909 reason = " reason=PSK_FAILURE";
910 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800911 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
912 reason = " reason=FREQ_CONFLICT";
913 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 default:
915 reason = "";
916 break;
917 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700918 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800919 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700920 P2P_EVENT_GROUP_REMOVED "%s %s%s",
921 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700922 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800924 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
925 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700926 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
927 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800928 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800929 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800930 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
931 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700932 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800933 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700934 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700935
Dmitry Shmidt15907092014-03-25 10:42:57 -0700936 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800937 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
938 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700939
Dmitry Shmidt96571392013-10-14 12:54:46 -0700940 /*
941 * Make sure wait for the first client does not remain active after the
942 * group has been removed.
943 */
944 wpa_s->global->p2p_go_wait_client.sec = 0;
945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700946 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
947 struct wpa_global *global;
948 char *ifname;
949 enum wpa_driver_if_type type;
950 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
951 wpa_s->ifname);
952 global = wpa_s->global;
953 ifname = os_strdup(wpa_s->ifname);
954 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800955 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700956 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 wpa_s = global->ifaces;
958 if (wpa_s && ifname)
959 wpa_drv_if_remove(wpa_s, type, ifname);
960 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300961 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 }
963
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800964 /*
965 * The primary interface was used for P2P group operations, so
966 * need to reset its p2pdev.
967 */
968 wpa_s->p2pdev = wpa_s->parent;
969
Dmitry Shmidt96571392013-10-14 12:54:46 -0700970 if (!wpa_s->p2p_go_group_formation_completed) {
971 wpa_s->global->p2p_group_formation = NULL;
972 wpa_s->p2p_in_provisioning = 0;
973 }
974
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800975 wpa_s->show_group_started = 0;
976 os_free(wpa_s->go_params);
977 wpa_s->go_params = NULL;
978
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800979 os_free(wpa_s->p2p_group_common_freqs);
980 wpa_s->p2p_group_common_freqs = NULL;
981 wpa_s->p2p_group_common_freqs_num = 0;
982
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800983 wpa_s->waiting_presence_resp = 0;
984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
986 if (ssid && (ssid->p2p_group ||
987 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
988 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
989 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700990 if (ssid == wpa_s->current_ssid) {
991 wpa_sm_set_config(wpa_s->wpa, NULL);
992 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700994 }
995 /*
996 * Networks objects created during any P2P activities are not
997 * exposed out as they might/will confuse certain non-P2P aware
998 * applications since these network objects won't behave like
999 * regular ones.
1000 *
1001 * Likewise, we don't send out network removed signals for such
1002 * network objects.
1003 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 wpa_config_remove_network(wpa_s->conf, id);
1005 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001006 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 } else {
1008 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1009 "found");
1010 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001011 if (wpa_s->ap_iface)
1012 wpa_supplicant_ap_deinit(wpa_s);
1013 else
1014 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001015
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001016 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001018 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019}
1020
1021
1022static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1023 u8 *go_dev_addr,
1024 const u8 *ssid, size_t ssid_len)
1025{
1026 struct wpa_bss *bss;
1027 const u8 *bssid;
1028 struct wpabuf *p2p;
1029 u8 group_capab;
1030 const u8 *addr;
1031
1032 if (wpa_s->go_params)
1033 bssid = wpa_s->go_params->peer_interface_addr;
1034 else
1035 bssid = wpa_s->bssid;
1036
1037 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001038 if (bss == NULL && wpa_s->go_params &&
1039 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1040 bss = wpa_bss_get_p2p_dev_addr(
1041 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 if (bss == NULL) {
1043 u8 iface_addr[ETH_ALEN];
1044 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1045 iface_addr) == 0)
1046 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1047 }
1048 if (bss == NULL) {
1049 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1050 "group is persistent - BSS " MACSTR " not found",
1051 MAC2STR(bssid));
1052 return 0;
1053 }
1054
1055 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001056 if (p2p == NULL)
1057 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1058 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 if (p2p == NULL) {
1060 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1061 "group is persistent - BSS " MACSTR
1062 " did not include P2P IE", MAC2STR(bssid));
1063 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1064 (u8 *) (bss + 1), bss->ie_len);
1065 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1066 ((u8 *) bss + 1) + bss->ie_len,
1067 bss->beacon_ie_len);
1068 return 0;
1069 }
1070
1071 group_capab = p2p_get_group_capab(p2p);
1072 addr = p2p_get_go_dev_addr(p2p);
1073 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1074 "group_capab=0x%x", group_capab);
1075 if (addr) {
1076 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1077 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1078 MAC2STR(addr));
1079 } else
1080 os_memset(go_dev_addr, 0, ETH_ALEN);
1081 wpabuf_free(p2p);
1082
1083 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1084 "go_dev_addr=" MACSTR,
1085 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1086
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001087 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088}
1089
1090
Jouni Malinen75ecf522011-06-27 15:19:46 -07001091static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1092 struct wpa_ssid *ssid,
1093 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094{
1095 struct wpa_ssid *s;
1096 int changed = 0;
1097
1098 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1099 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1100 for (s = wpa_s->conf->ssid; s; s = s->next) {
1101 if (s->disabled == 2 &&
1102 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1103 s->ssid_len == ssid->ssid_len &&
1104 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1105 break;
1106 }
1107
1108 if (s) {
1109 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1110 "entry");
1111 if (ssid->passphrase && !s->passphrase)
1112 changed = 1;
1113 else if (ssid->passphrase && s->passphrase &&
1114 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1115 changed = 1;
1116 } else {
1117 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1118 "entry");
1119 changed = 1;
1120 s = wpa_config_add_network(wpa_s->conf);
1121 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001122 return -1;
1123
1124 /*
1125 * Instead of network_added we emit persistent_group_added
1126 * notification. Also to keep the defense checks in
1127 * persistent_group obj registration method, we set the
1128 * relevant flags in s to designate it as a persistent group.
1129 */
1130 s->p2p_group = 1;
1131 s->p2p_persistent_group = 1;
1132 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpa_config_set_network_defaults(s);
1134 }
1135
1136 s->p2p_group = 1;
1137 s->p2p_persistent_group = 1;
1138 s->disabled = 2;
1139 s->bssid_set = 1;
1140 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1141 s->mode = ssid->mode;
1142 s->auth_alg = WPA_AUTH_ALG_OPEN;
1143 s->key_mgmt = WPA_KEY_MGMT_PSK;
1144 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001145 s->pbss = ssid->pbss;
1146 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 s->export_keys = 1;
1148 if (ssid->passphrase) {
1149 os_free(s->passphrase);
1150 s->passphrase = os_strdup(ssid->passphrase);
1151 }
1152 if (ssid->psk_set) {
1153 s->psk_set = 1;
1154 os_memcpy(s->psk, ssid->psk, 32);
1155 }
1156 if (s->passphrase && !s->psk_set)
1157 wpa_config_update_psk(s);
1158 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1159 os_free(s->ssid);
1160 s->ssid = os_malloc(ssid->ssid_len);
1161 }
1162 if (s->ssid) {
1163 s->ssid_len = ssid->ssid_len;
1164 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1165 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001166 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1167 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1168 wpa_s->global->add_psk = NULL;
1169 changed = 1;
1170 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172 if (changed && wpa_s->conf->update_config &&
1173 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1174 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1175 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001176
1177 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178}
1179
1180
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001181static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1182 const u8 *addr)
1183{
1184 struct wpa_ssid *ssid, *s;
1185 u8 *n;
1186 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001187 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001188 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001189
1190 ssid = wpa_s->current_ssid;
1191 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1192 !ssid->p2p_persistent_group)
1193 return;
1194
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001195 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001196 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1197 continue;
1198
1199 if (s->ssid_len == ssid->ssid_len &&
1200 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1201 break;
1202 }
1203
1204 if (s == NULL)
1205 return;
1206
1207 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001208 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001209 ETH_ALEN) != 0)
1210 continue;
1211
1212 if (i == s->num_p2p_clients - 1)
1213 return; /* already the most recent entry */
1214
1215 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001216 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1217 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1218 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001219 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001220 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1221 ETH_ALEN);
1222 os_memset(s->p2p_client_list +
1223 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1224 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001225 found = 1;
1226 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001227 }
1228
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001229 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1230 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001231 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001232 if (n == NULL)
1233 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001234 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1235 ETH_ALEN);
1236 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1237 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001238 s->p2p_client_list = n;
1239 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001240 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001241 /* Not enough room for an additional entry - drop the oldest
1242 * entry */
1243 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001244 s->p2p_client_list + 2 * ETH_ALEN,
1245 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001247 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001248 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001249 os_memset(s->p2p_client_list +
1250 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1251 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001252 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001253
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001254 if (p2p_wpa_s->conf->update_config &&
1255 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001256 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001257}
1258
1259
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001260static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1261 int go, struct wpa_ssid *ssid, int freq,
1262 const u8 *psk, const char *passphrase,
1263 const u8 *go_dev_addr, int persistent,
1264 const char *extra)
1265{
1266 const char *ssid_txt;
1267 char psk_txt[65];
1268
1269 if (psk)
1270 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1271 else
1272 psk_txt[0] = '\0';
1273
1274 if (ssid)
1275 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1276 else
1277 ssid_txt = "";
1278
1279 if (passphrase && passphrase[0] == '\0')
1280 passphrase = NULL;
1281
1282 /*
1283 * Include PSK/passphrase only in the control interface message and
1284 * leave it out from the debug log entry.
1285 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001286 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001287 P2P_EVENT_GROUP_STARTED
1288 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1289 MACSTR "%s%s",
1290 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1291 psk ? " psk=" : "", psk_txt,
1292 passphrase ? " passphrase=\"" : "",
1293 passphrase ? passphrase : "",
1294 passphrase ? "\"" : "",
1295 MAC2STR(go_dev_addr),
1296 persistent ? " [PERSISTENT]" : "", extra);
1297 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1298 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1299 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1300 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1301 extra);
1302}
1303
1304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001306 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307{
1308 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 int client;
1310 int persistent;
1311 u8 go_dev_addr[ETH_ALEN];
1312
1313 /*
1314 * This callback is likely called for the main interface. Update wpa_s
1315 * to use the group interface if a new interface was created for the
1316 * group.
1317 */
1318 if (wpa_s->global->p2p_group_formation)
1319 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001320 if (wpa_s->p2p_go_group_formation_completed) {
1321 wpa_s->global->p2p_group_formation = NULL;
1322 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001323 } else if (wpa_s->p2p_in_provisioning && !success) {
1324 wpa_msg(wpa_s, MSG_DEBUG,
1325 "P2P: Stop provisioning state due to failure");
1326 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001327 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001328 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001329 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330
1331 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001332 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001333 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001334 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001335 if (already_deleted)
1336 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001337 wpas_p2p_group_delete(wpa_s,
1338 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 return;
1340 }
1341
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001342 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001343 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344
1345 ssid = wpa_s->current_ssid;
1346 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1347 ssid->mode = WPAS_MODE_P2P_GO;
1348 p2p_group_notif_formation_done(wpa_s->p2p_group);
1349 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1350 }
1351
1352 persistent = 0;
1353 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 client = ssid->mode == WPAS_MODE_INFRA;
1355 if (ssid->mode == WPAS_MODE_P2P_GO) {
1356 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001357 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1358 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 } else
1360 persistent = wpas_p2p_persistent_group(wpa_s,
1361 go_dev_addr,
1362 ssid->ssid,
1363 ssid->ssid_len);
1364 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 client = wpa_s->p2p_group_interface ==
1366 P2P_GROUP_INTERFACE_CLIENT;
1367 os_memset(go_dev_addr, 0, ETH_ALEN);
1368 }
1369
1370 wpa_s->show_group_started = 0;
1371 if (client) {
1372 /*
1373 * Indicate event only after successfully completed 4-way
1374 * handshake, i.e., when the interface is ready for data
1375 * packets.
1376 */
1377 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001379 wpas_p2p_group_started(wpa_s, 1, ssid,
1380 ssid ? ssid->frequency : 0,
1381 ssid && ssid->passphrase == NULL &&
1382 ssid->psk_set ? ssid->psk : NULL,
1383 ssid ? ssid->passphrase : NULL,
1384 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 wpas_p2p_cross_connect_setup(wpa_s);
1386 wpas_p2p_set_group_idle_timeout(wpa_s);
1387 }
1388
1389 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001390 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1391 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001392 else {
1393 os_free(wpa_s->global->add_psk);
1394 wpa_s->global->add_psk = NULL;
1395 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001396
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001397 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001398 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001399 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401}
1402
1403
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001404struct send_action_work {
1405 unsigned int freq;
1406 u8 dst[ETH_ALEN];
1407 u8 src[ETH_ALEN];
1408 u8 bssid[ETH_ALEN];
1409 size_t len;
1410 unsigned int wait_time;
1411 u8 buf[0];
1412};
1413
1414
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001415static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1416{
1417 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1418
1419 wpa_printf(MSG_DEBUG,
1420 "P2P: Free Action frame radio work @%p (freq=%u dst="
1421 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1422 wpa_s->p2p_send_action_work, awork->freq,
1423 MAC2STR(awork->dst), MAC2STR(awork->src),
1424 MAC2STR(awork->bssid), awork->wait_time);
1425 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1426 awork->buf, awork->len);
1427 os_free(awork);
1428 wpa_s->p2p_send_action_work->ctx = NULL;
1429 radio_work_done(wpa_s->p2p_send_action_work);
1430 wpa_s->p2p_send_action_work = NULL;
1431}
1432
1433
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001434static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1435 void *timeout_ctx)
1436{
1437 struct wpa_supplicant *wpa_s = eloop_ctx;
1438
1439 if (!wpa_s->p2p_send_action_work)
1440 return;
1441
1442 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001443 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001444}
1445
1446
Dmitry Shmidt03658832014-08-13 11:03:49 -07001447static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001449 if (wpa_s->p2p_send_action_work) {
1450 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001451
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001452 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001453 wpa_printf(MSG_DEBUG,
1454 "P2P: Clear Action TX work @%p (wait_time=%u)",
1455 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001456 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001457 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001458 } else {
1459 /*
1460 * In theory, this should not be needed, but number of
1461 * places in the P2P code is still using non-zero wait
1462 * time for the last Action frame in the sequence and
1463 * some of these do not call send_action_done().
1464 */
1465 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1466 wpa_s, NULL);
1467 eloop_register_timeout(
1468 0, awork->wait_time * 1000,
1469 wpas_p2p_send_action_work_timeout,
1470 wpa_s, NULL);
1471 }
1472 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001473}
1474
1475
1476static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1477 unsigned int freq,
1478 const u8 *dst, const u8 *src,
1479 const u8 *bssid,
1480 const u8 *data, size_t data_len,
1481 enum offchannel_send_action_result
1482 result)
1483{
1484 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1485
1486 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1489 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001490
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001491 switch (result) {
1492 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1493 res = P2P_SEND_ACTION_SUCCESS;
1494 break;
1495 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1496 res = P2P_SEND_ACTION_NO_ACK;
1497 break;
1498 case OFFCHANNEL_SEND_ACTION_FAILED:
1499 res = P2P_SEND_ACTION_FAILED;
1500 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 }
1502
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001503 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001505 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1506 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001508 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1509 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001511 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1512 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001513 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001514 P2P_EVENT_FALLBACK_TO_GO_NEG
1515 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001516 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1517 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 }
1519}
1520
1521
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001522static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1523{
1524 struct wpa_supplicant *wpa_s = work->wpa_s;
1525 struct send_action_work *awork = work->ctx;
1526
1527 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001528 if (work->started) {
1529 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1530 wpa_s, NULL);
1531 wpa_s->p2p_send_action_work = NULL;
1532 offchannel_send_action_done(wpa_s);
1533 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001534 os_free(awork);
1535 return;
1536 }
1537
1538 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1539 awork->bssid, awork->buf, awork->len,
1540 awork->wait_time,
1541 wpas_p2p_send_action_tx_status, 1) < 0) {
1542 os_free(awork);
1543 radio_work_done(work);
1544 return;
1545 }
1546 wpa_s->p2p_send_action_work = work;
1547}
1548
1549
1550static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1551 unsigned int freq, const u8 *dst,
1552 const u8 *src, const u8 *bssid, const u8 *buf,
1553 size_t len, unsigned int wait_time)
1554{
1555 struct send_action_work *awork;
1556
1557 if (wpa_s->p2p_send_action_work) {
1558 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1559 return -1;
1560 }
1561
1562 awork = os_zalloc(sizeof(*awork) + len);
1563 if (awork == NULL)
1564 return -1;
1565
1566 awork->freq = freq;
1567 os_memcpy(awork->dst, dst, ETH_ALEN);
1568 os_memcpy(awork->src, src, ETH_ALEN);
1569 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1570 awork->len = len;
1571 awork->wait_time = wait_time;
1572 os_memcpy(awork->buf, buf, len);
1573
1574 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1575 wpas_send_action_cb, awork) < 0) {
1576 os_free(awork);
1577 return -1;
1578 }
1579
1580 return 0;
1581}
1582
1583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1585 const u8 *src, const u8 *bssid, const u8 *buf,
1586 size_t len, unsigned int wait_time)
1587{
1588 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001589 int listen_freq = -1, send_freq = -1;
1590
1591 if (wpa_s->p2p_listen_work)
1592 listen_freq = wpa_s->p2p_listen_work->freq;
1593 if (wpa_s->p2p_send_action_work)
1594 send_freq = wpa_s->p2p_send_action_work->freq;
1595 if (listen_freq != (int) freq && send_freq != (int) freq) {
1596 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1597 listen_freq, send_freq);
1598 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1599 len, wait_time);
1600 }
1601
1602 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001603 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1604 wait_time,
1605 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606}
1607
1608
1609static void wpas_send_action_done(void *ctx)
1610{
1611 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001612
1613 if (wpa_s->p2p_send_action_work) {
1614 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1615 wpa_s, NULL);
1616 os_free(wpa_s->p2p_send_action_work->ctx);
1617 radio_work_done(wpa_s->p2p_send_action_work);
1618 wpa_s->p2p_send_action_work = NULL;
1619 }
1620
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001621 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622}
1623
1624
1625static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1626 struct p2p_go_neg_results *params)
1627{
1628 if (wpa_s->go_params == NULL) {
1629 wpa_s->go_params = os_malloc(sizeof(*params));
1630 if (wpa_s->go_params == NULL)
1631 return -1;
1632 }
1633 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1634 return 0;
1635}
1636
1637
1638static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1639 struct p2p_go_neg_results *res)
1640{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001641 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001642 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1643 " dev_addr " MACSTR " wps_method %d",
1644 MAC2STR(res->peer_interface_addr),
1645 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1647 res->ssid, res->ssid_len);
1648 wpa_supplicant_ap_deinit(wpa_s);
1649 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001650 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001652#ifdef CONFIG_WPS_NFC
1653 } else if (res->wps_method == WPS_NFC) {
1654 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1655 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001656 wpa_s->p2pdev->p2p_oob_dev_pw,
1657 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1658 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001659 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001660 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001661 NULL,
1662 NULL, 0, 0);
1663#endif /* CONFIG_WPS_NFC */
1664 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001666 if (wpa_s->p2p_wps_method == WPS_P2PS)
1667 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1669 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1670 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1671 wpa_s->p2p_pin, 1, dev_pw_id);
1672 }
1673}
1674
1675
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001676static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1677 struct wpa_ssid *ssid)
1678{
1679 struct wpa_ssid *persistent;
1680 struct psk_list_entry *psk;
1681 struct hostapd_data *hapd;
1682
1683 if (!wpa_s->ap_iface)
1684 return;
1685
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001686 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001687 ssid->ssid_len);
1688 if (persistent == NULL)
1689 return;
1690
1691 hapd = wpa_s->ap_iface->bss[0];
1692
1693 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1694 list) {
1695 struct hostapd_wpa_psk *hpsk;
1696
1697 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1698 MACSTR " psk=%d",
1699 MAC2STR(psk->addr), psk->p2p);
1700 hpsk = os_zalloc(sizeof(*hpsk));
1701 if (hpsk == NULL)
1702 break;
1703 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1704 if (psk->p2p)
1705 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1706 else
1707 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1708 hpsk->next = hapd->conf->ssid.wpa_psk;
1709 hapd->conf->ssid.wpa_psk = hpsk;
1710 }
1711}
1712
1713
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001714static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1715{
1716 unsigned int i;
1717
1718 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1719 wpa_s->p2p_group_common_freqs_num);
1720
1721 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1722 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1723 i, wpa_s->p2p_group_common_freqs[i]);
1724}
1725
1726
1727static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1728 struct p2p_go_neg_results *params)
1729{
1730 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1731
1732 wpa_s->p2p_group_common_freqs_num = 0;
1733 os_free(wpa_s->p2p_group_common_freqs);
1734 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1735 if (!wpa_s->p2p_group_common_freqs)
1736 return;
1737
1738 for (i = 0; i < len; i++) {
1739 if (!wpa_s->go_params->freq_list[i])
1740 break;
1741 wpa_s->p2p_group_common_freqs[i] =
1742 wpa_s->go_params->freq_list[i];
1743 }
1744 wpa_s->p2p_group_common_freqs_num = i;
1745}
1746
1747
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001748static void p2p_config_write(struct wpa_supplicant *wpa_s)
1749{
1750#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001751 if (wpa_s->p2pdev->conf->update_config &&
1752 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001753 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1754#endif /* CONFIG_NO_CONFIG_WRITE */
1755}
1756
1757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758static void p2p_go_configured(void *ctx, void *data)
1759{
1760 struct wpa_supplicant *wpa_s = ctx;
1761 struct p2p_go_neg_results *params = data;
1762 struct wpa_ssid *ssid;
1763
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001764 wpa_s->ap_configured_cb = NULL;
1765 wpa_s->ap_configured_cb_ctx = NULL;
1766 wpa_s->ap_configured_cb_data = NULL;
1767 if (!wpa_s->go_params) {
1768 wpa_printf(MSG_ERROR,
1769 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1770 return;
1771 }
1772
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001773 p2p_go_save_group_common_freqs(wpa_s, params);
1774 p2p_go_dump_common_freqs(wpa_s);
1775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 ssid = wpa_s->current_ssid;
1777 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1778 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1779 if (wpa_s->global->p2p_group_formation == wpa_s)
1780 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001781 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1782 params->passphrase[0] == '\0' ?
1783 params->psk : NULL,
1784 params->passphrase,
1785 wpa_s->global->p2p_dev_addr,
1786 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001787 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001788
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001789 if (wpa_s->p2pdev->p2ps_method_config_any) {
1790 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001791 wpa_dbg(wpa_s, MSG_DEBUG,
1792 "P2PS: Setting default PIN for ANY");
1793 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1794 "12345670", NULL, 0,
1795 0);
1796 } else {
1797 wpa_dbg(wpa_s, MSG_DEBUG,
1798 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001799 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001800 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001801 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001802 "12345670", NULL, 0, 0);
1803 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001804 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001805 }
1806
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001807 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001808 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001809 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001810 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001811 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001812 wpas_p2p_add_psk_list(wpa_s, ssid);
1813 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001814
1815 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001816 params->persistent_group, 0,
1817 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818 wpas_p2p_cross_connect_setup(wpa_s);
1819 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001820
1821 if (wpa_s->p2p_first_connection_timeout) {
1822 wpa_dbg(wpa_s, MSG_DEBUG,
1823 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1824 wpa_s->p2p_first_connection_timeout);
1825 wpa_s->p2p_go_group_formation_completed = 0;
1826 wpa_s->global->p2p_group_formation = wpa_s;
1827 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001828 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001829 eloop_register_timeout(
1830 wpa_s->p2p_first_connection_timeout, 0,
1831 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001832 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001833 }
1834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 return;
1836 }
1837
1838 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1839 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1840 params->peer_interface_addr)) {
1841 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1842 "filtering");
1843 return;
1844 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001845 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001847 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001848#ifdef CONFIG_WPS_NFC
1849 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001850 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001851 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001852 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001853 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1854 return;
1855 }
1856 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001857 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1858 wpa_s->p2pdev->p2p_oob_dev_pw,
1859 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1860 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001861#endif /* CONFIG_WPS_NFC */
1862 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001864 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 os_free(wpa_s->go_params);
1866 wpa_s->go_params = NULL;
1867}
1868
1869
1870static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1871 struct p2p_go_neg_results *params,
1872 int group_formation)
1873{
1874 struct wpa_ssid *ssid;
1875
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001876 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1877 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1878 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1879 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001880 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882
1883 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001884 if (ssid == NULL) {
1885 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001887 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001889 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001890 wpa_s->p2p_go_group_formation_completed = 0;
1891 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001892 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 wpa_config_set_network_defaults(ssid);
1895 ssid->temporary = 1;
1896 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001897 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1899 WPAS_MODE_P2P_GO;
1900 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001901 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001902 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001903 ssid->max_oper_chwidth = params->max_oper_chwidth;
1904 ssid->vht_center_freq2 = params->vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905 ssid->ssid = os_zalloc(params->ssid_len + 1);
1906 if (ssid->ssid) {
1907 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1908 ssid->ssid_len = params->ssid_len;
1909 }
1910 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1911 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1912 ssid->proto = WPA_PROTO_RSN;
1913 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001914 ssid->group_cipher = WPA_CIPHER_CCMP;
1915 if (params->freq > 56160) {
1916 /*
1917 * Enable GCMP instead of CCMP as pairwise_cipher and
1918 * group_cipher in 60 GHz.
1919 */
1920 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1921 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001922 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1923 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001925 if (os_strlen(params->passphrase) > 0) {
1926 ssid->passphrase = os_strdup(params->passphrase);
1927 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001928 wpa_msg_global(wpa_s, MSG_ERROR,
1929 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001930 wpa_config_remove_network(wpa_s->conf, ssid->id);
1931 return;
1932 }
1933 } else
1934 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001935 ssid->psk_set = params->psk_set;
1936 if (ssid->psk_set)
1937 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001938 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001939 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001940 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941
1942 wpa_s->ap_configured_cb = p2p_go_configured;
1943 wpa_s->ap_configured_cb_ctx = wpa_s;
1944 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001945 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001946 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 wpa_s->reassociate = 1;
1948 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1950 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 wpa_supplicant_req_scan(wpa_s, 0, 0);
1952}
1953
1954
1955static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1956 const struct wpa_supplicant *src)
1957{
1958 struct wpa_config *d;
1959 const struct wpa_config *s;
1960
1961 d = dst->conf;
1962 s = src->conf;
1963
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001964#define C(n) \
1965do { \
1966 if (s->n && !d->n) \
1967 d->n = os_strdup(s->n); \
1968} while (0)
1969
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970 C(device_name);
1971 C(manufacturer);
1972 C(model_name);
1973 C(model_number);
1974 C(serial_number);
1975 C(config_methods);
1976#undef C
1977
1978 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1979 os_memcpy(d->sec_device_type, s->sec_device_type,
1980 sizeof(d->sec_device_type));
1981 d->num_sec_device_types = s->num_sec_device_types;
1982
1983 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001984 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001986 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001987 d->max_num_sta = s->max_num_sta;
1988 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001989 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001990 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001991 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001992 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001993 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001994 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001995 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001996
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001997 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
1998 !d->wps_nfc_pw_from_config) {
1999 wpabuf_free(d->wps_nfc_dh_privkey);
2000 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002001 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
2002 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
2003 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002004 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002005 d->go_interworking = s->go_interworking;
2006 d->go_access_network_type = s->go_access_network_type;
2007 d->go_internet = s->go_internet;
2008 d->go_venue_group = s->go_venue_group;
2009 d->go_venue_type = s->go_venue_type;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010}
2011
2012
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002013static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
2014 char *ifname, size_t len)
2015{
2016 char *ifname_ptr = wpa_s->ifname;
2017
2018 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2019 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2020 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2021 }
2022
2023 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2024 if (os_strlen(ifname) >= IFNAMSIZ &&
2025 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002026 int res;
2027
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002028 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002029 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2030 if (os_snprintf_error(len, res) && len)
2031 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002032 }
2033}
2034
2035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2037 enum wpa_driver_if_type type)
2038{
2039 char ifname[120], force_ifname[120];
2040
2041 if (wpa_s->pending_interface_name[0]) {
2042 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2043 "- skip creation of a new one");
2044 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2045 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2046 "unknown?! ifname='%s'",
2047 wpa_s->pending_interface_name);
2048 return -1;
2049 }
2050 return 0;
2051 }
2052
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002053 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002054 force_ifname[0] = '\0';
2055
2056 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2057 ifname);
2058 wpa_s->p2p_group_idx++;
2059
2060 wpa_s->pending_interface_type = type;
2061 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2062 wpa_s->pending_interface_addr, NULL) < 0) {
2063 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2064 "interface");
2065 return -1;
2066 }
2067
2068 if (force_ifname[0]) {
2069 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2070 force_ifname);
2071 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2072 sizeof(wpa_s->pending_interface_name));
2073 } else
2074 os_strlcpy(wpa_s->pending_interface_name, ifname,
2075 sizeof(wpa_s->pending_interface_name));
2076 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2077 MACSTR, wpa_s->pending_interface_name,
2078 MAC2STR(wpa_s->pending_interface_addr));
2079
2080 return 0;
2081}
2082
2083
2084static void wpas_p2p_remove_pending_group_interface(
2085 struct wpa_supplicant *wpa_s)
2086{
2087 if (!wpa_s->pending_interface_name[0] ||
2088 is_zero_ether_addr(wpa_s->pending_interface_addr))
2089 return; /* No pending virtual interface */
2090
2091 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2092 wpa_s->pending_interface_name);
2093 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2094 wpa_s->pending_interface_name);
2095 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2096 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002097 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098}
2099
2100
2101static struct wpa_supplicant *
2102wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2103{
2104 struct wpa_interface iface;
2105 struct wpa_supplicant *group_wpa_s;
2106
2107 if (!wpa_s->pending_interface_name[0]) {
2108 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2109 if (!wpas_p2p_create_iface(wpa_s))
2110 return NULL;
2111 /*
2112 * Something has forced us to remove the pending interface; try
2113 * to create a new one and hope for the best that we will get
2114 * the same local address.
2115 */
2116 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2117 WPA_IF_P2P_CLIENT) < 0)
2118 return NULL;
2119 }
2120
2121 os_memset(&iface, 0, sizeof(iface));
2122 iface.ifname = wpa_s->pending_interface_name;
2123 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002124 if (wpa_s->conf->ctrl_interface == NULL &&
2125 wpa_s->parent != wpa_s &&
2126 wpa_s->p2p_mgmt &&
2127 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2128 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2129 else
2130 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002131 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002132 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133 if (group_wpa_s == NULL) {
2134 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2135 "wpa_supplicant interface");
2136 return NULL;
2137 }
2138 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2140 P2P_GROUP_INTERFACE_CLIENT;
2141 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002142 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143
2144 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2145
2146 return group_wpa_s;
2147}
2148
2149
2150static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2151 void *timeout_ctx)
2152{
2153 struct wpa_supplicant *wpa_s = eloop_ctx;
2154 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002155 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002156}
2157
2158
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002159static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2160 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002161{
2162 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002163 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 if (wpa_s->global->p2p)
2165 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002166 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167}
2168
2169
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002170static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2171{
2172 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2173 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002174 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002175 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002176 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002177 wpa_s->global->p2p_fail_on_wps_complete = 0;
2178}
2179
2180
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002181void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2182{
2183 if (wpa_s->global->p2p_group_formation != wpa_s)
2184 return;
2185 /* Speed up group formation timeout since this cannot succeed */
2186 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002187 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002188 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002189 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002190}
2191
2192
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002193static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194{
2195 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002196 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002197
2198 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2199 wpa_drv_cancel_remain_on_channel(wpa_s);
2200 wpa_s->off_channel_freq = 0;
2201 wpa_s->roc_waiting_drv_freq = 0;
2202 }
2203
2204 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002205 wpa_msg_global(wpa_s, MSG_INFO,
2206 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2207 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002208 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 wpas_p2p_remove_pending_group_interface(wpa_s);
2210 return;
2211 }
2212
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002213 if (!res->role_go) {
2214 /* Inform driver of the operating channel of GO. */
2215 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2216 }
2217
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002218 if (wpa_s->p2p_go_ht40)
2219 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002220 if (wpa_s->p2p_go_vht)
2221 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002222 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2223 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002224
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002225 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2226 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2227 " wps_method=%s",
2228 res->role_go ? "GO" : "client", res->freq, res->ht40,
2229 MAC2STR(res->peer_device_addr),
2230 MAC2STR(res->peer_interface_addr),
2231 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002232 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233
Dmitry Shmidt04949592012-07-19 12:16:46 -07002234 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2235 struct wpa_ssid *ssid;
2236 ssid = wpa_config_get_network(wpa_s->conf,
2237 wpa_s->p2p_persistent_id);
2238 if (ssid && ssid->disabled == 2 &&
2239 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2240 size_t len = os_strlen(ssid->passphrase);
2241 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2242 "on requested persistent group");
2243 os_memcpy(res->passphrase, ssid->passphrase, len);
2244 res->passphrase[len] = '\0';
2245 }
2246 }
2247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002249 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2251 if (group_wpa_s == NULL) {
2252 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002253 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2254 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002255 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 return;
2257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002258 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2259 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002261 group_wpa_s = wpa_s->parent;
2262 wpa_s->global->p2p_group_formation = group_wpa_s;
2263 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002264 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002267 group_wpa_s->p2p_in_provisioning = 1;
2268 group_wpa_s->p2pdev = wpa_s;
2269 if (group_wpa_s != wpa_s) {
2270 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2271 sizeof(group_wpa_s->p2p_pin));
2272 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2273 }
2274 if (res->role_go) {
2275 wpas_start_wps_go(group_wpa_s, res, 1);
2276 } else {
2277 os_get_reltime(&group_wpa_s->scan_min_time);
2278 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 }
2280
2281 wpa_s->p2p_long_listen = 0;
2282 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2283
2284 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2285 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2286 (res->peer_config_timeout % 100) * 10000,
2287 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2288}
2289
2290
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002291static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2292 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293{
2294 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002295 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002296 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2297 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002298
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002299 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300}
2301
2302
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002303static void wpas_dev_found(void *ctx, const u8 *addr,
2304 const struct p2p_peer_info *info,
2305 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002306{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002307 u8 *wfd_dev_info = NULL;
2308 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002309#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310 struct wpa_supplicant *wpa_s = ctx;
2311 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002312 char *wfd_dev_info_hex = NULL;
2313
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002314#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002315 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2316 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002317 if (wfd_dev_info_hex) {
2318 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2319 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2320 // Only used for notification, so not handling error.
2321 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2322 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002323#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002324
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002325 if (info->p2ps_instance) {
2326 char str[256];
2327 const u8 *buf = wpabuf_head(info->p2ps_instance);
2328 size_t len = wpabuf_len(info->p2ps_instance);
2329
2330 while (len) {
2331 u32 id;
2332 u16 methods;
2333 u8 str_len;
2334
2335 if (len < 4 + 2 + 1)
2336 break;
2337 id = WPA_GET_LE32(buf);
2338 buf += sizeof(u32);
2339 methods = WPA_GET_BE16(buf);
2340 buf += sizeof(u16);
2341 str_len = *buf++;
2342 if (str_len > len - 4 - 2 - 1)
2343 break;
2344 os_memcpy(str, buf, str_len);
2345 str[str_len] = '\0';
2346 buf += str_len;
2347 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2348
2349 wpa_msg_global(wpa_s, MSG_INFO,
2350 P2P_EVENT_DEVICE_FOUND MACSTR
2351 " p2p_dev_addr=" MACSTR
2352 " pri_dev_type=%s name='%s'"
2353 " config_methods=0x%x"
2354 " dev_capab=0x%x"
2355 " group_capab=0x%x"
2356 " adv_id=%x asp_svc=%s%s",
2357 MAC2STR(addr),
2358 MAC2STR(info->p2p_device_addr),
2359 wps_dev_type_bin2str(
2360 info->pri_dev_type,
2361 devtype, sizeof(devtype)),
2362 info->device_name, methods,
2363 info->dev_capab, info->group_capab,
2364 id, str,
2365 info->vendor_elems ?
2366 " vendor_elems=1" : "");
2367 }
2368 goto done;
2369 }
2370
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002371 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2372 " p2p_dev_addr=" MACSTR
2373 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002374 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002375 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2376 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2377 sizeof(devtype)),
2378 info->device_name, info->config_methods,
2379 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002380 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002381 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002382 info->vendor_elems ? " vendor_elems=1" : "",
2383 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002385done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002386 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002387#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002388
Roshan Piusfd2fd662017-01-23 13:41:57 -08002389 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2390 wfd_dev_info_len, new_device);
2391 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392}
2393
2394
2395static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2396{
2397 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002398
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002399 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2400 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2403}
2404
2405
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002406static void wpas_find_stopped(void *ctx)
2407{
2408 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002409
2410 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2411 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2412
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002413 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002414 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002415}
2416
2417
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002418struct wpas_p2p_listen_work {
2419 unsigned int freq;
2420 unsigned int duration;
2421 struct wpabuf *probe_resp_ie;
2422};
2423
2424
2425static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2426{
2427 if (lwork == NULL)
2428 return;
2429 wpabuf_free(lwork->probe_resp_ie);
2430 os_free(lwork);
2431}
2432
2433
2434static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2435{
2436 struct wpas_p2p_listen_work *lwork;
2437
2438 if (!wpa_s->p2p_listen_work)
2439 return;
2440
2441 lwork = wpa_s->p2p_listen_work->ctx;
2442 wpas_p2p_listen_work_free(lwork);
2443 radio_work_done(wpa_s->p2p_listen_work);
2444 wpa_s->p2p_listen_work = NULL;
2445}
2446
2447
2448static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2449{
2450 struct wpa_supplicant *wpa_s = work->wpa_s;
2451 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002452 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002453
2454 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002455 if (work->started) {
2456 wpa_s->p2p_listen_work = NULL;
2457 wpas_stop_listen(wpa_s);
2458 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002459 wpas_p2p_listen_work_free(lwork);
2460 return;
2461 }
2462
2463 wpa_s->p2p_listen_work = work;
2464
2465 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2466
2467 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2468 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2469 "report received Probe Request frames");
2470 wpas_p2p_listen_work_done(wpa_s);
2471 return;
2472 }
2473
2474 wpa_s->pending_listen_freq = lwork->freq;
2475 wpa_s->pending_listen_duration = lwork->duration;
2476
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002477 duration = lwork->duration;
2478#ifdef CONFIG_TESTING_OPTIONS
2479 if (wpa_s->extra_roc_dur) {
2480 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2481 duration, duration + wpa_s->extra_roc_dur);
2482 duration += wpa_s->extra_roc_dur;
2483 }
2484#endif /* CONFIG_TESTING_OPTIONS */
2485
2486 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002487 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2488 "to remain on channel (%u MHz) for Listen "
2489 "state", lwork->freq);
2490 wpas_p2p_listen_work_done(wpa_s);
2491 wpa_s->pending_listen_freq = 0;
2492 return;
2493 }
2494 wpa_s->off_channel_freq = 0;
2495 wpa_s->roc_waiting_drv_freq = lwork->freq;
2496}
2497
2498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499static int wpas_start_listen(void *ctx, unsigned int freq,
2500 unsigned int duration,
2501 const struct wpabuf *probe_resp_ie)
2502{
2503 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002504 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002506 if (wpa_s->p2p_listen_work) {
2507 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508 return -1;
2509 }
2510
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002511 lwork = os_zalloc(sizeof(*lwork));
2512 if (lwork == NULL)
2513 return -1;
2514 lwork->freq = freq;
2515 lwork->duration = duration;
2516 if (probe_resp_ie) {
2517 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2518 if (lwork->probe_resp_ie == NULL) {
2519 wpas_p2p_listen_work_free(lwork);
2520 return -1;
2521 }
2522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002524 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2525 lwork) < 0) {
2526 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 return -1;
2528 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529
2530 return 0;
2531}
2532
2533
2534static void wpas_stop_listen(void *ctx)
2535{
2536 struct wpa_supplicant *wpa_s = ctx;
2537 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2538 wpa_drv_cancel_remain_on_channel(wpa_s);
2539 wpa_s->off_channel_freq = 0;
2540 wpa_s->roc_waiting_drv_freq = 0;
2541 }
2542 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002543
2544 /*
2545 * Don't cancel Probe Request RX reporting for a connected P2P Client
2546 * handling Probe Request frames.
2547 */
2548 if (!wpa_s->p2p_cli_probe)
2549 wpa_drv_probe_req_report(wpa_s, 0);
2550
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002551 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552}
2553
2554
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002555static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2556 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557{
2558 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002559 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2560 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561}
2562
2563
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2565 const u8 *peer, const char *params,
2566 unsigned int generated_pin)
2567{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002568 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2569 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570}
2571
2572
2573static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2574 const u8 *peer, const char *params)
2575{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002576 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2577 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578}
2579
2580
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002581static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2582 const u8 *dev_addr, const u8 *pri_dev_type,
2583 const char *dev_name, u16 supp_config_methods,
2584 u8 dev_capab, u8 group_capab, const u8 *group_id,
2585 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586{
2587 struct wpa_supplicant *wpa_s = ctx;
2588 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002589 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 u8 empty_dev_type[8];
2591 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002592 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002593 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002594
2595 if (group_id) {
2596 for (group = wpa_s->global->ifaces; group; group = group->next)
2597 {
2598 struct wpa_ssid *s = group->current_ssid;
2599 if (s != NULL &&
2600 s->mode == WPAS_MODE_P2P_GO &&
2601 group_id_len - ETH_ALEN == s->ssid_len &&
2602 os_memcmp(group_id + ETH_ALEN, s->ssid,
2603 s->ssid_len) == 0)
2604 break;
2605 }
2606 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607
2608 if (pri_dev_type == NULL) {
2609 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2610 pri_dev_type = empty_dev_type;
2611 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002612 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2613 " pri_dev_type=%s name='%s' config_methods=0x%x "
2614 "dev_capab=0x%x group_capab=0x%x%s%s",
2615 MAC2STR(dev_addr),
2616 wps_dev_type_bin2str(pri_dev_type, devtype,
2617 sizeof(devtype)),
2618 dev_name, supp_config_methods, dev_capab, group_capab,
2619 group ? " group=" : "",
2620 group ? group->ifname : "");
2621 if (os_snprintf_error(sizeof(params), res))
2622 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002626 if (wps_generate_pin(&generated_pin) < 0) {
2627 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2628 wpas_notify_p2p_provision_discovery(
2629 wpa_s, peer, 0 /* response */,
2630 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2631 return;
2632 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633 wpas_prov_disc_local_display(wpa_s, peer, params,
2634 generated_pin);
2635 } else if (config_methods & WPS_CONFIG_KEYPAD)
2636 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2637 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002638 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2639 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002640
2641 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2642 P2P_PROV_DISC_SUCCESS,
2643 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644}
2645
2646
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002647static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648{
2649 struct wpa_supplicant *wpa_s = ctx;
2650 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002651 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002653 if (wpa_s->pending_pd_before_join &&
2654 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2655 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2656 wpa_s->pending_pd_before_join = 0;
2657 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2658 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002659 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002660 return;
2661 }
2662
Dmitry Shmidt04949592012-07-19 12:16:46 -07002663 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002664 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2665 int res;
2666
2667 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2668 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2669 if (os_snprintf_error(sizeof(params), res))
2670 params[sizeof(params) - 1] = '\0';
2671 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002672 params[0] = '\0';
2673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002675 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002677 if (wps_generate_pin(&generated_pin) < 0) {
2678 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2679 wpas_notify_p2p_provision_discovery(
2680 wpa_s, peer, 0 /* response */,
2681 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2682 return;
2683 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002684 wpas_prov_disc_local_display(wpa_s, peer, params,
2685 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002686 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002687 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2688 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689
Jouni Malinen75ecf522011-06-27 15:19:46 -07002690 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2691 P2P_PROV_DISC_SUCCESS,
2692 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693}
2694
2695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002696static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002697 enum p2p_prov_disc_status status,
2698 u32 adv_id, const u8 *adv_mac,
2699 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002700{
2701 struct wpa_supplicant *wpa_s = ctx;
2702
Dmitry Shmidt04949592012-07-19 12:16:46 -07002703 if (wpa_s->p2p_fallback_to_go_neg) {
2704 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2705 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002706 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002707 P2P_EVENT_FALLBACK_TO_GO_NEG
2708 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002709 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2710 return;
2711 }
2712
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002713 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002714 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002715 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2716 "join-existing-group operation (no ACK for PD "
2717 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002718 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002719 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002720 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002721
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002722 if (adv_id && adv_mac && deferred_session_resp) {
2723 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2724 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2725 " deferred_session_resp='%s'",
2726 MAC2STR(peer), status, adv_id,
2727 deferred_session_resp);
2728 } else if (adv_id && adv_mac) {
2729 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2730 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2731 MAC2STR(peer), status, adv_id);
2732 } else {
2733 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2734 " p2p_dev_addr=" MACSTR " status=%d",
2735 MAC2STR(peer), status);
2736 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002737
Jouni Malinen75ecf522011-06-27 15:19:46 -07002738 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2739 status, 0, 0);
2740}
2741
2742
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002743static int freq_included(struct wpa_supplicant *wpa_s,
2744 const struct p2p_channels *channels,
2745 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002746{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002747 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2748 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2749 return 1;
2750 return 0;
2751}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002752
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002753
2754static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2755{
2756 unsigned int num = P2P_MAX_CHANNELS;
2757 int *common_freqs;
2758 int ret;
2759
2760 p2p_go_dump_common_freqs(wpa_s);
2761 common_freqs = os_calloc(num, sizeof(int));
2762 if (!common_freqs)
2763 return;
2764
2765 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2766 if (ret < 0) {
2767 wpa_dbg(wpa_s, MSG_DEBUG,
2768 "P2P: Failed to get group common freqs");
2769 os_free(common_freqs);
2770 return;
2771 }
2772
2773 os_free(wpa_s->p2p_group_common_freqs);
2774 wpa_s->p2p_group_common_freqs = common_freqs;
2775 wpa_s->p2p_group_common_freqs_num = num;
2776 p2p_go_dump_common_freqs(wpa_s);
2777}
2778
2779
2780/*
2781 * Check if the given frequency is one of the possible operating frequencies
2782 * set after the completion of the GO Negotiation.
2783 */
2784static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2785{
2786 unsigned int i;
2787
2788 p2p_go_dump_common_freqs(wpa_s);
2789
2790 /* assume no restrictions */
2791 if (!wpa_s->p2p_group_common_freqs_num)
2792 return 1;
2793
2794 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2795 if (wpa_s->p2p_group_common_freqs[i] == freq)
2796 return 1;
2797 }
2798 return 0;
2799}
2800
2801
2802static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2803 struct sta_info *sta, void *ctx)
2804{
2805 int *ecsa_support = ctx;
2806
2807 *ecsa_support &= sta->ecsa_supported;
2808
2809 return 0;
2810}
2811
2812
2813/* Check if all the peers support eCSA */
2814static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2815{
2816 int ecsa_support = 1;
2817
2818 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2819 &ecsa_support);
2820
2821 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002822}
2823
2824
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002825/**
2826 * Pick the best frequency to use from all the currently used frequencies.
2827 */
2828static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2829 struct wpa_used_freq_data *freqs,
2830 unsigned int num)
2831{
2832 unsigned int i, c;
2833
2834 /* find a candidate freq that is supported by P2P */
2835 for (c = 0; c < num; c++)
2836 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2837 break;
2838
2839 if (c == num)
2840 return 0;
2841
2842 /* once we have a candidate, try to find a 'better' one */
2843 for (i = c + 1; i < num; i++) {
2844 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2845 continue;
2846
2847 /*
2848 * 1. Infrastructure station interfaces have higher preference.
2849 * 2. P2P Clients have higher preference.
2850 * 3. All others.
2851 */
2852 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2853 c = i;
2854 break;
2855 }
2856
2857 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2858 c = i;
2859 }
2860 return freqs[c].freq;
2861}
2862
2863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2865 const u8 *go_dev_addr, const u8 *ssid,
2866 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002867 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002868 const struct p2p_channels *channels,
2869 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870{
2871 struct wpa_supplicant *wpa_s = ctx;
2872 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002873 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002875 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876
2877 if (!persistent_group) {
2878 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002879 " to join an active group (SSID: %s)",
2880 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002881 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2882 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2883 == 0 ||
2884 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2885 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2886 "authorized invitation");
2887 goto accept_inv;
2888 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002889
2890#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002891 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2892 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002893 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002894 wpa_s->p2p_wps_method = WPS_NFC;
2895 wpa_s->pending_join_wps_method = WPS_NFC;
2896 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002897 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002898 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002899 bssid, ETH_ALEN);
2900 goto accept_inv;
2901 }
2902#endif /* CONFIG_WPS_NFC */
2903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 /*
2905 * Do not accept the invitation automatically; notify user and
2906 * request approval.
2907 */
2908 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2909 }
2910
2911 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2912 if (grp) {
2913 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2914 "running persistent group");
2915 if (*go)
2916 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2917 goto accept_inv;
2918 }
2919
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002920 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2921 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2922 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2923 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002924 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002925 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002926 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2927
2928 for (s = wpa_s->conf->ssid; s; s = s->next) {
2929 if (s->disabled == 2 &&
2930 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2931 s->ssid_len == ssid_len &&
2932 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2933 break;
2934 }
2935
2936 if (!s) {
2937 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2938 " requested reinvocation of an unknown group",
2939 MAC2STR(sa));
2940 return P2P_SC_FAIL_UNKNOWN_GROUP;
2941 }
2942
2943 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2944 *go = 1;
2945 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2946 wpa_printf(MSG_DEBUG, "P2P: The only available "
2947 "interface is already in use - reject "
2948 "invitation");
2949 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2950 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002951 if (wpa_s->p2p_mgmt)
2952 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2953 ETH_ALEN);
2954 else
2955 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956 } else if (s->mode == WPAS_MODE_P2P_GO) {
2957 *go = 1;
2958 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2959 {
2960 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2961 "interface address for the group");
2962 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2963 }
2964 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2965 ETH_ALEN);
2966 }
2967
2968accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002969 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2970
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002971 best_freq = 0;
2972 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2973 sizeof(struct wpa_used_freq_data));
2974 if (freqs) {
2975 int num_channels = wpa_s->num_multichan_concurrent;
2976 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2977 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2978 os_free(freqs);
2979 }
2980
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002981 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002982 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002983 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002984 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002985
2986 if (wpa_s->num_multichan_concurrent < 2 ||
2987 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2988 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 -07002989 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002991 }
2992
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002993 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2994 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002995 if (*go == 0) {
2996 /* We are the client */
2997 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2998 "running a GO but we are capable of MCC, "
2999 "figure out the best channel to use");
3000 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003001 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003002 /* We are the GO, and *force_freq is not in the
3003 * intersection */
3004 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3005 "in intersection but we are capable of MCC, "
3006 "figure out the best channel to use",
3007 *force_freq);
3008 *force_freq = 0;
3009 }
3010 }
3011
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012 return P2P_SC_SUCCESS;
3013}
3014
3015
3016static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3017 const u8 *ssid, size_t ssid_len,
3018 const u8 *go_dev_addr, u8 status,
3019 int op_freq)
3020{
3021 struct wpa_supplicant *wpa_s = ctx;
3022 struct wpa_ssid *s;
3023
3024 for (s = wpa_s->conf->ssid; s; s = s->next) {
3025 if (s->disabled == 2 &&
3026 s->ssid_len == ssid_len &&
3027 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3028 break;
3029 }
3030
3031 if (status == P2P_SC_SUCCESS) {
3032 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003033 " was accepted; op_freq=%d MHz, SSID=%s",
3034 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003036 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003037 if (go) {
3038 wpa_msg_global(wpa_s, MSG_INFO,
3039 P2P_EVENT_INVITATION_ACCEPTED
3040 "sa=" MACSTR
3041 " persistent=%d freq=%d",
3042 MAC2STR(sa), s->id, op_freq);
3043 } else {
3044 wpa_msg_global(wpa_s, MSG_INFO,
3045 P2P_EVENT_INVITATION_ACCEPTED
3046 "sa=" MACSTR
3047 " persistent=%d",
3048 MAC2STR(sa), s->id);
3049 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003051 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003052 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3053 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003054 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003055 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003056 wpa_msg_global(wpa_s, MSG_INFO,
3057 P2P_EVENT_INVITATION_ACCEPTED
3058 "sa=" MACSTR " go_dev_addr=" MACSTR
3059 " bssid=" MACSTR " unknown-network",
3060 MAC2STR(sa), MAC2STR(go_dev_addr),
3061 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003063 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003064 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065 }
3066 return;
3067 }
3068
3069 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3070 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3071 " was rejected (status %u)", MAC2STR(sa), status);
3072 return;
3073 }
3074
3075 if (!s) {
3076 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003077 wpa_msg_global(wpa_s, MSG_INFO,
3078 P2P_EVENT_INVITATION_RECEIVED
3079 "sa=" MACSTR " go_dev_addr=" MACSTR
3080 " bssid=" MACSTR " unknown-network",
3081 MAC2STR(sa), MAC2STR(go_dev_addr),
3082 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003084 wpa_msg_global(wpa_s, MSG_INFO,
3085 P2P_EVENT_INVITATION_RECEIVED
3086 "sa=" MACSTR " go_dev_addr=" MACSTR
3087 " unknown-network",
3088 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003089 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003090 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3091 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 return;
3093 }
3094
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003095 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003096 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3097 "sa=" MACSTR " persistent=%d freq=%d",
3098 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003099 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003100 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3101 "sa=" MACSTR " persistent=%d",
3102 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003103 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003104 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3105 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106}
3107
3108
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003109static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3110 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003111 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003112{
3113 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003114 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003115
3116 if (ssid == NULL)
3117 return;
3118
3119 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003120 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003121 ETH_ALEN) == 0)
3122 break;
3123 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003124 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003125 if (ssid->mode != WPAS_MODE_P2P_GO &&
3126 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3127 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3128 "due to invitation result", ssid->id);
3129 wpas_notify_network_removed(wpa_s, ssid);
3130 wpa_config_remove_network(wpa_s->conf, ssid->id);
3131 return;
3132 }
3133 return; /* Peer not found in client list */
3134 }
3135
3136 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003137 "group %d client list%s",
3138 MAC2STR(peer), ssid->id,
3139 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003140 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3141 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3142 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003143 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003144 if (p2p_wpa_s->conf->update_config &&
3145 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003146 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003147}
3148
3149
3150static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3151 const u8 *peer)
3152{
3153 struct wpa_ssid *ssid;
3154
3155 wpa_s = wpa_s->global->p2p_invite_group;
3156 if (wpa_s == NULL)
3157 return; /* No known invitation group */
3158 ssid = wpa_s->current_ssid;
3159 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3160 !ssid->p2p_persistent_group)
3161 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003162 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003163 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003164 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003165}
3166
3167
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003168static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003169 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003170 const u8 *peer, int neg_freq,
3171 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003172{
3173 struct wpa_supplicant *wpa_s = ctx;
3174 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003175 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003176
3177 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003178 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3179 "status=%d " MACSTR,
3180 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003182 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3183 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 }
3185 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3186
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003187 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3188 status, MAC2STR(peer));
3189 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003190 struct wpa_supplicant *group_if =
3191 wpa_s->global->p2p_invite_group;
3192
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003193 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3194 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003195
3196 /*
3197 * Invitation to an active group. If this is successful and we
3198 * are the GO, set the client wait to postpone some concurrent
3199 * operations and to allow provisioning and connection to happen
3200 * more quickly.
3201 */
3202 if (status == P2P_SC_SUCCESS &&
3203 group_if && group_if->current_ssid &&
3204 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3205 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3206#ifdef CONFIG_TESTING_OPTIONS
3207 if (group_if->p2p_go_csa_on_inv) {
3208 wpa_printf(MSG_DEBUG,
3209 "Testing: force P2P GO CSA after invitation");
3210 eloop_cancel_timeout(
3211 wpas_p2p_reconsider_moving_go,
3212 wpa_s, NULL);
3213 eloop_register_timeout(
3214 0, 50000,
3215 wpas_p2p_reconsider_moving_go,
3216 wpa_s, NULL);
3217 }
3218#endif /* CONFIG_TESTING_OPTIONS */
3219 }
3220 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003223 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3224 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3225 "invitation exchange to indicate readiness for "
3226 "re-invocation");
3227 }
3228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003230 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3231 ssid = wpa_config_get_network(
3232 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003233 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003234 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 wpas_p2p_remove_pending_group_interface(wpa_s);
3236 return;
3237 }
3238
3239 ssid = wpa_config_get_network(wpa_s->conf,
3240 wpa_s->pending_invite_ssid_id);
3241 if (ssid == NULL) {
3242 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3243 "data matching with invitation");
3244 return;
3245 }
3246
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003247 /*
3248 * The peer could have missed our ctrl::ack frame for Invitation
3249 * Response and continue retransmitting the frame. To reduce the
3250 * likelihood of the peer not getting successful TX status for the
3251 * Invitation Response frame, wait a short time here before starting
3252 * the persistent group so that we will remain on the current channel to
3253 * acknowledge any possible retransmission from the peer.
3254 */
3255 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3256 "starting persistent group");
3257 os_sleep(0, 50000);
3258
Dmitry Shmidt15907092014-03-25 10:42:57 -07003259 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003260 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003261 freq = neg_freq;
3262 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003263 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003264 freq = peer_oper_freq;
3265 else
3266 freq = 0;
3267
3268 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3269 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003271 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003272 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003273 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003274 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003275 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003276 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003277 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003278 ssid->mode == WPAS_MODE_P2P_GO ?
3279 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003280 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003281}
3282
3283
Dmitry Shmidt04949592012-07-19 12:16:46 -07003284static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3285 unsigned int freq)
3286{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003287 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3288 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003289 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003290}
3291
3292
3293static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3294{
3295 reg->channel[reg->channels] = chan;
3296 reg->channels++;
3297}
3298
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003301 struct p2p_channels *chan,
3302 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303{
3304 int i, cla = 0;
3305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003306 wpa_s->global->p2p_24ghz_social_channels = 1;
3307
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003308 os_memset(cli_chan, 0, sizeof(*cli_chan));
3309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003310 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3311 "band");
3312
3313 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3314 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003315 chan->reg_class[cla].channels = 0;
3316 for (i = 0; i < 11; i++) {
3317 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3318 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3319 }
3320 if (chan->reg_class[cla].channels)
3321 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003322
3323 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3324 "band");
3325
3326 /* Operating class 115 - 5 GHz, channels 36-48 */
3327 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003328 chan->reg_class[cla].channels = 0;
3329 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3330 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3331 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3332 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3333 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3334 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3335 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3336 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3337 if (chan->reg_class[cla].channels)
3338 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339
3340 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3341 "band");
3342
3343 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3344 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003345 chan->reg_class[cla].channels = 0;
3346 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3347 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3348 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3349 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3350 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3351 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3352 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3353 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3354 if (chan->reg_class[cla].channels)
3355 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003356
3357 chan->reg_classes = cla;
3358 return 0;
3359}
3360
3361
Dmitry Shmidt04949592012-07-19 12:16:46 -07003362static int has_channel(struct wpa_global *global,
3363 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364{
3365 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003366 unsigned int freq;
3367
3368 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3369 chan * 5;
3370 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003371 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372
3373 for (i = 0; i < mode->num_channels; i++) {
3374 if (mode->channels[i].chan == chan) {
3375 if (flags)
3376 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003377 if (mode->channels[i].flag &
3378 (HOSTAPD_CHAN_DISABLED |
3379 HOSTAPD_CHAN_RADAR))
3380 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003381 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3382 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003383 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384 }
3385 }
3386
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003387 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003388}
3389
3390
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003391static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3392 struct hostapd_hw_modes *mode,
3393 u8 channel)
3394{
3395 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003396 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003397
3398 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3399 return 0;
3400
3401 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3402 /*
3403 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3404 * so the center channel is 6 channels away from the start/end.
3405 */
3406 if (channel >= center_channels[i] - 6 &&
3407 channel <= center_channels[i] + 6)
3408 return center_channels[i];
3409
3410 return 0;
3411}
3412
3413
3414static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3415 struct hostapd_hw_modes *mode,
3416 u8 channel, u8 bw)
3417{
3418 u8 center_chan;
3419 int i, flags;
3420 enum chan_allowed res, ret = ALLOWED;
3421
3422 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3423 if (!center_chan)
3424 return NOT_ALLOWED;
3425 if (center_chan >= 58 && center_chan <= 138)
3426 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3427
3428 /* check all the channels are available */
3429 for (i = 0; i < 4; i++) {
3430 int adj_chan = center_chan - 6 + i * 4;
3431
3432 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3433 if (res == NOT_ALLOWED)
3434 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003435 if (res == NO_IR)
3436 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003437
3438 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3439 return NOT_ALLOWED;
3440 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3441 return NOT_ALLOWED;
3442 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3443 return NOT_ALLOWED;
3444 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3445 return NOT_ALLOWED;
3446 }
3447
3448 return ret;
3449}
3450
3451
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003452static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3453 struct hostapd_hw_modes *mode,
3454 u8 channel)
3455{
3456 u8 center_channels[] = { 50, 114 };
3457 unsigned int i;
3458
3459 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3460 return 0;
3461
3462 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3463 /*
3464 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3465 * so the center channel is 14 channels away from the start/end.
3466 */
3467 if (channel >= center_channels[i] - 14 &&
3468 channel <= center_channels[i] + 14)
3469 return center_channels[i];
3470
3471 return 0;
3472}
3473
3474
3475static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3476 struct hostapd_hw_modes *mode,
3477 u8 channel, u8 bw)
3478{
3479 u8 center_chan;
3480 int i, flags;
3481 enum chan_allowed res, ret = ALLOWED;
3482
3483 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3484 if (!center_chan)
3485 return NOT_ALLOWED;
3486 /* VHT 160 MHz uses DFS channels in most countries. */
3487
3488 /* Check all the channels are available */
3489 for (i = 0; i < 8; i++) {
3490 int adj_chan = center_chan - 14 + i * 4;
3491
3492 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3493 if (res == NOT_ALLOWED)
3494 return NOT_ALLOWED;
3495
3496 if (res == NO_IR)
3497 ret = NO_IR;
3498
3499 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3500 return NOT_ALLOWED;
3501 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3502 return NOT_ALLOWED;
3503 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3504 return NOT_ALLOWED;
3505 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3506 return NOT_ALLOWED;
3507 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3508 return NOT_ALLOWED;
3509 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3510 return NOT_ALLOWED;
3511 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3512 return NOT_ALLOWED;
3513 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3514 return NOT_ALLOWED;
3515 }
3516
3517 return ret;
3518}
3519
3520
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003521static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3522 struct hostapd_hw_modes *mode,
3523 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003524{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003525 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003526 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003527
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003528 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3529 if (bw == BW40MINUS) {
3530 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3531 return NOT_ALLOWED;
3532 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3533 } else if (bw == BW40PLUS) {
3534 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3535 return NOT_ALLOWED;
3536 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3537 } else if (bw == BW80) {
3538 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003539 } else if (bw == BW160) {
3540 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003541 }
3542
3543 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3544 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003545 if (res == NO_IR || res2 == NO_IR)
3546 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003547 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003548}
3549
3550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003552 struct p2p_channels *chan,
3553 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003555 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003556 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003558 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3560 "of all supported channels; assume dualband "
3561 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003562 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003563 }
3564
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003565 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003567 for (op = 0; global_op_class[op].op_class; op++) {
3568 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003570 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003572 if (o->p2p == NO_P2P_SUPP)
3573 continue;
3574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003575 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 if (mode == NULL)
3577 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003578 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3579 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003581 enum chan_allowed res;
3582 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3583 if (res == ALLOWED) {
3584 if (reg == NULL) {
3585 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3586 o->op_class);
3587 reg = &chan->reg_class[cla];
3588 cla++;
3589 reg->reg_class = o->op_class;
3590 }
3591 reg->channel[reg->channels] = ch;
3592 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003593 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003594 wpa_s->conf->p2p_add_cli_chan) {
3595 if (cli_reg == NULL) {
3596 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3597 o->op_class);
3598 cli_reg = &cli_chan->reg_class[cli_cla];
3599 cli_cla++;
3600 cli_reg->reg_class = o->op_class;
3601 }
3602 cli_reg->channel[cli_reg->channels] = ch;
3603 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 }
3606 if (reg) {
3607 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3608 reg->channel, reg->channels);
3609 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003610 if (cli_reg) {
3611 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3612 cli_reg->channel, cli_reg->channels);
3613 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 }
3615
3616 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003617 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 return 0;
3620}
3621
3622
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003623int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3624 struct hostapd_hw_modes *mode, u8 channel)
3625{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003626 int op;
3627 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003628
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003629 for (op = 0; global_op_class[op].op_class; op++) {
3630 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003631 u8 ch;
3632
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003633 if (o->p2p == NO_P2P_SUPP)
3634 continue;
3635
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003636 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3637 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003638 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3639 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003640 continue;
3641 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003642 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003643 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003644 }
3645 }
3646 return 0;
3647}
3648
3649
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003650int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3651 struct hostapd_hw_modes *mode, u8 channel)
3652{
3653 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3654 return 0;
3655
3656 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3657}
3658
3659
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003660int wpas_p2p_get_vht160_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, BW160))
3664 return 0;
3665 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3666}
3667
3668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3670 size_t buf_len)
3671{
3672 struct wpa_supplicant *wpa_s = ctx;
3673
3674 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3675 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3676 break;
3677 }
3678 if (wpa_s == NULL)
3679 return -1;
3680
3681 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3682}
3683
3684
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003685struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3686 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003687{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003688 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3689 struct wpa_ssid *s = wpa_s->current_ssid;
3690 if (s == NULL)
3691 continue;
3692 if (s->mode != WPAS_MODE_P2P_GO &&
3693 s->mode != WPAS_MODE_AP &&
3694 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3695 continue;
3696 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003697 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003698 continue;
3699 return wpa_s;
3700 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003701
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003702 return NULL;
3703
3704}
3705
3706
3707struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3708 const u8 *peer_dev_addr)
3709{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003710 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3711 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003712 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003713 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003714 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3715 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003716 }
3717
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003718 return NULL;
3719}
3720
3721
3722static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3723{
3724 struct wpa_supplicant *wpa_s = ctx;
3725
3726 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003727}
3728
3729
Dmitry Shmidt18463232014-01-24 12:29:41 -08003730static int wpas_is_concurrent_session_active(void *ctx)
3731{
3732 struct wpa_supplicant *wpa_s = ctx;
3733 struct wpa_supplicant *ifs;
3734
3735 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3736 if (ifs == wpa_s)
3737 continue;
3738 if (ifs->wpa_state > WPA_ASSOCIATED)
3739 return 1;
3740 }
3741 return 0;
3742}
3743
3744
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003745static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3746{
3747 struct wpa_supplicant *wpa_s = ctx;
3748 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3749}
3750
3751
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003752int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3753 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003754{
3755 struct wpa_interface iface;
3756 struct wpa_supplicant *p2pdev_wpa_s;
3757 char ifname[100];
3758 char force_name[100];
3759 int ret;
3760
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003761 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3762 wpa_s->ifname);
3763 if (os_snprintf_error(sizeof(ifname), ret))
3764 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003765 force_name[0] = '\0';
3766 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3767 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3768 force_name, wpa_s->pending_interface_addr, NULL);
3769 if (ret < 0) {
3770 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3771 return ret;
3772 }
3773 os_strlcpy(wpa_s->pending_interface_name, ifname,
3774 sizeof(wpa_s->pending_interface_name));
3775
3776 os_memset(&iface, 0, sizeof(iface));
3777 iface.p2p_mgmt = 1;
3778 iface.ifname = wpa_s->pending_interface_name;
3779 iface.driver = wpa_s->driver->name;
3780 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003781
3782 /*
3783 * If a P2P Device configuration file was given, use it as the interface
3784 * configuration file (instead of using parent's configuration file.
3785 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003786 if (conf_p2p_dev) {
3787 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003788 iface.ctrl_interface = NULL;
3789 } else {
3790 iface.confname = wpa_s->confname;
3791 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3792 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003793
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003794 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003795 if (!p2pdev_wpa_s) {
3796 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3797 return -1;
3798 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003799
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003800 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003801 wpa_s->pending_interface_name[0] = '\0';
3802 return 0;
3803}
3804
3805
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003806static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3807 const u8 *noa, size_t noa_len)
3808{
3809 struct wpa_supplicant *wpa_s, *intf = ctx;
3810 char hex[100];
3811
3812 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3813 if (wpa_s->waiting_presence_resp)
3814 break;
3815 }
3816 if (!wpa_s) {
3817 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3818 return;
3819 }
3820 wpa_s->waiting_presence_resp = 0;
3821
3822 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3823 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3824 " status=%u noa=%s", MAC2STR(src), status, hex);
3825}
3826
3827
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003828static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3829 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003830 u8 *ret_ssid, size_t *ret_ssid_len,
3831 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003832{
3833 struct wpa_supplicant *wpa_s = ctx;
3834 struct wpa_ssid *s;
3835
3836 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3837 if (s) {
3838 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3839 *ret_ssid_len = s->ssid_len;
3840 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003841
3842 if (s->mode != WPAS_MODE_P2P_GO) {
3843 os_memset(intended_iface_addr, 0, ETH_ALEN);
3844 } else if (wpas_p2p_create_iface(wpa_s)) {
3845 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3846 return 0;
3847
3848 os_memcpy(intended_iface_addr,
3849 wpa_s->pending_interface_addr, ETH_ALEN);
3850 } else {
3851 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3852 ETH_ALEN);
3853 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003854 return 1;
3855 }
3856
3857 return 0;
3858}
3859
3860
3861static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003862 u8 *ssid, size_t *ssid_len, int *group_iface,
3863 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003864{
3865 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003866 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003867 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003868
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003869 /*
3870 * group_iface will be set to 1 only if a dedicated interface for P2P
3871 * role is required. First, we try to reuse an active GO. However,
3872 * if it is not present, we will try to reactivate an existing
3873 * persistent group and set group_iface to 1, so the caller will know
3874 * that the pending interface should be used.
3875 */
3876 *group_iface = 0;
3877
3878 if (freq)
3879 *freq = 0;
3880
3881 go = wpas_p2p_get_go_group(wpa_s);
3882 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003883 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003884 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003885 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003886 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3887 else
3888 return 0;
3889 } else {
3890 s = go->current_ssid;
3891 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3892 if (freq)
3893 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003894 }
3895
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003896 os_memcpy(ssid, s->ssid, s->ssid_len);
3897 *ssid_len = s->ssid_len;
3898
3899 return 1;
3900}
3901
3902
3903static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3904 const u8 *ssid, size_t ssid_len)
3905{
3906 struct wpa_supplicant *wpa_s = ctx;
3907 struct wpa_ssid *s;
3908 int save_config = 0;
3909 size_t i;
3910
3911 /* Start with our first choice of Persistent Groups */
3912 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3913 if (go && ssid && ssid_len &&
3914 s->ssid_len == ssid_len &&
3915 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3916 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3917 break;
3918
3919 /* Remove stale persistent group */
3920 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3921 wpa_config_remove_network(wpa_s->conf, s->id);
3922 save_config = 1;
3923 continue;
3924 }
3925
3926 for (i = 0; i < s->num_p2p_clients; i++) {
3927 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3928 peer, ETH_ALEN) != 0)
3929 continue;
3930
3931 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3932 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3933 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3934 break;
3935 }
3936 s->num_p2p_clients--;
3937 save_config = 1;
3938 }
3939
3940 if (save_config)
3941 p2p_config_write(wpa_s);
3942
3943 /* Return TRUE if valid SSID remains */
3944 return s != NULL;
3945}
3946
3947
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003948static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3949 const u8 *feat_cap, size_t feat_cap_len)
3950{
3951 static const char pref[] = " feature_cap=";
3952 int ret;
3953
3954 buf[0] = '\0';
3955
3956 /*
3957 * We expect a feature capability to contain at least one byte to be
3958 * reported. The string buffer provided by the caller function is
3959 * expected to be big enough to contain all bytes of the attribute for
3960 * known specifications. This function truncates the reported bytes if
3961 * the feature capability data exceeds the string buffer size.
3962 */
3963 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3964 return;
3965
3966 os_memcpy(buf, pref, sizeof(pref));
3967 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3968 buf_len - sizeof(pref) + 1,
3969 feat_cap, feat_cap_len);
3970
3971 if (ret != (2 * (int) feat_cap_len))
3972 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3973}
3974
3975
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003976static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3977 const u8 *adv_mac, const u8 *ses_mac,
3978 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3979 u8 conncap, int passwd_id,
3980 const u8 *persist_ssid,
3981 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003982 int prov_start, const char *session_info,
3983 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003984 unsigned int freq,
3985 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003986{
3987 struct wpa_supplicant *wpa_s = ctx;
3988 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003989 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003990 int save_config = 0;
3991 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003992 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003993
3994 if (!dev)
3995 return;
3996
3997 os_memset(mac, 0, ETH_ALEN);
3998 if (!adv_mac)
3999 adv_mac = mac;
4000 if (!ses_mac)
4001 ses_mac = mac;
4002 if (!grp_mac)
4003 grp_mac = mac;
4004
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004005 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
4006 feat_cap, feat_cap_len);
4007
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004008 if (prov_start) {
4009 if (session_info == NULL) {
4010 wpa_msg_global(wpa_s, MSG_INFO,
4011 P2P_EVENT_P2PS_PROVISION_START MACSTR
4012 " adv_id=%x conncap=%x"
4013 " adv_mac=" MACSTR
4014 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004015 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004016 MAC2STR(dev), adv_id, conncap,
4017 MAC2STR(adv_mac),
4018 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004019 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004020 } else {
4021 wpa_msg_global(wpa_s, MSG_INFO,
4022 P2P_EVENT_P2PS_PROVISION_START MACSTR
4023 " adv_id=%x conncap=%x"
4024 " adv_mac=" MACSTR
4025 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004026 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004027 MAC2STR(dev), adv_id, conncap,
4028 MAC2STR(adv_mac),
4029 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004030 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004031 }
4032 return;
4033 }
4034
4035 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4036 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4037
4038 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4039 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4040 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4041
4042 if (persistent_go && !persistent_go->num_p2p_clients) {
4043 /* remove empty persistent GO */
4044 wpa_config_remove_network(wpa_s->conf,
4045 persistent_go->id);
4046 }
4047
4048 wpa_msg_global(wpa_s, MSG_INFO,
4049 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4050 " status=%d"
4051 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004052 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004053 MAC2STR(dev), status,
4054 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004055 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004056 return;
4057 }
4058
4059 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004060 if (persist_ssid && persist_ssid_size)
4061 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4062 persist_ssid_size);
4063
4064 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4065 is_zero_ether_addr(grp_mac)) {
4066 wpa_dbg(wpa_s, MSG_ERROR,
4067 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4068 return;
4069 }
4070
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004071 for (;;) {
4072 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4073 if (!stale)
4074 break;
4075
4076 if (s && s->ssid_len == stale->ssid_len &&
4077 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4078 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4079 break;
4080
4081 /* Remove stale persistent group */
4082 if (stale->mode != WPAS_MODE_P2P_GO ||
4083 stale->num_p2p_clients <= 1) {
4084 wpa_config_remove_network(wpa_s->conf, stale->id);
4085 } else {
4086 size_t i;
4087
4088 for (i = 0; i < stale->num_p2p_clients; i++) {
4089 if (os_memcmp(stale->p2p_client_list +
4090 i * ETH_ALEN,
4091 dev, ETH_ALEN) == 0) {
4092 os_memmove(stale->p2p_client_list +
4093 i * ETH_ALEN,
4094 stale->p2p_client_list +
4095 (i + 1) * ETH_ALEN,
4096 (stale->num_p2p_clients -
4097 i - 1) * ETH_ALEN);
4098 break;
4099 }
4100 }
4101 stale->num_p2p_clients--;
4102 }
4103 save_config = 1;
4104 }
4105
4106 if (save_config)
4107 p2p_config_write(wpa_s);
4108
4109 if (s) {
4110 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4111 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4112
4113 if (persistent_go && s != persistent_go &&
4114 !persistent_go->num_p2p_clients) {
4115 /* remove empty persistent GO */
4116 wpa_config_remove_network(wpa_s->conf,
4117 persistent_go->id);
4118 /* Save config */
4119 }
4120
4121 wpa_msg_global(wpa_s, MSG_INFO,
4122 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4123 " status=%d"
4124 " adv_id=%x adv_mac=" MACSTR
4125 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004126 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004127 MAC2STR(dev), status,
4128 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004130 return;
4131 }
4132
4133 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004134 /*
4135 * We need to copy the interface name. Simply saving a
4136 * pointer isn't enough, since if we use pending_interface_name
4137 * it will be overwritten when the group is added.
4138 */
4139 char go_ifname[100];
4140
4141 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004142 if (!go_wpa_s) {
4143 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004144 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004145
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004146 if (!wpas_p2p_create_iface(wpa_s))
4147 os_memcpy(go_ifname, wpa_s->ifname,
4148 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004149 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004150 os_memcpy(go_ifname,
4151 wpa_s->pending_interface_name,
4152 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004153
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004154 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004155 wpas_p2ps_prov_complete(
4156 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4157 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004158 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004159 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4160 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004161 return;
4162 }
4163
4164 /* If PD Resp complete, start up the GO */
4165 if (response_done && persistent_go) {
4166 wpas_p2p_group_add_persistent(
4167 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004168 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004169 persistent_go->mode ==
4170 WPAS_MODE_P2P_GO ?
4171 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004172 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004173 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004174 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004175 }
4176
4177 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004178 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4179 ETH_ALEN);
4180 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004181 }
4182 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004183 os_memcpy(go_ifname, go_wpa_s->ifname,
4184 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004186 if (is_zero_ether_addr(grp_mac)) {
4187 wpa_dbg(go_wpa_s, MSG_DEBUG,
4188 "P2P: Setting PIN-1 for ANY");
4189 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4190 "12345670", NULL, 0,
4191 0);
4192 } else {
4193 wpa_dbg(go_wpa_s, MSG_DEBUG,
4194 "P2P: Setting PIN-1 for " MACSTR,
4195 MAC2STR(grp_mac));
4196 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4197 "12345670", NULL, 0,
4198 0);
4199 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004200
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004201 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4202 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004203 }
4204
4205 wpa_msg_global(wpa_s, MSG_INFO,
4206 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4207 " status=%d conncap=%x"
4208 " adv_id=%x adv_mac=" MACSTR
4209 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004210 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004211 MAC2STR(dev), status, conncap,
4212 adv_id, MAC2STR(adv_mac),
4213 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004214 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004215 return;
4216 }
4217
4218 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4219 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4220
4221 if (persistent_go && !persistent_go->num_p2p_clients) {
4222 /* remove empty persistent GO */
4223 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4224 }
4225
4226 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004227 char ssid_hex[32 * 2 + 1];
4228
4229 if (group_ssid)
4230 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4231 group_ssid, group_ssid_len);
4232 else
4233 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004234 wpa_msg_global(wpa_s, MSG_INFO,
4235 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4236 " status=%d conncap=%x"
4237 " adv_id=%x adv_mac=" MACSTR
4238 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004239 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004240 MAC2STR(dev), status, conncap,
4241 adv_id, MAC2STR(adv_mac),
4242 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004243 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4244 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004245 } else {
4246 wpa_msg_global(wpa_s, MSG_INFO,
4247 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4248 " status=%d conncap=%x"
4249 " adv_id=%x adv_mac=" MACSTR
4250 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004251 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004252 MAC2STR(dev), status, conncap,
4253 adv_id, MAC2STR(adv_mac),
4254 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004255 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004256 }
4257}
4258
4259
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004260static int _wpas_p2p_in_progress(void *ctx)
4261{
4262 struct wpa_supplicant *wpa_s = ctx;
4263 return wpas_p2p_in_progress(wpa_s);
4264}
4265
4266
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004267static int wpas_prov_disc_resp_cb(void *ctx)
4268{
4269 struct wpa_supplicant *wpa_s = ctx;
4270 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004271 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004272
4273 if (!wpa_s->global->pending_p2ps_group)
4274 return 0;
4275
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004276 freq = wpa_s->global->pending_p2ps_group_freq;
4277 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004278 wpa_s->global->pending_p2ps_group = 0;
4279
4280 if (wpas_p2p_get_go_group(wpa_s))
4281 return 0;
4282 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4283
4284 if (persistent_go) {
4285 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004286 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004287 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004288 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004289 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004290 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004291 }
4292
4293 return 1;
4294}
4295
4296
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004297static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4298 unsigned int *len,
4299 unsigned int *freq_list)
4300{
4301 struct wpa_supplicant *wpa_s = ctx;
4302
4303 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4304 WPA_IF_P2P_CLIENT, len, freq_list);
4305}
4306
4307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308/**
4309 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4310 * @global: Pointer to global data from wpa_supplicant_init()
4311 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4312 * Returns: 0 on success, -1 on failure
4313 */
4314int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4315{
4316 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 int i;
4318
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004319 if (wpa_s->conf->p2p_disabled)
4320 return 0;
4321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4323 return 0;
4324
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004325 if (global->p2p)
4326 return 0;
4327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004328 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004329 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004330 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004331 p2p.p2p_scan = wpas_p2p_scan;
4332 p2p.send_action = wpas_send_action;
4333 p2p.send_action_done = wpas_send_action_done;
4334 p2p.go_neg_completed = wpas_go_neg_completed;
4335 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4336 p2p.dev_found = wpas_dev_found;
4337 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004338 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 p2p.start_listen = wpas_start_listen;
4340 p2p.stop_listen = wpas_stop_listen;
4341 p2p.send_probe_resp = wpas_send_probe_resp;
4342 p2p.sd_request = wpas_sd_request;
4343 p2p.sd_response = wpas_sd_response;
4344 p2p.prov_disc_req = wpas_prov_disc_req;
4345 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004346 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347 p2p.invitation_process = wpas_invitation_process;
4348 p2p.invitation_received = wpas_invitation_received;
4349 p2p.invitation_result = wpas_invitation_result;
4350 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004351 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004352 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004353 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004354 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004355 p2p.get_persistent_group = wpas_get_persistent_group;
4356 p2p.get_go_info = wpas_get_go_info;
4357 p2p.remove_stale_groups = wpas_remove_stale_groups;
4358 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4359 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4360 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004361 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362
4363 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004364 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 p2p.dev_name = wpa_s->conf->device_name;
4366 p2p.manufacturer = wpa_s->conf->manufacturer;
4367 p2p.model_name = wpa_s->conf->model_name;
4368 p2p.model_number = wpa_s->conf->model_number;
4369 p2p.serial_number = wpa_s->conf->serial_number;
4370 if (wpa_s->wps) {
4371 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4372 p2p.config_methods = wpa_s->wps->config_methods;
4373 }
4374
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004375 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4376 wpa_printf(MSG_ERROR,
4377 "P2P: Failed to configure supported channel list");
4378 return -1;
4379 }
4380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381 if (wpa_s->conf->p2p_listen_reg_class &&
4382 wpa_s->conf->p2p_listen_channel) {
4383 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4384 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004385 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387 /*
4388 * Pick one of the social channels randomly as the listen
4389 * channel.
4390 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004391 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4392 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004393 wpa_printf(MSG_INFO,
4394 "P2P: No social channels supported by the driver - do not enable P2P");
4395 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004396 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004397 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004398 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004399 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4400 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401
4402 if (wpa_s->conf->p2p_oper_reg_class &&
4403 wpa_s->conf->p2p_oper_channel) {
4404 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4405 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4406 p2p.cfg_op_channel = 1;
4407 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4408 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4409
4410 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004412 * Use random operation channel from 2.4 GHz band social
4413 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4414 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004416 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4417 &p2p.op_channel) != 0) {
4418 wpa_printf(MSG_ERROR,
4419 "P2P: Failed to select random social channel as operation channel");
4420 return -1;
4421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422 p2p.cfg_op_channel = 0;
4423 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4424 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4425 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004426
4427 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4428 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4429 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4430 }
4431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4433 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4434 p2p.country[2] = 0x04;
4435 } else
4436 os_memcpy(p2p.country, "XX\x04", 3);
4437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4439 WPS_DEV_TYPE_LEN);
4440
4441 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4442 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4443 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4444
4445 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4446 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4447
4448 p2p.max_peers = 100;
4449
4450 if (wpa_s->conf->p2p_ssid_postfix) {
4451 p2p.ssid_postfix_len =
4452 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4453 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4454 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4455 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4456 p2p.ssid_postfix_len);
4457 }
4458
4459 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4460
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004461 p2p.max_listen = wpa_s->max_remain_on_chan;
4462
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004463 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4464 wpa_s->conf->p2p_passphrase_len <= 63)
4465 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4466 else
4467 p2p.passphrase_len = 8;
4468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004469 global->p2p = p2p_init(&p2p);
4470 if (global->p2p == NULL)
4471 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004472 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004473
4474 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4475 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4476 continue;
4477 p2p_add_wps_vendor_extension(
4478 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4479 }
4480
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004481 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4482
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483 return 0;
4484}
4485
4486
4487/**
4488 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4489 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4490 *
4491 * This function deinitialize per-interface P2P data.
4492 */
4493void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4494{
4495 if (wpa_s->driver && wpa_s->drv_priv)
4496 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004497
4498 if (wpa_s->go_params) {
4499 /* Clear any stored provisioning info */
4500 p2p_clear_provisioning_info(
4501 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004502 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004503 }
4504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 os_free(wpa_s->go_params);
4506 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004507 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4509 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4510 wpa_s->p2p_long_listen = 0;
4511 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4512 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4513 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004514 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004515 wpas_p2p_listen_work_done(wpa_s);
4516 if (wpa_s->p2p_send_action_work) {
4517 os_free(wpa_s->p2p_send_action_work->ctx);
4518 radio_work_done(wpa_s->p2p_send_action_work);
4519 wpa_s->p2p_send_action_work = NULL;
4520 }
4521 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004523 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4524 wpa_s->p2p_oob_dev_pw = NULL;
4525
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004526 os_free(wpa_s->p2p_group_common_freqs);
4527 wpa_s->p2p_group_common_freqs = NULL;
4528 wpa_s->p2p_group_common_freqs_num = 0;
4529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004530 /* TODO: remove group interface from the driver if this wpa_s instance
4531 * is on top of a P2P group interface */
4532}
4533
4534
4535/**
4536 * wpas_p2p_deinit_global - Deinitialize global P2P module
4537 * @global: Pointer to global data from wpa_supplicant_init()
4538 *
4539 * This function deinitializes the global (per device) P2P module.
4540 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004541static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542{
4543 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004544
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004545 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004546
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004547 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548
4549 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4551 wpa_s = wpa_s->next;
4552 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553 tmp = global->ifaces;
4554 while (tmp &&
4555 (tmp == wpa_s ||
4556 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4557 tmp = tmp->next;
4558 }
4559 if (tmp == NULL)
4560 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004561 /* Disconnect from the P2P group and deinit the interface */
4562 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 }
4564
4565 /*
4566 * Deinit GO data on any possibly remaining interface (if main
4567 * interface is used as GO).
4568 */
4569 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4570 if (wpa_s->ap_iface)
4571 wpas_p2p_group_deinit(wpa_s);
4572 }
4573
4574 p2p_deinit(global->p2p);
4575 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004576 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004577}
4578
4579
4580static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4581{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004582 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004583 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584 if (wpa_s->drv_flags &
4585 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4586 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4587 return 1; /* P2P group requires a new interface in every case
4588 */
4589 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4590 return 0; /* driver does not support concurrent operations */
4591 if (wpa_s->global->ifaces->next)
4592 return 1; /* more that one interface already in use */
4593 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4594 return 1; /* this interface is already in use */
4595 return 0;
4596}
4597
4598
4599static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4600 const u8 *peer_addr,
4601 enum p2p_wps_method wps_method,
4602 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004603 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004604 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004606 if (persistent_group && wpa_s->conf->persistent_reconnect)
4607 persistent_group = 2;
4608
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004609 /*
4610 * Increase GO config timeout if HT40 is used since it takes some time
4611 * to scan channels for coex purposes before the BSS can be started.
4612 */
4613 p2p_set_config_timeout(wpa_s->global->p2p,
4614 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004616 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4617 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 persistent_group, ssid ? ssid->ssid : NULL,
4619 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004620 wpa_s->p2p_pd_before_go_neg, pref_freq,
4621 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4622 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623}
4624
4625
4626static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4627 const u8 *peer_addr,
4628 enum p2p_wps_method wps_method,
4629 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004630 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004631 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004633 if (persistent_group && wpa_s->conf->persistent_reconnect)
4634 persistent_group = 2;
4635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4637 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004639 ssid ? ssid->ssid_len : 0, pref_freq,
4640 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4641 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004642}
4643
4644
4645static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4646{
4647 wpa_s->p2p_join_scan_count++;
4648 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4649 wpa_s->p2p_join_scan_count);
4650 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4651 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4652 " for join operationg - stop join attempt",
4653 MAC2STR(wpa_s->pending_join_iface_addr));
4654 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655 if (wpa_s->p2p_auto_pd) {
4656 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004657 wpa_msg_global(wpa_s, MSG_INFO,
4658 P2P_EVENT_PROV_DISC_FAILURE
4659 " p2p_dev_addr=" MACSTR " status=N/A",
4660 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004661 return;
4662 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004663 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004664 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004665 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 }
4667}
4668
4669
Dmitry Shmidt04949592012-07-19 12:16:46 -07004670static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4671{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004672 int res;
4673 unsigned int num, i;
4674 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004675
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004676 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4677 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004678 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004679 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004681 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4682 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004683 if (!freqs)
4684 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004685
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004686 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4687 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004688
4689 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004690 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004691 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4692 freq);
4693 res = 0;
4694 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004695 }
4696 }
4697
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004698 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004699 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004701exit_free:
4702 os_free(freqs);
4703 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004704}
4705
4706
4707static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4708 const u8 *peer_dev_addr)
4709{
4710 struct wpa_bss *bss;
4711 int updated;
4712
4713 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4714 if (bss == NULL)
4715 return -1;
4716 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4717 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4718 "last scan");
4719 return 0;
4720 }
4721
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004722 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4723 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004724 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4725 "%ld.%06ld (%supdated in last scan)",
4726 bss->last_update.sec, bss->last_update.usec,
4727 updated ? "": "not ");
4728
4729 return updated;
4730}
4731
4732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4734 struct wpa_scan_results *scan_res)
4735{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004736 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737 int freq;
4738 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004740 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4741
4742 if (wpa_s->global->p2p_disabled)
4743 return;
4744
Dmitry Shmidt04949592012-07-19 12:16:46 -07004745 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4746 scan_res ? (int) scan_res->num : -1,
4747 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004748
4749 if (scan_res)
4750 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4751
Dmitry Shmidt04949592012-07-19 12:16:46 -07004752 if (wpa_s->p2p_auto_pd) {
4753 int join = wpas_p2p_peer_go(wpa_s,
4754 wpa_s->pending_join_dev_addr);
4755 if (join == 0 &&
4756 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4757 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004758 bss = wpa_bss_get_bssid_latest(
4759 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004760 if (bss) {
4761 freq = bss->freq;
4762 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4763 "the peer " MACSTR " at %d MHz",
4764 wpa_s->auto_pd_scan_retry,
4765 MAC2STR(wpa_s->
4766 pending_join_dev_addr),
4767 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004768 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 return;
4770 }
4771 }
4772
4773 if (join < 0)
4774 join = 0;
4775
4776 wpa_s->p2p_auto_pd = 0;
4777 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4778 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4779 MAC2STR(wpa_s->pending_join_dev_addr), join);
4780 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004781 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004782 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004783 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004784 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004785 wpa_msg_global(wpa_s, MSG_INFO,
4786 P2P_EVENT_PROV_DISC_FAILURE
4787 " p2p_dev_addr=" MACSTR " status=N/A",
4788 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004789 }
4790 return;
4791 }
4792
4793 if (wpa_s->p2p_auto_join) {
4794 int join = wpas_p2p_peer_go(wpa_s,
4795 wpa_s->pending_join_dev_addr);
4796 if (join < 0) {
4797 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4798 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004799 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004800 P2P_EVENT_FALLBACK_TO_GO_NEG
4801 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004802 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4803 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4804 wpa_s->p2p_persistent_group, 0, 0, 0,
4805 wpa_s->p2p_go_intent,
4806 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004807 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004808 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004809 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004810 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004811 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004812 wpa_s->p2p_go_max_oper_chwidth,
4813 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004814 return;
4815 }
4816
4817 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4818 "try to join the group", join ? "" :
4819 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004820 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004821 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004822 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004823 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004824 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004825 }
4826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004827 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4828 wpa_s->pending_join_iface_addr);
4829 if (freq < 0 &&
4830 p2p_get_interface_addr(wpa_s->global->p2p,
4831 wpa_s->pending_join_dev_addr,
4832 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004833 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4834 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4836 "address for join from " MACSTR " to " MACSTR
4837 " based on newly discovered P2P peer entry",
4838 MAC2STR(wpa_s->pending_join_iface_addr),
4839 MAC2STR(iface_addr));
4840 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4841 ETH_ALEN);
4842
4843 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4844 wpa_s->pending_join_iface_addr);
4845 }
4846 if (freq >= 0) {
4847 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4848 "from P2P peer table: %d MHz", freq);
4849 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004850 if (wpa_s->p2p_join_ssid_len) {
4851 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4852 MACSTR " and SSID %s",
4853 MAC2STR(wpa_s->pending_join_iface_addr),
4854 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4855 wpa_s->p2p_join_ssid_len));
4856 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4857 wpa_s->p2p_join_ssid,
4858 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004859 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004860 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4861 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4862 bss = wpa_bss_get_bssid_latest(wpa_s,
4863 wpa_s->pending_join_iface_addr);
4864 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004866 u8 dev_addr[ETH_ALEN];
4867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004868 freq = bss->freq;
4869 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004870 "from BSS table: %d MHz (SSID %s)", freq,
4871 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004872 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4873 dev_addr) == 0 &&
4874 os_memcmp(wpa_s->pending_join_dev_addr,
4875 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4876 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4877 ETH_ALEN) != 0) {
4878 wpa_printf(MSG_DEBUG,
4879 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4880 MAC2STR(dev_addr),
4881 MAC2STR(wpa_s->pending_join_dev_addr));
4882 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4883 ETH_ALEN);
4884 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885 }
4886 if (freq > 0) {
4887 u16 method;
4888
Dmitry Shmidt04949592012-07-19 12:16:46 -07004889 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004890 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004891 P2P_EVENT_GROUP_FORMATION_FAILURE
4892 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004893 wpas_notify_p2p_group_formation_failure(
4894 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004895 return;
4896 }
4897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004898 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4899 "prior to joining an existing group (GO " MACSTR
4900 " freq=%u MHz)",
4901 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4902 wpa_s->pending_pd_before_join = 1;
4903
4904 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 case WPS_PIN_DISPLAY:
4906 method = WPS_CONFIG_KEYPAD;
4907 break;
4908 case WPS_PIN_KEYPAD:
4909 method = WPS_CONFIG_DISPLAY;
4910 break;
4911 case WPS_PBC:
4912 method = WPS_CONFIG_PUSHBUTTON;
4913 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004914 case WPS_P2PS:
4915 method = WPS_CONFIG_P2PS;
4916 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004917 default:
4918 method = 0;
4919 break;
4920 }
4921
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004922 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4923 wpa_s->pending_join_dev_addr) ==
4924 method)) {
4925 /*
4926 * We have already performed provision discovery for
4927 * joining the group. Proceed directly to join
4928 * operation without duplicated provision discovery. */
4929 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4930 "with " MACSTR " already done - proceed to "
4931 "join",
4932 MAC2STR(wpa_s->pending_join_dev_addr));
4933 wpa_s->pending_pd_before_join = 0;
4934 goto start;
4935 }
4936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004938 wpa_s->pending_join_dev_addr,
4939 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004940 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4942 "Discovery Request before joining an "
4943 "existing group");
4944 wpa_s->pending_pd_before_join = 0;
4945 goto start;
4946 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947 return;
4948 }
4949
4950 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4951 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4952 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4953 wpas_p2p_check_join_scan_limit(wpa_s);
4954 return;
4955
4956start:
4957 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004958 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
4959 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960}
4961
4962
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004963static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4964 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 int ret;
4967 struct wpa_driver_scan_params params;
4968 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004969 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004970 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004971 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972
4973 os_memset(&params, 0, sizeof(params));
4974
4975 /* P2P Wildcard SSID */
4976 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004977 if (ssid && ssid_len) {
4978 params.ssids[0].ssid = ssid;
4979 params.ssids[0].ssid_len = ssid_len;
4980 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4981 wpa_s->p2p_join_ssid_len = ssid_len;
4982 } else {
4983 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4984 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4985 wpa_s->p2p_join_ssid_len = 0;
4986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987
4988 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004989 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4990 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4991 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 if (wps_ie == NULL) {
4993 wpas_p2p_scan_res_join(wpa_s, NULL);
4994 return;
4995 }
4996
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004997 if (!freq) {
4998 int oper_freq;
4999 /*
5000 * If freq is not provided, check the operating freq of the GO
5001 * and use a single channel scan on if possible.
5002 */
5003 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5004 wpa_s->pending_join_iface_addr);
5005 if (oper_freq > 0)
5006 freq = oper_freq;
5007 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005008 if (freq > 0) {
5009 freqs[0] = freq;
5010 params.freqs = freqs;
5011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005013 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5014 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5015 if (ies == NULL) {
5016 wpabuf_free(wps_ie);
5017 wpas_p2p_scan_res_join(wpa_s, NULL);
5018 return;
5019 }
5020 wpabuf_put_buf(ies, wps_ie);
5021 wpabuf_free(wps_ie);
5022
5023 bands = wpas_get_bands(wpa_s, freqs);
5024 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5025
5026 params.p2p_probe = 1;
5027 params.extra_ies = wpabuf_head(ies);
5028 params.extra_ies_len = wpabuf_len(ies);
5029
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005030 if (wpa_s->clear_driver_scan_cache) {
5031 wpa_printf(MSG_DEBUG,
5032 "Request driver to clear scan cache due to local BSS flush");
5033 params.only_new_results = 1;
5034 }
5035
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036 /*
5037 * Run a scan to update BSS table and start Provision Discovery once
5038 * the new scan results become available.
5039 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005040 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005041 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005042 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005043 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005044 wpa_s->own_scan_requested = 1;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005045 wpa_s->clear_driver_scan_cache = 0;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005047
5048 wpabuf_free(ies);
5049
5050 if (ret) {
5051 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5052 "try again later");
5053 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5054 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5055 wpas_p2p_check_join_scan_limit(wpa_s);
5056 }
5057}
5058
5059
Dmitry Shmidt04949592012-07-19 12:16:46 -07005060static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5061{
5062 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005063 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005064}
5065
5066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005068 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005069 int auto_join, int op_freq,
5070 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005071{
5072 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005073 MACSTR " dev " MACSTR " op_freq=%d)%s",
5074 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005075 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005076 if (ssid && ssid_len) {
5077 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5078 wpa_ssid_txt(ssid, ssid_len));
5079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080
Dmitry Shmidt04949592012-07-19 12:16:46 -07005081 wpa_s->p2p_auto_pd = 0;
5082 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5084 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5085 wpa_s->pending_join_wps_method = wps_method;
5086
5087 /* Make sure we are not running find during connection establishment */
5088 wpas_p2p_stop_find(wpa_s);
5089
5090 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005091 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 return 0;
5093}
5094
5095
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005096static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5097 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098{
5099 struct wpa_supplicant *group;
5100 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005101 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102
5103 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5104 if (group == NULL)
5105 return -1;
5106 if (group != wpa_s) {
5107 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5108 sizeof(group->p2p_pin));
5109 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005110 } else {
5111 /*
5112 * Need to mark the current interface for p2p_group_formation
5113 * when a separate group interface is not used. This is needed
5114 * to allow p2p_cancel stop a pending p2p_connect-join.
5115 * wpas_p2p_init_group_interface() addresses this for the case
5116 * where a separate group interface is used.
5117 */
5118 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119 }
5120
5121 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005122 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123
5124 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005125 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5127 ETH_ALEN);
5128 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005129 if (freq && ssid && ssid_len) {
5130 res.freq = freq;
5131 res.ssid_len = ssid_len;
5132 os_memcpy(res.ssid, ssid, ssid_len);
5133 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005134 if (ssid && ssid_len) {
5135 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5136 ssid, ssid_len);
5137 } else {
5138 bss = wpa_bss_get_bssid_latest(
5139 wpa_s, wpa_s->pending_join_iface_addr);
5140 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005141 if (bss) {
5142 res.freq = bss->freq;
5143 res.ssid_len = bss->ssid_len;
5144 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5145 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5146 bss->freq,
5147 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005148 } else if (ssid && ssid_len) {
5149 res.ssid_len = ssid_len;
5150 os_memcpy(res.ssid, ssid, ssid_len);
5151 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5152 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005153 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005154 }
5155
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005156 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5157 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5158 "starting client");
5159 wpa_drv_cancel_remain_on_channel(wpa_s);
5160 wpa_s->off_channel_freq = 0;
5161 wpa_s->roc_waiting_drv_freq = 0;
5162 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 wpas_start_wps_enrollee(group, &res);
5164
5165 /*
5166 * Allow a longer timeout for join-a-running-group than normal 15
5167 * second group formation timeout since the GO may not have authorized
5168 * our connection yet.
5169 */
5170 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5171 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5172 wpa_s, NULL);
5173
5174 return 0;
5175}
5176
5177
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005178static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005179 int *force_freq, int *pref_freq, int go,
5180 unsigned int *pref_freq_list,
5181 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005182{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005183 struct wpa_used_freq_data *freqs;
5184 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005185 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5186
5187 max_pref_freq = *num_pref_freq;
5188 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005189
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005190 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5191 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005192 if (!freqs)
5193 return -1;
5194
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005195 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5196 wpa_s->num_multichan_concurrent);
5197
5198 /*
5199 * It is possible that the total number of used frequencies is bigger
5200 * than the number of frequencies used for P2P, so get the system wide
5201 * number of unused frequencies.
5202 */
5203 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5204
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005205 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005206 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5207 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005208
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005209 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005210 int ret;
5211 if (go)
5212 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5213 else
5214 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5215 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005216 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5217 ieee80211_is_dfs(freq)) {
5218 /*
5219 * If freq is a DFS channel and DFS is offloaded
5220 * to the driver, allow P2P GO to use it.
5221 */
5222 wpa_printf(MSG_DEBUG,
5223 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5224 freq);
5225 } else {
5226 wpa_printf(MSG_DEBUG,
5227 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5228 freq);
5229 res = -3;
5230 goto exit_free;
5231 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005232 }
5233
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005234 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005235 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005236 freq_in_use = 1;
5237 }
5238
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005239 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005240 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5241 freq);
5242 res = -2;
5243 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005244 }
5245 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5246 "requested channel (%u MHz)", freq);
5247 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005248 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005249 }
5250
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005251 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005252
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005253 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5254 enum wpa_driver_if_type iface_type;
5255
5256 if (go)
5257 iface_type = WPA_IF_P2P_GO;
5258 else
5259 iface_type = WPA_IF_P2P_CLIENT;
5260
5261 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5262 best_freq, go);
5263
5264 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5265 &max_pref_freq,
5266 pref_freq_list);
5267 if (!res && max_pref_freq > 0) {
5268 *num_pref_freq = max_pref_freq;
5269 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005270 while (i < *num_pref_freq &&
5271 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005272 pref_freq_list[i]) ||
5273 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005274 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005275 wpa_printf(MSG_DEBUG,
5276 "P2P: preferred_freq_list[%d]=%d is disallowed",
5277 i, pref_freq_list[i]);
5278 i++;
5279 }
5280 if (i != *num_pref_freq) {
5281 best_freq = pref_freq_list[i];
5282 wpa_printf(MSG_DEBUG,
5283 "P2P: Using preferred_freq_list[%d]=%d",
5284 i, best_freq);
5285 } else {
5286 wpa_printf(MSG_DEBUG,
5287 "P2P: All driver preferred frequencies are disallowed for P2P use");
5288 *num_pref_freq = 0;
5289 }
5290 } else {
5291 wpa_printf(MSG_DEBUG,
5292 "P2P: No preferred frequency list available");
5293 }
5294 }
5295
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005296 /* We have a candidate frequency to use */
5297 if (best_freq > 0) {
5298 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005299 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005300 best_freq);
5301 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005302 } else {
5303 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 -07005304 best_freq);
5305 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005306 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005307 } else if (num_unused > 0) {
5308 wpa_printf(MSG_DEBUG,
5309 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5310 *force_freq = 0;
5311 } else {
5312 wpa_printf(MSG_DEBUG,
5313 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5314 res = -2;
5315 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005316 }
5317
5318exit_ok:
5319 res = 0;
5320exit_free:
5321 os_free(freqs);
5322 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005323}
5324
5325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326/**
5327 * wpas_p2p_connect - Request P2P Group Formation to be started
5328 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5329 * @peer_addr: Address of the peer P2P Device
5330 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5331 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005332 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333 * @join: Whether to join an existing group (as a client) instead of starting
5334 * Group Owner negotiation; @peer_addr is BSSID in that case
5335 * @auth: Whether to only authorize the connection instead of doing that and
5336 * initiating Group Owner negotiation
5337 * @go_intent: GO Intent or -1 to use default
5338 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005339 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005340 * @persistent_id: Persistent group credentials to use for forcing GO
5341 * parameters or -1 to generate new values (SSID/passphrase)
5342 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5343 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005344 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005345 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005346 * @vht_chwidth: Channel width supported by GO operating with VHT support
5347 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005348 * @group_ssid: Specific Group SSID for join or %NULL if not set
5349 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5351 * failure, -2 on failure due to channel not currently available,
5352 * -3 if forced channel is not supported
5353 */
5354int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5355 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005356 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005357 int go_intent, int freq, unsigned int vht_center_freq2,
5358 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005359 unsigned int vht_chwidth, const u8 *group_ssid,
5360 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005361{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005362 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005363 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005365 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005366 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005367 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368
5369 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5370 return -1;
5371
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372 if (persistent_id >= 0) {
5373 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5374 if (ssid == NULL || ssid->disabled != 2 ||
5375 ssid->mode != WPAS_MODE_P2P_GO)
5376 return -1;
5377 }
5378
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005379 os_free(wpa_s->global->add_psk);
5380 wpa_s->global->add_psk = NULL;
5381
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005382 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005383 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005384 wpa_s->global->pending_p2ps_group_freq = 0;
5385 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 if (go_intent < 0)
5388 go_intent = wpa_s->conf->p2p_go_intent;
5389
5390 if (!auth)
5391 wpa_s->p2p_long_listen = 0;
5392
5393 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005394 wpa_s->p2p_persistent_group = !!persistent_group;
5395 wpa_s->p2p_persistent_id = persistent_id;
5396 wpa_s->p2p_go_intent = go_intent;
5397 wpa_s->p2p_connect_freq = freq;
5398 wpa_s->p2p_fallback_to_go_neg = 0;
5399 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005400 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005401 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005402 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5403 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005404
5405 if (pin)
5406 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5407 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005408 if (wps_generate_pin((unsigned int *) &ret) < 0)
5409 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005410 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5411 "%08d", ret);
5412 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5413 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5415 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005416 } else if (wps_method == WPS_P2PS) {
5417 /* Force the P2Ps default PIN to be used */
5418 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419 } else
5420 wpa_s->p2p_pin[0] = '\0';
5421
Dmitry Shmidt04949592012-07-19 12:16:46 -07005422 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5424 if (auth) {
5425 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5426 "connect a running group from " MACSTR,
5427 MAC2STR(peer_addr));
5428 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5429 return ret;
5430 }
5431 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5432 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5433 iface_addr) < 0) {
5434 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5435 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5436 dev_addr);
5437 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005438 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005439 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005440 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5441 "%ld.%06ld",
5442 wpa_s->p2p_auto_started.sec,
5443 wpa_s->p2p_auto_started.usec);
5444 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005445 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005446 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005447 auto_join, freq,
5448 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005449 return -1;
5450 return ret;
5451 }
5452
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005453 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005454 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005455 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005456 if (res)
5457 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005458 wpas_p2p_set_own_freq_preference(wpa_s,
5459 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005461 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5464
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465 if (wpa_s->create_p2p_iface) {
5466 /* Prepare to add a new interface for the group */
5467 iftype = WPA_IF_P2P_GROUP;
5468 if (go_intent == 15)
5469 iftype = WPA_IF_P2P_GO;
5470 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5471 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5472 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005474 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005476 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005477 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005478 if (wpa_s->p2p_mgmt)
5479 if_addr = wpa_s->parent->own_addr;
5480 else
5481 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005482 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5483 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484
5485 if (auth) {
5486 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005487 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005488 force_freq, persistent_group, ssid,
5489 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490 return -1;
5491 return ret;
5492 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005493
5494 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5495 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005496 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005497 if (wpa_s->create_p2p_iface)
5498 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 return -1;
5500 }
5501 return ret;
5502}
5503
5504
5505/**
5506 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5507 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5508 * @freq: Frequency of the channel in MHz
5509 * @duration: Duration of the stay on the channel in milliseconds
5510 *
5511 * This callback is called when the driver indicates that it has started the
5512 * requested remain-on-channel duration.
5513 */
5514void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5515 unsigned int freq, unsigned int duration)
5516{
5517 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5518 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005519 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)",
5520 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5521 wpa_s->roc_waiting_drv_freq, freq, duration);
5522 if (wpa_s->off_channel_freq &&
5523 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005524 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5525 wpa_s->pending_listen_duration);
5526 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005527 } else {
5528 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5529 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5530 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005531 }
5532}
5533
5534
Jithu Jance57cea1a2014-08-20 10:22:04 -07005535int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536{
5537 /* Limit maximum Listen state time based on driver limitation. */
5538 if (timeout > wpa_s->max_remain_on_chan)
5539 timeout = wpa_s->max_remain_on_chan;
5540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 return p2p_listen(wpa_s->global->p2p, timeout);
5542}
5543
5544
5545/**
5546 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5547 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5548 * @freq: Frequency of the channel in MHz
5549 *
5550 * This callback is called when the driver indicates that a remain-on-channel
5551 * operation has been completed, i.e., the duration on the requested channel
5552 * has timed out.
5553 */
5554void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5555 unsigned int freq)
5556{
5557 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5558 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005559 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005560 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005561 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5562 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005563 if (wpa_s->p2p_long_listen > 0)
5564 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005565 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5566 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005567 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569 if (wpa_s->p2p_long_listen > 0) {
5570 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5571 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005572 } else {
5573 /*
5574 * When listen duration is over, stop listen & update p2p_state
5575 * to IDLE.
5576 */
5577 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 }
5579}
5580
5581
5582/**
5583 * wpas_p2p_group_remove - Remove a P2P group
5584 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5585 * @ifname: Network interface name of the group interface or "*" to remove all
5586 * groups
5587 * Returns: 0 on success, -1 on failure
5588 *
5589 * This function is used to remove a P2P group. This can be used to disconnect
5590 * from a group in which the local end is a P2P Client or to end a P2P Group in
5591 * case the local end is the Group Owner. If a virtual network interface was
5592 * created for this group, that interface will be removed. Otherwise, only the
5593 * configured P2P group network will be removed from the interface.
5594 */
5595int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5596{
5597 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005598 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599
5600 if (os_strcmp(ifname, "*") == 0) {
5601 struct wpa_supplicant *prev;
5602 wpa_s = global->ifaces;
5603 while (wpa_s) {
5604 prev = wpa_s;
5605 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005606 if (prev->p2p_group_interface !=
5607 NOT_P2P_GROUP_INTERFACE ||
5608 (prev->current_ssid &&
5609 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005610 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005611 }
5612 return 0;
5613 }
5614
5615 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5616 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5617 break;
5618 }
5619
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005620 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621}
5622
5623
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005624static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5625{
5626 unsigned int r;
5627
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005628 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5629 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5630 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5631 int res;
5632
5633 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5634 &size, pref_freq_list);
5635 if (!res && size > 0) {
5636 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005637 while (i < size &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005638 (!p2p_supported_freq(wpa_s->global->p2p,
5639 pref_freq_list[i]) ||
5640 wpas_p2p_disallowed_freq(wpa_s->global,
5641 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005642 wpa_printf(MSG_DEBUG,
5643 "P2P: preferred_freq_list[%d]=%d is disallowed",
5644 i, pref_freq_list[i]);
5645 i++;
5646 }
5647 if (i != size) {
5648 freq = pref_freq_list[i];
5649 wpa_printf(MSG_DEBUG,
5650 "P2P: Using preferred_freq_list[%d]=%d",
5651 i, freq);
5652 } else {
5653 wpa_printf(MSG_DEBUG,
5654 "P2P: All driver preferred frequencies are disallowed for P2P use");
5655 }
5656 } else {
5657 wpa_printf(MSG_DEBUG,
5658 "P2P: No preferred frequency list available");
5659 }
5660 }
5661
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005662 if (freq == 2) {
5663 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5664 "band");
5665 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005666 p2p_supported_freq_go(wpa_s->global->p2p,
5667 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005668 freq = wpa_s->best_24_freq;
5669 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5670 "channel: %d MHz", freq);
5671 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005672 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5673 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005674 freq = 2412 + (r % 3) * 25;
5675 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5676 "channel: %d MHz", freq);
5677 }
5678 }
5679
5680 if (freq == 5) {
5681 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5682 "band");
5683 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005684 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005685 wpa_s->best_5_freq)) {
5686 freq = wpa_s->best_5_freq;
5687 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5688 "channel: %d MHz", freq);
5689 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005690 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5691 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005692 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005693 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005694 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5695 "5 GHz channel for P2P group");
5696 return -1;
5697 }
5698 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5699 "channel: %d MHz", freq);
5700 }
5701 }
5702
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005703 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005704 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5705 ieee80211_is_dfs(freq)) {
5706 /*
5707 * If freq is a DFS channel and DFS is offloaded to the
5708 * driver, allow P2P GO to use it.
5709 */
5710 wpa_printf(MSG_DEBUG, "P2P: "
5711 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5712 __func__, freq);
5713 return freq;
5714 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005715 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5716 "(%u MHz) is not supported for P2P uses",
5717 freq);
5718 return -1;
5719 }
5720
5721 return freq;
5722}
5723
5724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005725static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5726 const struct p2p_channels *channels,
5727 int freq)
5728{
5729 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5730 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5731 freq_included(wpa_s, channels, freq))
5732 return 1;
5733 return 0;
5734}
5735
5736
5737static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5738 struct p2p_go_neg_results *params,
5739 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005740{
5741 unsigned int i, r;
5742
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005743 /* try all channels in operating class 115 */
5744 for (i = 0; i < 4; i++) {
5745 params->freq = 5180 + i * 20;
5746 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005747 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005748 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5749 goto out;
5750 }
5751
5752 /* try all channels in operating class 124 */
5753 for (i = 0; i < 4; i++) {
5754 params->freq = 5745 + i * 20;
5755 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005756 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005757 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5758 goto out;
5759 }
5760
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005761 /* try social channel class 180 channel 2 */
5762 params->freq = 58320 + 1 * 2160;
5763 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005764 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005765 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5766 goto out;
5767
5768 /* try all channels in reg. class 180 */
5769 for (i = 0; i < 4; i++) {
5770 params->freq = 58320 + i * 2160;
5771 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005772 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005773 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5774 goto out;
5775 }
5776
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005777 /* try some random selection of the social channels */
5778 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5779 return;
5780
5781 for (i = 0; i < 3; i++) {
5782 params->freq = 2412 + ((r + i) % 3) * 25;
5783 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5784 goto out;
5785 }
5786
5787 /* try all other channels in operating class 81 */
5788 for (i = 0; i < 11; i++) {
5789 params->freq = 2412 + i * 5;
5790
5791 /* skip social channels; covered in the previous loop */
5792 if (params->freq == 2412 ||
5793 params->freq == 2437 ||
5794 params->freq == 2462)
5795 continue;
5796
5797 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
5798 goto out;
5799 }
5800
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005801 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005802 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005803 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005804out:
5805 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5806 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005807}
5808
5809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005810static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5811 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005812 int freq, int vht_center_freq2, int ht40,
5813 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005814 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005815{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005816 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005817 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005818 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005819 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005820 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005821
5822 os_memset(params, 0, sizeof(*params));
5823 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005824 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005825 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005826 params->max_oper_chwidth = max_oper_chwidth;
5827 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005829 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5830 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005831 if (!freqs)
5832 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005833
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005834 num = get_shared_radio_freqs_data(wpa_s, freqs,
5835 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005836
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005837 if (wpa_s->current_ssid &&
5838 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5839 wpa_s->wpa_state == WPA_COMPLETED) {
5840 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5841 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005842
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005843 /*
5844 * If the frequency selection is done for an active P2P GO that
5845 * is not sharing a frequency, allow to select a new frequency
5846 * even if there are no unused frequencies as we are about to
5847 * move the P2P GO so its frequency can be re-used.
5848 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005849 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005850 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5851 freqs[i].flags == 0) {
5852 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005853 break;
5854 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005855 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005856 }
5857
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005858 /* try using the forced freq */
5859 if (freq) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005860 if (wpas_p2p_disallowed_freq(wpa_s->global, freq) ||
5861 !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005862 wpa_printf(MSG_DEBUG,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005863 "P2P: Forced GO freq %d MHz disallowed",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005864 freq);
5865 goto fail;
5866 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005867 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5868 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5869 ieee80211_is_dfs(freq)) {
5870 /*
5871 * If freq is a DFS channel and DFS is offloaded
5872 * to the driver, allow P2P GO to use it.
5873 */
5874 wpa_printf(MSG_DEBUG,
5875 "P2P: %s: The forced channel for GO (%u MHz) requires DFS and DFS is offloaded",
5876 __func__, freq);
5877 } else {
5878 wpa_printf(MSG_DEBUG,
5879 "P2P: The forced channel for GO (%u MHz) is not supported for P2P uses",
5880 freq);
5881 goto fail;
5882 }
5883 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005884
5885 for (i = 0; i < num; i++) {
5886 if (freqs[i].freq == freq) {
5887 wpa_printf(MSG_DEBUG,
5888 "P2P: forced freq (%d MHz) is also shared",
5889 freq);
5890 params->freq = freq;
5891 goto success;
5892 }
5893 }
5894
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005895 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005896 wpa_printf(MSG_DEBUG,
5897 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5898 freq);
5899 goto fail;
5900 }
5901
5902 wpa_printf(MSG_DEBUG,
5903 "P2P: force GO freq (%d MHz) on a free channel",
5904 freq);
5905 params->freq = freq;
5906 goto success;
5907 }
5908
5909 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005910 if (num &&
5911 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005912 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5913 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5914 wpa_printf(MSG_DEBUG,
5915 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005916 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005917 params->freq = cand;
5918 goto success;
5919 }
5920
5921 /* try using one of the shared freqs */
5922 for (i = 0; i < num; i++) {
5923 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5924 freqs[i].freq)) {
5925 wpa_printf(MSG_DEBUG,
5926 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005927 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005928 params->freq = freqs[i].freq;
5929 goto success;
5930 }
5931 }
5932 }
5933
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005934 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005935 wpa_printf(MSG_DEBUG,
5936 "P2P: Cannot force GO on any of the channels we are already using");
5937 goto fail;
5938 }
5939
5940 /* try using the setting from the configuration file */
5941 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5942 wpa_s->conf->p2p_oper_channel >= 1 &&
5943 wpa_s->conf->p2p_oper_channel <= 11 &&
5944 wpas_p2p_supported_freq_go(
5945 wpa_s, channels,
5946 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5947 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5948 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5949 "frequency %d MHz", params->freq);
5950 goto success;
5951 }
5952
5953 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5954 wpa_s->conf->p2p_oper_reg_class == 116 ||
5955 wpa_s->conf->p2p_oper_reg_class == 117 ||
5956 wpa_s->conf->p2p_oper_reg_class == 124 ||
5957 wpa_s->conf->p2p_oper_reg_class == 125 ||
5958 wpa_s->conf->p2p_oper_reg_class == 126 ||
5959 wpa_s->conf->p2p_oper_reg_class == 127) &&
5960 wpas_p2p_supported_freq_go(wpa_s, channels,
5961 5000 +
5962 5 * wpa_s->conf->p2p_oper_channel)) {
5963 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5964 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5965 "frequency %d MHz", params->freq);
5966 goto success;
5967 }
5968
5969 /* Try using best channels */
5970 if (wpa_s->conf->p2p_oper_channel == 0 &&
5971 wpa_s->best_overall_freq > 0 &&
5972 wpas_p2p_supported_freq_go(wpa_s, channels,
5973 wpa_s->best_overall_freq)) {
5974 params->freq = wpa_s->best_overall_freq;
5975 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5976 "channel %d MHz", params->freq);
5977 goto success;
5978 }
5979
5980 if (wpa_s->conf->p2p_oper_channel == 0 &&
5981 wpa_s->best_24_freq > 0 &&
5982 wpas_p2p_supported_freq_go(wpa_s, channels,
5983 wpa_s->best_24_freq)) {
5984 params->freq = wpa_s->best_24_freq;
5985 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5986 "channel %d MHz", params->freq);
5987 goto success;
5988 }
5989
5990 if (wpa_s->conf->p2p_oper_channel == 0 &&
5991 wpa_s->best_5_freq > 0 &&
5992 wpas_p2p_supported_freq_go(wpa_s, channels,
5993 wpa_s->best_5_freq)) {
5994 params->freq = wpa_s->best_5_freq;
5995 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5996 "channel %d MHz", params->freq);
5997 goto success;
5998 }
5999
6000 /* try using preferred channels */
6001 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
6002 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6003 params->freq = cand;
6004 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6005 "channels", params->freq);
6006 goto success;
6007 }
6008
6009 /* Try using one of the group common freqs */
6010 if (wpa_s->p2p_group_common_freqs) {
6011 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
6012 cand = wpa_s->p2p_group_common_freqs[i];
6013 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
6014 params->freq = cand;
6015 wpa_printf(MSG_DEBUG,
6016 "P2P: Use freq %d MHz common with the peer",
6017 params->freq);
6018 goto success;
6019 }
6020 }
6021 }
6022
6023 /* no preference, select some channel */
6024 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
6025
6026 if (params->freq == 0) {
6027 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
6028 goto fail;
6029 }
6030
6031success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006032 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006033 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006034fail:
6035 os_free(freqs);
6036 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037}
6038
6039
6040static struct wpa_supplicant *
6041wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6042 int go)
6043{
6044 struct wpa_supplicant *group_wpa_s;
6045
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006046 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006047 if (wpa_s->p2p_mgmt) {
6048 /*
6049 * We may be called on the p2p_dev interface which
6050 * cannot be used for group operations, so always use
6051 * the primary interface.
6052 */
6053 wpa_s->parent->p2pdev = wpa_s;
6054 wpa_s = wpa_s->parent;
6055 }
6056 wpa_dbg(wpa_s, MSG_DEBUG,
6057 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006058 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006059 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006060 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006062 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063
6064 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006065 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006066 wpa_msg_global(wpa_s, MSG_ERROR,
6067 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006068 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6071 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006072 wpa_msg_global(wpa_s, MSG_ERROR,
6073 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074 wpas_p2p_remove_pending_group_interface(wpa_s);
6075 return NULL;
6076 }
6077
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006078 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6079 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006080 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006081 return group_wpa_s;
6082}
6083
6084
6085/**
6086 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6087 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6088 * @persistent_group: Whether to create a persistent group
6089 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006090 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006091 * @ht40: Start GO with 40 MHz channel width
6092 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006093 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094 * Returns: 0 on success, -1 on failure
6095 *
6096 * This function creates a new P2P group with the local end as the Group Owner,
6097 * i.e., without using Group Owner Negotiation.
6098 */
6099int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006100 int freq, int vht_center_freq2, int ht40, int vht,
6101 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102{
6103 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006104
6105 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6106 return -1;
6107
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006108 os_free(wpa_s->global->add_psk);
6109 wpa_s->global->add_psk = NULL;
6110
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006111 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006112 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006113 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006114
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006115 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6116 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006118
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006119 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6120 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006121 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006123 p2p_go_params(wpa_s->global->p2p, &params);
6124 params.persistent_group = persistent_group;
6125
6126 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6127 if (wpa_s == NULL)
6128 return -1;
6129 wpas_start_wps_go(wpa_s, &params, 0);
6130
6131 return 0;
6132}
6133
6134
6135static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006136 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006137 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138{
6139 struct wpa_ssid *ssid;
6140
6141 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6142 if (wpa_s == NULL)
6143 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006144 if (force_scan)
6145 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006146 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006147
6148 wpa_supplicant_ap_deinit(wpa_s);
6149
6150 ssid = wpa_config_add_network(wpa_s->conf);
6151 if (ssid == NULL)
6152 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006153 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 wpa_config_set_network_defaults(ssid);
6155 ssid->temporary = 1;
6156 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006157 ssid->pbss = params->pbss;
6158 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6159 WPA_CIPHER_CCMP;
6160 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6162 ssid->ssid = os_malloc(params->ssid_len);
6163 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006164 wpa_config_remove_network(wpa_s->conf, ssid->id);
6165 return -1;
6166 }
6167 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6168 ssid->ssid_len = params->ssid_len;
6169 ssid->p2p_group = 1;
6170 ssid->export_keys = 1;
6171 if (params->psk_set) {
6172 os_memcpy(ssid->psk, params->psk, 32);
6173 ssid->psk_set = 1;
6174 }
6175 if (params->passphrase)
6176 ssid->passphrase = os_strdup(params->passphrase);
6177
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006179 wpa_s->p2p_in_invitation = 1;
6180 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006181 wpa_s->p2p_go_group_formation_completed = 0;
6182 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006183
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006184 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006185 NULL);
6186 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6187 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006188 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006189 wpa_supplicant_select_network(wpa_s, ssid);
6190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191 return 0;
6192}
6193
6194
6195int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6196 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006197 int force_freq, int neg_freq,
6198 int vht_center_freq2, int ht40,
6199 int vht, int max_oper_chwidth,
6200 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006201 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006202{
6203 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006204 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205
6206 if (ssid->disabled != 2 || ssid->ssid == NULL)
6207 return -1;
6208
6209 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6210 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6211 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6212 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006213 if (go == 0 &&
6214 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006215 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006216 /*
6217 * This can happen if Invitation Response frame was lost
6218 * and the peer (GO of a persistent group) tries to
6219 * invite us again. Reschedule the timeout to avoid
6220 * terminating the wait for the connection too early
6221 * since we now know that the peer is still trying to
6222 * invite us instead of having already started the GO.
6223 */
6224 wpa_printf(MSG_DEBUG,
6225 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6226 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6227 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006228 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 return 0;
6231 }
6232
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006233 os_free(wpa_s->global->add_psk);
6234 wpa_s->global->add_psk = NULL;
6235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006237 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238
Dmitry Shmidt04949592012-07-19 12:16:46 -07006239 wpa_s->p2p_fallback_to_go_neg = 0;
6240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006241 if (ssid->mode == WPAS_MODE_P2P_GO) {
6242 if (force_freq > 0) {
6243 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6244 if (freq < 0)
6245 return -1;
6246 } else {
6247 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6248 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006249 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006250 freq = 0;
6251 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006252 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006253 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006254 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006255 struct os_reltime now;
6256 struct wpa_bss *bss =
6257 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006258
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006259 os_get_reltime(&now);
6260 if (bss &&
6261 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006262 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006263 freq = bss->freq;
6264 else
6265 freq = 0;
6266 }
6267
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006268 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6269 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006270 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006271 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006272 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006273
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006274 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6275 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006276 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277
6278 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006279 params.psk_set = ssid->psk_set;
6280 if (params.psk_set)
6281 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006282 if (ssid->passphrase) {
6283 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6284 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6285 "persistent group");
6286 return -1;
6287 }
6288 os_strlcpy(params.passphrase, ssid->passphrase,
6289 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006290 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6292 params.ssid_len = ssid->ssid_len;
6293 params.persistent_group = 1;
6294
6295 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6296 if (wpa_s == NULL)
6297 return -1;
6298
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006299 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6300
Dmitry Shmidt56052862013-10-04 10:23:25 -07006301 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 wpas_start_wps_go(wpa_s, &params, 0);
6303
6304 return 0;
6305}
6306
6307
6308static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6309 struct wpabuf *proberesp_ies)
6310{
6311 struct wpa_supplicant *wpa_s = ctx;
6312 if (wpa_s->ap_iface) {
6313 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006314 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6315 wpabuf_free(beacon_ies);
6316 wpabuf_free(proberesp_ies);
6317 return;
6318 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006319 if (beacon_ies) {
6320 wpabuf_free(hapd->p2p_beacon_ie);
6321 hapd->p2p_beacon_ie = beacon_ies;
6322 }
6323 wpabuf_free(hapd->p2p_probe_resp_ie);
6324 hapd->p2p_probe_resp_ie = proberesp_ies;
6325 } else {
6326 wpabuf_free(beacon_ies);
6327 wpabuf_free(proberesp_ies);
6328 }
6329 wpa_supplicant_ap_update_beacon(wpa_s);
6330}
6331
6332
6333static void wpas_p2p_idle_update(void *ctx, int idle)
6334{
6335 struct wpa_supplicant *wpa_s = ctx;
6336 if (!wpa_s->ap_iface)
6337 return;
6338 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006339 if (idle) {
6340 if (wpa_s->global->p2p_fail_on_wps_complete &&
6341 wpa_s->p2p_in_provisioning) {
6342 wpas_p2p_grpform_fail_after_wps(wpa_s);
6343 return;
6344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006345 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006346 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6348}
6349
6350
6351struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006352 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353{
6354 struct p2p_group *group;
6355 struct p2p_group_config *cfg;
6356
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006357 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6358 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359 return NULL;
6360
6361 cfg = os_zalloc(sizeof(*cfg));
6362 if (cfg == NULL)
6363 return NULL;
6364
Dmitry Shmidt04949592012-07-19 12:16:46 -07006365 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006366 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006367 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006368 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006369 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6370 if (wpa_s->max_stations &&
6371 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6372 cfg->max_clients = wpa_s->max_stations;
6373 else
6374 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006375 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6376 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006377 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006378 cfg->cb_ctx = wpa_s;
6379 cfg->ie_update = wpas_p2p_ie_update;
6380 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006381 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6382 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006383
6384 group = p2p_group_init(wpa_s->global->p2p, cfg);
6385 if (group == NULL)
6386 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006387 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006388 p2p_group_notif_formation_done(group);
6389 wpa_s->p2p_group = group;
6390 return group;
6391}
6392
6393
6394void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6395 int registrar)
6396{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006397 struct wpa_ssid *ssid = wpa_s->current_ssid;
6398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006399 if (!wpa_s->p2p_in_provisioning) {
6400 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6401 "provisioning not in progress");
6402 return;
6403 }
6404
Dmitry Shmidt04949592012-07-19 12:16:46 -07006405 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6406 u8 go_dev_addr[ETH_ALEN];
6407 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6408 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6409 ssid->ssid_len);
6410 /* Clear any stored provisioning info */
6411 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6412 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006413
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006414 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006415 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006416 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006417 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6418 /*
6419 * Use a separate timeout for initial data connection to
6420 * complete to allow the group to be removed automatically if
6421 * something goes wrong in this step before the P2P group idle
6422 * timeout mechanism is taken into use.
6423 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006424 wpa_dbg(wpa_s, MSG_DEBUG,
6425 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6426 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006427 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6428 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006429 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006430 /* Complete group formation on successful data connection. */
6431 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006432 } else if (ssid) {
6433 /*
6434 * Use a separate timeout for initial data connection to
6435 * complete to allow the group to be removed automatically if
6436 * the client does not complete data connection successfully.
6437 */
6438 wpa_dbg(wpa_s, MSG_DEBUG,
6439 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6440 P2P_MAX_INITIAL_CONN_WAIT_GO);
6441 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6442 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006443 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006444 /*
6445 * Complete group formation on first successful data connection
6446 */
6447 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006448 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006449 if (wpa_s->global->p2p)
6450 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006451 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006452}
6453
6454
Jouni Malinen75ecf522011-06-27 15:19:46 -07006455void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6456 struct wps_event_fail *fail)
6457{
6458 if (!wpa_s->p2p_in_provisioning) {
6459 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6460 "provisioning not in progress");
6461 return;
6462 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006463
6464 if (wpa_s->go_params) {
6465 p2p_clear_provisioning_info(
6466 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006467 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006468 }
6469
Jouni Malinen75ecf522011-06-27 15:19:46 -07006470 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006471
6472 if (wpa_s == wpa_s->global->p2p_group_formation) {
6473 /*
6474 * Allow some time for the failed WPS negotiation exchange to
6475 * complete, but remove the group since group formation cannot
6476 * succeed after provisioning failure.
6477 */
6478 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6479 wpa_s->global->p2p_fail_on_wps_complete = 1;
6480 eloop_deplete_timeout(0, 50000,
6481 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006482 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006483 }
6484}
6485
6486
6487int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6488{
6489 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6490 !wpa_s->p2p_in_provisioning)
6491 return 0;
6492
6493 wpas_p2p_grpform_fail_after_wps(wpa_s);
6494
6495 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006496}
6497
6498
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006500 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006501 enum wpas_p2p_prov_disc_use use,
6502 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503{
6504 u16 config_methods;
6505
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006506 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006507 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006508 wpa_s->p2p_fallback_to_go_neg = 0;
6509 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006510 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6511 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006512 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6513 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6514
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006515 wpa_printf(MSG_DEBUG,
6516 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6517 __func__, p2ps_prov->conncap,
6518 p2ps_prov->adv_id, p2ps_prov->conncap,
6519 p2ps_prov->status, p2ps_prov->info);
6520
6521 config_methods = 0;
6522 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006524 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006526 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6527 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006529 else {
6530 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006531 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006534
Dmitry Shmidt04949592012-07-19 12:16:46 -07006535 if (use == WPAS_P2P_PD_AUTO) {
6536 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6537 wpa_s->pending_pd_config_methods = config_methods;
6538 wpa_s->p2p_auto_pd = 1;
6539 wpa_s->p2p_auto_join = 0;
6540 wpa_s->pending_pd_before_join = 0;
6541 wpa_s->auto_pd_scan_retry = 0;
6542 wpas_p2p_stop_find(wpa_s);
6543 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006544 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006545 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6546 wpa_s->p2p_auto_started.sec,
6547 wpa_s->p2p_auto_started.usec);
6548 wpas_p2p_join_scan(wpa_s, NULL);
6549 return 0;
6550 }
6551
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006552 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6553 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006555 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006556
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006557 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006558 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006559 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560}
6561
6562
6563int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6564 char *end)
6565{
6566 return p2p_scan_result_text(ies, ies_len, buf, end);
6567}
6568
6569
6570static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6571{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006572 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006573 return;
6574
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006575 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006576 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006577 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6578 wpa_s, NULL);
6579 offchannel_send_action_done(wpa_s);
6580 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006582 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6583 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006584 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006585}
6586
6587
6588int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6589 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006590 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006591 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006592 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593{
6594 wpas_p2p_clear_pending_action_tx(wpa_s);
6595 wpa_s->p2p_long_listen = 0;
6596
Dmitry Shmidt04949592012-07-19 12:16:46 -07006597 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006598 wpa_s->p2p_in_provisioning) {
6599 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Reject p2p_find operation%s%s",
6600 (wpa_s->global->p2p_disabled || !wpa_s->global->p2p) ?
6601 " (P2P disabled)" : "",
6602 wpa_s->p2p_in_provisioning ?
6603 " (p2p_in_provisioning)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006606
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006607 wpa_supplicant_cancel_sched_scan(wpa_s);
6608
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006609 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006610 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006611 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006612}
6613
6614
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006615static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6616 struct wpa_scan_results *scan_res)
6617{
6618 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6619
6620 if (wpa_s->p2p_scan_work) {
6621 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6622 wpa_s->p2p_scan_work = NULL;
6623 radio_work_done(work);
6624 }
6625
6626 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6627 return;
6628
6629 /*
6630 * Indicate that results have been processed so that the P2P module can
6631 * continue pending tasks.
6632 */
6633 p2p_scan_res_handled(wpa_s->global->p2p);
6634}
6635
6636
6637static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006638{
6639 wpas_p2p_clear_pending_action_tx(wpa_s);
6640 wpa_s->p2p_long_listen = 0;
6641 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6642 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006643
6644 if (wpa_s->global->p2p)
6645 p2p_stop_find(wpa_s->global->p2p);
6646
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006647 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6648 wpa_printf(MSG_DEBUG,
6649 "P2P: Do not consider the scan results after stop_find");
6650 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6651 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006652}
6653
6654
6655void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6656{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006657 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006658 if (!wpa_s->global->pending_group_iface_for_p2ps)
6659 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660}
6661
6662
6663static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6664{
6665 struct wpa_supplicant *wpa_s = eloop_ctx;
6666 wpa_s->p2p_long_listen = 0;
6667}
6668
6669
6670int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6671{
6672 int res;
6673
6674 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6675 return -1;
6676
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006677 if (wpa_s->p2p_lo_started) {
6678 wpa_printf(MSG_DEBUG,
6679 "P2P: Cannot start P2P listen, it is offloaded");
6680 return -1;
6681 }
6682
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006683 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006684 wpas_p2p_clear_pending_action_tx(wpa_s);
6685
6686 if (timeout == 0) {
6687 /*
6688 * This is a request for unlimited Listen state. However, at
6689 * least for now, this is mapped to a Listen state for one
6690 * hour.
6691 */
6692 timeout = 3600;
6693 }
6694 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6695 wpa_s->p2p_long_listen = 0;
6696
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006697 /*
6698 * Stop previous find/listen operation to avoid trying to request a new
6699 * remain-on-channel operation while the driver is still running the
6700 * previous one.
6701 */
6702 if (wpa_s->global->p2p)
6703 p2p_stop_find(wpa_s->global->p2p);
6704
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006705 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6706 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6707 wpa_s->p2p_long_listen = timeout * 1000;
6708 eloop_register_timeout(timeout, 0,
6709 wpas_p2p_long_listen_timeout,
6710 wpa_s, NULL);
6711 }
6712
6713 return res;
6714}
6715
6716
6717int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6718 u8 *buf, size_t len, int p2p_group)
6719{
6720 struct wpabuf *p2p_ie;
6721 int ret;
6722
6723 if (wpa_s->global->p2p_disabled)
6724 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006725 /*
6726 * Advertize mandatory cross connection capability even on
6727 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6728 */
6729 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006730 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006731 if (wpa_s->global->p2p == NULL)
6732 return -1;
6733 if (bss == NULL)
6734 return -1;
6735
6736 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6737 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6738 p2p_group, p2p_ie);
6739 wpabuf_free(p2p_ie);
6740
6741 return ret;
6742}
6743
6744
6745int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006746 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006747 const u8 *ie, size_t ie_len,
6748 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006749{
6750 if (wpa_s->global->p2p_disabled)
6751 return 0;
6752 if (wpa_s->global->p2p == NULL)
6753 return 0;
6754
Dmitry Shmidt04949592012-07-19 12:16:46 -07006755 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006756 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006757 case P2P_PREQ_NOT_P2P:
6758 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6759 ssi_signal);
6760 /* fall through */
6761 case P2P_PREQ_MALFORMED:
6762 case P2P_PREQ_NOT_LISTEN:
6763 case P2P_PREQ_NOT_PROCESSED:
6764 default: /* make gcc happy */
6765 return 0;
6766 case P2P_PREQ_PROCESSED:
6767 return 1;
6768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006769}
6770
6771
6772void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6773 const u8 *sa, const u8 *bssid,
6774 u8 category, const u8 *data, size_t len, int freq)
6775{
6776 if (wpa_s->global->p2p_disabled)
6777 return;
6778 if (wpa_s->global->p2p == NULL)
6779 return;
6780
6781 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6782 freq);
6783}
6784
6785
6786void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6787{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006788 unsigned int bands;
6789
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006790 if (wpa_s->global->p2p_disabled)
6791 return;
6792 if (wpa_s->global->p2p == NULL)
6793 return;
6794
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006795 bands = wpas_get_bands(wpa_s, NULL);
6796 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797}
6798
6799
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006800static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801{
6802 p2p_group_deinit(wpa_s->p2p_group);
6803 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006804
6805 wpa_s->ap_configured_cb = NULL;
6806 wpa_s->ap_configured_cb_ctx = NULL;
6807 wpa_s->ap_configured_cb_data = NULL;
6808 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006809}
6810
6811
6812int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6813{
6814 wpa_s->p2p_long_listen = 0;
6815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006816 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6817 return -1;
6818
6819 return p2p_reject(wpa_s->global->p2p, addr);
6820}
6821
6822
6823/* Invite to reinvoke a persistent group */
6824int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006825 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006826 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6827 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006828{
6829 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006830 u8 *bssid = NULL;
6831 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006832 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006833 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006834 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006835
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006836 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006837 if (peer_addr)
6838 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6839 else
6840 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006841
Jouni Malinen31be0a42012-08-31 21:20:51 +03006842 wpa_s->p2p_persistent_go_freq = freq;
6843 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006844 wpa_s->p2p_go_vht = !!vht;
6845 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
6846 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006847 if (ssid->mode == WPAS_MODE_P2P_GO) {
6848 role = P2P_INVITE_ROLE_GO;
6849 if (peer_addr == NULL) {
6850 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6851 "address in invitation command");
6852 return -1;
6853 }
6854 if (wpas_p2p_create_iface(wpa_s)) {
6855 if (wpas_p2p_add_group_interface(wpa_s,
6856 WPA_IF_P2P_GO) < 0) {
6857 wpa_printf(MSG_ERROR, "P2P: Failed to "
6858 "allocate a new interface for the "
6859 "group");
6860 return -1;
6861 }
6862 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006863 } else if (wpa_s->p2p_mgmt)
6864 bssid = wpa_s->parent->own_addr;
6865 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006866 bssid = wpa_s->own_addr;
6867 } else {
6868 role = P2P_INVITE_ROLE_CLIENT;
6869 peer_addr = ssid->bssid;
6870 }
6871 wpa_s->pending_invite_ssid_id = ssid->id;
6872
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006873 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006874 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006875 role == P2P_INVITE_ROLE_GO,
6876 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006877 if (res)
6878 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006880 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6881 return -1;
6882
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006883 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6884
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006885 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6886 no_pref_freq_given && pref_freq > 0 &&
6887 wpa_s->num_multichan_concurrent > 1 &&
6888 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6889 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6890 pref_freq);
6891 pref_freq = 0;
6892 }
6893
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006894 /*
6895 * Stop any find/listen operations before invitation and possibly
6896 * connection establishment.
6897 */
6898 wpas_p2p_stop_find_oper(wpa_s);
6899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006901 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006902 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006903}
6904
6905
6906/* Invite to join an active group */
6907int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6908 const u8 *peer_addr, const u8 *go_dev_addr)
6909{
6910 struct wpa_global *global = wpa_s->global;
6911 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006912 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006913 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006914 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006915 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006916 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006917 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006918
Jouni Malinen31be0a42012-08-31 21:20:51 +03006919 wpa_s->p2p_persistent_go_freq = 0;
6920 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006921 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006922 wpa_s->p2p_go_vht_center_freq2 = 0;
6923 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6926 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6927 break;
6928 }
6929 if (wpa_s == NULL) {
6930 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6931 return -1;
6932 }
6933
6934 ssid = wpa_s->current_ssid;
6935 if (ssid == NULL) {
6936 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6937 "invitation");
6938 return -1;
6939 }
6940
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006941 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006942 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006943 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006944 ssid->ssid, ssid->ssid_len);
6945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006946 if (ssid->mode == WPAS_MODE_P2P_GO) {
6947 role = P2P_INVITE_ROLE_ACTIVE_GO;
6948 bssid = wpa_s->own_addr;
6949 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006950 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006951 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952 } else {
6953 role = P2P_INVITE_ROLE_CLIENT;
6954 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6955 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6956 "invite to current group");
6957 return -1;
6958 }
6959 bssid = wpa_s->bssid;
6960 if (go_dev_addr == NULL &&
6961 !is_zero_ether_addr(wpa_s->go_dev_addr))
6962 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006963 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6964 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006965 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006966 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6969 return -1;
6970
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006971 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006972 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006973 role == P2P_INVITE_ROLE_ACTIVE_GO,
6974 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006975 if (res)
6976 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006977 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006979 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006980 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006981 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006982}
6983
6984
6985void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6986{
6987 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006988 u8 go_dev_addr[ETH_ALEN];
6989 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006991 u8 ip[3 * 4];
6992 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006993
Dmitry Shmidt04949592012-07-19 12:16:46 -07006994 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6995 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006996 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006997 }
6998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006999 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007000 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007001
7002 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007003 if (!wpa_s->p2p_go_group_formation_completed &&
7004 wpa_s->global->p2p_group_formation == wpa_s) {
7005 wpa_dbg(wpa_s, MSG_DEBUG,
7006 "P2P: Marking group formation completed on client on data connection");
7007 wpa_s->p2p_go_group_formation_completed = 1;
7008 wpa_s->global->p2p_group_formation = NULL;
7009 wpa_s->p2p_in_provisioning = 0;
7010 wpa_s->p2p_in_invitation = 0;
7011 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007012
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007013 os_memset(go_dev_addr, 0, ETH_ALEN);
7014 if (ssid->bssid_set)
7015 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7016 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7017 ssid->ssid_len);
7018 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7019
7020 if (wpa_s->global->p2p_group_formation == wpa_s)
7021 wpa_s->global->p2p_group_formation = NULL;
7022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007023 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7024 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007025
7026 ip_addr[0] = '\0';
7027 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007028 int res;
7029
7030 res = os_snprintf(ip_addr, sizeof(ip_addr),
7031 " ip_addr=%u.%u.%u.%u "
7032 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7033 ip[0], ip[1], ip[2], ip[3],
7034 ip[4], ip[5], ip[6], ip[7],
7035 ip[8], ip[9], ip[10], ip[11]);
7036 if (os_snprintf_error(sizeof(ip_addr), res))
7037 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007038 }
7039
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007040 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7041 ssid->passphrase == NULL && ssid->psk_set ?
7042 ssid->psk : NULL,
7043 ssid->passphrase, go_dev_addr, persistent,
7044 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007045
7046 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007047 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7048 ssid, go_dev_addr);
7049
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007050 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007051}
7052
7053
7054int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7055 u32 interval1, u32 duration2, u32 interval2)
7056{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007057 int ret;
7058
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007059 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7060 return -1;
7061
7062 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7063 wpa_s->current_ssid == NULL ||
7064 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7065 return -1;
7066
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007067 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7068 wpa_s->own_addr, wpa_s->assoc_freq,
7069 duration1, interval1, duration2, interval2);
7070 if (ret == 0)
7071 wpa_s->waiting_presence_resp = 1;
7072
7073 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074}
7075
7076
7077int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7078 unsigned int interval)
7079{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7081 return -1;
7082
7083 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7084}
7085
7086
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007087static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7088{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007089 if (wpa_s->current_ssid == NULL) {
7090 /*
7091 * current_ssid can be cleared when P2P client interface gets
7092 * disconnected, so assume this interface was used as P2P
7093 * client.
7094 */
7095 return 1;
7096 }
7097 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007098 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7099}
7100
7101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007102static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7103{
7104 struct wpa_supplicant *wpa_s = eloop_ctx;
7105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007106 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007107 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7108 "disabled");
7109 return;
7110 }
7111
Dmitry Shmidt04949592012-07-19 12:16:46 -07007112 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7113 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007114 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007115}
7116
7117
7118static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7119{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007120 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007121
Dmitry Shmidt04949592012-07-19 12:16:46 -07007122 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7123 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007125 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7126 return;
7127
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007128 timeout = wpa_s->conf->p2p_group_idle;
7129 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7130 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7131 timeout = P2P_MAX_CLIENT_IDLE;
7132
7133 if (timeout == 0)
7134 return;
7135
Dmitry Shmidt04949592012-07-19 12:16:46 -07007136 if (timeout < 0) {
7137 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7138 timeout = 0; /* special client mode no-timeout */
7139 else
7140 return;
7141 }
7142
7143 if (wpa_s->p2p_in_provisioning) {
7144 /*
7145 * Use the normal group formation timeout during the
7146 * provisioning phase to avoid terminating this process too
7147 * early due to group idle timeout.
7148 */
7149 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7150 "during provisioning");
7151 return;
7152 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307153
Dmitry Shmidt04949592012-07-19 12:16:46 -07007154 if (wpa_s->show_group_started) {
7155 /*
7156 * Use the normal group formation timeout between the end of
7157 * the provisioning phase and completion of 4-way handshake to
7158 * avoid terminating this process too early due to group idle
7159 * timeout.
7160 */
7161 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7162 "while waiting for initial 4-way handshake to "
7163 "complete");
7164 return;
7165 }
7166
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007168 timeout);
7169 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7170 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007171}
7172
7173
Jouni Malinen2b89da82012-08-31 22:04:41 +03007174/* Returns 1 if the interface was removed */
7175int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7176 u16 reason_code, const u8 *ie, size_t ie_len,
7177 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007178{
7179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007180 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181
Dmitry Shmidt04949592012-07-19 12:16:46 -07007182 if (!locally_generated)
7183 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7184 ie_len);
7185
7186 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7187 wpa_s->current_ssid &&
7188 wpa_s->current_ssid->p2p_group &&
7189 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7190 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7191 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007192 if (wpas_p2p_group_delete(wpa_s,
7193 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7194 > 0)
7195 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007196 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007197
7198 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007199}
7200
7201
7202void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007203 u16 reason_code, const u8 *ie, size_t ie_len,
7204 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007205{
7206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7207 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007208
Dmitry Shmidt04949592012-07-19 12:16:46 -07007209 if (!locally_generated)
7210 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7211 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007212}
7213
7214
7215void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7216{
7217 struct p2p_data *p2p = wpa_s->global->p2p;
7218
7219 if (p2p == NULL)
7220 return;
7221
7222 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7223 return;
7224
7225 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7226 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7227
7228 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7229 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7230
7231 if (wpa_s->wps &&
7232 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7233 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7234
7235 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7236 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7237
7238 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7239 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7240 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7241 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7242 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7243 }
7244
7245 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7246 p2p_set_sec_dev_types(p2p,
7247 (void *) wpa_s->conf->sec_device_type,
7248 wpa_s->conf->num_sec_device_types);
7249
7250 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7251 int i;
7252 p2p_remove_wps_vendor_extensions(p2p);
7253 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7254 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7255 continue;
7256 p2p_add_wps_vendor_extension(
7257 p2p, wpa_s->conf->wps_vendor_ext[i]);
7258 }
7259 }
7260
7261 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7262 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7263 char country[3];
7264 country[0] = wpa_s->conf->country[0];
7265 country[1] = wpa_s->conf->country[1];
7266 country[2] = 0x04;
7267 p2p_set_country(p2p, country);
7268 }
7269
7270 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7271 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7272 wpa_s->conf->p2p_ssid_postfix ?
7273 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7274 0);
7275 }
7276
7277 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7278 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007279
7280 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7281 u8 reg_class, channel;
7282 int ret;
7283 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007284 u8 channel_forced;
7285
Jouni Malinen75ecf522011-06-27 15:19:46 -07007286 if (wpa_s->conf->p2p_listen_reg_class &&
7287 wpa_s->conf->p2p_listen_channel) {
7288 reg_class = wpa_s->conf->p2p_listen_reg_class;
7289 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007290 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007291 } else {
7292 reg_class = 81;
7293 /*
7294 * Pick one of the social channels randomly as the
7295 * listen channel.
7296 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007297 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7298 channel = 1;
7299 else
7300 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007301 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007302 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007303 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7304 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007305 if (ret)
7306 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7307 "failed: %d", ret);
7308 }
7309 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7310 u8 op_reg_class, op_channel, cfg_op_channel;
7311 int ret = 0;
7312 unsigned int r;
7313 if (wpa_s->conf->p2p_oper_reg_class &&
7314 wpa_s->conf->p2p_oper_channel) {
7315 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7316 op_channel = wpa_s->conf->p2p_oper_channel;
7317 cfg_op_channel = 1;
7318 } else {
7319 op_reg_class = 81;
7320 /*
7321 * Use random operation channel from (1, 6, 11)
7322 *if no other preference is indicated.
7323 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007324 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7325 op_channel = 1;
7326 else
7327 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007328 cfg_op_channel = 0;
7329 }
7330 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7331 cfg_op_channel);
7332 if (ret)
7333 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7334 "failed: %d", ret);
7335 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007336
7337 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7338 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7339 wpa_s->conf->p2p_pref_chan) < 0) {
7340 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7341 "update failed");
7342 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007343
7344 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7345 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7346 "update failed");
7347 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007348 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007349
7350 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7351 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007352}
7353
7354
7355int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7356 int duration)
7357{
7358 if (!wpa_s->ap_iface)
7359 return -1;
7360 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7361 duration);
7362}
7363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007364
7365int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7366{
7367 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7368 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007369
7370 wpa_s->global->cross_connection = enabled;
7371 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7372
7373 if (!enabled) {
7374 struct wpa_supplicant *iface;
7375
7376 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7377 {
7378 if (iface->cross_connect_enabled == 0)
7379 continue;
7380
7381 iface->cross_connect_enabled = 0;
7382 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007383 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007384 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7385 iface->ifname,
7386 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007387 }
7388 }
7389
7390 return 0;
7391}
7392
7393
7394static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7395{
7396 struct wpa_supplicant *iface;
7397
7398 if (!uplink->global->cross_connection)
7399 return;
7400
7401 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7402 if (!iface->cross_connect_enabled)
7403 continue;
7404 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7405 0)
7406 continue;
7407 if (iface->ap_iface == NULL)
7408 continue;
7409 if (iface->cross_connect_in_use)
7410 continue;
7411
7412 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007413 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007414 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7415 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007416 }
7417}
7418
7419
7420static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7421{
7422 struct wpa_supplicant *iface;
7423
7424 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7425 if (!iface->cross_connect_enabled)
7426 continue;
7427 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7428 0)
7429 continue;
7430 if (!iface->cross_connect_in_use)
7431 continue;
7432
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007433 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007434 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7435 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007436 iface->cross_connect_in_use = 0;
7437 }
7438}
7439
7440
7441void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7442{
7443 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7444 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7445 wpa_s->cross_connect_disallowed)
7446 wpas_p2p_disable_cross_connect(wpa_s);
7447 else
7448 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007449 if (!wpa_s->ap_iface &&
7450 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007452}
7453
7454
7455void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7456{
7457 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007458 if (!wpa_s->ap_iface &&
7459 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7460 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007461 wpas_p2p_set_group_idle_timeout(wpa_s);
7462}
7463
7464
7465static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7466{
7467 struct wpa_supplicant *iface;
7468
7469 if (!wpa_s->global->cross_connection)
7470 return;
7471
7472 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7473 if (iface == wpa_s)
7474 continue;
7475 if (iface->drv_flags &
7476 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7477 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007478 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7479 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007480 continue;
7481
7482 wpa_s->cross_connect_enabled = 1;
7483 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7484 sizeof(wpa_s->cross_connect_uplink));
7485 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7486 "%s to %s whenever uplink is available",
7487 wpa_s->ifname, wpa_s->cross_connect_uplink);
7488
7489 if (iface->ap_iface || iface->current_ssid == NULL ||
7490 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7491 iface->cross_connect_disallowed ||
7492 iface->wpa_state != WPA_COMPLETED)
7493 break;
7494
7495 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007496 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007497 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7498 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007499 break;
7500 }
7501}
7502
7503
7504int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7505{
7506 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7507 !wpa_s->p2p_in_provisioning)
7508 return 0; /* not P2P client operation */
7509
7510 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7511 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007512 if (wpa_s != wpa_s->p2pdev)
7513 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007514 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007515 return 1;
7516}
7517
7518
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007519void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7520{
7521 struct wpa_supplicant *wpa_s = eloop_ctx;
7522 wpas_p2p_notif_pbc_overlap(wpa_s);
7523}
7524
7525
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007526void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7527 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007528{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007529 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007530 struct wpa_used_freq_data *freqs = NULL;
7531 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007532
7533 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7534 return;
7535
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007536 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7537 if (!freqs)
7538 return;
7539
7540 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007542 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007543 os_memset(&cli_chan, 0, sizeof(cli_chan));
7544 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007545 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7546 "channel list");
7547 return;
7548 }
7549
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007550 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007551
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007552 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007553
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007554 /*
7555 * The used frequencies map changed, so it is possible that a GO is
7556 * using a channel that is no longer valid for P2P use. It is also
7557 * possible that due to policy consideration, it would be preferable to
7558 * move it to a frequency already used by other station interfaces.
7559 */
7560 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7561
7562 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563}
7564
7565
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007566static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7567 struct wpa_scan_results *scan_res)
7568{
7569 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7570}
7571
7572
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007573int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7574{
7575 struct wpa_global *global = wpa_s->global;
7576 int found = 0;
7577 const u8 *peer;
7578
7579 if (global->p2p == NULL)
7580 return -1;
7581
7582 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7583
7584 if (wpa_s->pending_interface_name[0] &&
7585 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7586 found = 1;
7587
7588 peer = p2p_get_go_neg_peer(global->p2p);
7589 if (peer) {
7590 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7591 MACSTR, MAC2STR(peer));
7592 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007593 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007594 }
7595
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007596 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7597 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7598 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7599 found = 1;
7600 }
7601
7602 if (wpa_s->pending_pd_before_join) {
7603 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7604 wpa_s->pending_pd_before_join = 0;
7605 found = 1;
7606 }
7607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007608 wpas_p2p_stop_find(wpa_s);
7609
7610 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7611 if (wpa_s == global->p2p_group_formation &&
7612 (wpa_s->p2p_in_provisioning ||
7613 wpa_s->parent->pending_interface_type ==
7614 WPA_IF_P2P_CLIENT)) {
7615 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7616 "formation found - cancelling",
7617 wpa_s->ifname);
7618 found = 1;
7619 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007620 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007621 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007622 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007623 break;
7624 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007625 wpas_p2p_group_delete(wpa_s,
7626 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007628 } else if (wpa_s->p2p_in_invitation) {
7629 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7630 wpa_s->ifname);
7631 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007632 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007633 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007634 }
7635 }
7636
7637 if (!found) {
7638 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7639 return -1;
7640 }
7641
7642 return 0;
7643}
7644
7645
7646void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7647{
7648 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7649 return;
7650
7651 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7652 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007653 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007654}
7655
7656
7657void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7658 int freq_24, int freq_5, int freq_overall)
7659{
7660 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007661 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007662 return;
7663 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7664}
7665
7666
7667int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7668{
7669 u8 peer[ETH_ALEN];
7670 struct p2p_data *p2p = wpa_s->global->p2p;
7671
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007672 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007673 return -1;
7674
7675 if (hwaddr_aton(addr, peer))
7676 return -1;
7677
7678 return p2p_unauthorize(p2p, peer);
7679}
7680
7681
7682/**
7683 * wpas_p2p_disconnect - Disconnect from a P2P Group
7684 * @wpa_s: Pointer to wpa_supplicant data
7685 * Returns: 0 on success, -1 on failure
7686 *
7687 * This can be used to disconnect from a group in which the local end is a P2P
7688 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7689 * virtual network interface was created for this group, that interface will be
7690 * removed. Otherwise, only the configured P2P group network will be removed
7691 * from the interface.
7692 */
7693int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7694{
7695
7696 if (wpa_s == NULL)
7697 return -1;
7698
Jouni Malinen2b89da82012-08-31 22:04:41 +03007699 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7700 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007701}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007702
7703
7704int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7705{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007706 int ret;
7707
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007708 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7709 return 0;
7710
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007711 ret = p2p_in_progress(wpa_s->global->p2p);
7712 if (ret == 0) {
7713 /*
7714 * Check whether there is an ongoing WPS provisioning step (or
7715 * other parts of group formation) on another interface since
7716 * p2p_in_progress() does not report this to avoid issues for
7717 * scans during such provisioning step.
7718 */
7719 if (wpa_s->global->p2p_group_formation &&
7720 wpa_s->global->p2p_group_formation != wpa_s) {
7721 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7722 "in group formation",
7723 wpa_s->global->p2p_group_formation->ifname);
7724 ret = 1;
7725 }
7726 }
7727
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007728 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007729 struct os_reltime now;
7730 os_get_reltime(&now);
7731 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7732 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007733 /* Wait for the first client has expired */
7734 wpa_s->global->p2p_go_wait_client.sec = 0;
7735 } else {
7736 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7737 ret = 1;
7738 }
7739 }
7740
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007741 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007742}
7743
7744
7745void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7746 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007747{
7748 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7749 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007750 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007751 /**
7752 * Remove the network by scheduling the group formation
7753 * timeout to happen immediately. The teardown code
7754 * needs to be scheduled to run asynch later so that we
7755 * don't delete data from under ourselves unexpectedly.
7756 * Calling wpas_p2p_group_formation_timeout directly
7757 * causes a series of crashes in WPS failure scenarios.
7758 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007759 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7760 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007761 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007762 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007763 }
7764}
7765
7766
7767struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007768 const u8 *addr, const u8 *ssid,
7769 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007770{
7771 struct wpa_ssid *s;
7772 size_t i;
7773
7774 for (s = wpa_s->conf->ssid; s; s = s->next) {
7775 if (s->disabled != 2)
7776 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007777 if (ssid &&
7778 (ssid_len != s->ssid_len ||
7779 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7780 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007781 if (addr == NULL) {
7782 if (s->mode == WPAS_MODE_P2P_GO)
7783 return s;
7784 continue;
7785 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007786 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7787 return s; /* peer is GO in the persistent group */
7788 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7789 continue;
7790 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007791 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007792 addr, ETH_ALEN) == 0)
7793 return s; /* peer is P2P client in persistent
7794 * group */
7795 }
7796 }
7797
7798 return NULL;
7799}
7800
7801
7802void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7803 const u8 *addr)
7804{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007805 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007806 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007807 /*
7808 * This can happen if WPS provisioning step is not terminated
7809 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7810 * peer was able to connect, there is no need to time out group
7811 * formation after this, though. In addition, this is used with
7812 * the initial connection wait on the GO as a separate formation
7813 * timeout and as such, expected to be hit after the initial WPS
7814 * provisioning step.
7815 */
7816 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007817
7818 if (!wpa_s->p2p_go_group_formation_completed &&
7819 !wpa_s->group_formation_reported) {
7820 /*
7821 * GO has not yet notified group formation success since
7822 * the WPS step was not completed cleanly. Do that
7823 * notification now since the P2P Client was able to
7824 * connect and as such, must have received the
7825 * credential from the WPS step.
7826 */
7827 if (wpa_s->global->p2p)
7828 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007829 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007830 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007831 }
7832 if (!wpa_s->p2p_go_group_formation_completed) {
7833 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7834 wpa_s->p2p_go_group_formation_completed = 1;
7835 wpa_s->global->p2p_group_formation = NULL;
7836 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007837 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007838 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007839 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007840 if (addr == NULL)
7841 return;
7842 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7843}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007844
Dmitry Shmidt04949592012-07-19 12:16:46 -07007845
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007846static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7847 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007848{
7849 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007850 int ret = 0;
7851
Dmitry Shmidt04949592012-07-19 12:16:46 -07007852 if (wpa_s->global->p2p_group_formation)
7853 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007854 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007855 offchannel_send_action_done(wpa_s);
7856 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007857 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007858 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7859 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7860 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7861 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007862 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007863 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007864 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007865 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007866 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007867 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007868 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007869}
7870
7871
7872int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7873{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007874 int res;
7875
Dmitry Shmidt04949592012-07-19 12:16:46 -07007876 if (!wpa_s->p2p_fallback_to_go_neg ||
7877 wpa_s->p2p_in_provisioning <= 5)
7878 return 0;
7879
7880 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7881 return 0; /* peer operating as a GO */
7882
7883 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7884 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007885 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007886 "reason=GO-not-found");
7887 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007888
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007889 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007890}
7891
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007892
7893unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7894{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007895 struct wpa_supplicant *ifs;
7896
7897 if (wpa_s->wpa_state > WPA_SCANNING) {
7898 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7899 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007900 wpa_s->conf->p2p_search_delay);
7901 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007902 }
7903
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007904 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7905 radio_list) {
7906 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007907 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7908 "delay due to concurrent operation on "
7909 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007910 wpa_s->conf->p2p_search_delay,
7911 ifs->ifname);
7912 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007913 }
7914 }
7915
7916 return 0;
7917}
7918
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007919
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007920static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7921 struct wpa_ssid *s, const u8 *addr,
7922 int iface_addr)
7923{
7924 struct psk_list_entry *psk, *tmp;
7925 int changed = 0;
7926
7927 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7928 list) {
7929 if ((iface_addr && !psk->p2p &&
7930 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7931 (!iface_addr && psk->p2p &&
7932 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7933 wpa_dbg(wpa_s, MSG_DEBUG,
7934 "P2P: Remove persistent group PSK list entry for "
7935 MACSTR " p2p=%u",
7936 MAC2STR(psk->addr), psk->p2p);
7937 dl_list_del(&psk->list);
7938 os_free(psk);
7939 changed++;
7940 }
7941 }
7942
7943 return changed;
7944}
7945
7946
7947void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7948 const u8 *p2p_dev_addr,
7949 const u8 *psk, size_t psk_len)
7950{
7951 struct wpa_ssid *ssid = wpa_s->current_ssid;
7952 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007953 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007954
7955 if (psk_len != sizeof(p->psk))
7956 return;
7957
7958 if (p2p_dev_addr) {
7959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7960 " p2p_dev_addr=" MACSTR,
7961 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7962 if (is_zero_ether_addr(p2p_dev_addr))
7963 p2p_dev_addr = NULL;
7964 } else {
7965 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7966 MAC2STR(mac_addr));
7967 }
7968
7969 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7971 /* To be added to persistent group once created */
7972 if (wpa_s->global->add_psk == NULL) {
7973 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7974 if (wpa_s->global->add_psk == NULL)
7975 return;
7976 }
7977 p = wpa_s->global->add_psk;
7978 if (p2p_dev_addr) {
7979 p->p2p = 1;
7980 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7981 } else {
7982 p->p2p = 0;
7983 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7984 }
7985 os_memcpy(p->psk, psk, psk_len);
7986 return;
7987 }
7988
7989 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7991 return;
7992 }
7993
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007994 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007995 ssid->ssid_len);
7996 if (!persistent) {
7997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7998 return;
7999 }
8000
8001 p = os_zalloc(sizeof(*p));
8002 if (p == NULL)
8003 return;
8004 if (p2p_dev_addr) {
8005 p->p2p = 1;
8006 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8007 } else {
8008 p->p2p = 0;
8009 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8010 }
8011 os_memcpy(p->psk, psk, psk_len);
8012
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008013 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8014 (last = dl_list_last(&persistent->psk_list,
8015 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008016 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8017 MACSTR " (p2p=%u) to make room for a new one",
8018 MAC2STR(last->addr), last->p2p);
8019 dl_list_del(&last->list);
8020 os_free(last);
8021 }
8022
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008023 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008024 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8025 p2p_dev_addr == NULL);
8026 if (p2p_dev_addr) {
8027 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8028 MACSTR, MAC2STR(p2p_dev_addr));
8029 } else {
8030 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8031 MAC2STR(mac_addr));
8032 }
8033 dl_list_add(&persistent->psk_list, &p->list);
8034
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008035 if (wpa_s->p2pdev->conf->update_config &&
8036 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008037 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008038}
8039
8040
8041static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8042 struct wpa_ssid *s, const u8 *addr,
8043 int iface_addr)
8044{
8045 int res;
8046
8047 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008048 if (res > 0 && wpa_s->conf->update_config &&
8049 wpa_config_write(wpa_s->confname, wpa_s->conf))
8050 wpa_dbg(wpa_s, MSG_DEBUG,
8051 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008052}
8053
8054
8055static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8056 const u8 *peer, int iface_addr)
8057{
8058 struct hostapd_data *hapd;
8059 struct hostapd_wpa_psk *psk, *prev, *rem;
8060 struct sta_info *sta;
8061
8062 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8063 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8064 return;
8065
8066 /* Remove per-station PSK entry */
8067 hapd = wpa_s->ap_iface->bss[0];
8068 prev = NULL;
8069 psk = hapd->conf->ssid.wpa_psk;
8070 while (psk) {
8071 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8072 (!iface_addr &&
8073 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8075 MACSTR " iface_addr=%d",
8076 MAC2STR(peer), iface_addr);
8077 if (prev)
8078 prev->next = psk->next;
8079 else
8080 hapd->conf->ssid.wpa_psk = psk->next;
8081 rem = psk;
8082 psk = psk->next;
8083 os_free(rem);
8084 } else {
8085 prev = psk;
8086 psk = psk->next;
8087 }
8088 }
8089
8090 /* Disconnect from group */
8091 if (iface_addr)
8092 sta = ap_get_sta(hapd, peer);
8093 else
8094 sta = ap_get_sta_p2p(hapd, peer);
8095 if (sta) {
8096 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8097 " (iface_addr=%d) from group",
8098 MAC2STR(peer), iface_addr);
8099 hostapd_drv_sta_deauth(hapd, sta->addr,
8100 WLAN_REASON_DEAUTH_LEAVING);
8101 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8102 }
8103}
8104
8105
8106void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8107 int iface_addr)
8108{
8109 struct wpa_ssid *s;
8110 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008111 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008112
8113 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8114
8115 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008116 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008117 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8118 continue;
8119 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008120 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8121 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008122 }
8123
8124 /* Remove from any operating group */
8125 for (w = wpa_s->global->ifaces; w; w = w->next)
8126 wpas_p2p_remove_client_go(w, peer, iface_addr);
8127}
8128
8129
8130static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8131{
8132 struct wpa_supplicant *wpa_s = eloop_ctx;
8133 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8134}
8135
8136
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008137static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8138{
8139 struct wpa_supplicant *wpa_s = eloop_ctx;
8140
8141 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8142 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8143}
8144
8145
8146int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8147 struct wpa_ssid *ssid)
8148{
8149 struct wpa_supplicant *iface;
8150
8151 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8152 if (!iface->current_ssid ||
8153 iface->current_ssid->frequency == freq ||
8154 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8155 !iface->current_ssid->p2p_group))
8156 continue;
8157
8158 /* Remove the connection with least priority */
8159 if (!wpas_is_p2p_prioritized(iface)) {
8160 /* STA connection has priority over existing
8161 * P2P connection, so remove the interface. */
8162 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8163 eloop_register_timeout(0, 0,
8164 wpas_p2p_group_freq_conflict,
8165 iface, NULL);
8166 /* If connection in progress is P2P connection, do not
8167 * proceed for the connection. */
8168 if (wpa_s == iface)
8169 return -1;
8170 else
8171 return 0;
8172 } else {
8173 /* P2P connection has priority, disable the STA network
8174 */
8175 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8176 ssid);
8177 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8178 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8179 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8180 ETH_ALEN);
8181 /* If P2P connection is in progress, continue
8182 * connecting...*/
8183 if (wpa_s == iface)
8184 return 0;
8185 else
8186 return -1;
8187 }
8188 }
8189
8190 return 0;
8191}
8192
8193
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008194int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8195{
8196 struct wpa_ssid *ssid = wpa_s->current_ssid;
8197
8198 if (ssid == NULL || !ssid->p2p_group)
8199 return 0;
8200
8201 if (wpa_s->p2p_last_4way_hs_fail &&
8202 wpa_s->p2p_last_4way_hs_fail == ssid) {
8203 u8 go_dev_addr[ETH_ALEN];
8204 struct wpa_ssid *persistent;
8205
8206 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8207 ssid->ssid,
8208 ssid->ssid_len) <= 0) {
8209 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8210 goto disconnect;
8211 }
8212
8213 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8214 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008215 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008216 ssid->ssid,
8217 ssid->ssid_len);
8218 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8219 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8220 goto disconnect;
8221 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008222 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008223 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8224 persistent->id);
8225 disconnect:
8226 wpa_s->p2p_last_4way_hs_fail = NULL;
8227 /*
8228 * Remove the group from a timeout to avoid issues with caller
8229 * continuing to use the interface if this is on a P2P group
8230 * interface.
8231 */
8232 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8233 wpa_s, NULL);
8234 return 1;
8235 }
8236
8237 wpa_s->p2p_last_4way_hs_fail = ssid;
8238 return 0;
8239}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008240
8241
8242#ifdef CONFIG_WPS_NFC
8243
8244static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8245 struct wpabuf *p2p)
8246{
8247 struct wpabuf *ret;
8248 size_t wsc_len;
8249
8250 if (p2p == NULL) {
8251 wpabuf_free(wsc);
8252 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8253 return NULL;
8254 }
8255
8256 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8257 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8258 if (ret == NULL) {
8259 wpabuf_free(wsc);
8260 wpabuf_free(p2p);
8261 return NULL;
8262 }
8263
8264 wpabuf_put_be16(ret, wsc_len);
8265 if (wsc)
8266 wpabuf_put_buf(ret, wsc);
8267 wpabuf_put_be16(ret, wpabuf_len(p2p));
8268 wpabuf_put_buf(ret, p2p);
8269
8270 wpabuf_free(wsc);
8271 wpabuf_free(p2p);
8272 wpa_hexdump_buf(MSG_DEBUG,
8273 "P2P: Generated NFC connection handover message", ret);
8274
8275 if (ndef && ret) {
8276 struct wpabuf *tmp;
8277 tmp = ndef_build_p2p(ret);
8278 wpabuf_free(ret);
8279 if (tmp == NULL) {
8280 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8281 return NULL;
8282 }
8283 ret = tmp;
8284 }
8285
8286 return ret;
8287}
8288
8289
8290static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8291 struct wpa_ssid **ssid, u8 *go_dev_addr)
8292{
8293 struct wpa_supplicant *iface;
8294
8295 if (go_dev_addr)
8296 os_memset(go_dev_addr, 0, ETH_ALEN);
8297 if (ssid)
8298 *ssid = NULL;
8299 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8300 if (iface->wpa_state < WPA_ASSOCIATING ||
8301 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8302 !iface->current_ssid->p2p_group ||
8303 iface->current_ssid->mode != WPAS_MODE_INFRA)
8304 continue;
8305 if (ssid)
8306 *ssid = iface->current_ssid;
8307 if (go_dev_addr)
8308 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8309 return iface->assoc_freq;
8310 }
8311 return 0;
8312}
8313
8314
8315struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8316 int ndef)
8317{
8318 struct wpabuf *wsc, *p2p;
8319 struct wpa_ssid *ssid;
8320 u8 go_dev_addr[ETH_ALEN];
8321 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8322
8323 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8324 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8325 return NULL;
8326 }
8327
8328 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8329 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8330 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8331 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8332 return NULL;
8333 }
8334
8335 if (cli_freq == 0) {
8336 wsc = wps_build_nfc_handover_req_p2p(
8337 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8338 } else
8339 wsc = NULL;
8340 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8341 go_dev_addr, ssid ? ssid->ssid : NULL,
8342 ssid ? ssid->ssid_len : 0);
8343
8344 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8345}
8346
8347
8348struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8349 int ndef, int tag)
8350{
8351 struct wpabuf *wsc, *p2p;
8352 struct wpa_ssid *ssid;
8353 u8 go_dev_addr[ETH_ALEN];
8354 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8355
8356 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8357 return NULL;
8358
8359 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8360 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8361 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8362 return NULL;
8363
8364 if (cli_freq == 0) {
8365 wsc = wps_build_nfc_handover_sel_p2p(
8366 wpa_s->parent->wps,
8367 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8368 DEV_PW_NFC_CONNECTION_HANDOVER,
8369 wpa_s->conf->wps_nfc_dh_pubkey,
8370 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8371 } else
8372 wsc = NULL;
8373 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8374 go_dev_addr, ssid ? ssid->ssid : NULL,
8375 ssid ? ssid->ssid_len : 0);
8376
8377 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8378}
8379
8380
8381static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8382 struct p2p_nfc_params *params)
8383{
8384 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8385 "connection handover (freq=%d)",
8386 params->go_freq);
8387
8388 if (params->go_freq && params->go_ssid_len) {
8389 wpa_s->p2p_wps_method = WPS_NFC;
8390 wpa_s->pending_join_wps_method = WPS_NFC;
8391 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8392 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8393 ETH_ALEN);
8394 return wpas_p2p_join_start(wpa_s, params->go_freq,
8395 params->go_ssid,
8396 params->go_ssid_len);
8397 }
8398
8399 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8400 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008401 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008402 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8403 params->go_ssid_len ? params->go_ssid : NULL,
8404 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008405}
8406
8407
8408static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8409 struct p2p_nfc_params *params, int tag)
8410{
8411 int res, persistent;
8412 struct wpa_ssid *ssid;
8413
8414 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8415 "connection handover");
8416 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8417 ssid = wpa_s->current_ssid;
8418 if (ssid == NULL)
8419 continue;
8420 if (ssid->mode != WPAS_MODE_P2P_GO)
8421 continue;
8422 if (wpa_s->ap_iface == NULL)
8423 continue;
8424 break;
8425 }
8426 if (wpa_s == NULL) {
8427 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8428 return -1;
8429 }
8430
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008431 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008432 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008433 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008434 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8435 return -1;
8436 }
8437 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008438 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8439 wpa_s->p2pdev->p2p_oob_dev_pw,
8440 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8441 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008442 if (res)
8443 return res;
8444
8445 if (!tag) {
8446 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8447 return 0;
8448 }
8449
8450 if (!params->peer ||
8451 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8452 return 0;
8453
8454 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8455 " to join", MAC2STR(params->peer->p2p_device_addr));
8456
8457 wpa_s->global->p2p_invite_group = wpa_s;
8458 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008459 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008460 params->peer->p2p_device_addr,
8461 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008462 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008463
8464 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8465 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8466 ssid->ssid, ssid->ssid_len, ssid->frequency,
8467 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008468 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008469}
8470
8471
8472static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8473 struct p2p_nfc_params *params,
8474 int forced_freq)
8475{
8476 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8477 "connection handover");
8478 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8479 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008480 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008481 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8482 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008483}
8484
8485
8486static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8487 struct p2p_nfc_params *params,
8488 int forced_freq)
8489{
8490 int res;
8491
8492 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8493 "connection handover");
8494 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8495 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008496 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008497 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8498 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008499 if (res)
8500 return res;
8501
8502 res = wpas_p2p_listen(wpa_s, 60);
8503 if (res) {
8504 p2p_unauthorize(wpa_s->global->p2p,
8505 params->peer->p2p_device_addr);
8506 }
8507
8508 return res;
8509}
8510
8511
8512static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8513 const struct wpabuf *data,
8514 int sel, int tag, int forced_freq)
8515{
8516 const u8 *pos, *end;
8517 u16 len, id;
8518 struct p2p_nfc_params params;
8519 int res;
8520
8521 os_memset(&params, 0, sizeof(params));
8522 params.sel = sel;
8523
8524 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8525
8526 pos = wpabuf_head(data);
8527 end = pos + wpabuf_len(data);
8528
8529 if (end - pos < 2) {
8530 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8531 "attributes");
8532 return -1;
8533 }
8534 len = WPA_GET_BE16(pos);
8535 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008536 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008537 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8538 "attributes");
8539 return -1;
8540 }
8541 params.wsc_attr = pos;
8542 params.wsc_len = len;
8543 pos += len;
8544
8545 if (end - pos < 2) {
8546 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8547 "attributes");
8548 return -1;
8549 }
8550 len = WPA_GET_BE16(pos);
8551 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008552 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008553 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8554 "attributes");
8555 return -1;
8556 }
8557 params.p2p_attr = pos;
8558 params.p2p_len = len;
8559 pos += len;
8560
8561 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8562 params.wsc_attr, params.wsc_len);
8563 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8564 params.p2p_attr, params.p2p_len);
8565 if (pos < end) {
8566 wpa_hexdump(MSG_DEBUG,
8567 "P2P: Ignored extra data after P2P attributes",
8568 pos, end - pos);
8569 }
8570
8571 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8572 if (res)
8573 return res;
8574
8575 if (params.next_step == NO_ACTION)
8576 return 0;
8577
8578 if (params.next_step == BOTH_GO) {
8579 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8580 MAC2STR(params.peer->p2p_device_addr));
8581 return 0;
8582 }
8583
8584 if (params.next_step == PEER_CLIENT) {
8585 if (!is_zero_ether_addr(params.go_dev_addr)) {
8586 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8587 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8588 " ssid=\"%s\"",
8589 MAC2STR(params.peer->p2p_device_addr),
8590 params.go_freq,
8591 MAC2STR(params.go_dev_addr),
8592 wpa_ssid_txt(params.go_ssid,
8593 params.go_ssid_len));
8594 } else {
8595 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8596 "peer=" MACSTR " freq=%d",
8597 MAC2STR(params.peer->p2p_device_addr),
8598 params.go_freq);
8599 }
8600 return 0;
8601 }
8602
8603 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8604 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8605 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8606 return 0;
8607 }
8608
8609 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8610 wpa_s->p2p_oob_dev_pw = NULL;
8611
8612 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8613 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8614 "received");
8615 return -1;
8616 }
8617
8618 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8619 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8620 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8621 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8622 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8623 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8624 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8625
8626 if (tag) {
8627 if (id < 0x10) {
8628 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8629 "peer OOB Device Password Id %u", id);
8630 return -1;
8631 }
8632 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8633 "Device Password Id %u", id);
8634 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8635 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8636 params.oob_dev_pw_len -
8637 WPS_OOB_PUBKEY_HASH_LEN - 2);
8638 wpa_s->p2p_oob_dev_pw_id = id;
8639 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8640 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8641 params.oob_dev_pw_len -
8642 WPS_OOB_PUBKEY_HASH_LEN - 2);
8643 if (wpa_s->p2p_oob_dev_pw == NULL)
8644 return -1;
8645
8646 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8647 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8648 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8649 return -1;
8650 } else {
8651 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8652 "without Device Password");
8653 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8654 }
8655
8656 switch (params.next_step) {
8657 case NO_ACTION:
8658 case BOTH_GO:
8659 case PEER_CLIENT:
8660 /* already covered above */
8661 return 0;
8662 case JOIN_GROUP:
8663 return wpas_p2p_nfc_join_group(wpa_s, &params);
8664 case AUTH_JOIN:
8665 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8666 case INIT_GO_NEG:
8667 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8668 case RESP_GO_NEG:
8669 /* TODO: use own OOB Dev Pw */
8670 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8671 }
8672
8673 return -1;
8674}
8675
8676
8677int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8678 const struct wpabuf *data, int forced_freq)
8679{
8680 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8681 return -1;
8682
8683 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8684}
8685
8686
8687int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8688 const struct wpabuf *req,
8689 const struct wpabuf *sel, int forced_freq)
8690{
8691 struct wpabuf *tmp;
8692 int ret;
8693
8694 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8695 return -1;
8696
8697 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8698
8699 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8700 wpabuf_head(req), wpabuf_len(req));
8701 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8702 wpabuf_head(sel), wpabuf_len(sel));
8703 if (forced_freq)
8704 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8705 tmp = ndef_parse_p2p(init ? sel : req);
8706 if (tmp == NULL) {
8707 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8708 return -1;
8709 }
8710
8711 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8712 forced_freq);
8713 wpabuf_free(tmp);
8714
8715 return ret;
8716}
8717
8718
8719int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8720{
8721 const u8 *if_addr;
8722 int go_intent = wpa_s->conf->p2p_go_intent;
8723 struct wpa_supplicant *iface;
8724
8725 if (wpa_s->global->p2p == NULL)
8726 return -1;
8727
8728 if (!enabled) {
8729 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8730 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8731 {
8732 if (!iface->ap_iface)
8733 continue;
8734 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8735 }
8736 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8737 0, NULL);
8738 if (wpa_s->p2p_nfc_tag_enabled)
8739 wpas_p2p_remove_pending_group_interface(wpa_s);
8740 wpa_s->p2p_nfc_tag_enabled = 0;
8741 return 0;
8742 }
8743
8744 if (wpa_s->global->p2p_disabled)
8745 return -1;
8746
8747 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8748 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8749 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8750 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8751 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8752 "to allow static handover cases");
8753 return -1;
8754 }
8755
8756 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8757
8758 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8759 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8760 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8761 if (wpa_s->p2p_oob_dev_pw == NULL)
8762 return -1;
8763 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8764
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008765 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8766 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8767 /*
8768 * P2P Group Interface present and the command came on group
8769 * interface, so enable the token for the current interface.
8770 */
8771 wpa_s->create_p2p_iface = 0;
8772 } else {
8773 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8774 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008775
8776 if (wpa_s->create_p2p_iface) {
8777 enum wpa_driver_if_type iftype;
8778 /* Prepare to add a new interface for the group */
8779 iftype = WPA_IF_P2P_GROUP;
8780 if (go_intent == 15)
8781 iftype = WPA_IF_P2P_GO;
8782 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8783 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8784 "interface for the group");
8785 return -1;
8786 }
8787
8788 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008789 } else if (wpa_s->p2p_mgmt)
8790 if_addr = wpa_s->parent->own_addr;
8791 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008792 if_addr = wpa_s->own_addr;
8793
8794 wpa_s->p2p_nfc_tag_enabled = enabled;
8795
8796 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8797 struct hostapd_data *hapd;
8798 if (iface->ap_iface == NULL)
8799 continue;
8800 hapd = iface->ap_iface->bss[0];
8801 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8802 hapd->conf->wps_nfc_dh_pubkey =
8803 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8804 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8805 hapd->conf->wps_nfc_dh_privkey =
8806 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8807 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8808 hapd->conf->wps_nfc_dev_pw =
8809 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8810 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8811
8812 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8813 wpa_dbg(iface, MSG_DEBUG,
8814 "P2P: Failed to enable NFC Tag for GO");
8815 }
8816 }
8817 p2p_set_authorized_oob_dev_pw_id(
8818 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8819 if_addr);
8820
8821 return 0;
8822}
8823
8824#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008825
8826
8827static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8828 struct wpa_used_freq_data *freqs,
8829 unsigned int num)
8830{
8831 u8 curr_chan, cand, chan;
8832 unsigned int i;
8833
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008834 /*
8835 * If possible, optimize the Listen channel to be a channel that is
8836 * already used by one of the other interfaces.
8837 */
8838 if (!wpa_s->conf->p2p_optimize_listen_chan)
8839 return;
8840
8841 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8842 return;
8843
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008844 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8845 for (i = 0, cand = 0; i < num; i++) {
8846 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8847 if (curr_chan == chan) {
8848 cand = 0;
8849 break;
8850 }
8851
8852 if (chan == 1 || chan == 6 || chan == 11)
8853 cand = chan;
8854 }
8855
8856 if (cand) {
8857 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008858 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008859 cand);
8860 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8861 }
8862}
8863
8864
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008865static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008866{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008867 struct hostapd_config *conf;
8868 struct p2p_go_neg_results params;
8869 struct csa_settings csa_settings;
8870 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8871 int old_freq = current_ssid->frequency;
8872 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008873
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008874 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8875 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8876 return -1;
8877 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008878
8879 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008880 * TODO: This function may not always work correctly. For example,
8881 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008882 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008883 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008884 wpa_dbg(wpa_s, MSG_DEBUG,
8885 "P2P CSA: Failed to select new frequency for GO");
8886 return -1;
8887 }
8888
8889 if (current_ssid->frequency == params.freq) {
8890 wpa_dbg(wpa_s, MSG_DEBUG,
8891 "P2P CSA: Selected same frequency - not moving GO");
8892 return 0;
8893 }
8894
8895 conf = hostapd_config_defaults();
8896 if (!conf) {
8897 wpa_dbg(wpa_s, MSG_DEBUG,
8898 "P2P CSA: Failed to allocate default config");
8899 return -1;
8900 }
8901
8902 current_ssid->frequency = params.freq;
8903 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8904 wpa_dbg(wpa_s, MSG_DEBUG,
8905 "P2P CSA: Failed to create new GO config");
8906 ret = -1;
8907 goto out;
8908 }
8909
8910 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8911 wpa_dbg(wpa_s, MSG_DEBUG,
8912 "P2P CSA: CSA to a different band is not supported");
8913 ret = -1;
8914 goto out;
8915 }
8916
8917 os_memset(&csa_settings, 0, sizeof(csa_settings));
8918 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8919 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8920 csa_settings.freq_params.freq = params.freq;
8921 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8922 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8923 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8924
8925 if (conf->ieee80211ac) {
8926 int freq1 = 0, freq2 = 0;
8927 u8 chan, opclass;
8928
8929 if (ieee80211_freq_to_channel_ext(params.freq,
8930 conf->secondary_channel,
8931 conf->vht_oper_chwidth,
8932 &opclass, &chan) ==
8933 NUM_HOSTAPD_MODES) {
8934 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8935 ret = -1;
8936 goto out;
8937 }
8938
8939 if (conf->vht_oper_centr_freq_seg0_idx)
8940 freq1 = ieee80211_chan_to_freq(
8941 NULL, opclass,
8942 conf->vht_oper_centr_freq_seg0_idx);
8943
8944 if (conf->vht_oper_centr_freq_seg1_idx)
8945 freq2 = ieee80211_chan_to_freq(
8946 NULL, opclass,
8947 conf->vht_oper_centr_freq_seg1_idx);
8948
8949 if (freq1 < 0 || freq2 < 0) {
8950 wpa_dbg(wpa_s, MSG_DEBUG,
8951 "P2P CSA: Selected invalid VHT center freqs");
8952 ret = -1;
8953 goto out;
8954 }
8955
8956 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8957 csa_settings.freq_params.center_freq1 = freq1;
8958 csa_settings.freq_params.center_freq2 = freq2;
8959
8960 switch (conf->vht_oper_chwidth) {
8961 case VHT_CHANWIDTH_80MHZ:
8962 case VHT_CHANWIDTH_80P80MHZ:
8963 csa_settings.freq_params.bandwidth = 80;
8964 break;
8965 case VHT_CHANWIDTH_160MHZ:
8966 csa_settings.freq_params.bandwidth = 160;
8967 break;
8968 }
8969 }
8970
8971 ret = ap_switch_channel(wpa_s, &csa_settings);
8972out:
8973 current_ssid->frequency = old_freq;
8974 hostapd_config_free(conf);
8975 return ret;
8976}
8977
8978
8979static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8980{
8981 struct p2p_go_neg_results params;
8982 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8983
8984 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8985
8986 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8987 current_ssid->frequency);
8988
8989 /* Stop the AP functionality */
8990 /* TODO: Should do this in a way that does not indicated to possible
8991 * P2P Clients in the group that the group is terminated. */
8992 wpa_supplicant_ap_deinit(wpa_s);
8993
8994 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008995 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8997 wpas_p2p_group_delete(wpa_s,
8998 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8999 return;
9000 }
9001 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
9002 params.freq);
9003
9004 if (params.freq &&
9005 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
9006 wpa_printf(MSG_DEBUG,
9007 "P2P: Selected freq (%u MHz) is not valid for P2P",
9008 params.freq);
9009 wpas_p2p_group_delete(wpa_s,
9010 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
9011 return;
9012 }
9013
9014 /* Update the frequency */
9015 current_ssid->frequency = params.freq;
9016 wpa_s->connect_without_scan = current_ssid;
9017 wpa_s->reassociate = 1;
9018 wpa_s->disconnected = 0;
9019 wpa_supplicant_req_scan(wpa_s, 0, 0);
9020}
9021
9022
9023static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
9024{
9025 struct wpa_supplicant *wpa_s = eloop_ctx;
9026
9027 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009028 return;
9029
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009030 wpas_p2p_go_update_common_freqs(wpa_s);
9031
9032 /* Do not move GO in the middle of a CSA */
9033 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9034 wpa_printf(MSG_DEBUG,
9035 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009036 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009037 }
9038
9039 /*
9040 * First, try a channel switch flow. If it is not supported or fails,
9041 * take down the GO and bring it up again.
9042 */
9043 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9044 wpas_p2p_move_go_no_csa(wpa_s);
9045}
9046
9047
9048static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9049{
9050 struct wpa_supplicant *wpa_s = eloop_ctx;
9051 struct wpa_used_freq_data *freqs = NULL;
9052 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009053
9054 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9055 if (!freqs)
9056 return;
9057
9058 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9059
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009060 /* Previous attempt to move a GO was not possible -- try again. */
9061 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9062 WPAS_P2P_CHANNEL_UPDATE_ANY);
9063
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009064 os_free(freqs);
9065}
9066
9067
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009068/*
9069 * Consider moving a GO from its currently used frequency:
9070 * 1. It is possible that due to regulatory consideration the frequency
9071 * can no longer be used and there is a need to evacuate the GO.
9072 * 2. It is possible that due to MCC considerations, it would be preferable
9073 * to move the GO to a channel that is currently used by some other
9074 * station interface.
9075 *
9076 * In case a frequency that became invalid is once again valid, cancel a
9077 * previously initiated GO frequency change.
9078 */
9079static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9080 struct wpa_used_freq_data *freqs,
9081 unsigned int num)
9082{
9083 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9084 unsigned int timeout;
9085 int freq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009086 int dfs_offload;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009087
9088 wpas_p2p_go_update_common_freqs(wpa_s);
9089
9090 freq = wpa_s->current_ssid->frequency;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009091 dfs_offload = (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
9092 ieee80211_is_dfs(freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009093 for (i = 0, invalid_freq = 0; i < num; i++) {
9094 if (freqs[i].freq == freq) {
9095 flags = freqs[i].flags;
9096
9097 /* The channel is invalid, must change it */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009098 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
9099 !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009100 wpa_dbg(wpa_s, MSG_DEBUG,
9101 "P2P: Freq=%d MHz no longer valid for GO",
9102 freq);
9103 invalid_freq = 1;
9104 }
9105 } else if (freqs[i].flags == 0) {
9106 /* Freq is not used by any other station interface */
9107 continue;
9108 } else if (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07009109 freqs[i].freq) && !dfs_offload) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009110 /* Freq is not valid for P2P use cases */
9111 continue;
9112 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9113 P2P_GO_FREQ_MOVE_SCM) {
9114 policy_move = 1;
9115 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9116 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9117 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9118 policy_move = 1;
9119 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9120 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9121 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9122 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9123 policy_move = 1;
9124 } else if ((wpa_s->drv_flags &
9125 WPA_DRIVER_FLAGS_AP_CSA) &&
9126 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9127 u8 chan;
9128
9129 /*
9130 * We do not support CSA between bands, so move
9131 * GO only within the same band.
9132 */
9133 if (wpa_s->ap_iface->current_mode->mode ==
9134 ieee80211_freq_to_chan(freqs[i].freq,
9135 &chan))
9136 policy_move = 1;
9137 }
9138 }
9139 }
9140
9141 wpa_dbg(wpa_s, MSG_DEBUG,
9142 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9143 invalid_freq, policy_move, flags);
9144
9145 /*
9146 * The channel is valid, or we are going to have a policy move, so
9147 * cancel timeout.
9148 */
9149 if (!invalid_freq || policy_move) {
9150 wpa_dbg(wpa_s, MSG_DEBUG,
9151 "P2P: Cancel a GO move from freq=%d MHz", freq);
9152 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9153
9154 if (wpas_p2p_in_progress(wpa_s)) {
9155 wpa_dbg(wpa_s, MSG_DEBUG,
9156 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9157 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9158 wpa_s, NULL);
9159 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9160 wpas_p2p_reconsider_moving_go,
9161 wpa_s, NULL);
9162 return;
9163 }
9164 }
9165
9166 if (!invalid_freq && (!policy_move || flags != 0)) {
9167 wpa_dbg(wpa_s, MSG_DEBUG,
9168 "P2P: Not initiating a GO frequency change");
9169 return;
9170 }
9171
9172 /*
9173 * Do not consider moving GO if it is in the middle of a CSA. When the
9174 * CSA is finished this flow should be retriggered.
9175 */
9176 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9177 wpa_dbg(wpa_s, MSG_DEBUG,
9178 "P2P: Not initiating a GO frequency change - CSA is in progress");
9179 return;
9180 }
9181
9182 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9183 timeout = P2P_GO_FREQ_CHANGE_TIME;
9184 else
9185 timeout = 0;
9186
9187 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9188 freq, timeout);
9189 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9190 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9191}
9192
9193
9194static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9195 struct wpa_used_freq_data *freqs,
9196 unsigned int num,
9197 enum wpas_p2p_channel_update_trig trig)
9198{
9199 struct wpa_supplicant *ifs;
9200
9201 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9202 NULL);
9203
9204 /*
9205 * Travers all the radio interfaces, and for each GO interface, check
9206 * if there is a need to move the GO from the frequency it is using,
9207 * or in case the frequency is valid again, cancel the evacuation flow.
9208 */
9209 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9210 radio_list) {
9211 if (ifs->current_ssid == NULL ||
9212 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9213 continue;
9214
9215 /*
9216 * The GO was just started or completed channel switch, no need
9217 * to move it.
9218 */
9219 if (wpa_s == ifs &&
9220 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9221 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9222 wpa_dbg(wpa_s, MSG_DEBUG,
9223 "P2P: GO move - schedule re-consideration");
9224 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9225 wpas_p2p_reconsider_moving_go,
9226 wpa_s, NULL);
9227 continue;
9228 }
9229
9230 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9231 }
9232}
9233
9234
9235void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9236{
9237 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9238 return;
9239
9240 wpas_p2p_update_channel_list(wpa_s,
9241 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9242}
9243
9244
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009245void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9246{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009247 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9248 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9249 "the management interface is being removed");
9250 wpas_p2p_deinit_global(wpa_s->global);
9251 }
9252}
9253
9254
9255void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9256{
9257 if (wpa_s->ap_iface->bss)
9258 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9259 wpas_p2p_group_deinit(wpa_s);
9260}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009261
9262
9263int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9264 unsigned int period, unsigned int interval,
9265 unsigned int count)
9266{
9267 struct p2p_data *p2p = wpa_s->global->p2p;
9268 u8 *device_types;
9269 size_t dev_types_len;
9270 struct wpabuf *buf;
9271 int ret;
9272
9273 if (wpa_s->p2p_lo_started) {
9274 wpa_dbg(wpa_s, MSG_DEBUG,
9275 "P2P Listen offload is already started");
9276 return 0;
9277 }
9278
9279 if (wpa_s->global->p2p == NULL ||
9280 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9281 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9282 return -1;
9283 }
9284
9285 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9286 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9287 freq);
9288 return -1;
9289 }
9290
9291 /* Get device type */
9292 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9293 WPS_DEV_TYPE_LEN;
9294 device_types = os_malloc(dev_types_len);
9295 if (!device_types)
9296 return -1;
9297 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9298 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9299 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9300
9301 /* Get Probe Response IE(s) */
9302 buf = p2p_build_probe_resp_template(p2p, freq);
9303 if (!buf) {
9304 os_free(device_types);
9305 return -1;
9306 }
9307
9308 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9309 device_types, dev_types_len,
9310 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9311 if (ret < 0)
9312 wpa_dbg(wpa_s, MSG_DEBUG,
9313 "P2P: Failed to start P2P listen offload");
9314
9315 os_free(device_types);
9316 wpabuf_free(buf);
9317
9318 if (ret == 0) {
9319 wpa_s->p2p_lo_started = 1;
9320
9321 /* Stop current P2P listen if any */
9322 wpas_stop_listen(wpa_s);
9323 }
9324
9325 return ret;
9326}
9327
9328
9329int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9330{
9331 int ret;
9332
9333 if (!wpa_s->p2p_lo_started)
9334 return 0;
9335
9336 ret = wpa_drv_p2p_lo_stop(wpa_s);
9337 if (ret < 0)
9338 wpa_dbg(wpa_s, MSG_DEBUG,
9339 "P2P: Failed to stop P2P listen offload");
9340
9341 wpa_s->p2p_lo_started = 0;
9342 return ret;
9343}