blob: a24505f5ee6bd53c292b0f727969f6f1f5d0ac27 [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
310 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800311 if (ret == 0)
312 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800313 wpa_scan_free_params(params);
314 work->ctx = NULL;
315 if (ret) {
316 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 return;
319 }
320
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800321 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 os_get_reltime(&wpa_s->scan_trigger_time);
323 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
324 wpa_s->own_scan_requested = 1;
325 wpa_s->p2p_scan_work = work;
326}
327
328
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800329static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
330 int freq)
331{
332 if (wpa_s->global->p2p_24ghz_social_channels &&
333 (freq == 2412 || freq == 2437 || freq == 2462)) {
334 /*
335 * Search all social channels regardless of whether these have
336 * been disabled for P2P operating channel use to avoid missing
337 * peers.
338 */
339 return 1;
340 }
341 return p2p_supported_freq(wpa_s->global->p2p, freq);
342}
343
344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
346 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700347 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348{
349 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800350 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700352 unsigned int num_channels = 0;
353 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700355 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800356 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
358 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
359 return -1;
360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 if (wpa_s->p2p_scan_work) {
362 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
363 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700364 }
365
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800366 params = os_zalloc(sizeof(*params));
367 if (params == NULL)
368 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
370 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800371 params->num_ssids = 1;
372 n = os_malloc(P2P_WILDCARD_SSID_LEN);
373 if (n == NULL)
374 goto fail;
375 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
376 params->ssids[0].ssid = n;
377 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378
379 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
381 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 num_req_dev_types, req_dev_types);
383 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800384 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 switch (type) {
387 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700388 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
389 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 if (params->freqs == NULL)
391 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700392 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393 if (wpas_p2p_search_social_channel(
394 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700395 params->freqs[num_channels++] =
396 social_channels_freq[i];
397 }
398 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 break;
400 case P2P_SCAN_FULL:
401 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800402 case P2P_SCAN_SPECIFIC:
403 params->freqs = os_calloc(2, sizeof(int));
404 if (params->freqs == NULL)
405 goto fail;
406 params->freqs[0] = freq;
407 params->freqs[1] = 0;
408 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700410 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
411 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800412 if (params->freqs == NULL)
413 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700414 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800415 if (wpas_p2p_search_social_channel(
416 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700417 params->freqs[num_channels++] =
418 social_channels_freq[i];
419 }
420 if (p2p_supported_freq(wpa_s->global->p2p, freq))
421 params->freqs[num_channels++] = freq;
422 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 break;
424 }
425
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800426 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
427 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
428 if (ies == NULL) {
429 wpabuf_free(wps_ie);
430 goto fail;
431 }
432 wpabuf_put_buf(ies, wps_ie);
433 wpabuf_free(wps_ie);
434
435 bands = wpas_get_bands(wpa_s, params->freqs);
436 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
437
438 params->p2p_probe = 1;
439 n = os_malloc(wpabuf_len(ies));
440 if (n == NULL) {
441 wpabuf_free(ies);
442 goto fail;
443 }
444 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
445 params->extra_ies = n;
446 params->extra_ies_len = wpabuf_len(ies);
447 wpabuf_free(ies);
448
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800449 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800450 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
451 params) < 0)
452 goto fail;
453 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800455fail:
456 wpa_scan_free_params(params);
457 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458}
459
460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
462{
463 switch (p2p_group_interface) {
464 case P2P_GROUP_INTERFACE_PENDING:
465 return WPA_IF_P2P_GROUP;
466 case P2P_GROUP_INTERFACE_GO:
467 return WPA_IF_P2P_GO;
468 case P2P_GROUP_INTERFACE_CLIENT:
469 return WPA_IF_P2P_CLIENT;
470 }
471
472 return WPA_IF_P2P_GROUP;
473}
474
475
476static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
477 const u8 *ssid,
478 size_t ssid_len, int *go)
479{
480 struct wpa_ssid *s;
481
482 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
483 for (s = wpa_s->conf->ssid; s; s = s->next) {
484 if (s->disabled != 0 || !s->p2p_group ||
485 s->ssid_len != ssid_len ||
486 os_memcmp(ssid, s->ssid, ssid_len) != 0)
487 continue;
488 if (s->mode == WPAS_MODE_P2P_GO &&
489 s != wpa_s->current_ssid)
490 continue;
491 if (go)
492 *go = s->mode == WPAS_MODE_P2P_GO;
493 return wpa_s;
494 }
495 }
496
497 return NULL;
498}
499
500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800501static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
502{
503 struct wpa_supplicant *wpa_s = eloop_ctx;
504 wpa_printf(MSG_DEBUG,
505 "P2P: Complete previously requested removal of %s",
506 wpa_s->ifname);
507 wpas_p2p_disconnect(wpa_s);
508}
509
510
511static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
512 struct wpa_supplicant *calling_wpa_s)
513{
514 if (calling_wpa_s == wpa_s && wpa_s &&
515 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
516 /*
517 * The calling wpa_s instance is going to be removed. Do that
518 * from an eloop callback to keep the instance available until
519 * the caller has returned. This my be needed, e.g., to provide
520 * control interface responses on the per-interface socket.
521 */
522 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
523 wpa_s, NULL) < 0)
524 return -1;
525 return 0;
526 }
527
528 return wpas_p2p_disconnect(wpa_s);
529}
530
531
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800532/* Determine total number of clients in active groups where we are the GO */
533static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
534{
535 unsigned int count = 0;
536 struct wpa_ssid *s;
537
538 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
539 for (s = wpa_s->conf->ssid; s; s = s->next) {
540 wpa_printf(MSG_DEBUG,
541 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
542 wpa_s, s, s->disabled, s->p2p_group,
543 s->mode);
544 if (!s->disabled && s->p2p_group &&
545 s->mode == WPAS_MODE_P2P_GO) {
546 count += p2p_get_group_num_members(
547 wpa_s->p2p_group);
548 }
549 }
550 }
551
552 return count;
553}
554
555
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800556static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
557{
558 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
559 !wpa_s->current_ssid->disabled &&
560 wpa_s->current_ssid->p2p_group &&
561 wpa_s->current_ssid->p2p_persistent_group;
562}
563
564
565static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
566{
567 return p2p_is_active_persistent_group(wpa_s) &&
568 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
569}
570
571
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800572/* Find an interface for a P2P group where we are the GO */
573static struct wpa_supplicant *
574wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
575{
576 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800577
578 if (!wpa_s)
579 return NULL;
580
581 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800582 if (!p2p_is_active_persistent_go(wpa_s))
583 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800584
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800585 /* Prefer a group with connected clients */
586 if (p2p_get_group_num_members(wpa_s->p2p_group))
587 return wpa_s;
588 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800589 }
590
591 /* No group with connected clients, so pick the one without (if any) */
592 return save;
593}
594
595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800596static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800597{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800598 return p2p_is_active_persistent_group(wpa_s) &&
599 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
600}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800601
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800603/* Find an interface for a P2P group where we are the P2P Client */
604static struct wpa_supplicant *
605wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
606{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800608 if (p2p_is_active_persistent_cli(wpa_s))
609 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800610 }
611
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800612 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613}
614
615
616/* Find a persistent group where we are the GO */
617static struct wpa_ssid *
618wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
619{
620 struct wpa_ssid *s;
621
622 for (s = wpa_s->conf->ssid; s; s = s->next) {
623 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
624 return s;
625 }
626
627 return NULL;
628}
629
630
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800631static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
632 unsigned int *force_freq,
633 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800634{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800636 struct wpa_ssid *s;
637 u8 conncap = P2PS_SETUP_NONE;
638 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800639 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640 struct wpa_ssid *persistent_go;
641 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800642 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800643
644 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646 if (force_freq)
647 *force_freq = 0;
648 if (pref_freq)
649 *pref_freq = 0;
650
651 size = P2P_MAX_PREF_CHANNELS;
652 if (force_freq && pref_freq &&
653 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
654 (int *) pref_freq, 0, pref_freq_list, &size))
655 wpas_p2p_set_own_freq_preference(wpa_s,
656 *force_freq ? *force_freq :
657 *pref_freq);
658
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800659 /*
660 * For non-concurrent capable devices:
661 * If persistent_go, then no new.
662 * If GO, then no client.
663 * If client, then no GO.
664 */
665 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800666 if (go_wpa_s)
667 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800668 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
670 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800671
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 wpa_printf(MSG_DEBUG,
673 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
674 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800675
676 /* If not concurrent, restrict our choices */
677 if (p2p_no_group_iface) {
678 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
679
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800680 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681 return P2PS_SETUP_NONE;
682
683 if (go_wpa_s) {
684 if (role == P2PS_SETUP_CLIENT ||
685 incoming == P2PS_SETUP_GROUP_OWNER ||
686 p2p_client_limit_reached(go_wpa_s->p2p_group))
687 return P2PS_SETUP_NONE;
688
689 return P2PS_SETUP_GROUP_OWNER;
690 }
691
692 if (persistent_go) {
693 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
694 if (!incoming)
695 return P2PS_SETUP_GROUP_OWNER |
696 P2PS_SETUP_CLIENT;
697 if (incoming == P2PS_SETUP_NEW) {
698 u8 r;
699
700 if (os_get_random(&r, sizeof(r)) < 0 ||
701 (r & 1))
702 return P2PS_SETUP_CLIENT;
703 return P2PS_SETUP_GROUP_OWNER;
704 }
705 }
706 }
707 }
708
709 /* If a required role has been specified, handle it here */
710 if (role && role != P2PS_SETUP_NEW) {
711 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800712 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
713 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
714 /*
715 * Peer has an active GO, so if the role allows it and
716 * we do not have any active roles, become client.
717 */
718 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
719 !cli_wpa_s)
720 return P2PS_SETUP_CLIENT;
721
722 /* fall through */
723
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 case P2PS_SETUP_NONE:
725 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 conncap = role;
727 goto grp_owner;
728
729 case P2PS_SETUP_GROUP_OWNER:
730 /*
731 * Must be a complimentary role - cannot be a client to
732 * more than one peer.
733 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800734 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735 return P2PS_SETUP_NONE;
736
737 return P2PS_SETUP_CLIENT;
738
739 case P2PS_SETUP_CLIENT:
740 /* Must be a complimentary role */
741 if (incoming != role) {
742 conncap = P2PS_SETUP_GROUP_OWNER;
743 goto grp_owner;
744 }
745
746 default:
747 return P2PS_SETUP_NONE;
748 }
749 }
750
751 /*
752 * For now, we only will support ownership of one group, and being a
753 * client of one group. Therefore, if we have either an existing GO
754 * group, or an existing client group, we will not do a new GO
755 * negotiation, but rather try to re-use the existing groups.
756 */
757 switch (incoming) {
758 case P2PS_SETUP_NONE:
759 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800760 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800761 conncap = P2PS_SETUP_GROUP_OWNER;
762 else if (!owned_members)
763 conncap = P2PS_SETUP_NEW;
764 else if (incoming == P2PS_SETUP_NONE)
765 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
766 else
767 conncap = P2PS_SETUP_CLIENT;
768 break;
769
770 case P2PS_SETUP_CLIENT:
771 conncap = P2PS_SETUP_GROUP_OWNER;
772 break;
773
774 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800775 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800776 conncap = P2PS_SETUP_CLIENT;
777 break;
778
779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
780 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800781 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800782 conncap = P2PS_SETUP_GROUP_OWNER;
783 else {
784 u8 r;
785
786 if (os_get_random(&r, sizeof(r)) < 0 ||
787 (r & 1))
788 conncap = P2PS_SETUP_CLIENT;
789 else
790 conncap = P2PS_SETUP_GROUP_OWNER;
791 }
792 break;
793
794 default:
795 return P2PS_SETUP_NONE;
796 }
797
798grp_owner:
799 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
800 (!incoming && (conncap & P2PS_SETUP_NEW))) {
801 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
802 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800803
804 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800805 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800806 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800807 wpa_s->p2p_mgmt ?
808 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800809 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800810 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800811 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800812 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 wpa_printf(MSG_ERROR,
814 "P2P: Failed to allocate a new interface for the group");
815 return P2PS_SETUP_NONE;
816 }
817 wpa_s->global->pending_group_iface_for_p2ps = 1;
818 p2p_set_intended_addr(wpa_s->global->p2p,
819 wpa_s->pending_interface_addr);
820 }
821 }
822
823 return conncap;
824}
825
826
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700827static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
828 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829{
830 struct wpa_ssid *ssid;
831 char *gtype;
832 const char *reason;
833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 ssid = wpa_s->current_ssid;
835 if (ssid == NULL) {
836 /*
837 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700838 * pending P2P group interface waiting for provisioning or a
839 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 */
841 ssid = wpa_s->conf->ssid;
842 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700843 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 break;
845 ssid = ssid->next;
846 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300847 if (ssid == NULL &&
848 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
849 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700850 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
851 "not found");
852 return -1;
853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 }
855 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
856 gtype = "GO";
857 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
858 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
859 wpa_s->reassociate = 0;
860 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 gtype = "client";
862 } else
863 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700864
865 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
866 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
867
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800868 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700869 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
871 wpa_s, NULL)) {
872 wpa_printf(MSG_DEBUG,
873 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
874 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
875 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
876 wpa_s, NULL);
877 }
878 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (wpa_s->cross_connect_in_use) {
881 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800882 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700883 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
884 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700886 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 case P2P_GROUP_REMOVAL_REQUESTED:
888 reason = " reason=REQUESTED";
889 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700890 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
891 reason = " reason=FORMATION_FAILED";
892 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
894 reason = " reason=IDLE";
895 break;
896 case P2P_GROUP_REMOVAL_UNAVAILABLE:
897 reason = " reason=UNAVAILABLE";
898 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700899 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
900 reason = " reason=GO_ENDING_SESSION";
901 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700902 case P2P_GROUP_REMOVAL_PSK_FAILURE:
903 reason = " reason=PSK_FAILURE";
904 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800905 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
906 reason = " reason=FREQ_CONFLICT";
907 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 default:
909 reason = "";
910 break;
911 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800913 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700914 P2P_EVENT_GROUP_REMOVED "%s %s%s",
915 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800918 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
919 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700920 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
921 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800922 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800923 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800924 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
925 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700926 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800927 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700928 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700929
Dmitry Shmidt15907092014-03-25 10:42:57 -0700930 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800931 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
932 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700933
Dmitry Shmidt96571392013-10-14 12:54:46 -0700934 /*
935 * Make sure wait for the first client does not remain active after the
936 * group has been removed.
937 */
938 wpa_s->global->p2p_go_wait_client.sec = 0;
939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
941 struct wpa_global *global;
942 char *ifname;
943 enum wpa_driver_if_type type;
944 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
945 wpa_s->ifname);
946 global = wpa_s->global;
947 ifname = os_strdup(wpa_s->ifname);
948 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800949 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700950 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_s = global->ifaces;
952 if (wpa_s && ifname)
953 wpa_drv_if_remove(wpa_s, type, ifname);
954 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300955 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 }
957
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800958 /*
959 * The primary interface was used for P2P group operations, so
960 * need to reset its p2pdev.
961 */
962 wpa_s->p2pdev = wpa_s->parent;
963
Dmitry Shmidt96571392013-10-14 12:54:46 -0700964 if (!wpa_s->p2p_go_group_formation_completed) {
965 wpa_s->global->p2p_group_formation = NULL;
966 wpa_s->p2p_in_provisioning = 0;
967 }
968
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800969 wpa_s->show_group_started = 0;
970 os_free(wpa_s->go_params);
971 wpa_s->go_params = NULL;
972
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 os_free(wpa_s->p2p_group_common_freqs);
974 wpa_s->p2p_group_common_freqs = NULL;
975 wpa_s->p2p_group_common_freqs_num = 0;
976
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800977 wpa_s->waiting_presence_resp = 0;
978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
980 if (ssid && (ssid->p2p_group ||
981 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
982 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
983 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700984 if (ssid == wpa_s->current_ssid) {
985 wpa_sm_set_config(wpa_s->wpa, NULL);
986 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700988 }
989 /*
990 * Networks objects created during any P2P activities are not
991 * exposed out as they might/will confuse certain non-P2P aware
992 * applications since these network objects won't behave like
993 * regular ones.
994 *
995 * Likewise, we don't send out network removed signals for such
996 * network objects.
997 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 wpa_config_remove_network(wpa_s->conf, id);
999 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001000 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 } else {
1002 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1003 "found");
1004 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 if (wpa_s->ap_iface)
1006 wpa_supplicant_ap_deinit(wpa_s);
1007 else
1008 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001009
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001010 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1011
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013}
1014
1015
1016static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1017 u8 *go_dev_addr,
1018 const u8 *ssid, size_t ssid_len)
1019{
1020 struct wpa_bss *bss;
1021 const u8 *bssid;
1022 struct wpabuf *p2p;
1023 u8 group_capab;
1024 const u8 *addr;
1025
1026 if (wpa_s->go_params)
1027 bssid = wpa_s->go_params->peer_interface_addr;
1028 else
1029 bssid = wpa_s->bssid;
1030
1031 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001032 if (bss == NULL && wpa_s->go_params &&
1033 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1034 bss = wpa_bss_get_p2p_dev_addr(
1035 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 if (bss == NULL) {
1037 u8 iface_addr[ETH_ALEN];
1038 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1039 iface_addr) == 0)
1040 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1041 }
1042 if (bss == NULL) {
1043 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1044 "group is persistent - BSS " MACSTR " not found",
1045 MAC2STR(bssid));
1046 return 0;
1047 }
1048
1049 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001050 if (p2p == NULL)
1051 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1052 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 if (p2p == NULL) {
1054 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1055 "group is persistent - BSS " MACSTR
1056 " did not include P2P IE", MAC2STR(bssid));
1057 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1058 (u8 *) (bss + 1), bss->ie_len);
1059 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1060 ((u8 *) bss + 1) + bss->ie_len,
1061 bss->beacon_ie_len);
1062 return 0;
1063 }
1064
1065 group_capab = p2p_get_group_capab(p2p);
1066 addr = p2p_get_go_dev_addr(p2p);
1067 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1068 "group_capab=0x%x", group_capab);
1069 if (addr) {
1070 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1071 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1072 MAC2STR(addr));
1073 } else
1074 os_memset(go_dev_addr, 0, ETH_ALEN);
1075 wpabuf_free(p2p);
1076
1077 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1078 "go_dev_addr=" MACSTR,
1079 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1080
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001081 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082}
1083
1084
Jouni Malinen75ecf522011-06-27 15:19:46 -07001085static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1086 struct wpa_ssid *ssid,
1087 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088{
1089 struct wpa_ssid *s;
1090 int changed = 0;
1091
1092 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1093 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1094 for (s = wpa_s->conf->ssid; s; s = s->next) {
1095 if (s->disabled == 2 &&
1096 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1097 s->ssid_len == ssid->ssid_len &&
1098 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1099 break;
1100 }
1101
1102 if (s) {
1103 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1104 "entry");
1105 if (ssid->passphrase && !s->passphrase)
1106 changed = 1;
1107 else if (ssid->passphrase && s->passphrase &&
1108 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1109 changed = 1;
1110 } else {
1111 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1112 "entry");
1113 changed = 1;
1114 s = wpa_config_add_network(wpa_s->conf);
1115 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001116 return -1;
1117
1118 /*
1119 * Instead of network_added we emit persistent_group_added
1120 * notification. Also to keep the defense checks in
1121 * persistent_group obj registration method, we set the
1122 * relevant flags in s to designate it as a persistent group.
1123 */
1124 s->p2p_group = 1;
1125 s->p2p_persistent_group = 1;
1126 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_config_set_network_defaults(s);
1128 }
1129
1130 s->p2p_group = 1;
1131 s->p2p_persistent_group = 1;
1132 s->disabled = 2;
1133 s->bssid_set = 1;
1134 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1135 s->mode = ssid->mode;
1136 s->auth_alg = WPA_AUTH_ALG_OPEN;
1137 s->key_mgmt = WPA_KEY_MGMT_PSK;
1138 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001139 s->pbss = ssid->pbss;
1140 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 s->export_keys = 1;
1142 if (ssid->passphrase) {
1143 os_free(s->passphrase);
1144 s->passphrase = os_strdup(ssid->passphrase);
1145 }
1146 if (ssid->psk_set) {
1147 s->psk_set = 1;
1148 os_memcpy(s->psk, ssid->psk, 32);
1149 }
1150 if (s->passphrase && !s->psk_set)
1151 wpa_config_update_psk(s);
1152 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1153 os_free(s->ssid);
1154 s->ssid = os_malloc(ssid->ssid_len);
1155 }
1156 if (s->ssid) {
1157 s->ssid_len = ssid->ssid_len;
1158 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1159 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001160 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1161 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1162 wpa_s->global->add_psk = NULL;
1163 changed = 1;
1164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 if (changed && wpa_s->conf->update_config &&
1167 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1168 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1169 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001170
1171 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172}
1173
1174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1176 const u8 *addr)
1177{
1178 struct wpa_ssid *ssid, *s;
1179 u8 *n;
1180 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001181 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001182 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183
1184 ssid = wpa_s->current_ssid;
1185 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1186 !ssid->p2p_persistent_group)
1187 return;
1188
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1191 continue;
1192
1193 if (s->ssid_len == ssid->ssid_len &&
1194 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1195 break;
1196 }
1197
1198 if (s == NULL)
1199 return;
1200
1201 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001202 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001203 ETH_ALEN) != 0)
1204 continue;
1205
1206 if (i == s->num_p2p_clients - 1)
1207 return; /* already the most recent entry */
1208
1209 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001210 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1211 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1212 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001213 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001214 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1215 ETH_ALEN);
1216 os_memset(s->p2p_client_list +
1217 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1218 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001219 found = 1;
1220 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 }
1222
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001223 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1224 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001225 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 if (n == NULL)
1227 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001228 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1229 ETH_ALEN);
1230 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1231 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001232 s->p2p_client_list = n;
1233 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001234 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001235 /* Not enough room for an additional entry - drop the oldest
1236 * entry */
1237 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001238 s->p2p_client_list + 2 * ETH_ALEN,
1239 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001241 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001243 os_memset(s->p2p_client_list +
1244 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1245 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001248 if (p2p_wpa_s->conf->update_config &&
1249 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251}
1252
1253
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001254static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1255 int go, struct wpa_ssid *ssid, int freq,
1256 const u8 *psk, const char *passphrase,
1257 const u8 *go_dev_addr, int persistent,
1258 const char *extra)
1259{
1260 const char *ssid_txt;
1261 char psk_txt[65];
1262
1263 if (psk)
1264 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1265 else
1266 psk_txt[0] = '\0';
1267
1268 if (ssid)
1269 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1270 else
1271 ssid_txt = "";
1272
1273 if (passphrase && passphrase[0] == '\0')
1274 passphrase = NULL;
1275
1276 /*
1277 * Include PSK/passphrase only in the control interface message and
1278 * leave it out from the debug log entry.
1279 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001280 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001281 P2P_EVENT_GROUP_STARTED
1282 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1283 MACSTR "%s%s",
1284 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1285 psk ? " psk=" : "", psk_txt,
1286 passphrase ? " passphrase=\"" : "",
1287 passphrase ? passphrase : "",
1288 passphrase ? "\"" : "",
1289 MAC2STR(go_dev_addr),
1290 persistent ? " [PERSISTENT]" : "", extra);
1291 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1292 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1293 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1294 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1295 extra);
1296}
1297
1298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001300 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301{
1302 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 int client;
1304 int persistent;
1305 u8 go_dev_addr[ETH_ALEN];
1306
1307 /*
1308 * This callback is likely called for the main interface. Update wpa_s
1309 * to use the group interface if a new interface was created for the
1310 * group.
1311 */
1312 if (wpa_s->global->p2p_group_formation)
1313 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001314 if (wpa_s->p2p_go_group_formation_completed) {
1315 wpa_s->global->p2p_group_formation = NULL;
1316 wpa_s->p2p_in_provisioning = 0;
1317 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001318 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320
1321 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001322 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001323 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001324 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001325 if (already_deleted)
1326 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001327 wpas_p2p_group_delete(wpa_s,
1328 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return;
1330 }
1331
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_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334
1335 ssid = wpa_s->current_ssid;
1336 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1337 ssid->mode = WPAS_MODE_P2P_GO;
1338 p2p_group_notif_formation_done(wpa_s->p2p_group);
1339 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1340 }
1341
1342 persistent = 0;
1343 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 client = ssid->mode == WPAS_MODE_INFRA;
1345 if (ssid->mode == WPAS_MODE_P2P_GO) {
1346 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001347 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1348 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 } else
1350 persistent = wpas_p2p_persistent_group(wpa_s,
1351 go_dev_addr,
1352 ssid->ssid,
1353 ssid->ssid_len);
1354 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 client = wpa_s->p2p_group_interface ==
1356 P2P_GROUP_INTERFACE_CLIENT;
1357 os_memset(go_dev_addr, 0, ETH_ALEN);
1358 }
1359
1360 wpa_s->show_group_started = 0;
1361 if (client) {
1362 /*
1363 * Indicate event only after successfully completed 4-way
1364 * handshake, i.e., when the interface is ready for data
1365 * packets.
1366 */
1367 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001369 wpas_p2p_group_started(wpa_s, 1, ssid,
1370 ssid ? ssid->frequency : 0,
1371 ssid && ssid->passphrase == NULL &&
1372 ssid->psk_set ? ssid->psk : NULL,
1373 ssid ? ssid->passphrase : NULL,
1374 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 wpas_p2p_cross_connect_setup(wpa_s);
1376 wpas_p2p_set_group_idle_timeout(wpa_s);
1377 }
1378
1379 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001380 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1381 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001382 else {
1383 os_free(wpa_s->global->add_psk);
1384 wpa_s->global->add_psk = NULL;
1385 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001386
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001387 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001388 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001389 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391}
1392
1393
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001394struct send_action_work {
1395 unsigned int freq;
1396 u8 dst[ETH_ALEN];
1397 u8 src[ETH_ALEN];
1398 u8 bssid[ETH_ALEN];
1399 size_t len;
1400 unsigned int wait_time;
1401 u8 buf[0];
1402};
1403
1404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001405static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1406{
1407 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1408
1409 wpa_printf(MSG_DEBUG,
1410 "P2P: Free Action frame radio work @%p (freq=%u dst="
1411 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1412 wpa_s->p2p_send_action_work, awork->freq,
1413 MAC2STR(awork->dst), MAC2STR(awork->src),
1414 MAC2STR(awork->bssid), awork->wait_time);
1415 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1416 awork->buf, awork->len);
1417 os_free(awork);
1418 wpa_s->p2p_send_action_work->ctx = NULL;
1419 radio_work_done(wpa_s->p2p_send_action_work);
1420 wpa_s->p2p_send_action_work = NULL;
1421}
1422
1423
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001424static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1425 void *timeout_ctx)
1426{
1427 struct wpa_supplicant *wpa_s = eloop_ctx;
1428
1429 if (!wpa_s->p2p_send_action_work)
1430 return;
1431
1432 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001433 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001434}
1435
1436
Dmitry Shmidt03658832014-08-13 11:03:49 -07001437static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001439 if (wpa_s->p2p_send_action_work) {
1440 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001441
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001442 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001443 wpa_printf(MSG_DEBUG,
1444 "P2P: Clear Action TX work @%p (wait_time=%u)",
1445 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001446 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001447 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001448 } else {
1449 /*
1450 * In theory, this should not be needed, but number of
1451 * places in the P2P code is still using non-zero wait
1452 * time for the last Action frame in the sequence and
1453 * some of these do not call send_action_done().
1454 */
1455 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1456 wpa_s, NULL);
1457 eloop_register_timeout(
1458 0, awork->wait_time * 1000,
1459 wpas_p2p_send_action_work_timeout,
1460 wpa_s, NULL);
1461 }
1462 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001463}
1464
1465
1466static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1467 unsigned int freq,
1468 const u8 *dst, const u8 *src,
1469 const u8 *bssid,
1470 const u8 *data, size_t data_len,
1471 enum offchannel_send_action_result
1472 result)
1473{
1474 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1475
1476 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1479 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001481 switch (result) {
1482 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1483 res = P2P_SEND_ACTION_SUCCESS;
1484 break;
1485 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1486 res = P2P_SEND_ACTION_NO_ACK;
1487 break;
1488 case OFFCHANNEL_SEND_ACTION_FAILED:
1489 res = P2P_SEND_ACTION_FAILED;
1490 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 }
1492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001495 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1496 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001498 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1499 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001501 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1502 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001503 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001504 P2P_EVENT_FALLBACK_TO_GO_NEG
1505 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001506 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1507 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 }
1509}
1510
1511
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001512static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1513{
1514 struct wpa_supplicant *wpa_s = work->wpa_s;
1515 struct send_action_work *awork = work->ctx;
1516
1517 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001518 if (work->started) {
1519 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1520 wpa_s, NULL);
1521 wpa_s->p2p_send_action_work = NULL;
1522 offchannel_send_action_done(wpa_s);
1523 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 os_free(awork);
1525 return;
1526 }
1527
1528 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1529 awork->bssid, awork->buf, awork->len,
1530 awork->wait_time,
1531 wpas_p2p_send_action_tx_status, 1) < 0) {
1532 os_free(awork);
1533 radio_work_done(work);
1534 return;
1535 }
1536 wpa_s->p2p_send_action_work = work;
1537}
1538
1539
1540static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1541 unsigned int freq, const u8 *dst,
1542 const u8 *src, const u8 *bssid, const u8 *buf,
1543 size_t len, unsigned int wait_time)
1544{
1545 struct send_action_work *awork;
1546
1547 if (wpa_s->p2p_send_action_work) {
1548 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1549 return -1;
1550 }
1551
1552 awork = os_zalloc(sizeof(*awork) + len);
1553 if (awork == NULL)
1554 return -1;
1555
1556 awork->freq = freq;
1557 os_memcpy(awork->dst, dst, ETH_ALEN);
1558 os_memcpy(awork->src, src, ETH_ALEN);
1559 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1560 awork->len = len;
1561 awork->wait_time = wait_time;
1562 os_memcpy(awork->buf, buf, len);
1563
1564 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1565 wpas_send_action_cb, awork) < 0) {
1566 os_free(awork);
1567 return -1;
1568 }
1569
1570 return 0;
1571}
1572
1573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1575 const u8 *src, const u8 *bssid, const u8 *buf,
1576 size_t len, unsigned int wait_time)
1577{
1578 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001579 int listen_freq = -1, send_freq = -1;
1580
1581 if (wpa_s->p2p_listen_work)
1582 listen_freq = wpa_s->p2p_listen_work->freq;
1583 if (wpa_s->p2p_send_action_work)
1584 send_freq = wpa_s->p2p_send_action_work->freq;
1585 if (listen_freq != (int) freq && send_freq != (int) freq) {
1586 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1587 listen_freq, send_freq);
1588 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1589 len, wait_time);
1590 }
1591
1592 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1594 wait_time,
1595 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596}
1597
1598
1599static void wpas_send_action_done(void *ctx)
1600{
1601 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001602
1603 if (wpa_s->p2p_send_action_work) {
1604 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1605 wpa_s, NULL);
1606 os_free(wpa_s->p2p_send_action_work->ctx);
1607 radio_work_done(wpa_s->p2p_send_action_work);
1608 wpa_s->p2p_send_action_work = NULL;
1609 }
1610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612}
1613
1614
1615static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1616 struct p2p_go_neg_results *params)
1617{
1618 if (wpa_s->go_params == NULL) {
1619 wpa_s->go_params = os_malloc(sizeof(*params));
1620 if (wpa_s->go_params == NULL)
1621 return -1;
1622 }
1623 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1624 return 0;
1625}
1626
1627
1628static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1629 struct p2p_go_neg_results *res)
1630{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001631 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001632 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1633 " dev_addr " MACSTR " wps_method %d",
1634 MAC2STR(res->peer_interface_addr),
1635 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1637 res->ssid, res->ssid_len);
1638 wpa_supplicant_ap_deinit(wpa_s);
1639 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001640 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001642#ifdef CONFIG_WPS_NFC
1643 } else if (res->wps_method == WPS_NFC) {
1644 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1645 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001646 wpa_s->p2pdev->p2p_oob_dev_pw,
1647 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1648 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001649 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001650 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 NULL,
1652 NULL, 0, 0);
1653#endif /* CONFIG_WPS_NFC */
1654 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001656 if (wpa_s->p2p_wps_method == WPS_P2PS)
1657 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1659 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1660 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1661 wpa_s->p2p_pin, 1, dev_pw_id);
1662 }
1663}
1664
1665
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001666static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1667 struct wpa_ssid *ssid)
1668{
1669 struct wpa_ssid *persistent;
1670 struct psk_list_entry *psk;
1671 struct hostapd_data *hapd;
1672
1673 if (!wpa_s->ap_iface)
1674 return;
1675
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001676 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001677 ssid->ssid_len);
1678 if (persistent == NULL)
1679 return;
1680
1681 hapd = wpa_s->ap_iface->bss[0];
1682
1683 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1684 list) {
1685 struct hostapd_wpa_psk *hpsk;
1686
1687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1688 MACSTR " psk=%d",
1689 MAC2STR(psk->addr), psk->p2p);
1690 hpsk = os_zalloc(sizeof(*hpsk));
1691 if (hpsk == NULL)
1692 break;
1693 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1694 if (psk->p2p)
1695 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1696 else
1697 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1698 hpsk->next = hapd->conf->ssid.wpa_psk;
1699 hapd->conf->ssid.wpa_psk = hpsk;
1700 }
1701}
1702
1703
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001704static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1705{
1706 unsigned int i;
1707
1708 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1709 wpa_s->p2p_group_common_freqs_num);
1710
1711 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1712 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1713 i, wpa_s->p2p_group_common_freqs[i]);
1714}
1715
1716
1717static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1718 struct p2p_go_neg_results *params)
1719{
1720 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1721
1722 wpa_s->p2p_group_common_freqs_num = 0;
1723 os_free(wpa_s->p2p_group_common_freqs);
1724 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1725 if (!wpa_s->p2p_group_common_freqs)
1726 return;
1727
1728 for (i = 0; i < len; i++) {
1729 if (!wpa_s->go_params->freq_list[i])
1730 break;
1731 wpa_s->p2p_group_common_freqs[i] =
1732 wpa_s->go_params->freq_list[i];
1733 }
1734 wpa_s->p2p_group_common_freqs_num = i;
1735}
1736
1737
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001738static void p2p_config_write(struct wpa_supplicant *wpa_s)
1739{
1740#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001741 if (wpa_s->p2pdev->conf->update_config &&
1742 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001743 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1744#endif /* CONFIG_NO_CONFIG_WRITE */
1745}
1746
1747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748static void p2p_go_configured(void *ctx, void *data)
1749{
1750 struct wpa_supplicant *wpa_s = ctx;
1751 struct p2p_go_neg_results *params = data;
1752 struct wpa_ssid *ssid;
1753
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001754 wpa_s->ap_configured_cb = NULL;
1755 wpa_s->ap_configured_cb_ctx = NULL;
1756 wpa_s->ap_configured_cb_data = NULL;
1757 if (!wpa_s->go_params) {
1758 wpa_printf(MSG_ERROR,
1759 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1760 return;
1761 }
1762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001763 p2p_go_save_group_common_freqs(wpa_s, params);
1764 p2p_go_dump_common_freqs(wpa_s);
1765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 ssid = wpa_s->current_ssid;
1767 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1768 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1769 if (wpa_s->global->p2p_group_formation == wpa_s)
1770 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001771 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1772 params->passphrase[0] == '\0' ?
1773 params->psk : NULL,
1774 params->passphrase,
1775 wpa_s->global->p2p_dev_addr,
1776 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001777 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001779 if (wpa_s->p2pdev->p2ps_method_config_any) {
1780 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001781 wpa_dbg(wpa_s, MSG_DEBUG,
1782 "P2PS: Setting default PIN for ANY");
1783 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1784 "12345670", NULL, 0,
1785 0);
1786 } else {
1787 wpa_dbg(wpa_s, MSG_DEBUG,
1788 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001789 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001790 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001791 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001792 "12345670", NULL, 0, 0);
1793 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001794 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001795 }
1796
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001797 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001798 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001799 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001800 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001801 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001802 wpas_p2p_add_psk_list(wpa_s, ssid);
1803 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001804
1805 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001806 params->persistent_group, 0,
1807 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 wpas_p2p_cross_connect_setup(wpa_s);
1809 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001810
1811 if (wpa_s->p2p_first_connection_timeout) {
1812 wpa_dbg(wpa_s, MSG_DEBUG,
1813 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1814 wpa_s->p2p_first_connection_timeout);
1815 wpa_s->p2p_go_group_formation_completed = 0;
1816 wpa_s->global->p2p_group_formation = wpa_s;
1817 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001818 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001819 eloop_register_timeout(
1820 wpa_s->p2p_first_connection_timeout, 0,
1821 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001822 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001823 }
1824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return;
1826 }
1827
1828 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1829 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1830 params->peer_interface_addr)) {
1831 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1832 "filtering");
1833 return;
1834 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001835 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001837 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001838#ifdef CONFIG_WPS_NFC
1839 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001840 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001841 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001842 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001843 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1844 return;
1845 }
1846 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001847 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1848 wpa_s->p2pdev->p2p_oob_dev_pw,
1849 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1850 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001851#endif /* CONFIG_WPS_NFC */
1852 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001854 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 os_free(wpa_s->go_params);
1856 wpa_s->go_params = NULL;
1857}
1858
1859
1860static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1861 struct p2p_go_neg_results *params,
1862 int group_formation)
1863{
1864 struct wpa_ssid *ssid;
1865
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001866 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1867 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1869 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872
1873 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001874 if (ssid == NULL) {
1875 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 wpa_s->p2p_go_group_formation_completed = 0;
1881 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 wpa_config_set_network_defaults(ssid);
1885 ssid->temporary = 1;
1886 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001887 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1889 WPAS_MODE_P2P_GO;
1890 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001891 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001892 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001893 ssid->max_oper_chwidth = params->max_oper_chwidth;
1894 ssid->vht_center_freq2 = params->vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 ssid->ssid = os_zalloc(params->ssid_len + 1);
1896 if (ssid->ssid) {
1897 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1898 ssid->ssid_len = params->ssid_len;
1899 }
1900 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1901 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1902 ssid->proto = WPA_PROTO_RSN;
1903 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904 ssid->group_cipher = WPA_CIPHER_CCMP;
1905 if (params->freq > 56160) {
1906 /*
1907 * Enable GCMP instead of CCMP as pairwise_cipher and
1908 * group_cipher in 60 GHz.
1909 */
1910 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1911 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001912 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1913 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001914 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001915 if (os_strlen(params->passphrase) > 0) {
1916 ssid->passphrase = os_strdup(params->passphrase);
1917 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001918 wpa_msg_global(wpa_s, MSG_ERROR,
1919 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001920 wpa_config_remove_network(wpa_s->conf, ssid->id);
1921 return;
1922 }
1923 } else
1924 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001925 ssid->psk_set = params->psk_set;
1926 if (ssid->psk_set)
1927 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001928 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001929 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001930 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931
1932 wpa_s->ap_configured_cb = p2p_go_configured;
1933 wpa_s->ap_configured_cb_ctx = wpa_s;
1934 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001935 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001936 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 wpa_s->reassociate = 1;
1938 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001939 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1940 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 wpa_supplicant_req_scan(wpa_s, 0, 0);
1942}
1943
1944
1945static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1946 const struct wpa_supplicant *src)
1947{
1948 struct wpa_config *d;
1949 const struct wpa_config *s;
1950
1951 d = dst->conf;
1952 s = src->conf;
1953
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001954#define C(n) \
1955do { \
1956 if (s->n && !d->n) \
1957 d->n = os_strdup(s->n); \
1958} while (0)
1959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 C(device_name);
1961 C(manufacturer);
1962 C(model_name);
1963 C(model_number);
1964 C(serial_number);
1965 C(config_methods);
1966#undef C
1967
1968 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1969 os_memcpy(d->sec_device_type, s->sec_device_type,
1970 sizeof(d->sec_device_type));
1971 d->num_sec_device_types = s->num_sec_device_types;
1972
1973 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001974 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001976 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001977 d->max_num_sta = s->max_num_sta;
1978 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001979 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001980 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001981 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001982 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001983 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001984 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001985 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001986
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001987 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
1988 !d->wps_nfc_pw_from_config) {
1989 wpabuf_free(d->wps_nfc_dh_privkey);
1990 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001991 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1992 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1993 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001994 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995}
1996
1997
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001998static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1999 char *ifname, size_t len)
2000{
2001 char *ifname_ptr = wpa_s->ifname;
2002
2003 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2004 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2005 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2006 }
2007
2008 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2009 if (os_strlen(ifname) >= IFNAMSIZ &&
2010 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002011 int res;
2012
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002013 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002014 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2015 if (os_snprintf_error(len, res) && len)
2016 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002017 }
2018}
2019
2020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2022 enum wpa_driver_if_type type)
2023{
2024 char ifname[120], force_ifname[120];
2025
2026 if (wpa_s->pending_interface_name[0]) {
2027 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2028 "- skip creation of a new one");
2029 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2030 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2031 "unknown?! ifname='%s'",
2032 wpa_s->pending_interface_name);
2033 return -1;
2034 }
2035 return 0;
2036 }
2037
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 force_ifname[0] = '\0';
2040
2041 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2042 ifname);
2043 wpa_s->p2p_group_idx++;
2044
2045 wpa_s->pending_interface_type = type;
2046 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2047 wpa_s->pending_interface_addr, NULL) < 0) {
2048 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2049 "interface");
2050 return -1;
2051 }
2052
2053 if (force_ifname[0]) {
2054 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2055 force_ifname);
2056 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2057 sizeof(wpa_s->pending_interface_name));
2058 } else
2059 os_strlcpy(wpa_s->pending_interface_name, ifname,
2060 sizeof(wpa_s->pending_interface_name));
2061 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2062 MACSTR, wpa_s->pending_interface_name,
2063 MAC2STR(wpa_s->pending_interface_addr));
2064
2065 return 0;
2066}
2067
2068
2069static void wpas_p2p_remove_pending_group_interface(
2070 struct wpa_supplicant *wpa_s)
2071{
2072 if (!wpa_s->pending_interface_name[0] ||
2073 is_zero_ether_addr(wpa_s->pending_interface_addr))
2074 return; /* No pending virtual interface */
2075
2076 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2077 wpa_s->pending_interface_name);
2078 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2079 wpa_s->pending_interface_name);
2080 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2081 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002082 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083}
2084
2085
2086static struct wpa_supplicant *
2087wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2088{
2089 struct wpa_interface iface;
2090 struct wpa_supplicant *group_wpa_s;
2091
2092 if (!wpa_s->pending_interface_name[0]) {
2093 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2094 if (!wpas_p2p_create_iface(wpa_s))
2095 return NULL;
2096 /*
2097 * Something has forced us to remove the pending interface; try
2098 * to create a new one and hope for the best that we will get
2099 * the same local address.
2100 */
2101 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2102 WPA_IF_P2P_CLIENT) < 0)
2103 return NULL;
2104 }
2105
2106 os_memset(&iface, 0, sizeof(iface));
2107 iface.ifname = wpa_s->pending_interface_name;
2108 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002109 if (wpa_s->conf->ctrl_interface == NULL &&
2110 wpa_s->parent != wpa_s &&
2111 wpa_s->p2p_mgmt &&
2112 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2113 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2114 else
2115 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002117 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 if (group_wpa_s == NULL) {
2119 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2120 "wpa_supplicant interface");
2121 return NULL;
2122 }
2123 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2125 P2P_GROUP_INTERFACE_CLIENT;
2126 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002127 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128
2129 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2130
2131 return group_wpa_s;
2132}
2133
2134
2135static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2136 void *timeout_ctx)
2137{
2138 struct wpa_supplicant *wpa_s = eloop_ctx;
2139 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002140 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002141}
2142
2143
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002144static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2145 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002146{
2147 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002148 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 if (wpa_s->global->p2p)
2150 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002151 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152}
2153
2154
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002155static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2156{
2157 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002159 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002160 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002161 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002162 wpa_s->global->p2p_fail_on_wps_complete = 0;
2163}
2164
2165
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002166void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2167{
2168 if (wpa_s->global->p2p_group_formation != wpa_s)
2169 return;
2170 /* Speed up group formation timeout since this cannot succeed */
2171 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002172 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002173 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002174 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002175}
2176
2177
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002178static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179{
2180 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002181 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182
2183 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2184 wpa_drv_cancel_remain_on_channel(wpa_s);
2185 wpa_s->off_channel_freq = 0;
2186 wpa_s->roc_waiting_drv_freq = 0;
2187 }
2188
2189 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002190 wpa_msg_global(wpa_s, MSG_INFO,
2191 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2192 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002193 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpas_p2p_remove_pending_group_interface(wpa_s);
2195 return;
2196 }
2197
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002198 if (!res->role_go) {
2199 /* Inform driver of the operating channel of GO. */
2200 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2201 }
2202
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203 if (wpa_s->p2p_go_ht40)
2204 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002205 if (wpa_s->p2p_go_vht)
2206 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002207 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2208 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002209
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002210 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2211 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2212 " wps_method=%s",
2213 res->role_go ? "GO" : "client", res->freq, res->ht40,
2214 MAC2STR(res->peer_device_addr),
2215 MAC2STR(res->peer_interface_addr),
2216 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218
Dmitry Shmidt04949592012-07-19 12:16:46 -07002219 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2220 struct wpa_ssid *ssid;
2221 ssid = wpa_config_get_network(wpa_s->conf,
2222 wpa_s->p2p_persistent_id);
2223 if (ssid && ssid->disabled == 2 &&
2224 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2225 size_t len = os_strlen(ssid->passphrase);
2226 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2227 "on requested persistent group");
2228 os_memcpy(res->passphrase, ssid->passphrase, len);
2229 res->passphrase[len] = '\0';
2230 }
2231 }
2232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002234 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2236 if (group_wpa_s == NULL) {
2237 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002238 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2239 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002240 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 return;
2242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2244 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002246 group_wpa_s = wpa_s->parent;
2247 wpa_s->global->p2p_group_formation = group_wpa_s;
2248 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002249 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002252 group_wpa_s->p2p_in_provisioning = 1;
2253 group_wpa_s->p2pdev = wpa_s;
2254 if (group_wpa_s != wpa_s) {
2255 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2256 sizeof(group_wpa_s->p2p_pin));
2257 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2258 }
2259 if (res->role_go) {
2260 wpas_start_wps_go(group_wpa_s, res, 1);
2261 } else {
2262 os_get_reltime(&group_wpa_s->scan_min_time);
2263 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 }
2265
2266 wpa_s->p2p_long_listen = 0;
2267 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2268
2269 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2270 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2271 (res->peer_config_timeout % 100) * 10000,
2272 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2273}
2274
2275
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002276static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2277 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278{
2279 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002280 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002281 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2282 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002284 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285}
2286
2287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002288static void wpas_dev_found(void *ctx, const u8 *addr,
2289 const struct p2p_peer_info *info,
2290 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002292 u8 *wfd_dev_info = NULL;
2293 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002294#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 struct wpa_supplicant *wpa_s = ctx;
2296 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002297 char *wfd_dev_info_hex = NULL;
2298
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002299#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002300 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2301 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002302 if (wfd_dev_info_hex) {
2303 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2304 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2305 // Only used for notification, so not handling error.
2306 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2307 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002308#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002309
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002310 if (info->p2ps_instance) {
2311 char str[256];
2312 const u8 *buf = wpabuf_head(info->p2ps_instance);
2313 size_t len = wpabuf_len(info->p2ps_instance);
2314
2315 while (len) {
2316 u32 id;
2317 u16 methods;
2318 u8 str_len;
2319
2320 if (len < 4 + 2 + 1)
2321 break;
2322 id = WPA_GET_LE32(buf);
2323 buf += sizeof(u32);
2324 methods = WPA_GET_BE16(buf);
2325 buf += sizeof(u16);
2326 str_len = *buf++;
2327 if (str_len > len - 4 - 2 - 1)
2328 break;
2329 os_memcpy(str, buf, str_len);
2330 str[str_len] = '\0';
2331 buf += str_len;
2332 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2333
2334 wpa_msg_global(wpa_s, MSG_INFO,
2335 P2P_EVENT_DEVICE_FOUND MACSTR
2336 " p2p_dev_addr=" MACSTR
2337 " pri_dev_type=%s name='%s'"
2338 " config_methods=0x%x"
2339 " dev_capab=0x%x"
2340 " group_capab=0x%x"
2341 " adv_id=%x asp_svc=%s%s",
2342 MAC2STR(addr),
2343 MAC2STR(info->p2p_device_addr),
2344 wps_dev_type_bin2str(
2345 info->pri_dev_type,
2346 devtype, sizeof(devtype)),
2347 info->device_name, methods,
2348 info->dev_capab, info->group_capab,
2349 id, str,
2350 info->vendor_elems ?
2351 " vendor_elems=1" : "");
2352 }
2353 goto done;
2354 }
2355
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002356 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2357 " p2p_dev_addr=" MACSTR
2358 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002359 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002360 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2361 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2362 sizeof(devtype)),
2363 info->device_name, info->config_methods,
2364 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002365 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002366 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002367 info->vendor_elems ? " vendor_elems=1" : "",
2368 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002370done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002371 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002372#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002373
Roshan Piusfd2fd662017-01-23 13:41:57 -08002374 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2375 wfd_dev_info_len, new_device);
2376 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377}
2378
2379
2380static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2381{
2382 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002383
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002384 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2385 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2388}
2389
2390
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002391static void wpas_find_stopped(void *ctx)
2392{
2393 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002394
2395 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2396 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2397
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002398 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002399 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002400}
2401
2402
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002403struct wpas_p2p_listen_work {
2404 unsigned int freq;
2405 unsigned int duration;
2406 struct wpabuf *probe_resp_ie;
2407};
2408
2409
2410static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2411{
2412 if (lwork == NULL)
2413 return;
2414 wpabuf_free(lwork->probe_resp_ie);
2415 os_free(lwork);
2416}
2417
2418
2419static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2420{
2421 struct wpas_p2p_listen_work *lwork;
2422
2423 if (!wpa_s->p2p_listen_work)
2424 return;
2425
2426 lwork = wpa_s->p2p_listen_work->ctx;
2427 wpas_p2p_listen_work_free(lwork);
2428 radio_work_done(wpa_s->p2p_listen_work);
2429 wpa_s->p2p_listen_work = NULL;
2430}
2431
2432
2433static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2434{
2435 struct wpa_supplicant *wpa_s = work->wpa_s;
2436 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002437 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002438
2439 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002440 if (work->started) {
2441 wpa_s->p2p_listen_work = NULL;
2442 wpas_stop_listen(wpa_s);
2443 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002444 wpas_p2p_listen_work_free(lwork);
2445 return;
2446 }
2447
2448 wpa_s->p2p_listen_work = work;
2449
2450 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2451
2452 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2453 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2454 "report received Probe Request frames");
2455 wpas_p2p_listen_work_done(wpa_s);
2456 return;
2457 }
2458
2459 wpa_s->pending_listen_freq = lwork->freq;
2460 wpa_s->pending_listen_duration = lwork->duration;
2461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002462 duration = lwork->duration;
2463#ifdef CONFIG_TESTING_OPTIONS
2464 if (wpa_s->extra_roc_dur) {
2465 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2466 duration, duration + wpa_s->extra_roc_dur);
2467 duration += wpa_s->extra_roc_dur;
2468 }
2469#endif /* CONFIG_TESTING_OPTIONS */
2470
2471 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002472 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2473 "to remain on channel (%u MHz) for Listen "
2474 "state", lwork->freq);
2475 wpas_p2p_listen_work_done(wpa_s);
2476 wpa_s->pending_listen_freq = 0;
2477 return;
2478 }
2479 wpa_s->off_channel_freq = 0;
2480 wpa_s->roc_waiting_drv_freq = lwork->freq;
2481}
2482
2483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484static int wpas_start_listen(void *ctx, unsigned int freq,
2485 unsigned int duration,
2486 const struct wpabuf *probe_resp_ie)
2487{
2488 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002489 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002491 if (wpa_s->p2p_listen_work) {
2492 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 return -1;
2494 }
2495
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002496 lwork = os_zalloc(sizeof(*lwork));
2497 if (lwork == NULL)
2498 return -1;
2499 lwork->freq = freq;
2500 lwork->duration = duration;
2501 if (probe_resp_ie) {
2502 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2503 if (lwork->probe_resp_ie == NULL) {
2504 wpas_p2p_listen_work_free(lwork);
2505 return -1;
2506 }
2507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002509 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2510 lwork) < 0) {
2511 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 return -1;
2513 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514
2515 return 0;
2516}
2517
2518
2519static void wpas_stop_listen(void *ctx)
2520{
2521 struct wpa_supplicant *wpa_s = ctx;
2522 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2523 wpa_drv_cancel_remain_on_channel(wpa_s);
2524 wpa_s->off_channel_freq = 0;
2525 wpa_s->roc_waiting_drv_freq = 0;
2526 }
2527 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002528
2529 /*
2530 * Don't cancel Probe Request RX reporting for a connected P2P Client
2531 * handling Probe Request frames.
2532 */
2533 if (!wpa_s->p2p_cli_probe)
2534 wpa_drv_probe_req_report(wpa_s, 0);
2535
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002536 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537}
2538
2539
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002540static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2541 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542{
2543 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002544 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2545 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546}
2547
2548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2550 const u8 *peer, const char *params,
2551 unsigned int generated_pin)
2552{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002553 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2554 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555}
2556
2557
2558static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2559 const u8 *peer, const char *params)
2560{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002561 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2562 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563}
2564
2565
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002566static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2567 const u8 *dev_addr, const u8 *pri_dev_type,
2568 const char *dev_name, u16 supp_config_methods,
2569 u8 dev_capab, u8 group_capab, const u8 *group_id,
2570 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571{
2572 struct wpa_supplicant *wpa_s = ctx;
2573 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002574 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 u8 empty_dev_type[8];
2576 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002578 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002579
2580 if (group_id) {
2581 for (group = wpa_s->global->ifaces; group; group = group->next)
2582 {
2583 struct wpa_ssid *s = group->current_ssid;
2584 if (s != NULL &&
2585 s->mode == WPAS_MODE_P2P_GO &&
2586 group_id_len - ETH_ALEN == s->ssid_len &&
2587 os_memcmp(group_id + ETH_ALEN, s->ssid,
2588 s->ssid_len) == 0)
2589 break;
2590 }
2591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592
2593 if (pri_dev_type == NULL) {
2594 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2595 pri_dev_type = empty_dev_type;
2596 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002597 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2598 " pri_dev_type=%s name='%s' config_methods=0x%x "
2599 "dev_capab=0x%x group_capab=0x%x%s%s",
2600 MAC2STR(dev_addr),
2601 wps_dev_type_bin2str(pri_dev_type, devtype,
2602 sizeof(devtype)),
2603 dev_name, supp_config_methods, dev_capab, group_capab,
2604 group ? " group=" : "",
2605 group ? group->ifname : "");
2606 if (os_snprintf_error(sizeof(params), res))
2607 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002611 if (wps_generate_pin(&generated_pin) < 0) {
2612 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2613 wpas_notify_p2p_provision_discovery(
2614 wpa_s, peer, 0 /* response */,
2615 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2616 return;
2617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 wpas_prov_disc_local_display(wpa_s, peer, params,
2619 generated_pin);
2620 } else if (config_methods & WPS_CONFIG_KEYPAD)
2621 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2622 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002623 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2624 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002625
2626 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2627 P2P_PROV_DISC_SUCCESS,
2628 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629}
2630
2631
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002632static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633{
2634 struct wpa_supplicant *wpa_s = ctx;
2635 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002636 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002638 if (wpa_s->pending_pd_before_join &&
2639 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2640 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2641 wpa_s->pending_pd_before_join = 0;
2642 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2643 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002644 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002645 return;
2646 }
2647
Dmitry Shmidt04949592012-07-19 12:16:46 -07002648 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002649 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2650 int res;
2651
2652 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2653 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2654 if (os_snprintf_error(sizeof(params), res))
2655 params[sizeof(params) - 1] = '\0';
2656 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002657 params[0] = '\0';
2658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002660 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002662 if (wps_generate_pin(&generated_pin) < 0) {
2663 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2664 wpas_notify_p2p_provision_discovery(
2665 wpa_s, peer, 0 /* response */,
2666 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2667 return;
2668 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002669 wpas_prov_disc_local_display(wpa_s, peer, params,
2670 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002672 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2673 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674
Jouni Malinen75ecf522011-06-27 15:19:46 -07002675 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2676 P2P_PROV_DISC_SUCCESS,
2677 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678}
2679
2680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002681static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002682 enum p2p_prov_disc_status status,
2683 u32 adv_id, const u8 *adv_mac,
2684 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002685{
2686 struct wpa_supplicant *wpa_s = ctx;
2687
Dmitry Shmidt04949592012-07-19 12:16:46 -07002688 if (wpa_s->p2p_fallback_to_go_neg) {
2689 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2690 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002691 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002692 P2P_EVENT_FALLBACK_TO_GO_NEG
2693 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002694 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2695 return;
2696 }
2697
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002698 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002699 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002700 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2701 "join-existing-group operation (no ACK for PD "
2702 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002703 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002704 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002705 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002706
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002707 if (adv_id && adv_mac && deferred_session_resp) {
2708 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2709 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2710 " deferred_session_resp='%s'",
2711 MAC2STR(peer), status, adv_id,
2712 deferred_session_resp);
2713 } else if (adv_id && adv_mac) {
2714 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2715 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2716 MAC2STR(peer), status, adv_id);
2717 } else {
2718 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2719 " p2p_dev_addr=" MACSTR " status=%d",
2720 MAC2STR(peer), status);
2721 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002722
Jouni Malinen75ecf522011-06-27 15:19:46 -07002723 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2724 status, 0, 0);
2725}
2726
2727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002728static int freq_included(struct wpa_supplicant *wpa_s,
2729 const struct p2p_channels *channels,
2730 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002731{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002732 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2733 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2734 return 1;
2735 return 0;
2736}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002737
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002738
2739static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2740{
2741 unsigned int num = P2P_MAX_CHANNELS;
2742 int *common_freqs;
2743 int ret;
2744
2745 p2p_go_dump_common_freqs(wpa_s);
2746 common_freqs = os_calloc(num, sizeof(int));
2747 if (!common_freqs)
2748 return;
2749
2750 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2751 if (ret < 0) {
2752 wpa_dbg(wpa_s, MSG_DEBUG,
2753 "P2P: Failed to get group common freqs");
2754 os_free(common_freqs);
2755 return;
2756 }
2757
2758 os_free(wpa_s->p2p_group_common_freqs);
2759 wpa_s->p2p_group_common_freqs = common_freqs;
2760 wpa_s->p2p_group_common_freqs_num = num;
2761 p2p_go_dump_common_freqs(wpa_s);
2762}
2763
2764
2765/*
2766 * Check if the given frequency is one of the possible operating frequencies
2767 * set after the completion of the GO Negotiation.
2768 */
2769static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2770{
2771 unsigned int i;
2772
2773 p2p_go_dump_common_freqs(wpa_s);
2774
2775 /* assume no restrictions */
2776 if (!wpa_s->p2p_group_common_freqs_num)
2777 return 1;
2778
2779 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2780 if (wpa_s->p2p_group_common_freqs[i] == freq)
2781 return 1;
2782 }
2783 return 0;
2784}
2785
2786
2787static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2788 struct sta_info *sta, void *ctx)
2789{
2790 int *ecsa_support = ctx;
2791
2792 *ecsa_support &= sta->ecsa_supported;
2793
2794 return 0;
2795}
2796
2797
2798/* Check if all the peers support eCSA */
2799static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2800{
2801 int ecsa_support = 1;
2802
2803 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2804 &ecsa_support);
2805
2806 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002807}
2808
2809
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002810/**
2811 * Pick the best frequency to use from all the currently used frequencies.
2812 */
2813static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2814 struct wpa_used_freq_data *freqs,
2815 unsigned int num)
2816{
2817 unsigned int i, c;
2818
2819 /* find a candidate freq that is supported by P2P */
2820 for (c = 0; c < num; c++)
2821 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2822 break;
2823
2824 if (c == num)
2825 return 0;
2826
2827 /* once we have a candidate, try to find a 'better' one */
2828 for (i = c + 1; i < num; i++) {
2829 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2830 continue;
2831
2832 /*
2833 * 1. Infrastructure station interfaces have higher preference.
2834 * 2. P2P Clients have higher preference.
2835 * 3. All others.
2836 */
2837 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2838 c = i;
2839 break;
2840 }
2841
2842 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2843 c = i;
2844 }
2845 return freqs[c].freq;
2846}
2847
2848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2850 const u8 *go_dev_addr, const u8 *ssid,
2851 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002852 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002853 const struct p2p_channels *channels,
2854 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855{
2856 struct wpa_supplicant *wpa_s = ctx;
2857 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002858 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002860 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861
2862 if (!persistent_group) {
2863 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002864 " to join an active group (SSID: %s)",
2865 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2867 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2868 == 0 ||
2869 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2870 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2871 "authorized invitation");
2872 goto accept_inv;
2873 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002874
2875#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002876 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2877 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002878 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002879 wpa_s->p2p_wps_method = WPS_NFC;
2880 wpa_s->pending_join_wps_method = WPS_NFC;
2881 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002882 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002883 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002884 bssid, ETH_ALEN);
2885 goto accept_inv;
2886 }
2887#endif /* CONFIG_WPS_NFC */
2888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 /*
2890 * Do not accept the invitation automatically; notify user and
2891 * request approval.
2892 */
2893 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2894 }
2895
2896 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2897 if (grp) {
2898 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2899 "running persistent group");
2900 if (*go)
2901 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2902 goto accept_inv;
2903 }
2904
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002905 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2906 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2907 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2908 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002909 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002910 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2912
2913 for (s = wpa_s->conf->ssid; s; s = s->next) {
2914 if (s->disabled == 2 &&
2915 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2916 s->ssid_len == ssid_len &&
2917 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2918 break;
2919 }
2920
2921 if (!s) {
2922 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2923 " requested reinvocation of an unknown group",
2924 MAC2STR(sa));
2925 return P2P_SC_FAIL_UNKNOWN_GROUP;
2926 }
2927
2928 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2929 *go = 1;
2930 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2931 wpa_printf(MSG_DEBUG, "P2P: The only available "
2932 "interface is already in use - reject "
2933 "invitation");
2934 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2935 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002936 if (wpa_s->p2p_mgmt)
2937 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2938 ETH_ALEN);
2939 else
2940 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 } else if (s->mode == WPAS_MODE_P2P_GO) {
2942 *go = 1;
2943 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2944 {
2945 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2946 "interface address for the group");
2947 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2948 }
2949 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2950 ETH_ALEN);
2951 }
2952
2953accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002954 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2955
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002956 best_freq = 0;
2957 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2958 sizeof(struct wpa_used_freq_data));
2959 if (freqs) {
2960 int num_channels = wpa_s->num_multichan_concurrent;
2961 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2962 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2963 os_free(freqs);
2964 }
2965
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002966 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002967 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002968 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002969 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002970
2971 if (wpa_s->num_multichan_concurrent < 2 ||
2972 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2973 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 -07002974 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 }
2977
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002978 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2979 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002980 if (*go == 0) {
2981 /* We are the client */
2982 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2983 "running a GO but we are capable of MCC, "
2984 "figure out the best channel to use");
2985 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002986 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002987 /* We are the GO, and *force_freq is not in the
2988 * intersection */
2989 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2990 "in intersection but we are capable of MCC, "
2991 "figure out the best channel to use",
2992 *force_freq);
2993 *force_freq = 0;
2994 }
2995 }
2996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 return P2P_SC_SUCCESS;
2998}
2999
3000
3001static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3002 const u8 *ssid, size_t ssid_len,
3003 const u8 *go_dev_addr, u8 status,
3004 int op_freq)
3005{
3006 struct wpa_supplicant *wpa_s = ctx;
3007 struct wpa_ssid *s;
3008
3009 for (s = wpa_s->conf->ssid; s; s = s->next) {
3010 if (s->disabled == 2 &&
3011 s->ssid_len == ssid_len &&
3012 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3013 break;
3014 }
3015
3016 if (status == P2P_SC_SUCCESS) {
3017 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003018 " was accepted; op_freq=%d MHz, SSID=%s",
3019 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003021 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003022 if (go) {
3023 wpa_msg_global(wpa_s, MSG_INFO,
3024 P2P_EVENT_INVITATION_ACCEPTED
3025 "sa=" MACSTR
3026 " persistent=%d freq=%d",
3027 MAC2STR(sa), s->id, op_freq);
3028 } else {
3029 wpa_msg_global(wpa_s, MSG_INFO,
3030 P2P_EVENT_INVITATION_ACCEPTED
3031 "sa=" MACSTR
3032 " persistent=%d",
3033 MAC2STR(sa), s->id);
3034 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003036 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003037 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3038 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003040 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003041 wpa_msg_global(wpa_s, MSG_INFO,
3042 P2P_EVENT_INVITATION_ACCEPTED
3043 "sa=" MACSTR " go_dev_addr=" MACSTR
3044 " bssid=" MACSTR " unknown-network",
3045 MAC2STR(sa), MAC2STR(go_dev_addr),
3046 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003048 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003049 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 }
3051 return;
3052 }
3053
3054 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3055 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3056 " was rejected (status %u)", MAC2STR(sa), status);
3057 return;
3058 }
3059
3060 if (!s) {
3061 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003062 wpa_msg_global(wpa_s, MSG_INFO,
3063 P2P_EVENT_INVITATION_RECEIVED
3064 "sa=" MACSTR " go_dev_addr=" MACSTR
3065 " bssid=" MACSTR " unknown-network",
3066 MAC2STR(sa), MAC2STR(go_dev_addr),
3067 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003069 wpa_msg_global(wpa_s, MSG_INFO,
3070 P2P_EVENT_INVITATION_RECEIVED
3071 "sa=" MACSTR " go_dev_addr=" MACSTR
3072 " unknown-network",
3073 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003075 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3076 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 return;
3078 }
3079
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003080 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003081 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3082 "sa=" MACSTR " persistent=%d freq=%d",
3083 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003084 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003085 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3086 "sa=" MACSTR " persistent=%d",
3087 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003088 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003089 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3090 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091}
3092
3093
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003094static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3095 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003096 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003097{
3098 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003099 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003100
3101 if (ssid == NULL)
3102 return;
3103
3104 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003105 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003106 ETH_ALEN) == 0)
3107 break;
3108 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003109 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003110 if (ssid->mode != WPAS_MODE_P2P_GO &&
3111 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3112 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3113 "due to invitation result", ssid->id);
3114 wpas_notify_network_removed(wpa_s, ssid);
3115 wpa_config_remove_network(wpa_s->conf, ssid->id);
3116 return;
3117 }
3118 return; /* Peer not found in client list */
3119 }
3120
3121 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003122 "group %d client list%s",
3123 MAC2STR(peer), ssid->id,
3124 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003125 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3126 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3127 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003128 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003129 if (p2p_wpa_s->conf->update_config &&
3130 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003131 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003132}
3133
3134
3135static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3136 const u8 *peer)
3137{
3138 struct wpa_ssid *ssid;
3139
3140 wpa_s = wpa_s->global->p2p_invite_group;
3141 if (wpa_s == NULL)
3142 return; /* No known invitation group */
3143 ssid = wpa_s->current_ssid;
3144 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3145 !ssid->p2p_persistent_group)
3146 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003147 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003148 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003149 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003150}
3151
3152
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003153static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003154 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003155 const u8 *peer, int neg_freq,
3156 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157{
3158 struct wpa_supplicant *wpa_s = ctx;
3159 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003160 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161
3162 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003163 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3164 "status=%d " MACSTR,
3165 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003167 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3168 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 }
3170 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3171
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003172 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3173 status, MAC2STR(peer));
3174 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003175 struct wpa_supplicant *group_if =
3176 wpa_s->global->p2p_invite_group;
3177
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003178 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3179 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003180
3181 /*
3182 * Invitation to an active group. If this is successful and we
3183 * are the GO, set the client wait to postpone some concurrent
3184 * operations and to allow provisioning and connection to happen
3185 * more quickly.
3186 */
3187 if (status == P2P_SC_SUCCESS &&
3188 group_if && group_if->current_ssid &&
3189 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3190 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3191#ifdef CONFIG_TESTING_OPTIONS
3192 if (group_if->p2p_go_csa_on_inv) {
3193 wpa_printf(MSG_DEBUG,
3194 "Testing: force P2P GO CSA after invitation");
3195 eloop_cancel_timeout(
3196 wpas_p2p_reconsider_moving_go,
3197 wpa_s, NULL);
3198 eloop_register_timeout(
3199 0, 50000,
3200 wpas_p2p_reconsider_moving_go,
3201 wpa_s, NULL);
3202 }
3203#endif /* CONFIG_TESTING_OPTIONS */
3204 }
3205 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003208 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3209 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3210 "invitation exchange to indicate readiness for "
3211 "re-invocation");
3212 }
3213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003215 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3216 ssid = wpa_config_get_network(
3217 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003218 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 wpas_p2p_remove_pending_group_interface(wpa_s);
3221 return;
3222 }
3223
3224 ssid = wpa_config_get_network(wpa_s->conf,
3225 wpa_s->pending_invite_ssid_id);
3226 if (ssid == NULL) {
3227 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3228 "data matching with invitation");
3229 return;
3230 }
3231
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003232 /*
3233 * The peer could have missed our ctrl::ack frame for Invitation
3234 * Response and continue retransmitting the frame. To reduce the
3235 * likelihood of the peer not getting successful TX status for the
3236 * Invitation Response frame, wait a short time here before starting
3237 * the persistent group so that we will remain on the current channel to
3238 * acknowledge any possible retransmission from the peer.
3239 */
3240 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3241 "starting persistent group");
3242 os_sleep(0, 50000);
3243
Dmitry Shmidt15907092014-03-25 10:42:57 -07003244 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003245 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003246 freq = neg_freq;
3247 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003248 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003249 freq = peer_oper_freq;
3250 else
3251 freq = 0;
3252
3253 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3254 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003256 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003257 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003258 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003259 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003260 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003261 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003262 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003263 ssid->mode == WPAS_MODE_P2P_GO ?
3264 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003265 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266}
3267
3268
Dmitry Shmidt04949592012-07-19 12:16:46 -07003269static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3270 unsigned int freq)
3271{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003272 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3273 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003274 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003275}
3276
3277
3278static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3279{
3280 reg->channel[reg->channels] = chan;
3281 reg->channels++;
3282}
3283
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003286 struct p2p_channels *chan,
3287 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288{
3289 int i, cla = 0;
3290
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003291 wpa_s->global->p2p_24ghz_social_channels = 1;
3292
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003293 os_memset(cli_chan, 0, sizeof(*cli_chan));
3294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3296 "band");
3297
3298 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3299 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003300 chan->reg_class[cla].channels = 0;
3301 for (i = 0; i < 11; i++) {
3302 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3303 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3304 }
3305 if (chan->reg_class[cla].channels)
3306 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307
3308 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3309 "band");
3310
3311 /* Operating class 115 - 5 GHz, channels 36-48 */
3312 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 chan->reg_class[cla].channels = 0;
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3316 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3317 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3318 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3319 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3320 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3321 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3322 if (chan->reg_class[cla].channels)
3323 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324
3325 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3326 "band");
3327
3328 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3329 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003330 chan->reg_class[cla].channels = 0;
3331 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3332 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3333 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3334 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3335 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3336 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3337 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3338 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3339 if (chan->reg_class[cla].channels)
3340 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003341
3342 chan->reg_classes = cla;
3343 return 0;
3344}
3345
3346
Dmitry Shmidt04949592012-07-19 12:16:46 -07003347static int has_channel(struct wpa_global *global,
3348 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003349{
3350 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003351 unsigned int freq;
3352
3353 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3354 chan * 5;
3355 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003356 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003357
3358 for (i = 0; i < mode->num_channels; i++) {
3359 if (mode->channels[i].chan == chan) {
3360 if (flags)
3361 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003362 if (mode->channels[i].flag &
3363 (HOSTAPD_CHAN_DISABLED |
3364 HOSTAPD_CHAN_RADAR))
3365 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003366 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3367 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003368 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369 }
3370 }
3371
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003372 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373}
3374
3375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003376static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3377 struct hostapd_hw_modes *mode,
3378 u8 channel)
3379{
3380 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003381 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003382
3383 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3384 return 0;
3385
3386 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3387 /*
3388 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3389 * so the center channel is 6 channels away from the start/end.
3390 */
3391 if (channel >= center_channels[i] - 6 &&
3392 channel <= center_channels[i] + 6)
3393 return center_channels[i];
3394
3395 return 0;
3396}
3397
3398
3399static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3400 struct hostapd_hw_modes *mode,
3401 u8 channel, u8 bw)
3402{
3403 u8 center_chan;
3404 int i, flags;
3405 enum chan_allowed res, ret = ALLOWED;
3406
3407 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3408 if (!center_chan)
3409 return NOT_ALLOWED;
3410 if (center_chan >= 58 && center_chan <= 138)
3411 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3412
3413 /* check all the channels are available */
3414 for (i = 0; i < 4; i++) {
3415 int adj_chan = center_chan - 6 + i * 4;
3416
3417 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3418 if (res == NOT_ALLOWED)
3419 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003420 if (res == NO_IR)
3421 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003422
3423 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3424 return NOT_ALLOWED;
3425 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3426 return NOT_ALLOWED;
3427 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3428 return NOT_ALLOWED;
3429 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3430 return NOT_ALLOWED;
3431 }
3432
3433 return ret;
3434}
3435
3436
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003437static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3438 struct hostapd_hw_modes *mode,
3439 u8 channel)
3440{
3441 u8 center_channels[] = { 50, 114 };
3442 unsigned int i;
3443
3444 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3445 return 0;
3446
3447 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3448 /*
3449 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3450 * so the center channel is 14 channels away from the start/end.
3451 */
3452 if (channel >= center_channels[i] - 14 &&
3453 channel <= center_channels[i] + 14)
3454 return center_channels[i];
3455
3456 return 0;
3457}
3458
3459
3460static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3461 struct hostapd_hw_modes *mode,
3462 u8 channel, u8 bw)
3463{
3464 u8 center_chan;
3465 int i, flags;
3466 enum chan_allowed res, ret = ALLOWED;
3467
3468 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3469 if (!center_chan)
3470 return NOT_ALLOWED;
3471 /* VHT 160 MHz uses DFS channels in most countries. */
3472
3473 /* Check all the channels are available */
3474 for (i = 0; i < 8; i++) {
3475 int adj_chan = center_chan - 14 + i * 4;
3476
3477 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3478 if (res == NOT_ALLOWED)
3479 return NOT_ALLOWED;
3480
3481 if (res == NO_IR)
3482 ret = NO_IR;
3483
3484 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3485 return NOT_ALLOWED;
3486 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3487 return NOT_ALLOWED;
3488 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3489 return NOT_ALLOWED;
3490 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3491 return NOT_ALLOWED;
3492 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3493 return NOT_ALLOWED;
3494 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3495 return NOT_ALLOWED;
3496 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3497 return NOT_ALLOWED;
3498 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3499 return NOT_ALLOWED;
3500 }
3501
3502 return ret;
3503}
3504
3505
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003506static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3507 struct hostapd_hw_modes *mode,
3508 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003509{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003510 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003511 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003512
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003513 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3514 if (bw == BW40MINUS) {
3515 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3516 return NOT_ALLOWED;
3517 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3518 } else if (bw == BW40PLUS) {
3519 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3520 return NOT_ALLOWED;
3521 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3522 } else if (bw == BW80) {
3523 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003524 } else if (bw == BW160) {
3525 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003526 }
3527
3528 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3529 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003530 if (res == NO_IR || res2 == NO_IR)
3531 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003532 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003533}
3534
3535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003537 struct p2p_channels *chan,
3538 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003540 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003541 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003543 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3545 "of all supported channels; assume dualband "
3546 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003547 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 }
3549
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003550 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003552 for (op = 0; global_op_class[op].op_class; op++) {
3553 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003555 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003556
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003557 if (o->p2p == NO_P2P_SUPP)
3558 continue;
3559
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003560 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003561 if (mode == NULL)
3562 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003563 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3564 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003566 enum chan_allowed res;
3567 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3568 if (res == ALLOWED) {
3569 if (reg == NULL) {
3570 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3571 o->op_class);
3572 reg = &chan->reg_class[cla];
3573 cla++;
3574 reg->reg_class = o->op_class;
3575 }
3576 reg->channel[reg->channels] = ch;
3577 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003578 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003579 wpa_s->conf->p2p_add_cli_chan) {
3580 if (cli_reg == NULL) {
3581 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3582 o->op_class);
3583 cli_reg = &cli_chan->reg_class[cli_cla];
3584 cli_cla++;
3585 cli_reg->reg_class = o->op_class;
3586 }
3587 cli_reg->channel[cli_reg->channels] = ch;
3588 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003589 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003590 }
3591 if (reg) {
3592 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3593 reg->channel, reg->channels);
3594 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003595 if (cli_reg) {
3596 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3597 cli_reg->channel, cli_reg->channels);
3598 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599 }
3600
3601 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003602 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 return 0;
3605}
3606
3607
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003608int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3609 struct hostapd_hw_modes *mode, u8 channel)
3610{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003611 int op;
3612 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003613
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003614 for (op = 0; global_op_class[op].op_class; op++) {
3615 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003616 u8 ch;
3617
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003618 if (o->p2p == NO_P2P_SUPP)
3619 continue;
3620
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003621 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3622 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003623 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3624 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003625 continue;
3626 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003627 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003628 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003629 }
3630 }
3631 return 0;
3632}
3633
3634
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003635int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3636 struct hostapd_hw_modes *mode, u8 channel)
3637{
3638 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3639 return 0;
3640
3641 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3642}
3643
3644
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003645int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3646 struct hostapd_hw_modes *mode, u8 channel)
3647{
3648 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3649 return 0;
3650 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3651}
3652
3653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3655 size_t buf_len)
3656{
3657 struct wpa_supplicant *wpa_s = ctx;
3658
3659 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3660 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3661 break;
3662 }
3663 if (wpa_s == NULL)
3664 return -1;
3665
3666 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3667}
3668
3669
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003670struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3671 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003672{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003673 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3674 struct wpa_ssid *s = wpa_s->current_ssid;
3675 if (s == NULL)
3676 continue;
3677 if (s->mode != WPAS_MODE_P2P_GO &&
3678 s->mode != WPAS_MODE_AP &&
3679 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3680 continue;
3681 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003682 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003683 continue;
3684 return wpa_s;
3685 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003686
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003687 return NULL;
3688
3689}
3690
3691
3692struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3693 const u8 *peer_dev_addr)
3694{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003695 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3696 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003697 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003698 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003699 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3700 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003701 }
3702
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003703 return NULL;
3704}
3705
3706
3707static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3708{
3709 struct wpa_supplicant *wpa_s = ctx;
3710
3711 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003712}
3713
3714
Dmitry Shmidt18463232014-01-24 12:29:41 -08003715static int wpas_is_concurrent_session_active(void *ctx)
3716{
3717 struct wpa_supplicant *wpa_s = ctx;
3718 struct wpa_supplicant *ifs;
3719
3720 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3721 if (ifs == wpa_s)
3722 continue;
3723 if (ifs->wpa_state > WPA_ASSOCIATED)
3724 return 1;
3725 }
3726 return 0;
3727}
3728
3729
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003730static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3731{
3732 struct wpa_supplicant *wpa_s = ctx;
3733 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3734}
3735
3736
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003737int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3738 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003739{
3740 struct wpa_interface iface;
3741 struct wpa_supplicant *p2pdev_wpa_s;
3742 char ifname[100];
3743 char force_name[100];
3744 int ret;
3745
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003746 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3747 wpa_s->ifname);
3748 if (os_snprintf_error(sizeof(ifname), ret))
3749 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003750 force_name[0] = '\0';
3751 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3752 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3753 force_name, wpa_s->pending_interface_addr, NULL);
3754 if (ret < 0) {
3755 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3756 return ret;
3757 }
3758 os_strlcpy(wpa_s->pending_interface_name, ifname,
3759 sizeof(wpa_s->pending_interface_name));
3760
3761 os_memset(&iface, 0, sizeof(iface));
3762 iface.p2p_mgmt = 1;
3763 iface.ifname = wpa_s->pending_interface_name;
3764 iface.driver = wpa_s->driver->name;
3765 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003766
3767 /*
3768 * If a P2P Device configuration file was given, use it as the interface
3769 * configuration file (instead of using parent's configuration file.
3770 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003771 if (conf_p2p_dev) {
3772 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003773 iface.ctrl_interface = NULL;
3774 } else {
3775 iface.confname = wpa_s->confname;
3776 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3777 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003778
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003779 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003780 if (!p2pdev_wpa_s) {
3781 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3782 return -1;
3783 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003784
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003785 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003786 wpa_s->pending_interface_name[0] = '\0';
3787 return 0;
3788}
3789
3790
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003791static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3792 const u8 *noa, size_t noa_len)
3793{
3794 struct wpa_supplicant *wpa_s, *intf = ctx;
3795 char hex[100];
3796
3797 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3798 if (wpa_s->waiting_presence_resp)
3799 break;
3800 }
3801 if (!wpa_s) {
3802 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3803 return;
3804 }
3805 wpa_s->waiting_presence_resp = 0;
3806
3807 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3808 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3809 " status=%u noa=%s", MAC2STR(src), status, hex);
3810}
3811
3812
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003813static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3814 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003815 u8 *ret_ssid, size_t *ret_ssid_len,
3816 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003817{
3818 struct wpa_supplicant *wpa_s = ctx;
3819 struct wpa_ssid *s;
3820
3821 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3822 if (s) {
3823 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3824 *ret_ssid_len = s->ssid_len;
3825 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003826
3827 if (s->mode != WPAS_MODE_P2P_GO) {
3828 os_memset(intended_iface_addr, 0, ETH_ALEN);
3829 } else if (wpas_p2p_create_iface(wpa_s)) {
3830 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3831 return 0;
3832
3833 os_memcpy(intended_iface_addr,
3834 wpa_s->pending_interface_addr, ETH_ALEN);
3835 } else {
3836 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3837 ETH_ALEN);
3838 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003839 return 1;
3840 }
3841
3842 return 0;
3843}
3844
3845
3846static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003847 u8 *ssid, size_t *ssid_len, int *group_iface,
3848 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003849{
3850 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003851 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003852 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003853
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003854 /*
3855 * group_iface will be set to 1 only if a dedicated interface for P2P
3856 * role is required. First, we try to reuse an active GO. However,
3857 * if it is not present, we will try to reactivate an existing
3858 * persistent group and set group_iface to 1, so the caller will know
3859 * that the pending interface should be used.
3860 */
3861 *group_iface = 0;
3862
3863 if (freq)
3864 *freq = 0;
3865
3866 go = wpas_p2p_get_go_group(wpa_s);
3867 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003868 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003869 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003870 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003871 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3872 else
3873 return 0;
3874 } else {
3875 s = go->current_ssid;
3876 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3877 if (freq)
3878 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003879 }
3880
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003881 os_memcpy(ssid, s->ssid, s->ssid_len);
3882 *ssid_len = s->ssid_len;
3883
3884 return 1;
3885}
3886
3887
3888static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3889 const u8 *ssid, size_t ssid_len)
3890{
3891 struct wpa_supplicant *wpa_s = ctx;
3892 struct wpa_ssid *s;
3893 int save_config = 0;
3894 size_t i;
3895
3896 /* Start with our first choice of Persistent Groups */
3897 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3898 if (go && ssid && ssid_len &&
3899 s->ssid_len == ssid_len &&
3900 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3901 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3902 break;
3903
3904 /* Remove stale persistent group */
3905 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3906 wpa_config_remove_network(wpa_s->conf, s->id);
3907 save_config = 1;
3908 continue;
3909 }
3910
3911 for (i = 0; i < s->num_p2p_clients; i++) {
3912 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3913 peer, ETH_ALEN) != 0)
3914 continue;
3915
3916 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3917 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3918 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3919 break;
3920 }
3921 s->num_p2p_clients--;
3922 save_config = 1;
3923 }
3924
3925 if (save_config)
3926 p2p_config_write(wpa_s);
3927
3928 /* Return TRUE if valid SSID remains */
3929 return s != NULL;
3930}
3931
3932
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003933static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3934 const u8 *feat_cap, size_t feat_cap_len)
3935{
3936 static const char pref[] = " feature_cap=";
3937 int ret;
3938
3939 buf[0] = '\0';
3940
3941 /*
3942 * We expect a feature capability to contain at least one byte to be
3943 * reported. The string buffer provided by the caller function is
3944 * expected to be big enough to contain all bytes of the attribute for
3945 * known specifications. This function truncates the reported bytes if
3946 * the feature capability data exceeds the string buffer size.
3947 */
3948 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3949 return;
3950
3951 os_memcpy(buf, pref, sizeof(pref));
3952 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3953 buf_len - sizeof(pref) + 1,
3954 feat_cap, feat_cap_len);
3955
3956 if (ret != (2 * (int) feat_cap_len))
3957 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3958}
3959
3960
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003961static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3962 const u8 *adv_mac, const u8 *ses_mac,
3963 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3964 u8 conncap, int passwd_id,
3965 const u8 *persist_ssid,
3966 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003967 int prov_start, const char *session_info,
3968 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003969 unsigned int freq,
3970 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003971{
3972 struct wpa_supplicant *wpa_s = ctx;
3973 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003974 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003975 int save_config = 0;
3976 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003977 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003978
3979 if (!dev)
3980 return;
3981
3982 os_memset(mac, 0, ETH_ALEN);
3983 if (!adv_mac)
3984 adv_mac = mac;
3985 if (!ses_mac)
3986 ses_mac = mac;
3987 if (!grp_mac)
3988 grp_mac = mac;
3989
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003990 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3991 feat_cap, feat_cap_len);
3992
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003993 if (prov_start) {
3994 if (session_info == NULL) {
3995 wpa_msg_global(wpa_s, MSG_INFO,
3996 P2P_EVENT_P2PS_PROVISION_START MACSTR
3997 " adv_id=%x conncap=%x"
3998 " adv_mac=" MACSTR
3999 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004000 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004001 MAC2STR(dev), adv_id, conncap,
4002 MAC2STR(adv_mac),
4003 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004004 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004005 } else {
4006 wpa_msg_global(wpa_s, MSG_INFO,
4007 P2P_EVENT_P2PS_PROVISION_START MACSTR
4008 " adv_id=%x conncap=%x"
4009 " adv_mac=" MACSTR
4010 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004011 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004012 MAC2STR(dev), adv_id, conncap,
4013 MAC2STR(adv_mac),
4014 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004015 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004016 }
4017 return;
4018 }
4019
4020 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4021 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4022
4023 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4024 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4025 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4026
4027 if (persistent_go && !persistent_go->num_p2p_clients) {
4028 /* remove empty persistent GO */
4029 wpa_config_remove_network(wpa_s->conf,
4030 persistent_go->id);
4031 }
4032
4033 wpa_msg_global(wpa_s, MSG_INFO,
4034 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4035 " status=%d"
4036 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004037 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004038 MAC2STR(dev), status,
4039 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004040 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004041 return;
4042 }
4043
4044 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004045 if (persist_ssid && persist_ssid_size)
4046 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4047 persist_ssid_size);
4048
4049 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4050 is_zero_ether_addr(grp_mac)) {
4051 wpa_dbg(wpa_s, MSG_ERROR,
4052 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4053 return;
4054 }
4055
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004056 for (;;) {
4057 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4058 if (!stale)
4059 break;
4060
4061 if (s && s->ssid_len == stale->ssid_len &&
4062 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4063 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4064 break;
4065
4066 /* Remove stale persistent group */
4067 if (stale->mode != WPAS_MODE_P2P_GO ||
4068 stale->num_p2p_clients <= 1) {
4069 wpa_config_remove_network(wpa_s->conf, stale->id);
4070 } else {
4071 size_t i;
4072
4073 for (i = 0; i < stale->num_p2p_clients; i++) {
4074 if (os_memcmp(stale->p2p_client_list +
4075 i * ETH_ALEN,
4076 dev, ETH_ALEN) == 0) {
4077 os_memmove(stale->p2p_client_list +
4078 i * ETH_ALEN,
4079 stale->p2p_client_list +
4080 (i + 1) * ETH_ALEN,
4081 (stale->num_p2p_clients -
4082 i - 1) * ETH_ALEN);
4083 break;
4084 }
4085 }
4086 stale->num_p2p_clients--;
4087 }
4088 save_config = 1;
4089 }
4090
4091 if (save_config)
4092 p2p_config_write(wpa_s);
4093
4094 if (s) {
4095 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4096 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4097
4098 if (persistent_go && s != persistent_go &&
4099 !persistent_go->num_p2p_clients) {
4100 /* remove empty persistent GO */
4101 wpa_config_remove_network(wpa_s->conf,
4102 persistent_go->id);
4103 /* Save config */
4104 }
4105
4106 wpa_msg_global(wpa_s, MSG_INFO,
4107 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4108 " status=%d"
4109 " adv_id=%x adv_mac=" MACSTR
4110 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004111 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004112 MAC2STR(dev), status,
4113 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004114 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004115 return;
4116 }
4117
4118 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004119 /*
4120 * We need to copy the interface name. Simply saving a
4121 * pointer isn't enough, since if we use pending_interface_name
4122 * it will be overwritten when the group is added.
4123 */
4124 char go_ifname[100];
4125
4126 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004127 if (!go_wpa_s) {
4128 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004130
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004131 if (!wpas_p2p_create_iface(wpa_s))
4132 os_memcpy(go_ifname, wpa_s->ifname,
4133 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004134 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004135 os_memcpy(go_ifname,
4136 wpa_s->pending_interface_name,
4137 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004138
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004139 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004140 wpas_p2ps_prov_complete(
4141 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4142 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004143 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004144 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4145 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004146 return;
4147 }
4148
4149 /* If PD Resp complete, start up the GO */
4150 if (response_done && persistent_go) {
4151 wpas_p2p_group_add_persistent(
4152 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004153 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004154 persistent_go->mode ==
4155 WPAS_MODE_P2P_GO ?
4156 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004157 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004158 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004159 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004160 }
4161
4162 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004163 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4164 ETH_ALEN);
4165 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004166 }
4167 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004168 os_memcpy(go_ifname, go_wpa_s->ifname,
4169 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004170
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004171 if (is_zero_ether_addr(grp_mac)) {
4172 wpa_dbg(go_wpa_s, MSG_DEBUG,
4173 "P2P: Setting PIN-1 for ANY");
4174 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4175 "12345670", NULL, 0,
4176 0);
4177 } else {
4178 wpa_dbg(go_wpa_s, MSG_DEBUG,
4179 "P2P: Setting PIN-1 for " MACSTR,
4180 MAC2STR(grp_mac));
4181 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4182 "12345670", NULL, 0,
4183 0);
4184 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004185
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004186 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4187 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004188 }
4189
4190 wpa_msg_global(wpa_s, MSG_INFO,
4191 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4192 " status=%d conncap=%x"
4193 " adv_id=%x adv_mac=" MACSTR
4194 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004195 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004196 MAC2STR(dev), status, conncap,
4197 adv_id, MAC2STR(adv_mac),
4198 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004199 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004200 return;
4201 }
4202
4203 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4204 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4205
4206 if (persistent_go && !persistent_go->num_p2p_clients) {
4207 /* remove empty persistent GO */
4208 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4209 }
4210
4211 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004212 char ssid_hex[32 * 2 + 1];
4213
4214 if (group_ssid)
4215 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4216 group_ssid, group_ssid_len);
4217 else
4218 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004219 wpa_msg_global(wpa_s, MSG_INFO,
4220 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4221 " status=%d conncap=%x"
4222 " adv_id=%x adv_mac=" MACSTR
4223 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004224 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004225 MAC2STR(dev), status, conncap,
4226 adv_id, MAC2STR(adv_mac),
4227 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004228 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4229 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004230 } else {
4231 wpa_msg_global(wpa_s, MSG_INFO,
4232 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4233 " status=%d conncap=%x"
4234 " adv_id=%x adv_mac=" MACSTR
4235 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004236 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004237 MAC2STR(dev), status, conncap,
4238 adv_id, MAC2STR(adv_mac),
4239 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004240 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004241 }
4242}
4243
4244
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004245static int _wpas_p2p_in_progress(void *ctx)
4246{
4247 struct wpa_supplicant *wpa_s = ctx;
4248 return wpas_p2p_in_progress(wpa_s);
4249}
4250
4251
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004252static int wpas_prov_disc_resp_cb(void *ctx)
4253{
4254 struct wpa_supplicant *wpa_s = ctx;
4255 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004256 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004257
4258 if (!wpa_s->global->pending_p2ps_group)
4259 return 0;
4260
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004261 freq = wpa_s->global->pending_p2ps_group_freq;
4262 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004263 wpa_s->global->pending_p2ps_group = 0;
4264
4265 if (wpas_p2p_get_go_group(wpa_s))
4266 return 0;
4267 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4268
4269 if (persistent_go) {
4270 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004271 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004272 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004273 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004274 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004275 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004276 }
4277
4278 return 1;
4279}
4280
4281
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004282static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4283 unsigned int *len,
4284 unsigned int *freq_list)
4285{
4286 struct wpa_supplicant *wpa_s = ctx;
4287
4288 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4289 WPA_IF_P2P_CLIENT, len, freq_list);
4290}
4291
4292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004293/**
4294 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4295 * @global: Pointer to global data from wpa_supplicant_init()
4296 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4297 * Returns: 0 on success, -1 on failure
4298 */
4299int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4300{
4301 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 int i;
4303
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004304 if (wpa_s->conf->p2p_disabled)
4305 return 0;
4306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4308 return 0;
4309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 if (global->p2p)
4311 return 0;
4312
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004315 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 p2p.p2p_scan = wpas_p2p_scan;
4317 p2p.send_action = wpas_send_action;
4318 p2p.send_action_done = wpas_send_action_done;
4319 p2p.go_neg_completed = wpas_go_neg_completed;
4320 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4321 p2p.dev_found = wpas_dev_found;
4322 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004323 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004324 p2p.start_listen = wpas_start_listen;
4325 p2p.stop_listen = wpas_stop_listen;
4326 p2p.send_probe_resp = wpas_send_probe_resp;
4327 p2p.sd_request = wpas_sd_request;
4328 p2p.sd_response = wpas_sd_response;
4329 p2p.prov_disc_req = wpas_prov_disc_req;
4330 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004331 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 p2p.invitation_process = wpas_invitation_process;
4333 p2p.invitation_received = wpas_invitation_received;
4334 p2p.invitation_result = wpas_invitation_result;
4335 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004336 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004337 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004338 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004339 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004340 p2p.get_persistent_group = wpas_get_persistent_group;
4341 p2p.get_go_info = wpas_get_go_info;
4342 p2p.remove_stale_groups = wpas_remove_stale_groups;
4343 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4344 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4345 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004346 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347
4348 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004349 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350 p2p.dev_name = wpa_s->conf->device_name;
4351 p2p.manufacturer = wpa_s->conf->manufacturer;
4352 p2p.model_name = wpa_s->conf->model_name;
4353 p2p.model_number = wpa_s->conf->model_number;
4354 p2p.serial_number = wpa_s->conf->serial_number;
4355 if (wpa_s->wps) {
4356 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4357 p2p.config_methods = wpa_s->wps->config_methods;
4358 }
4359
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004360 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4361 wpa_printf(MSG_ERROR,
4362 "P2P: Failed to configure supported channel list");
4363 return -1;
4364 }
4365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 if (wpa_s->conf->p2p_listen_reg_class &&
4367 wpa_s->conf->p2p_listen_channel) {
4368 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4369 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004370 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004372 /*
4373 * Pick one of the social channels randomly as the listen
4374 * channel.
4375 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004376 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4377 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004378 wpa_printf(MSG_INFO,
4379 "P2P: No social channels supported by the driver - do not enable P2P");
4380 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004381 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004382 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004384 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4385 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386
4387 if (wpa_s->conf->p2p_oper_reg_class &&
4388 wpa_s->conf->p2p_oper_channel) {
4389 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4390 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4391 p2p.cfg_op_channel = 1;
4392 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4393 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4394
4395 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004397 * Use random operation channel from 2.4 GHz band social
4398 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4399 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004401 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4402 &p2p.op_channel) != 0) {
4403 wpa_printf(MSG_ERROR,
4404 "P2P: Failed to select random social channel as operation channel");
4405 return -1;
4406 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 p2p.cfg_op_channel = 0;
4408 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4409 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4410 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004411
4412 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4413 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4414 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4415 }
4416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4418 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4419 p2p.country[2] = 0x04;
4420 } else
4421 os_memcpy(p2p.country, "XX\x04", 3);
4422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4424 WPS_DEV_TYPE_LEN);
4425
4426 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4427 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4428 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4429
4430 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4431 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4432
4433 p2p.max_peers = 100;
4434
4435 if (wpa_s->conf->p2p_ssid_postfix) {
4436 p2p.ssid_postfix_len =
4437 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4438 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4439 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4440 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4441 p2p.ssid_postfix_len);
4442 }
4443
4444 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4445
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004446 p2p.max_listen = wpa_s->max_remain_on_chan;
4447
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004448 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4449 wpa_s->conf->p2p_passphrase_len <= 63)
4450 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4451 else
4452 p2p.passphrase_len = 8;
4453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 global->p2p = p2p_init(&p2p);
4455 if (global->p2p == NULL)
4456 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004457 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458
4459 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4460 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4461 continue;
4462 p2p_add_wps_vendor_extension(
4463 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4464 }
4465
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004466 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468 return 0;
4469}
4470
4471
4472/**
4473 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4474 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4475 *
4476 * This function deinitialize per-interface P2P data.
4477 */
4478void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4479{
4480 if (wpa_s->driver && wpa_s->drv_priv)
4481 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004482
4483 if (wpa_s->go_params) {
4484 /* Clear any stored provisioning info */
4485 p2p_clear_provisioning_info(
4486 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004487 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004488 }
4489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 os_free(wpa_s->go_params);
4491 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004492 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4494 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4495 wpa_s->p2p_long_listen = 0;
4496 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4497 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4498 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004499 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004500 wpas_p2p_listen_work_done(wpa_s);
4501 if (wpa_s->p2p_send_action_work) {
4502 os_free(wpa_s->p2p_send_action_work->ctx);
4503 radio_work_done(wpa_s->p2p_send_action_work);
4504 wpa_s->p2p_send_action_work = NULL;
4505 }
4506 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004507
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004508 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4509 wpa_s->p2p_oob_dev_pw = NULL;
4510
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004511 os_free(wpa_s->p2p_group_common_freqs);
4512 wpa_s->p2p_group_common_freqs = NULL;
4513 wpa_s->p2p_group_common_freqs_num = 0;
4514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 /* TODO: remove group interface from the driver if this wpa_s instance
4516 * is on top of a P2P group interface */
4517}
4518
4519
4520/**
4521 * wpas_p2p_deinit_global - Deinitialize global P2P module
4522 * @global: Pointer to global data from wpa_supplicant_init()
4523 *
4524 * This function deinitializes the global (per device) P2P module.
4525 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004526static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004527{
4528 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004530 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004531
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004532 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533
4534 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4536 wpa_s = wpa_s->next;
4537 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538 tmp = global->ifaces;
4539 while (tmp &&
4540 (tmp == wpa_s ||
4541 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4542 tmp = tmp->next;
4543 }
4544 if (tmp == NULL)
4545 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004546 /* Disconnect from the P2P group and deinit the interface */
4547 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 }
4549
4550 /*
4551 * Deinit GO data on any possibly remaining interface (if main
4552 * interface is used as GO).
4553 */
4554 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4555 if (wpa_s->ap_iface)
4556 wpas_p2p_group_deinit(wpa_s);
4557 }
4558
4559 p2p_deinit(global->p2p);
4560 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004561 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562}
4563
4564
4565static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4566{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004567 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004568 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 if (wpa_s->drv_flags &
4570 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4571 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4572 return 1; /* P2P group requires a new interface in every case
4573 */
4574 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4575 return 0; /* driver does not support concurrent operations */
4576 if (wpa_s->global->ifaces->next)
4577 return 1; /* more that one interface already in use */
4578 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4579 return 1; /* this interface is already in use */
4580 return 0;
4581}
4582
4583
4584static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4585 const u8 *peer_addr,
4586 enum p2p_wps_method wps_method,
4587 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004588 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004589 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004590{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004591 if (persistent_group && wpa_s->conf->persistent_reconnect)
4592 persistent_group = 2;
4593
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004594 /*
4595 * Increase GO config timeout if HT40 is used since it takes some time
4596 * to scan channels for coex purposes before the BSS can be started.
4597 */
4598 p2p_set_config_timeout(wpa_s->global->p2p,
4599 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4602 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004603 persistent_group, ssid ? ssid->ssid : NULL,
4604 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004605 wpa_s->p2p_pd_before_go_neg, pref_freq,
4606 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4607 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608}
4609
4610
4611static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4612 const u8 *peer_addr,
4613 enum p2p_wps_method wps_method,
4614 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004615 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004616 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004618 if (persistent_group && wpa_s->conf->persistent_reconnect)
4619 persistent_group = 2;
4620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4622 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004623 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004624 ssid ? ssid->ssid_len : 0, pref_freq,
4625 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4626 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004627}
4628
4629
4630static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4631{
4632 wpa_s->p2p_join_scan_count++;
4633 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4634 wpa_s->p2p_join_scan_count);
4635 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4636 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4637 " for join operationg - stop join attempt",
4638 MAC2STR(wpa_s->pending_join_iface_addr));
4639 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004640 if (wpa_s->p2p_auto_pd) {
4641 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004642 wpa_msg_global(wpa_s, MSG_INFO,
4643 P2P_EVENT_PROV_DISC_FAILURE
4644 " p2p_dev_addr=" MACSTR " status=N/A",
4645 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004646 return;
4647 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004648 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004649 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004650 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 }
4652}
4653
4654
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4656{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004657 int res;
4658 unsigned int num, i;
4659 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004660
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004661 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4662 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004663 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004664 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004666 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4667 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004668 if (!freqs)
4669 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004670
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004671 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4672 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004673
4674 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004675 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004676 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4677 freq);
4678 res = 0;
4679 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004680 }
4681 }
4682
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004683 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004684 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004685
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004686exit_free:
4687 os_free(freqs);
4688 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004689}
4690
4691
4692static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4693 const u8 *peer_dev_addr)
4694{
4695 struct wpa_bss *bss;
4696 int updated;
4697
4698 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4699 if (bss == NULL)
4700 return -1;
4701 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4702 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4703 "last scan");
4704 return 0;
4705 }
4706
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004707 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4708 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004709 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4710 "%ld.%06ld (%supdated in last scan)",
4711 bss->last_update.sec, bss->last_update.usec,
4712 updated ? "": "not ");
4713
4714 return updated;
4715}
4716
4717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4719 struct wpa_scan_results *scan_res)
4720{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004721 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 int freq;
4723 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4726
4727 if (wpa_s->global->p2p_disabled)
4728 return;
4729
Dmitry Shmidt04949592012-07-19 12:16:46 -07004730 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4731 scan_res ? (int) scan_res->num : -1,
4732 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004733
4734 if (scan_res)
4735 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4736
Dmitry Shmidt04949592012-07-19 12:16:46 -07004737 if (wpa_s->p2p_auto_pd) {
4738 int join = wpas_p2p_peer_go(wpa_s,
4739 wpa_s->pending_join_dev_addr);
4740 if (join == 0 &&
4741 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4742 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004743 bss = wpa_bss_get_bssid_latest(
4744 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004745 if (bss) {
4746 freq = bss->freq;
4747 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4748 "the peer " MACSTR " at %d MHz",
4749 wpa_s->auto_pd_scan_retry,
4750 MAC2STR(wpa_s->
4751 pending_join_dev_addr),
4752 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004753 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004754 return;
4755 }
4756 }
4757
4758 if (join < 0)
4759 join = 0;
4760
4761 wpa_s->p2p_auto_pd = 0;
4762 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4763 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4764 MAC2STR(wpa_s->pending_join_dev_addr), join);
4765 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004766 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004767 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004768 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004769 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004770 wpa_msg_global(wpa_s, MSG_INFO,
4771 P2P_EVENT_PROV_DISC_FAILURE
4772 " p2p_dev_addr=" MACSTR " status=N/A",
4773 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004774 }
4775 return;
4776 }
4777
4778 if (wpa_s->p2p_auto_join) {
4779 int join = wpas_p2p_peer_go(wpa_s,
4780 wpa_s->pending_join_dev_addr);
4781 if (join < 0) {
4782 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4783 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004784 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004785 P2P_EVENT_FALLBACK_TO_GO_NEG
4786 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004787 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4788 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4789 wpa_s->p2p_persistent_group, 0, 0, 0,
4790 wpa_s->p2p_go_intent,
4791 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004792 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004793 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004794 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004795 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004796 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004797 wpa_s->p2p_go_max_oper_chwidth,
4798 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004799 return;
4800 }
4801
4802 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4803 "try to join the group", join ? "" :
4804 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004805 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004806 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004807 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004808 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004809 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004810 }
4811
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004812 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4813 wpa_s->pending_join_iface_addr);
4814 if (freq < 0 &&
4815 p2p_get_interface_addr(wpa_s->global->p2p,
4816 wpa_s->pending_join_dev_addr,
4817 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004818 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4819 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4821 "address for join from " MACSTR " to " MACSTR
4822 " based on newly discovered P2P peer entry",
4823 MAC2STR(wpa_s->pending_join_iface_addr),
4824 MAC2STR(iface_addr));
4825 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4826 ETH_ALEN);
4827
4828 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4829 wpa_s->pending_join_iface_addr);
4830 }
4831 if (freq >= 0) {
4832 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4833 "from P2P peer table: %d MHz", freq);
4834 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004835 if (wpa_s->p2p_join_ssid_len) {
4836 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4837 MACSTR " and SSID %s",
4838 MAC2STR(wpa_s->pending_join_iface_addr),
4839 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4840 wpa_s->p2p_join_ssid_len));
4841 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4842 wpa_s->p2p_join_ssid,
4843 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004844 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004845 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4846 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4847 bss = wpa_bss_get_bssid_latest(wpa_s,
4848 wpa_s->pending_join_iface_addr);
4849 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004851 u8 dev_addr[ETH_ALEN];
4852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 freq = bss->freq;
4854 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004855 "from BSS table: %d MHz (SSID %s)", freq,
4856 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004857 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4858 dev_addr) == 0 &&
4859 os_memcmp(wpa_s->pending_join_dev_addr,
4860 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4861 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4862 ETH_ALEN) != 0) {
4863 wpa_printf(MSG_DEBUG,
4864 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4865 MAC2STR(dev_addr),
4866 MAC2STR(wpa_s->pending_join_dev_addr));
4867 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4868 ETH_ALEN);
4869 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004870 }
4871 if (freq > 0) {
4872 u16 method;
4873
Dmitry Shmidt04949592012-07-19 12:16:46 -07004874 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004875 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004876 P2P_EVENT_GROUP_FORMATION_FAILURE
4877 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004878 wpas_notify_p2p_group_formation_failure(
4879 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004880 return;
4881 }
4882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4884 "prior to joining an existing group (GO " MACSTR
4885 " freq=%u MHz)",
4886 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4887 wpa_s->pending_pd_before_join = 1;
4888
4889 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004890 case WPS_PIN_DISPLAY:
4891 method = WPS_CONFIG_KEYPAD;
4892 break;
4893 case WPS_PIN_KEYPAD:
4894 method = WPS_CONFIG_DISPLAY;
4895 break;
4896 case WPS_PBC:
4897 method = WPS_CONFIG_PUSHBUTTON;
4898 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004899 case WPS_P2PS:
4900 method = WPS_CONFIG_P2PS;
4901 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004902 default:
4903 method = 0;
4904 break;
4905 }
4906
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004907 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4908 wpa_s->pending_join_dev_addr) ==
4909 method)) {
4910 /*
4911 * We have already performed provision discovery for
4912 * joining the group. Proceed directly to join
4913 * operation without duplicated provision discovery. */
4914 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4915 "with " MACSTR " already done - proceed to "
4916 "join",
4917 MAC2STR(wpa_s->pending_join_dev_addr));
4918 wpa_s->pending_pd_before_join = 0;
4919 goto start;
4920 }
4921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004923 wpa_s->pending_join_dev_addr,
4924 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004925 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4927 "Discovery Request before joining an "
4928 "existing group");
4929 wpa_s->pending_pd_before_join = 0;
4930 goto start;
4931 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932 return;
4933 }
4934
4935 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4936 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4937 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4938 wpas_p2p_check_join_scan_limit(wpa_s);
4939 return;
4940
4941start:
4942 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004943 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
4944 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945}
4946
4947
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004948static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4949 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004950{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 int ret;
4952 struct wpa_driver_scan_params params;
4953 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004954 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004955 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004956 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004957
4958 os_memset(&params, 0, sizeof(params));
4959
4960 /* P2P Wildcard SSID */
4961 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004962 if (ssid && ssid_len) {
4963 params.ssids[0].ssid = ssid;
4964 params.ssids[0].ssid_len = ssid_len;
4965 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4966 wpa_s->p2p_join_ssid_len = ssid_len;
4967 } else {
4968 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4969 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4970 wpa_s->p2p_join_ssid_len = 0;
4971 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004972
4973 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004974 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4975 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4976 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977 if (wps_ie == NULL) {
4978 wpas_p2p_scan_res_join(wpa_s, NULL);
4979 return;
4980 }
4981
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004982 if (!freq) {
4983 int oper_freq;
4984 /*
4985 * If freq is not provided, check the operating freq of the GO
4986 * and use a single channel scan on if possible.
4987 */
4988 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4989 wpa_s->pending_join_iface_addr);
4990 if (oper_freq > 0)
4991 freq = oper_freq;
4992 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004993 if (freq > 0) {
4994 freqs[0] = freq;
4995 params.freqs = freqs;
4996 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004998 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4999 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5000 if (ies == NULL) {
5001 wpabuf_free(wps_ie);
5002 wpas_p2p_scan_res_join(wpa_s, NULL);
5003 return;
5004 }
5005 wpabuf_put_buf(ies, wps_ie);
5006 wpabuf_free(wps_ie);
5007
5008 bands = wpas_get_bands(wpa_s, freqs);
5009 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5010
5011 params.p2p_probe = 1;
5012 params.extra_ies = wpabuf_head(ies);
5013 params.extra_ies_len = wpabuf_len(ies);
5014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015 /*
5016 * Run a scan to update BSS table and start Provision Discovery once
5017 * the new scan results become available.
5018 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005019 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005020 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005021 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005022 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005023 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025
5026 wpabuf_free(ies);
5027
5028 if (ret) {
5029 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5030 "try again later");
5031 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5032 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5033 wpas_p2p_check_join_scan_limit(wpa_s);
5034 }
5035}
5036
5037
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5039{
5040 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005041 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005042}
5043
5044
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005046 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005047 int auto_join, int op_freq,
5048 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049{
5050 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005051 MACSTR " dev " MACSTR " op_freq=%d)%s",
5052 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005053 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005054 if (ssid && ssid_len) {
5055 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5056 wpa_ssid_txt(ssid, ssid_len));
5057 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005058
Dmitry Shmidt04949592012-07-19 12:16:46 -07005059 wpa_s->p2p_auto_pd = 0;
5060 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5062 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5063 wpa_s->pending_join_wps_method = wps_method;
5064
5065 /* Make sure we are not running find during connection establishment */
5066 wpas_p2p_stop_find(wpa_s);
5067
5068 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005069 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 return 0;
5071}
5072
5073
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005074static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5075 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005076{
5077 struct wpa_supplicant *group;
5078 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005079 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080
5081 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5082 if (group == NULL)
5083 return -1;
5084 if (group != wpa_s) {
5085 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5086 sizeof(group->p2p_pin));
5087 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005088 } else {
5089 /*
5090 * Need to mark the current interface for p2p_group_formation
5091 * when a separate group interface is not used. This is needed
5092 * to allow p2p_cancel stop a pending p2p_connect-join.
5093 * wpas_p2p_init_group_interface() addresses this for the case
5094 * where a separate group interface is used.
5095 */
5096 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097 }
5098
5099 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005100 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101
5102 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005103 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5105 ETH_ALEN);
5106 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005107 if (freq && ssid && ssid_len) {
5108 res.freq = freq;
5109 res.ssid_len = ssid_len;
5110 os_memcpy(res.ssid, ssid, ssid_len);
5111 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005112 if (ssid && ssid_len) {
5113 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5114 ssid, ssid_len);
5115 } else {
5116 bss = wpa_bss_get_bssid_latest(
5117 wpa_s, wpa_s->pending_join_iface_addr);
5118 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005119 if (bss) {
5120 res.freq = bss->freq;
5121 res.ssid_len = bss->ssid_len;
5122 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5123 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5124 bss->freq,
5125 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005126 } else if (ssid && ssid_len) {
5127 res.ssid_len = ssid_len;
5128 os_memcpy(res.ssid, ssid, ssid_len);
5129 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5130 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005131 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005132 }
5133
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005134 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5135 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5136 "starting client");
5137 wpa_drv_cancel_remain_on_channel(wpa_s);
5138 wpa_s->off_channel_freq = 0;
5139 wpa_s->roc_waiting_drv_freq = 0;
5140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005141 wpas_start_wps_enrollee(group, &res);
5142
5143 /*
5144 * Allow a longer timeout for join-a-running-group than normal 15
5145 * second group formation timeout since the GO may not have authorized
5146 * our connection yet.
5147 */
5148 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5149 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5150 wpa_s, NULL);
5151
5152 return 0;
5153}
5154
5155
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005156static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005157 int *force_freq, int *pref_freq, int go,
5158 unsigned int *pref_freq_list,
5159 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005160{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005161 struct wpa_used_freq_data *freqs;
5162 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005163 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5164
5165 max_pref_freq = *num_pref_freq;
5166 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005168 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5169 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005170 if (!freqs)
5171 return -1;
5172
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005173 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5174 wpa_s->num_multichan_concurrent);
5175
5176 /*
5177 * It is possible that the total number of used frequencies is bigger
5178 * than the number of frequencies used for P2P, so get the system wide
5179 * number of unused frequencies.
5180 */
5181 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5182
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005183 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005184 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5185 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005186
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005187 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005188 int ret;
5189 if (go)
5190 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5191 else
5192 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5193 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005194 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5195 ieee80211_is_dfs(freq)) {
5196 /*
5197 * If freq is a DFS channel and DFS is offloaded
5198 * to the driver, allow P2P GO to use it.
5199 */
5200 wpa_printf(MSG_DEBUG,
5201 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5202 freq);
5203 } else {
5204 wpa_printf(MSG_DEBUG,
5205 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5206 freq);
5207 res = -3;
5208 goto exit_free;
5209 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005210 }
5211
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005212 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005213 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005214 freq_in_use = 1;
5215 }
5216
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005217 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005218 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5219 freq);
5220 res = -2;
5221 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005222 }
5223 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5224 "requested channel (%u MHz)", freq);
5225 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005226 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005227 }
5228
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005229 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005230
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005231 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5232 enum wpa_driver_if_type iface_type;
5233
5234 if (go)
5235 iface_type = WPA_IF_P2P_GO;
5236 else
5237 iface_type = WPA_IF_P2P_CLIENT;
5238
5239 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5240 best_freq, go);
5241
5242 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5243 &max_pref_freq,
5244 pref_freq_list);
5245 if (!res && max_pref_freq > 0) {
5246 *num_pref_freq = max_pref_freq;
5247 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005248 while (i < *num_pref_freq &&
5249 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005250 pref_freq_list[i]) ||
5251 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005252 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005253 wpa_printf(MSG_DEBUG,
5254 "P2P: preferred_freq_list[%d]=%d is disallowed",
5255 i, pref_freq_list[i]);
5256 i++;
5257 }
5258 if (i != *num_pref_freq) {
5259 best_freq = pref_freq_list[i];
5260 wpa_printf(MSG_DEBUG,
5261 "P2P: Using preferred_freq_list[%d]=%d",
5262 i, best_freq);
5263 } else {
5264 wpa_printf(MSG_DEBUG,
5265 "P2P: All driver preferred frequencies are disallowed for P2P use");
5266 *num_pref_freq = 0;
5267 }
5268 } else {
5269 wpa_printf(MSG_DEBUG,
5270 "P2P: No preferred frequency list available");
5271 }
5272 }
5273
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005274 /* We have a candidate frequency to use */
5275 if (best_freq > 0) {
5276 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005277 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005278 best_freq);
5279 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005280 } else {
5281 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 -07005282 best_freq);
5283 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005284 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005285 } else if (num_unused > 0) {
5286 wpa_printf(MSG_DEBUG,
5287 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5288 *force_freq = 0;
5289 } else {
5290 wpa_printf(MSG_DEBUG,
5291 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5292 res = -2;
5293 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005294 }
5295
5296exit_ok:
5297 res = 0;
5298exit_free:
5299 os_free(freqs);
5300 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005301}
5302
5303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304/**
5305 * wpas_p2p_connect - Request P2P Group Formation to be started
5306 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5307 * @peer_addr: Address of the peer P2P Device
5308 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5309 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005310 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005311 * @join: Whether to join an existing group (as a client) instead of starting
5312 * Group Owner negotiation; @peer_addr is BSSID in that case
5313 * @auth: Whether to only authorize the connection instead of doing that and
5314 * initiating Group Owner negotiation
5315 * @go_intent: GO Intent or -1 to use default
5316 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005317 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005318 * @persistent_id: Persistent group credentials to use for forcing GO
5319 * parameters or -1 to generate new values (SSID/passphrase)
5320 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5321 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005322 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005323 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005324 * @vht_chwidth: Channel width supported by GO operating with VHT support
5325 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005326 * @group_ssid: Specific Group SSID for join or %NULL if not set
5327 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5329 * failure, -2 on failure due to channel not currently available,
5330 * -3 if forced channel is not supported
5331 */
5332int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5333 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005335 int go_intent, int freq, unsigned int vht_center_freq2,
5336 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005337 unsigned int vht_chwidth, const u8 *group_ssid,
5338 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005340 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005341 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005343 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005345 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346
5347 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5348 return -1;
5349
Dmitry Shmidt04949592012-07-19 12:16:46 -07005350 if (persistent_id >= 0) {
5351 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5352 if (ssid == NULL || ssid->disabled != 2 ||
5353 ssid->mode != WPAS_MODE_P2P_GO)
5354 return -1;
5355 }
5356
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005357 os_free(wpa_s->global->add_psk);
5358 wpa_s->global->add_psk = NULL;
5359
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005360 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005361 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005362 wpa_s->global->pending_p2ps_group_freq = 0;
5363 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 if (go_intent < 0)
5366 go_intent = wpa_s->conf->p2p_go_intent;
5367
5368 if (!auth)
5369 wpa_s->p2p_long_listen = 0;
5370
5371 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372 wpa_s->p2p_persistent_group = !!persistent_group;
5373 wpa_s->p2p_persistent_id = persistent_id;
5374 wpa_s->p2p_go_intent = go_intent;
5375 wpa_s->p2p_connect_freq = freq;
5376 wpa_s->p2p_fallback_to_go_neg = 0;
5377 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005378 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005379 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005380 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5381 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382
5383 if (pin)
5384 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5385 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005386 if (wps_generate_pin((unsigned int *) &ret) < 0)
5387 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005388 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5389 "%08d", ret);
5390 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5391 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5393 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005394 } else if (wps_method == WPS_P2PS) {
5395 /* Force the P2Ps default PIN to be used */
5396 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397 } else
5398 wpa_s->p2p_pin[0] = '\0';
5399
Dmitry Shmidt04949592012-07-19 12:16:46 -07005400 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5402 if (auth) {
5403 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5404 "connect a running group from " MACSTR,
5405 MAC2STR(peer_addr));
5406 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5407 return ret;
5408 }
5409 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5410 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5411 iface_addr) < 0) {
5412 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5413 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5414 dev_addr);
5415 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005416 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005417 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005418 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5419 "%ld.%06ld",
5420 wpa_s->p2p_auto_started.sec,
5421 wpa_s->p2p_auto_started.usec);
5422 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005423 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005424 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005425 auto_join, freq,
5426 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427 return -1;
5428 return ret;
5429 }
5430
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005431 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005432 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005433 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005434 if (res)
5435 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005436 wpas_p2p_set_own_freq_preference(wpa_s,
5437 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005439 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005441 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005443 if (wpa_s->create_p2p_iface) {
5444 /* Prepare to add a new interface for the group */
5445 iftype = WPA_IF_P2P_GROUP;
5446 if (go_intent == 15)
5447 iftype = WPA_IF_P2P_GO;
5448 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5449 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5450 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005452 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005453
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005454 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005455 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005456 if (wpa_s->p2p_mgmt)
5457 if_addr = wpa_s->parent->own_addr;
5458 else
5459 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005460 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462
5463 if (auth) {
5464 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005466 force_freq, persistent_group, ssid,
5467 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005468 return -1;
5469 return ret;
5470 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005471
5472 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5473 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005474 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005475 if (wpa_s->create_p2p_iface)
5476 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477 return -1;
5478 }
5479 return ret;
5480}
5481
5482
5483/**
5484 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5485 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5486 * @freq: Frequency of the channel in MHz
5487 * @duration: Duration of the stay on the channel in milliseconds
5488 *
5489 * This callback is called when the driver indicates that it has started the
5490 * requested remain-on-channel duration.
5491 */
5492void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5493 unsigned int freq, unsigned int duration)
5494{
5495 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5496 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005497 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)",
5498 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5499 wpa_s->roc_waiting_drv_freq, freq, duration);
5500 if (wpa_s->off_channel_freq &&
5501 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5503 wpa_s->pending_listen_duration);
5504 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005505 } else {
5506 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5507 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5508 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 }
5510}
5511
5512
Jithu Jance57cea1a2014-08-20 10:22:04 -07005513int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514{
5515 /* Limit maximum Listen state time based on driver limitation. */
5516 if (timeout > wpa_s->max_remain_on_chan)
5517 timeout = wpa_s->max_remain_on_chan;
5518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 return p2p_listen(wpa_s->global->p2p, timeout);
5520}
5521
5522
5523/**
5524 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5525 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5526 * @freq: Frequency of the channel in MHz
5527 *
5528 * This callback is called when the driver indicates that a remain-on-channel
5529 * operation has been completed, i.e., the duration on the requested channel
5530 * has timed out.
5531 */
5532void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5533 unsigned int freq)
5534{
5535 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5536 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005537 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005538 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5540 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005541 if (wpa_s->p2p_long_listen > 0)
5542 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5544 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005545 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 if (wpa_s->p2p_long_listen > 0) {
5548 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5549 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005550 } else {
5551 /*
5552 * When listen duration is over, stop listen & update p2p_state
5553 * to IDLE.
5554 */
5555 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556 }
5557}
5558
5559
5560/**
5561 * wpas_p2p_group_remove - Remove a P2P group
5562 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5563 * @ifname: Network interface name of the group interface or "*" to remove all
5564 * groups
5565 * Returns: 0 on success, -1 on failure
5566 *
5567 * This function is used to remove a P2P group. This can be used to disconnect
5568 * from a group in which the local end is a P2P Client or to end a P2P Group in
5569 * case the local end is the Group Owner. If a virtual network interface was
5570 * created for this group, that interface will be removed. Otherwise, only the
5571 * configured P2P group network will be removed from the interface.
5572 */
5573int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5574{
5575 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005576 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577
5578 if (os_strcmp(ifname, "*") == 0) {
5579 struct wpa_supplicant *prev;
5580 wpa_s = global->ifaces;
5581 while (wpa_s) {
5582 prev = wpa_s;
5583 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005584 if (prev->p2p_group_interface !=
5585 NOT_P2P_GROUP_INTERFACE ||
5586 (prev->current_ssid &&
5587 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005588 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 }
5590 return 0;
5591 }
5592
5593 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5594 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5595 break;
5596 }
5597
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005598 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005599}
5600
5601
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005602static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5603{
5604 unsigned int r;
5605
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005606 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5607 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5608 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5609 int res;
5610
5611 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5612 &size, pref_freq_list);
5613 if (!res && size > 0) {
5614 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005615 while (i < size &&
5616 wpas_p2p_disallowed_freq(wpa_s->global,
5617 pref_freq_list[i])) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005618 wpa_printf(MSG_DEBUG,
5619 "P2P: preferred_freq_list[%d]=%d is disallowed",
5620 i, pref_freq_list[i]);
5621 i++;
5622 }
5623 if (i != size) {
5624 freq = pref_freq_list[i];
5625 wpa_printf(MSG_DEBUG,
5626 "P2P: Using preferred_freq_list[%d]=%d",
5627 i, freq);
5628 } else {
5629 wpa_printf(MSG_DEBUG,
5630 "P2P: All driver preferred frequencies are disallowed for P2P use");
5631 }
5632 } else {
5633 wpa_printf(MSG_DEBUG,
5634 "P2P: No preferred frequency list available");
5635 }
5636 }
5637
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005638 if (freq == 2) {
5639 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5640 "band");
5641 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005642 p2p_supported_freq_go(wpa_s->global->p2p,
5643 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005644 freq = wpa_s->best_24_freq;
5645 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5646 "channel: %d MHz", freq);
5647 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005648 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5649 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005650 freq = 2412 + (r % 3) * 25;
5651 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5652 "channel: %d MHz", freq);
5653 }
5654 }
5655
5656 if (freq == 5) {
5657 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5658 "band");
5659 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005660 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005661 wpa_s->best_5_freq)) {
5662 freq = wpa_s->best_5_freq;
5663 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5664 "channel: %d MHz", freq);
5665 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005666 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5667 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005668 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005669 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005670 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5671 "5 GHz channel for P2P group");
5672 return -1;
5673 }
5674 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5675 "channel: %d MHz", freq);
5676 }
5677 }
5678
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005679 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005680 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5681 ieee80211_is_dfs(freq)) {
5682 /*
5683 * If freq is a DFS channel and DFS is offloaded to the
5684 * driver, allow P2P GO to use it.
5685 */
5686 wpa_printf(MSG_DEBUG, "P2P: "
5687 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5688 __func__, freq);
5689 return freq;
5690 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005691 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5692 "(%u MHz) is not supported for P2P uses",
5693 freq);
5694 return -1;
5695 }
5696
5697 return freq;
5698}
5699
5700
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005701static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5702 const struct p2p_channels *channels,
5703 int freq)
5704{
5705 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5706 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5707 freq_included(wpa_s, channels, freq))
5708 return 1;
5709 return 0;
5710}
5711
5712
5713static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5714 struct p2p_go_neg_results *params,
5715 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005716{
5717 unsigned int i, r;
5718
5719 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005720 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005721 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005722
5723 for (i = 0; i < 3; i++) {
5724 params->freq = 2412 + ((r + i) % 3) * 25;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005725 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005726 goto out;
5727 }
5728
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005729 /* try all other channels in operating class 81 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005730 for (i = 0; i < 11; i++) {
5731 params->freq = 2412 + i * 5;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005732
5733 /* skip social channels; covered in the previous loop */
5734 if (params->freq == 2412 ||
5735 params->freq == 2437 ||
5736 params->freq == 2462)
5737 continue;
5738
5739 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005740 goto out;
5741 }
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 Shmidtd80a4012015-11-05 16:35:40 -08005777 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005778 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005779 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005780out:
5781 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5782 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005783}
5784
5785
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005786static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5787 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005788 int freq, int vht_center_freq2, int ht40,
5789 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005790 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005792 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005793 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005794 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005795 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005796 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005797
5798 os_memset(params, 0, sizeof(*params));
5799 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005800 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005801 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005802 params->max_oper_chwidth = max_oper_chwidth;
5803 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005805 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5806 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005807 if (!freqs)
5808 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005810 num = get_shared_radio_freqs_data(wpa_s, freqs,
5811 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005813 if (wpa_s->current_ssid &&
5814 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5815 wpa_s->wpa_state == WPA_COMPLETED) {
5816 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5817 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005818
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005819 /*
5820 * If the frequency selection is done for an active P2P GO that
5821 * is not sharing a frequency, allow to select a new frequency
5822 * even if there are no unused frequencies as we are about to
5823 * move the P2P GO so its frequency can be re-used.
5824 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005825 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005826 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5827 freqs[i].flags == 0) {
5828 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005829 break;
5830 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005831 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005832 }
5833
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005834 /* try using the forced freq */
5835 if (freq) {
5836 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5837 wpa_printf(MSG_DEBUG,
5838 "P2P: Forced GO freq %d MHz not accepted",
5839 freq);
5840 goto fail;
5841 }
5842
5843 for (i = 0; i < num; i++) {
5844 if (freqs[i].freq == freq) {
5845 wpa_printf(MSG_DEBUG,
5846 "P2P: forced freq (%d MHz) is also shared",
5847 freq);
5848 params->freq = freq;
5849 goto success;
5850 }
5851 }
5852
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005853 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005854 wpa_printf(MSG_DEBUG,
5855 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5856 freq);
5857 goto fail;
5858 }
5859
5860 wpa_printf(MSG_DEBUG,
5861 "P2P: force GO freq (%d MHz) on a free channel",
5862 freq);
5863 params->freq = freq;
5864 goto success;
5865 }
5866
5867 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005868 if (num &&
5869 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005870 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5871 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5872 wpa_printf(MSG_DEBUG,
5873 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005874 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005875 params->freq = cand;
5876 goto success;
5877 }
5878
5879 /* try using one of the shared freqs */
5880 for (i = 0; i < num; i++) {
5881 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5882 freqs[i].freq)) {
5883 wpa_printf(MSG_DEBUG,
5884 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005885 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005886 params->freq = freqs[i].freq;
5887 goto success;
5888 }
5889 }
5890 }
5891
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005892 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005893 wpa_printf(MSG_DEBUG,
5894 "P2P: Cannot force GO on any of the channels we are already using");
5895 goto fail;
5896 }
5897
5898 /* try using the setting from the configuration file */
5899 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5900 wpa_s->conf->p2p_oper_channel >= 1 &&
5901 wpa_s->conf->p2p_oper_channel <= 11 &&
5902 wpas_p2p_supported_freq_go(
5903 wpa_s, channels,
5904 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5905 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5906 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5907 "frequency %d MHz", params->freq);
5908 goto success;
5909 }
5910
5911 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5912 wpa_s->conf->p2p_oper_reg_class == 116 ||
5913 wpa_s->conf->p2p_oper_reg_class == 117 ||
5914 wpa_s->conf->p2p_oper_reg_class == 124 ||
5915 wpa_s->conf->p2p_oper_reg_class == 125 ||
5916 wpa_s->conf->p2p_oper_reg_class == 126 ||
5917 wpa_s->conf->p2p_oper_reg_class == 127) &&
5918 wpas_p2p_supported_freq_go(wpa_s, channels,
5919 5000 +
5920 5 * wpa_s->conf->p2p_oper_channel)) {
5921 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5922 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5923 "frequency %d MHz", params->freq);
5924 goto success;
5925 }
5926
5927 /* Try using best channels */
5928 if (wpa_s->conf->p2p_oper_channel == 0 &&
5929 wpa_s->best_overall_freq > 0 &&
5930 wpas_p2p_supported_freq_go(wpa_s, channels,
5931 wpa_s->best_overall_freq)) {
5932 params->freq = wpa_s->best_overall_freq;
5933 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5934 "channel %d MHz", params->freq);
5935 goto success;
5936 }
5937
5938 if (wpa_s->conf->p2p_oper_channel == 0 &&
5939 wpa_s->best_24_freq > 0 &&
5940 wpas_p2p_supported_freq_go(wpa_s, channels,
5941 wpa_s->best_24_freq)) {
5942 params->freq = wpa_s->best_24_freq;
5943 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5944 "channel %d MHz", params->freq);
5945 goto success;
5946 }
5947
5948 if (wpa_s->conf->p2p_oper_channel == 0 &&
5949 wpa_s->best_5_freq > 0 &&
5950 wpas_p2p_supported_freq_go(wpa_s, channels,
5951 wpa_s->best_5_freq)) {
5952 params->freq = wpa_s->best_5_freq;
5953 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5954 "channel %d MHz", params->freq);
5955 goto success;
5956 }
5957
5958 /* try using preferred channels */
5959 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5960 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5961 params->freq = cand;
5962 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5963 "channels", params->freq);
5964 goto success;
5965 }
5966
5967 /* Try using one of the group common freqs */
5968 if (wpa_s->p2p_group_common_freqs) {
5969 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5970 cand = wpa_s->p2p_group_common_freqs[i];
5971 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5972 params->freq = cand;
5973 wpa_printf(MSG_DEBUG,
5974 "P2P: Use freq %d MHz common with the peer",
5975 params->freq);
5976 goto success;
5977 }
5978 }
5979 }
5980
5981 /* no preference, select some channel */
5982 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5983
5984 if (params->freq == 0) {
5985 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5986 goto fail;
5987 }
5988
5989success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005990 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005991 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005992fail:
5993 os_free(freqs);
5994 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005995}
5996
5997
5998static struct wpa_supplicant *
5999wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6000 int go)
6001{
6002 struct wpa_supplicant *group_wpa_s;
6003
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006004 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006005 if (wpa_s->p2p_mgmt) {
6006 /*
6007 * We may be called on the p2p_dev interface which
6008 * cannot be used for group operations, so always use
6009 * the primary interface.
6010 */
6011 wpa_s->parent->p2pdev = wpa_s;
6012 wpa_s = wpa_s->parent;
6013 }
6014 wpa_dbg(wpa_s, MSG_DEBUG,
6015 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006016 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006017 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006018 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006019 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021
6022 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006023 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006024 wpa_msg_global(wpa_s, MSG_ERROR,
6025 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006028 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6029 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006030 wpa_msg_global(wpa_s, MSG_ERROR,
6031 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 wpas_p2p_remove_pending_group_interface(wpa_s);
6033 return NULL;
6034 }
6035
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006036 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6037 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006038 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006039 return group_wpa_s;
6040}
6041
6042
6043/**
6044 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6045 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6046 * @persistent_group: Whether to create a persistent group
6047 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006048 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006049 * @ht40: Start GO with 40 MHz channel width
6050 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006051 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006052 * Returns: 0 on success, -1 on failure
6053 *
6054 * This function creates a new P2P group with the local end as the Group Owner,
6055 * i.e., without using Group Owner Negotiation.
6056 */
6057int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006058 int freq, int vht_center_freq2, int ht40, int vht,
6059 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060{
6061 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006062
6063 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6064 return -1;
6065
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006066 os_free(wpa_s->global->add_psk);
6067 wpa_s->global->add_psk = NULL;
6068
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006069 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006070 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006071 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006072
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006073 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6074 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006075 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006077 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6078 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006079 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006080 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006081 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006082 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6083 ieee80211_is_dfs(params.freq)) {
6084 /*
6085 * If freq is a DFS channel and DFS is offloaded to the
6086 * driver, allow P2P GO to use it.
6087 */
6088 wpa_printf(MSG_DEBUG,
6089 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
6090 __func__, params.freq);
6091 } else {
6092 wpa_printf(MSG_DEBUG,
6093 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
6094 params.freq);
6095 return -1;
6096 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006097 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098 p2p_go_params(wpa_s->global->p2p, &params);
6099 params.persistent_group = persistent_group;
6100
6101 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6102 if (wpa_s == NULL)
6103 return -1;
6104 wpas_start_wps_go(wpa_s, &params, 0);
6105
6106 return 0;
6107}
6108
6109
6110static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006111 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006112 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113{
6114 struct wpa_ssid *ssid;
6115
6116 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6117 if (wpa_s == NULL)
6118 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006119 if (force_scan)
6120 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006121 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122
6123 wpa_supplicant_ap_deinit(wpa_s);
6124
6125 ssid = wpa_config_add_network(wpa_s->conf);
6126 if (ssid == NULL)
6127 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006128 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129 wpa_config_set_network_defaults(ssid);
6130 ssid->temporary = 1;
6131 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006132 ssid->pbss = params->pbss;
6133 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6134 WPA_CIPHER_CCMP;
6135 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006136 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6137 ssid->ssid = os_malloc(params->ssid_len);
6138 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139 wpa_config_remove_network(wpa_s->conf, ssid->id);
6140 return -1;
6141 }
6142 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6143 ssid->ssid_len = params->ssid_len;
6144 ssid->p2p_group = 1;
6145 ssid->export_keys = 1;
6146 if (params->psk_set) {
6147 os_memcpy(ssid->psk, params->psk, 32);
6148 ssid->psk_set = 1;
6149 }
6150 if (params->passphrase)
6151 ssid->passphrase = os_strdup(params->passphrase);
6152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006154 wpa_s->p2p_in_invitation = 1;
6155 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006156 wpa_s->p2p_go_group_formation_completed = 0;
6157 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006159 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006160 NULL);
6161 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6162 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006163 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006164 wpa_supplicant_select_network(wpa_s, ssid);
6165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006166 return 0;
6167}
6168
6169
6170int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6171 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006172 int force_freq, int neg_freq,
6173 int vht_center_freq2, int ht40,
6174 int vht, int max_oper_chwidth,
6175 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006176 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006177{
6178 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006179 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006180
6181 if (ssid->disabled != 2 || ssid->ssid == NULL)
6182 return -1;
6183
6184 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6185 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6186 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6187 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006188 if (go == 0 &&
6189 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006190 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006191 /*
6192 * This can happen if Invitation Response frame was lost
6193 * and the peer (GO of a persistent group) tries to
6194 * invite us again. Reschedule the timeout to avoid
6195 * terminating the wait for the connection too early
6196 * since we now know that the peer is still trying to
6197 * invite us instead of having already started the GO.
6198 */
6199 wpa_printf(MSG_DEBUG,
6200 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6201 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6202 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006203 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205 return 0;
6206 }
6207
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006208 os_free(wpa_s->global->add_psk);
6209 wpa_s->global->add_psk = NULL;
6210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006211 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006212 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213
Dmitry Shmidt04949592012-07-19 12:16:46 -07006214 wpa_s->p2p_fallback_to_go_neg = 0;
6215
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006216 if (ssid->mode == WPAS_MODE_P2P_GO) {
6217 if (force_freq > 0) {
6218 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6219 if (freq < 0)
6220 return -1;
6221 } else {
6222 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6223 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006224 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006225 freq = 0;
6226 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006227 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006228 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006229 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006230 struct os_reltime now;
6231 struct wpa_bss *bss =
6232 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006233
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006234 os_get_reltime(&now);
6235 if (bss &&
6236 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006237 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006238 freq = bss->freq;
6239 else
6240 freq = 0;
6241 }
6242
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006243 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6244 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006245 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006246 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006247 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006248
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006249 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6250 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006251 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252
6253 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006254 params.psk_set = ssid->psk_set;
6255 if (params.psk_set)
6256 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006257 if (ssid->passphrase) {
6258 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6259 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6260 "persistent group");
6261 return -1;
6262 }
6263 os_strlcpy(params.passphrase, ssid->passphrase,
6264 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6267 params.ssid_len = ssid->ssid_len;
6268 params.persistent_group = 1;
6269
6270 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6271 if (wpa_s == NULL)
6272 return -1;
6273
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006274 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6275
Dmitry Shmidt56052862013-10-04 10:23:25 -07006276 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277 wpas_start_wps_go(wpa_s, &params, 0);
6278
6279 return 0;
6280}
6281
6282
6283static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6284 struct wpabuf *proberesp_ies)
6285{
6286 struct wpa_supplicant *wpa_s = ctx;
6287 if (wpa_s->ap_iface) {
6288 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006289 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6290 wpabuf_free(beacon_ies);
6291 wpabuf_free(proberesp_ies);
6292 return;
6293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294 if (beacon_ies) {
6295 wpabuf_free(hapd->p2p_beacon_ie);
6296 hapd->p2p_beacon_ie = beacon_ies;
6297 }
6298 wpabuf_free(hapd->p2p_probe_resp_ie);
6299 hapd->p2p_probe_resp_ie = proberesp_ies;
6300 } else {
6301 wpabuf_free(beacon_ies);
6302 wpabuf_free(proberesp_ies);
6303 }
6304 wpa_supplicant_ap_update_beacon(wpa_s);
6305}
6306
6307
6308static void wpas_p2p_idle_update(void *ctx, int idle)
6309{
6310 struct wpa_supplicant *wpa_s = ctx;
6311 if (!wpa_s->ap_iface)
6312 return;
6313 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006314 if (idle) {
6315 if (wpa_s->global->p2p_fail_on_wps_complete &&
6316 wpa_s->p2p_in_provisioning) {
6317 wpas_p2p_grpform_fail_after_wps(wpa_s);
6318 return;
6319 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006320 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006321 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6323}
6324
6325
6326struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006327 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006328{
6329 struct p2p_group *group;
6330 struct p2p_group_config *cfg;
6331
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006332 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6333 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 return NULL;
6335
6336 cfg = os_zalloc(sizeof(*cfg));
6337 if (cfg == NULL)
6338 return NULL;
6339
Dmitry Shmidt04949592012-07-19 12:16:46 -07006340 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006341 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006342 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006343 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006344 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6345 if (wpa_s->max_stations &&
6346 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6347 cfg->max_clients = wpa_s->max_stations;
6348 else
6349 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006350 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6351 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006352 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353 cfg->cb_ctx = wpa_s;
6354 cfg->ie_update = wpas_p2p_ie_update;
6355 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006356 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6357 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006358
6359 group = p2p_group_init(wpa_s->global->p2p, cfg);
6360 if (group == NULL)
6361 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006362 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006363 p2p_group_notif_formation_done(group);
6364 wpa_s->p2p_group = group;
6365 return group;
6366}
6367
6368
6369void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6370 int registrar)
6371{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006372 struct wpa_ssid *ssid = wpa_s->current_ssid;
6373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006374 if (!wpa_s->p2p_in_provisioning) {
6375 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6376 "provisioning not in progress");
6377 return;
6378 }
6379
Dmitry Shmidt04949592012-07-19 12:16:46 -07006380 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6381 u8 go_dev_addr[ETH_ALEN];
6382 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6383 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6384 ssid->ssid_len);
6385 /* Clear any stored provisioning info */
6386 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6387 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006388
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006389 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006391 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006392 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6393 /*
6394 * Use a separate timeout for initial data connection to
6395 * complete to allow the group to be removed automatically if
6396 * something goes wrong in this step before the P2P group idle
6397 * timeout mechanism is taken into use.
6398 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006399 wpa_dbg(wpa_s, MSG_DEBUG,
6400 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6401 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006402 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6403 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006404 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006405 /* Complete group formation on successful data connection. */
6406 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006407 } else if (ssid) {
6408 /*
6409 * Use a separate timeout for initial data connection to
6410 * complete to allow the group to be removed automatically if
6411 * the client does not complete data connection successfully.
6412 */
6413 wpa_dbg(wpa_s, MSG_DEBUG,
6414 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6415 P2P_MAX_INITIAL_CONN_WAIT_GO);
6416 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6417 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006418 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006419 /*
6420 * Complete group formation on first successful data connection
6421 */
6422 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006423 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424 if (wpa_s->global->p2p)
6425 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006426 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006427}
6428
6429
Jouni Malinen75ecf522011-06-27 15:19:46 -07006430void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6431 struct wps_event_fail *fail)
6432{
6433 if (!wpa_s->p2p_in_provisioning) {
6434 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6435 "provisioning not in progress");
6436 return;
6437 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006438
6439 if (wpa_s->go_params) {
6440 p2p_clear_provisioning_info(
6441 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006442 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006443 }
6444
Jouni Malinen75ecf522011-06-27 15:19:46 -07006445 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006446
6447 if (wpa_s == wpa_s->global->p2p_group_formation) {
6448 /*
6449 * Allow some time for the failed WPS negotiation exchange to
6450 * complete, but remove the group since group formation cannot
6451 * succeed after provisioning failure.
6452 */
6453 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6454 wpa_s->global->p2p_fail_on_wps_complete = 1;
6455 eloop_deplete_timeout(0, 50000,
6456 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006457 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006458 }
6459}
6460
6461
6462int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6463{
6464 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6465 !wpa_s->p2p_in_provisioning)
6466 return 0;
6467
6468 wpas_p2p_grpform_fail_after_wps(wpa_s);
6469
6470 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006471}
6472
6473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006474int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006475 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006476 enum wpas_p2p_prov_disc_use use,
6477 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478{
6479 u16 config_methods;
6480
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006481 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006482 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006483 wpa_s->p2p_fallback_to_go_neg = 0;
6484 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006485 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6486 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006487 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6488 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6489
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006490 wpa_printf(MSG_DEBUG,
6491 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6492 __func__, p2ps_prov->conncap,
6493 p2ps_prov->adv_id, p2ps_prov->conncap,
6494 p2ps_prov->status, p2ps_prov->info);
6495
6496 config_methods = 0;
6497 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006498 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006499 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006500 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006501 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6502 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006504 else {
6505 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006506 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509
Dmitry Shmidt04949592012-07-19 12:16:46 -07006510 if (use == WPAS_P2P_PD_AUTO) {
6511 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6512 wpa_s->pending_pd_config_methods = config_methods;
6513 wpa_s->p2p_auto_pd = 1;
6514 wpa_s->p2p_auto_join = 0;
6515 wpa_s->pending_pd_before_join = 0;
6516 wpa_s->auto_pd_scan_retry = 0;
6517 wpas_p2p_stop_find(wpa_s);
6518 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006519 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006520 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6521 wpa_s->p2p_auto_started.sec,
6522 wpa_s->p2p_auto_started.usec);
6523 wpas_p2p_join_scan(wpa_s, NULL);
6524 return 0;
6525 }
6526
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006527 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6528 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006530 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006531
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006532 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006533 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006534 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535}
6536
6537
6538int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6539 char *end)
6540{
6541 return p2p_scan_result_text(ies, ies_len, buf, end);
6542}
6543
6544
6545static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6546{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006547 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006548 return;
6549
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006550 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006551 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006552 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6553 wpa_s, NULL);
6554 offchannel_send_action_done(wpa_s);
6555 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006557 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6558 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006559 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006560}
6561
6562
6563int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6564 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006565 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006566 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006567 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568{
6569 wpas_p2p_clear_pending_action_tx(wpa_s);
6570 wpa_s->p2p_long_listen = 0;
6571
Dmitry Shmidt04949592012-07-19 12:16:46 -07006572 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6573 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006574 return -1;
6575
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006576 wpa_supplicant_cancel_sched_scan(wpa_s);
6577
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006579 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006580 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006581}
6582
6583
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006584static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6585 struct wpa_scan_results *scan_res)
6586{
6587 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6588
6589 if (wpa_s->p2p_scan_work) {
6590 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6591 wpa_s->p2p_scan_work = NULL;
6592 radio_work_done(work);
6593 }
6594
6595 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6596 return;
6597
6598 /*
6599 * Indicate that results have been processed so that the P2P module can
6600 * continue pending tasks.
6601 */
6602 p2p_scan_res_handled(wpa_s->global->p2p);
6603}
6604
6605
6606static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607{
6608 wpas_p2p_clear_pending_action_tx(wpa_s);
6609 wpa_s->p2p_long_listen = 0;
6610 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6611 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006612
6613 if (wpa_s->global->p2p)
6614 p2p_stop_find(wpa_s->global->p2p);
6615
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006616 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6617 wpa_printf(MSG_DEBUG,
6618 "P2P: Do not consider the scan results after stop_find");
6619 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6620 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006621}
6622
6623
6624void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6625{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006626 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006627 if (!wpa_s->global->pending_group_iface_for_p2ps)
6628 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006629}
6630
6631
6632static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6633{
6634 struct wpa_supplicant *wpa_s = eloop_ctx;
6635 wpa_s->p2p_long_listen = 0;
6636}
6637
6638
6639int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6640{
6641 int res;
6642
6643 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6644 return -1;
6645
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006646 if (wpa_s->p2p_lo_started) {
6647 wpa_printf(MSG_DEBUG,
6648 "P2P: Cannot start P2P listen, it is offloaded");
6649 return -1;
6650 }
6651
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006652 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006653 wpas_p2p_clear_pending_action_tx(wpa_s);
6654
6655 if (timeout == 0) {
6656 /*
6657 * This is a request for unlimited Listen state. However, at
6658 * least for now, this is mapped to a Listen state for one
6659 * hour.
6660 */
6661 timeout = 3600;
6662 }
6663 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6664 wpa_s->p2p_long_listen = 0;
6665
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006666 /*
6667 * Stop previous find/listen operation to avoid trying to request a new
6668 * remain-on-channel operation while the driver is still running the
6669 * previous one.
6670 */
6671 if (wpa_s->global->p2p)
6672 p2p_stop_find(wpa_s->global->p2p);
6673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006674 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6675 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6676 wpa_s->p2p_long_listen = timeout * 1000;
6677 eloop_register_timeout(timeout, 0,
6678 wpas_p2p_long_listen_timeout,
6679 wpa_s, NULL);
6680 }
6681
6682 return res;
6683}
6684
6685
6686int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6687 u8 *buf, size_t len, int p2p_group)
6688{
6689 struct wpabuf *p2p_ie;
6690 int ret;
6691
6692 if (wpa_s->global->p2p_disabled)
6693 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006694 /*
6695 * Advertize mandatory cross connection capability even on
6696 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6697 */
6698 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006699 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006700 if (wpa_s->global->p2p == NULL)
6701 return -1;
6702 if (bss == NULL)
6703 return -1;
6704
6705 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6706 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6707 p2p_group, p2p_ie);
6708 wpabuf_free(p2p_ie);
6709
6710 return ret;
6711}
6712
6713
6714int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006715 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006716 const u8 *ie, size_t ie_len,
6717 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006718{
6719 if (wpa_s->global->p2p_disabled)
6720 return 0;
6721 if (wpa_s->global->p2p == NULL)
6722 return 0;
6723
Dmitry Shmidt04949592012-07-19 12:16:46 -07006724 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006725 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006726 case P2P_PREQ_NOT_P2P:
6727 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6728 ssi_signal);
6729 /* fall through */
6730 case P2P_PREQ_MALFORMED:
6731 case P2P_PREQ_NOT_LISTEN:
6732 case P2P_PREQ_NOT_PROCESSED:
6733 default: /* make gcc happy */
6734 return 0;
6735 case P2P_PREQ_PROCESSED:
6736 return 1;
6737 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006738}
6739
6740
6741void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6742 const u8 *sa, const u8 *bssid,
6743 u8 category, const u8 *data, size_t len, int freq)
6744{
6745 if (wpa_s->global->p2p_disabled)
6746 return;
6747 if (wpa_s->global->p2p == NULL)
6748 return;
6749
6750 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6751 freq);
6752}
6753
6754
6755void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6756{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006757 unsigned int bands;
6758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006759 if (wpa_s->global->p2p_disabled)
6760 return;
6761 if (wpa_s->global->p2p == NULL)
6762 return;
6763
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006764 bands = wpas_get_bands(wpa_s, NULL);
6765 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006766}
6767
6768
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006769static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006770{
6771 p2p_group_deinit(wpa_s->p2p_group);
6772 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006773
6774 wpa_s->ap_configured_cb = NULL;
6775 wpa_s->ap_configured_cb_ctx = NULL;
6776 wpa_s->ap_configured_cb_data = NULL;
6777 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006778}
6779
6780
6781int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6782{
6783 wpa_s->p2p_long_listen = 0;
6784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006785 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6786 return -1;
6787
6788 return p2p_reject(wpa_s->global->p2p, addr);
6789}
6790
6791
6792/* Invite to reinvoke a persistent group */
6793int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006794 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006795 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6796 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006797{
6798 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006799 u8 *bssid = NULL;
6800 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006801 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006802 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006803 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006804
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006805 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006806 if (peer_addr)
6807 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6808 else
6809 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006810
Jouni Malinen31be0a42012-08-31 21:20:51 +03006811 wpa_s->p2p_persistent_go_freq = freq;
6812 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006813 wpa_s->p2p_go_vht = !!vht;
6814 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
6815 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006816 if (ssid->mode == WPAS_MODE_P2P_GO) {
6817 role = P2P_INVITE_ROLE_GO;
6818 if (peer_addr == NULL) {
6819 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6820 "address in invitation command");
6821 return -1;
6822 }
6823 if (wpas_p2p_create_iface(wpa_s)) {
6824 if (wpas_p2p_add_group_interface(wpa_s,
6825 WPA_IF_P2P_GO) < 0) {
6826 wpa_printf(MSG_ERROR, "P2P: Failed to "
6827 "allocate a new interface for the "
6828 "group");
6829 return -1;
6830 }
6831 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006832 } else if (wpa_s->p2p_mgmt)
6833 bssid = wpa_s->parent->own_addr;
6834 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006835 bssid = wpa_s->own_addr;
6836 } else {
6837 role = P2P_INVITE_ROLE_CLIENT;
6838 peer_addr = ssid->bssid;
6839 }
6840 wpa_s->pending_invite_ssid_id = ssid->id;
6841
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006842 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006843 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006844 role == P2P_INVITE_ROLE_GO,
6845 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006846 if (res)
6847 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006849 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6850 return -1;
6851
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006852 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6853
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006854 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6855 no_pref_freq_given && pref_freq > 0 &&
6856 wpa_s->num_multichan_concurrent > 1 &&
6857 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6858 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6859 pref_freq);
6860 pref_freq = 0;
6861 }
6862
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006863 /*
6864 * Stop any find/listen operations before invitation and possibly
6865 * connection establishment.
6866 */
6867 wpas_p2p_stop_find_oper(wpa_s);
6868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006869 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006870 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006871 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872}
6873
6874
6875/* Invite to join an active group */
6876int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6877 const u8 *peer_addr, const u8 *go_dev_addr)
6878{
6879 struct wpa_global *global = wpa_s->global;
6880 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006881 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006882 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006883 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006884 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006885 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006886 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006887
Jouni Malinen31be0a42012-08-31 21:20:51 +03006888 wpa_s->p2p_persistent_go_freq = 0;
6889 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006890 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006891 wpa_s->p2p_go_vht_center_freq2 = 0;
6892 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6895 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6896 break;
6897 }
6898 if (wpa_s == NULL) {
6899 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6900 return -1;
6901 }
6902
6903 ssid = wpa_s->current_ssid;
6904 if (ssid == NULL) {
6905 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6906 "invitation");
6907 return -1;
6908 }
6909
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006910 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006911 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006912 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006913 ssid->ssid, ssid->ssid_len);
6914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915 if (ssid->mode == WPAS_MODE_P2P_GO) {
6916 role = P2P_INVITE_ROLE_ACTIVE_GO;
6917 bssid = wpa_s->own_addr;
6918 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006919 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006920 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006921 } else {
6922 role = P2P_INVITE_ROLE_CLIENT;
6923 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6924 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6925 "invite to current group");
6926 return -1;
6927 }
6928 bssid = wpa_s->bssid;
6929 if (go_dev_addr == NULL &&
6930 !is_zero_ether_addr(wpa_s->go_dev_addr))
6931 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006932 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6933 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006934 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006935 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006937 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6938 return -1;
6939
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006940 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006941 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006942 role == P2P_INVITE_ROLE_ACTIVE_GO,
6943 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006944 if (res)
6945 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006946 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006947
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006948 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006949 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006950 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006951}
6952
6953
6954void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6955{
6956 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006957 u8 go_dev_addr[ETH_ALEN];
6958 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006959 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006960 u8 ip[3 * 4];
6961 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962
Dmitry Shmidt04949592012-07-19 12:16:46 -07006963 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6964 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006965 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006966 }
6967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006968 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006969 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006970
6971 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006972 if (!wpa_s->p2p_go_group_formation_completed &&
6973 wpa_s->global->p2p_group_formation == wpa_s) {
6974 wpa_dbg(wpa_s, MSG_DEBUG,
6975 "P2P: Marking group formation completed on client on data connection");
6976 wpa_s->p2p_go_group_formation_completed = 1;
6977 wpa_s->global->p2p_group_formation = NULL;
6978 wpa_s->p2p_in_provisioning = 0;
6979 wpa_s->p2p_in_invitation = 0;
6980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006982 os_memset(go_dev_addr, 0, ETH_ALEN);
6983 if (ssid->bssid_set)
6984 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6985 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6986 ssid->ssid_len);
6987 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6988
6989 if (wpa_s->global->p2p_group_formation == wpa_s)
6990 wpa_s->global->p2p_group_formation = NULL;
6991
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006992 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6993 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006994
6995 ip_addr[0] = '\0';
6996 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006997 int res;
6998
6999 res = os_snprintf(ip_addr, sizeof(ip_addr),
7000 " ip_addr=%u.%u.%u.%u "
7001 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7002 ip[0], ip[1], ip[2], ip[3],
7003 ip[4], ip[5], ip[6], ip[7],
7004 ip[8], ip[9], ip[10], ip[11]);
7005 if (os_snprintf_error(sizeof(ip_addr), res))
7006 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007007 }
7008
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007009 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7010 ssid->passphrase == NULL && ssid->psk_set ?
7011 ssid->psk : NULL,
7012 ssid->passphrase, go_dev_addr, persistent,
7013 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007014
7015 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007016 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7017 ssid, go_dev_addr);
7018
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007019 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007020}
7021
7022
7023int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7024 u32 interval1, u32 duration2, u32 interval2)
7025{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007026 int ret;
7027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7029 return -1;
7030
7031 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7032 wpa_s->current_ssid == NULL ||
7033 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7034 return -1;
7035
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007036 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7037 wpa_s->own_addr, wpa_s->assoc_freq,
7038 duration1, interval1, duration2, interval2);
7039 if (ret == 0)
7040 wpa_s->waiting_presence_resp = 1;
7041
7042 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043}
7044
7045
7046int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7047 unsigned int interval)
7048{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007049 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7050 return -1;
7051
7052 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7053}
7054
7055
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007056static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7057{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007058 if (wpa_s->current_ssid == NULL) {
7059 /*
7060 * current_ssid can be cleared when P2P client interface gets
7061 * disconnected, so assume this interface was used as P2P
7062 * client.
7063 */
7064 return 1;
7065 }
7066 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007067 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7068}
7069
7070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007071static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7072{
7073 struct wpa_supplicant *wpa_s = eloop_ctx;
7074
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007075 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007076 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7077 "disabled");
7078 return;
7079 }
7080
Dmitry Shmidt04949592012-07-19 12:16:46 -07007081 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7082 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007083 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084}
7085
7086
7087static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7088{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007089 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007090
Dmitry Shmidt04949592012-07-19 12:16:46 -07007091 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7092 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7095 return;
7096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007097 timeout = wpa_s->conf->p2p_group_idle;
7098 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7099 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7100 timeout = P2P_MAX_CLIENT_IDLE;
7101
7102 if (timeout == 0)
7103 return;
7104
Dmitry Shmidt04949592012-07-19 12:16:46 -07007105 if (timeout < 0) {
7106 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7107 timeout = 0; /* special client mode no-timeout */
7108 else
7109 return;
7110 }
7111
7112 if (wpa_s->p2p_in_provisioning) {
7113 /*
7114 * Use the normal group formation timeout during the
7115 * provisioning phase to avoid terminating this process too
7116 * early due to group idle timeout.
7117 */
7118 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7119 "during provisioning");
7120 return;
7121 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307122
Dmitry Shmidt04949592012-07-19 12:16:46 -07007123 if (wpa_s->show_group_started) {
7124 /*
7125 * Use the normal group formation timeout between the end of
7126 * the provisioning phase and completion of 4-way handshake to
7127 * avoid terminating this process too early due to group idle
7128 * timeout.
7129 */
7130 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7131 "while waiting for initial 4-way handshake to "
7132 "complete");
7133 return;
7134 }
7135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007136 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007137 timeout);
7138 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7139 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140}
7141
7142
Jouni Malinen2b89da82012-08-31 22:04:41 +03007143/* Returns 1 if the interface was removed */
7144int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7145 u16 reason_code, const u8 *ie, size_t ie_len,
7146 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007147{
7148 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007149 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007150
Dmitry Shmidt04949592012-07-19 12:16:46 -07007151 if (!locally_generated)
7152 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7153 ie_len);
7154
7155 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7156 wpa_s->current_ssid &&
7157 wpa_s->current_ssid->p2p_group &&
7158 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7159 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7160 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007161 if (wpas_p2p_group_delete(wpa_s,
7162 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7163 > 0)
7164 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007165 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007166
7167 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007168}
7169
7170
7171void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007172 u16 reason_code, const u8 *ie, size_t ie_len,
7173 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007174{
7175 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7176 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177
Dmitry Shmidt04949592012-07-19 12:16:46 -07007178 if (!locally_generated)
7179 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7180 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181}
7182
7183
7184void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7185{
7186 struct p2p_data *p2p = wpa_s->global->p2p;
7187
7188 if (p2p == NULL)
7189 return;
7190
7191 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7192 return;
7193
7194 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7195 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7196
7197 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7198 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7199
7200 if (wpa_s->wps &&
7201 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7202 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7203
7204 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7205 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7206
7207 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7208 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7209 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7210 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7211 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7212 }
7213
7214 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7215 p2p_set_sec_dev_types(p2p,
7216 (void *) wpa_s->conf->sec_device_type,
7217 wpa_s->conf->num_sec_device_types);
7218
7219 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7220 int i;
7221 p2p_remove_wps_vendor_extensions(p2p);
7222 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7223 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7224 continue;
7225 p2p_add_wps_vendor_extension(
7226 p2p, wpa_s->conf->wps_vendor_ext[i]);
7227 }
7228 }
7229
7230 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7231 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7232 char country[3];
7233 country[0] = wpa_s->conf->country[0];
7234 country[1] = wpa_s->conf->country[1];
7235 country[2] = 0x04;
7236 p2p_set_country(p2p, country);
7237 }
7238
7239 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7240 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7241 wpa_s->conf->p2p_ssid_postfix ?
7242 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7243 0);
7244 }
7245
7246 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7247 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007248
7249 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7250 u8 reg_class, channel;
7251 int ret;
7252 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007253 u8 channel_forced;
7254
Jouni Malinen75ecf522011-06-27 15:19:46 -07007255 if (wpa_s->conf->p2p_listen_reg_class &&
7256 wpa_s->conf->p2p_listen_channel) {
7257 reg_class = wpa_s->conf->p2p_listen_reg_class;
7258 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007259 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007260 } else {
7261 reg_class = 81;
7262 /*
7263 * Pick one of the social channels randomly as the
7264 * listen channel.
7265 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007266 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7267 channel = 1;
7268 else
7269 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007270 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007271 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007272 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7273 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007274 if (ret)
7275 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7276 "failed: %d", ret);
7277 }
7278 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7279 u8 op_reg_class, op_channel, cfg_op_channel;
7280 int ret = 0;
7281 unsigned int r;
7282 if (wpa_s->conf->p2p_oper_reg_class &&
7283 wpa_s->conf->p2p_oper_channel) {
7284 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7285 op_channel = wpa_s->conf->p2p_oper_channel;
7286 cfg_op_channel = 1;
7287 } else {
7288 op_reg_class = 81;
7289 /*
7290 * Use random operation channel from (1, 6, 11)
7291 *if no other preference is indicated.
7292 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007293 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7294 op_channel = 1;
7295 else
7296 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007297 cfg_op_channel = 0;
7298 }
7299 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7300 cfg_op_channel);
7301 if (ret)
7302 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7303 "failed: %d", ret);
7304 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007305
7306 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7307 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7308 wpa_s->conf->p2p_pref_chan) < 0) {
7309 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7310 "update failed");
7311 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007312
7313 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7314 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7315 "update failed");
7316 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007317 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007318
7319 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7320 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007321}
7322
7323
7324int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7325 int duration)
7326{
7327 if (!wpa_s->ap_iface)
7328 return -1;
7329 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7330 duration);
7331}
7332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007333
7334int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7335{
7336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7337 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007338
7339 wpa_s->global->cross_connection = enabled;
7340 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7341
7342 if (!enabled) {
7343 struct wpa_supplicant *iface;
7344
7345 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7346 {
7347 if (iface->cross_connect_enabled == 0)
7348 continue;
7349
7350 iface->cross_connect_enabled = 0;
7351 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007352 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007353 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7354 iface->ifname,
7355 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007356 }
7357 }
7358
7359 return 0;
7360}
7361
7362
7363static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7364{
7365 struct wpa_supplicant *iface;
7366
7367 if (!uplink->global->cross_connection)
7368 return;
7369
7370 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7371 if (!iface->cross_connect_enabled)
7372 continue;
7373 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7374 0)
7375 continue;
7376 if (iface->ap_iface == NULL)
7377 continue;
7378 if (iface->cross_connect_in_use)
7379 continue;
7380
7381 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007382 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007383 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7384 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007385 }
7386}
7387
7388
7389static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7390{
7391 struct wpa_supplicant *iface;
7392
7393 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7394 if (!iface->cross_connect_enabled)
7395 continue;
7396 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7397 0)
7398 continue;
7399 if (!iface->cross_connect_in_use)
7400 continue;
7401
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007402 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007403 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7404 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007405 iface->cross_connect_in_use = 0;
7406 }
7407}
7408
7409
7410void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7411{
7412 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7413 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7414 wpa_s->cross_connect_disallowed)
7415 wpas_p2p_disable_cross_connect(wpa_s);
7416 else
7417 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007418 if (!wpa_s->ap_iface &&
7419 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7420 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007421}
7422
7423
7424void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7425{
7426 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007427 if (!wpa_s->ap_iface &&
7428 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7429 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007430 wpas_p2p_set_group_idle_timeout(wpa_s);
7431}
7432
7433
7434static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7435{
7436 struct wpa_supplicant *iface;
7437
7438 if (!wpa_s->global->cross_connection)
7439 return;
7440
7441 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7442 if (iface == wpa_s)
7443 continue;
7444 if (iface->drv_flags &
7445 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7446 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007447 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7448 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007449 continue;
7450
7451 wpa_s->cross_connect_enabled = 1;
7452 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7453 sizeof(wpa_s->cross_connect_uplink));
7454 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7455 "%s to %s whenever uplink is available",
7456 wpa_s->ifname, wpa_s->cross_connect_uplink);
7457
7458 if (iface->ap_iface || iface->current_ssid == NULL ||
7459 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7460 iface->cross_connect_disallowed ||
7461 iface->wpa_state != WPA_COMPLETED)
7462 break;
7463
7464 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007465 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007466 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7467 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007468 break;
7469 }
7470}
7471
7472
7473int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7474{
7475 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7476 !wpa_s->p2p_in_provisioning)
7477 return 0; /* not P2P client operation */
7478
7479 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7480 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007481 if (wpa_s != wpa_s->p2pdev)
7482 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007483 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007484 return 1;
7485}
7486
7487
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007488void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7489{
7490 struct wpa_supplicant *wpa_s = eloop_ctx;
7491 wpas_p2p_notif_pbc_overlap(wpa_s);
7492}
7493
7494
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007495void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7496 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007497{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007498 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007499 struct wpa_used_freq_data *freqs = NULL;
7500 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007501
7502 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7503 return;
7504
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007505 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7506 if (!freqs)
7507 return;
7508
7509 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007511 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007512 os_memset(&cli_chan, 0, sizeof(cli_chan));
7513 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007514 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7515 "channel list");
7516 return;
7517 }
7518
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007519 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007520
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007521 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007522
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007523 /*
7524 * The used frequencies map changed, so it is possible that a GO is
7525 * using a channel that is no longer valid for P2P use. It is also
7526 * possible that due to policy consideration, it would be preferable to
7527 * move it to a frequency already used by other station interfaces.
7528 */
7529 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7530
7531 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007532}
7533
7534
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007535static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7536 struct wpa_scan_results *scan_res)
7537{
7538 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7539}
7540
7541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007542int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7543{
7544 struct wpa_global *global = wpa_s->global;
7545 int found = 0;
7546 const u8 *peer;
7547
7548 if (global->p2p == NULL)
7549 return -1;
7550
7551 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7552
7553 if (wpa_s->pending_interface_name[0] &&
7554 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7555 found = 1;
7556
7557 peer = p2p_get_go_neg_peer(global->p2p);
7558 if (peer) {
7559 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7560 MACSTR, MAC2STR(peer));
7561 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007562 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007563 }
7564
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007565 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7566 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7567 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7568 found = 1;
7569 }
7570
7571 if (wpa_s->pending_pd_before_join) {
7572 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7573 wpa_s->pending_pd_before_join = 0;
7574 found = 1;
7575 }
7576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007577 wpas_p2p_stop_find(wpa_s);
7578
7579 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7580 if (wpa_s == global->p2p_group_formation &&
7581 (wpa_s->p2p_in_provisioning ||
7582 wpa_s->parent->pending_interface_type ==
7583 WPA_IF_P2P_CLIENT)) {
7584 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7585 "formation found - cancelling",
7586 wpa_s->ifname);
7587 found = 1;
7588 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007589 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007590 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007591 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007592 break;
7593 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007594 wpas_p2p_group_delete(wpa_s,
7595 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007596 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007597 } else if (wpa_s->p2p_in_invitation) {
7598 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7599 wpa_s->ifname);
7600 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007601 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007602 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007603 }
7604 }
7605
7606 if (!found) {
7607 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7608 return -1;
7609 }
7610
7611 return 0;
7612}
7613
7614
7615void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7616{
7617 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7618 return;
7619
7620 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7621 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007622 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007623}
7624
7625
7626void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7627 int freq_24, int freq_5, int freq_overall)
7628{
7629 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007630 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007631 return;
7632 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7633}
7634
7635
7636int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7637{
7638 u8 peer[ETH_ALEN];
7639 struct p2p_data *p2p = wpa_s->global->p2p;
7640
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007641 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007642 return -1;
7643
7644 if (hwaddr_aton(addr, peer))
7645 return -1;
7646
7647 return p2p_unauthorize(p2p, peer);
7648}
7649
7650
7651/**
7652 * wpas_p2p_disconnect - Disconnect from a P2P Group
7653 * @wpa_s: Pointer to wpa_supplicant data
7654 * Returns: 0 on success, -1 on failure
7655 *
7656 * This can be used to disconnect from a group in which the local end is a P2P
7657 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7658 * virtual network interface was created for this group, that interface will be
7659 * removed. Otherwise, only the configured P2P group network will be removed
7660 * from the interface.
7661 */
7662int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7663{
7664
7665 if (wpa_s == NULL)
7666 return -1;
7667
Jouni Malinen2b89da82012-08-31 22:04:41 +03007668 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7669 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007670}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007671
7672
7673int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7674{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007675 int ret;
7676
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007677 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7678 return 0;
7679
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007680 ret = p2p_in_progress(wpa_s->global->p2p);
7681 if (ret == 0) {
7682 /*
7683 * Check whether there is an ongoing WPS provisioning step (or
7684 * other parts of group formation) on another interface since
7685 * p2p_in_progress() does not report this to avoid issues for
7686 * scans during such provisioning step.
7687 */
7688 if (wpa_s->global->p2p_group_formation &&
7689 wpa_s->global->p2p_group_formation != wpa_s) {
7690 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7691 "in group formation",
7692 wpa_s->global->p2p_group_formation->ifname);
7693 ret = 1;
7694 }
7695 }
7696
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007697 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007698 struct os_reltime now;
7699 os_get_reltime(&now);
7700 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7701 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007702 /* Wait for the first client has expired */
7703 wpa_s->global->p2p_go_wait_client.sec = 0;
7704 } else {
7705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7706 ret = 1;
7707 }
7708 }
7709
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007710 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007711}
7712
7713
7714void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7715 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007716{
7717 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7718 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007719 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007720 /**
7721 * Remove the network by scheduling the group formation
7722 * timeout to happen immediately. The teardown code
7723 * needs to be scheduled to run asynch later so that we
7724 * don't delete data from under ourselves unexpectedly.
7725 * Calling wpas_p2p_group_formation_timeout directly
7726 * causes a series of crashes in WPS failure scenarios.
7727 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007728 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7729 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007730 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007731 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007732 }
7733}
7734
7735
7736struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007737 const u8 *addr, const u8 *ssid,
7738 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007739{
7740 struct wpa_ssid *s;
7741 size_t i;
7742
7743 for (s = wpa_s->conf->ssid; s; s = s->next) {
7744 if (s->disabled != 2)
7745 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007746 if (ssid &&
7747 (ssid_len != s->ssid_len ||
7748 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7749 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007750 if (addr == NULL) {
7751 if (s->mode == WPAS_MODE_P2P_GO)
7752 return s;
7753 continue;
7754 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007755 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7756 return s; /* peer is GO in the persistent group */
7757 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7758 continue;
7759 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007760 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007761 addr, ETH_ALEN) == 0)
7762 return s; /* peer is P2P client in persistent
7763 * group */
7764 }
7765 }
7766
7767 return NULL;
7768}
7769
7770
7771void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7772 const u8 *addr)
7773{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007774 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007775 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007776 /*
7777 * This can happen if WPS provisioning step is not terminated
7778 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7779 * peer was able to connect, there is no need to time out group
7780 * formation after this, though. In addition, this is used with
7781 * the initial connection wait on the GO as a separate formation
7782 * timeout and as such, expected to be hit after the initial WPS
7783 * provisioning step.
7784 */
7785 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007786
7787 if (!wpa_s->p2p_go_group_formation_completed &&
7788 !wpa_s->group_formation_reported) {
7789 /*
7790 * GO has not yet notified group formation success since
7791 * the WPS step was not completed cleanly. Do that
7792 * notification now since the P2P Client was able to
7793 * connect and as such, must have received the
7794 * credential from the WPS step.
7795 */
7796 if (wpa_s->global->p2p)
7797 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007798 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007799 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007800 }
7801 if (!wpa_s->p2p_go_group_formation_completed) {
7802 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7803 wpa_s->p2p_go_group_formation_completed = 1;
7804 wpa_s->global->p2p_group_formation = NULL;
7805 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007806 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007807 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007808 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007809 if (addr == NULL)
7810 return;
7811 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7812}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007813
Dmitry Shmidt04949592012-07-19 12:16:46 -07007814
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007815static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7816 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007817{
7818 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007819 int ret = 0;
7820
Dmitry Shmidt04949592012-07-19 12:16:46 -07007821 if (wpa_s->global->p2p_group_formation)
7822 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007823 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007824 offchannel_send_action_done(wpa_s);
7825 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007826 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7828 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7829 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7830 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007831 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007832 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007833 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007834 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007835 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007836 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007837 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007838}
7839
7840
7841int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7842{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007843 int res;
7844
Dmitry Shmidt04949592012-07-19 12:16:46 -07007845 if (!wpa_s->p2p_fallback_to_go_neg ||
7846 wpa_s->p2p_in_provisioning <= 5)
7847 return 0;
7848
7849 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7850 return 0; /* peer operating as a GO */
7851
7852 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7853 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007854 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007855 "reason=GO-not-found");
7856 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007857
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007858 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007859}
7860
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007861
7862unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7863{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007864 struct wpa_supplicant *ifs;
7865
7866 if (wpa_s->wpa_state > WPA_SCANNING) {
7867 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7868 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007869 wpa_s->conf->p2p_search_delay);
7870 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007871 }
7872
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007873 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7874 radio_list) {
7875 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007876 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7877 "delay due to concurrent operation on "
7878 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007879 wpa_s->conf->p2p_search_delay,
7880 ifs->ifname);
7881 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007882 }
7883 }
7884
7885 return 0;
7886}
7887
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007888
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007889static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7890 struct wpa_ssid *s, const u8 *addr,
7891 int iface_addr)
7892{
7893 struct psk_list_entry *psk, *tmp;
7894 int changed = 0;
7895
7896 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7897 list) {
7898 if ((iface_addr && !psk->p2p &&
7899 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7900 (!iface_addr && psk->p2p &&
7901 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7902 wpa_dbg(wpa_s, MSG_DEBUG,
7903 "P2P: Remove persistent group PSK list entry for "
7904 MACSTR " p2p=%u",
7905 MAC2STR(psk->addr), psk->p2p);
7906 dl_list_del(&psk->list);
7907 os_free(psk);
7908 changed++;
7909 }
7910 }
7911
7912 return changed;
7913}
7914
7915
7916void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7917 const u8 *p2p_dev_addr,
7918 const u8 *psk, size_t psk_len)
7919{
7920 struct wpa_ssid *ssid = wpa_s->current_ssid;
7921 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007922 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007923
7924 if (psk_len != sizeof(p->psk))
7925 return;
7926
7927 if (p2p_dev_addr) {
7928 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7929 " p2p_dev_addr=" MACSTR,
7930 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7931 if (is_zero_ether_addr(p2p_dev_addr))
7932 p2p_dev_addr = NULL;
7933 } else {
7934 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7935 MAC2STR(mac_addr));
7936 }
7937
7938 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7939 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7940 /* To be added to persistent group once created */
7941 if (wpa_s->global->add_psk == NULL) {
7942 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7943 if (wpa_s->global->add_psk == NULL)
7944 return;
7945 }
7946 p = wpa_s->global->add_psk;
7947 if (p2p_dev_addr) {
7948 p->p2p = 1;
7949 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7950 } else {
7951 p->p2p = 0;
7952 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7953 }
7954 os_memcpy(p->psk, psk, psk_len);
7955 return;
7956 }
7957
7958 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7960 return;
7961 }
7962
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007963 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007964 ssid->ssid_len);
7965 if (!persistent) {
7966 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7967 return;
7968 }
7969
7970 p = os_zalloc(sizeof(*p));
7971 if (p == NULL)
7972 return;
7973 if (p2p_dev_addr) {
7974 p->p2p = 1;
7975 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7976 } else {
7977 p->p2p = 0;
7978 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7979 }
7980 os_memcpy(p->psk, psk, psk_len);
7981
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007982 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7983 (last = dl_list_last(&persistent->psk_list,
7984 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007985 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7986 MACSTR " (p2p=%u) to make room for a new one",
7987 MAC2STR(last->addr), last->p2p);
7988 dl_list_del(&last->list);
7989 os_free(last);
7990 }
7991
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007992 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007993 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7994 p2p_dev_addr == NULL);
7995 if (p2p_dev_addr) {
7996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7997 MACSTR, MAC2STR(p2p_dev_addr));
7998 } else {
7999 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8000 MAC2STR(mac_addr));
8001 }
8002 dl_list_add(&persistent->psk_list, &p->list);
8003
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008004 if (wpa_s->p2pdev->conf->update_config &&
8005 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008006 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008007}
8008
8009
8010static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8011 struct wpa_ssid *s, const u8 *addr,
8012 int iface_addr)
8013{
8014 int res;
8015
8016 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008017 if (res > 0 && wpa_s->conf->update_config &&
8018 wpa_config_write(wpa_s->confname, wpa_s->conf))
8019 wpa_dbg(wpa_s, MSG_DEBUG,
8020 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008021}
8022
8023
8024static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8025 const u8 *peer, int iface_addr)
8026{
8027 struct hostapd_data *hapd;
8028 struct hostapd_wpa_psk *psk, *prev, *rem;
8029 struct sta_info *sta;
8030
8031 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8032 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8033 return;
8034
8035 /* Remove per-station PSK entry */
8036 hapd = wpa_s->ap_iface->bss[0];
8037 prev = NULL;
8038 psk = hapd->conf->ssid.wpa_psk;
8039 while (psk) {
8040 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8041 (!iface_addr &&
8042 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8043 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8044 MACSTR " iface_addr=%d",
8045 MAC2STR(peer), iface_addr);
8046 if (prev)
8047 prev->next = psk->next;
8048 else
8049 hapd->conf->ssid.wpa_psk = psk->next;
8050 rem = psk;
8051 psk = psk->next;
8052 os_free(rem);
8053 } else {
8054 prev = psk;
8055 psk = psk->next;
8056 }
8057 }
8058
8059 /* Disconnect from group */
8060 if (iface_addr)
8061 sta = ap_get_sta(hapd, peer);
8062 else
8063 sta = ap_get_sta_p2p(hapd, peer);
8064 if (sta) {
8065 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8066 " (iface_addr=%d) from group",
8067 MAC2STR(peer), iface_addr);
8068 hostapd_drv_sta_deauth(hapd, sta->addr,
8069 WLAN_REASON_DEAUTH_LEAVING);
8070 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8071 }
8072}
8073
8074
8075void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8076 int iface_addr)
8077{
8078 struct wpa_ssid *s;
8079 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008080 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008081
8082 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8083
8084 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008085 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008086 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8087 continue;
8088 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008089 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8090 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008091 }
8092
8093 /* Remove from any operating group */
8094 for (w = wpa_s->global->ifaces; w; w = w->next)
8095 wpas_p2p_remove_client_go(w, peer, iface_addr);
8096}
8097
8098
8099static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8100{
8101 struct wpa_supplicant *wpa_s = eloop_ctx;
8102 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8103}
8104
8105
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008106static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8107{
8108 struct wpa_supplicant *wpa_s = eloop_ctx;
8109
8110 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8111 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8112}
8113
8114
8115int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8116 struct wpa_ssid *ssid)
8117{
8118 struct wpa_supplicant *iface;
8119
8120 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8121 if (!iface->current_ssid ||
8122 iface->current_ssid->frequency == freq ||
8123 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8124 !iface->current_ssid->p2p_group))
8125 continue;
8126
8127 /* Remove the connection with least priority */
8128 if (!wpas_is_p2p_prioritized(iface)) {
8129 /* STA connection has priority over existing
8130 * P2P connection, so remove the interface. */
8131 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8132 eloop_register_timeout(0, 0,
8133 wpas_p2p_group_freq_conflict,
8134 iface, NULL);
8135 /* If connection in progress is P2P connection, do not
8136 * proceed for the connection. */
8137 if (wpa_s == iface)
8138 return -1;
8139 else
8140 return 0;
8141 } else {
8142 /* P2P connection has priority, disable the STA network
8143 */
8144 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8145 ssid);
8146 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8147 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8148 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8149 ETH_ALEN);
8150 /* If P2P connection is in progress, continue
8151 * connecting...*/
8152 if (wpa_s == iface)
8153 return 0;
8154 else
8155 return -1;
8156 }
8157 }
8158
8159 return 0;
8160}
8161
8162
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008163int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8164{
8165 struct wpa_ssid *ssid = wpa_s->current_ssid;
8166
8167 if (ssid == NULL || !ssid->p2p_group)
8168 return 0;
8169
8170 if (wpa_s->p2p_last_4way_hs_fail &&
8171 wpa_s->p2p_last_4way_hs_fail == ssid) {
8172 u8 go_dev_addr[ETH_ALEN];
8173 struct wpa_ssid *persistent;
8174
8175 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8176 ssid->ssid,
8177 ssid->ssid_len) <= 0) {
8178 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8179 goto disconnect;
8180 }
8181
8182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8183 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008184 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008185 ssid->ssid,
8186 ssid->ssid_len);
8187 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8188 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8189 goto disconnect;
8190 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008191 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008192 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8193 persistent->id);
8194 disconnect:
8195 wpa_s->p2p_last_4way_hs_fail = NULL;
8196 /*
8197 * Remove the group from a timeout to avoid issues with caller
8198 * continuing to use the interface if this is on a P2P group
8199 * interface.
8200 */
8201 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8202 wpa_s, NULL);
8203 return 1;
8204 }
8205
8206 wpa_s->p2p_last_4way_hs_fail = ssid;
8207 return 0;
8208}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008209
8210
8211#ifdef CONFIG_WPS_NFC
8212
8213static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8214 struct wpabuf *p2p)
8215{
8216 struct wpabuf *ret;
8217 size_t wsc_len;
8218
8219 if (p2p == NULL) {
8220 wpabuf_free(wsc);
8221 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8222 return NULL;
8223 }
8224
8225 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8226 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8227 if (ret == NULL) {
8228 wpabuf_free(wsc);
8229 wpabuf_free(p2p);
8230 return NULL;
8231 }
8232
8233 wpabuf_put_be16(ret, wsc_len);
8234 if (wsc)
8235 wpabuf_put_buf(ret, wsc);
8236 wpabuf_put_be16(ret, wpabuf_len(p2p));
8237 wpabuf_put_buf(ret, p2p);
8238
8239 wpabuf_free(wsc);
8240 wpabuf_free(p2p);
8241 wpa_hexdump_buf(MSG_DEBUG,
8242 "P2P: Generated NFC connection handover message", ret);
8243
8244 if (ndef && ret) {
8245 struct wpabuf *tmp;
8246 tmp = ndef_build_p2p(ret);
8247 wpabuf_free(ret);
8248 if (tmp == NULL) {
8249 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8250 return NULL;
8251 }
8252 ret = tmp;
8253 }
8254
8255 return ret;
8256}
8257
8258
8259static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8260 struct wpa_ssid **ssid, u8 *go_dev_addr)
8261{
8262 struct wpa_supplicant *iface;
8263
8264 if (go_dev_addr)
8265 os_memset(go_dev_addr, 0, ETH_ALEN);
8266 if (ssid)
8267 *ssid = NULL;
8268 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8269 if (iface->wpa_state < WPA_ASSOCIATING ||
8270 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8271 !iface->current_ssid->p2p_group ||
8272 iface->current_ssid->mode != WPAS_MODE_INFRA)
8273 continue;
8274 if (ssid)
8275 *ssid = iface->current_ssid;
8276 if (go_dev_addr)
8277 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8278 return iface->assoc_freq;
8279 }
8280 return 0;
8281}
8282
8283
8284struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8285 int ndef)
8286{
8287 struct wpabuf *wsc, *p2p;
8288 struct wpa_ssid *ssid;
8289 u8 go_dev_addr[ETH_ALEN];
8290 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8291
8292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8293 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8294 return NULL;
8295 }
8296
8297 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8298 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8299 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8301 return NULL;
8302 }
8303
8304 if (cli_freq == 0) {
8305 wsc = wps_build_nfc_handover_req_p2p(
8306 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8307 } else
8308 wsc = NULL;
8309 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8310 go_dev_addr, ssid ? ssid->ssid : NULL,
8311 ssid ? ssid->ssid_len : 0);
8312
8313 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8314}
8315
8316
8317struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8318 int ndef, int tag)
8319{
8320 struct wpabuf *wsc, *p2p;
8321 struct wpa_ssid *ssid;
8322 u8 go_dev_addr[ETH_ALEN];
8323 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8324
8325 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8326 return NULL;
8327
8328 if (!tag && 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 return NULL;
8332
8333 if (cli_freq == 0) {
8334 wsc = wps_build_nfc_handover_sel_p2p(
8335 wpa_s->parent->wps,
8336 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8337 DEV_PW_NFC_CONNECTION_HANDOVER,
8338 wpa_s->conf->wps_nfc_dh_pubkey,
8339 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8340 } else
8341 wsc = NULL;
8342 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8343 go_dev_addr, ssid ? ssid->ssid : NULL,
8344 ssid ? ssid->ssid_len : 0);
8345
8346 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8347}
8348
8349
8350static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8351 struct p2p_nfc_params *params)
8352{
8353 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8354 "connection handover (freq=%d)",
8355 params->go_freq);
8356
8357 if (params->go_freq && params->go_ssid_len) {
8358 wpa_s->p2p_wps_method = WPS_NFC;
8359 wpa_s->pending_join_wps_method = WPS_NFC;
8360 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8361 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8362 ETH_ALEN);
8363 return wpas_p2p_join_start(wpa_s, params->go_freq,
8364 params->go_ssid,
8365 params->go_ssid_len);
8366 }
8367
8368 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8369 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008370 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008371 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8372 params->go_ssid_len ? params->go_ssid : NULL,
8373 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008374}
8375
8376
8377static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8378 struct p2p_nfc_params *params, int tag)
8379{
8380 int res, persistent;
8381 struct wpa_ssid *ssid;
8382
8383 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8384 "connection handover");
8385 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8386 ssid = wpa_s->current_ssid;
8387 if (ssid == NULL)
8388 continue;
8389 if (ssid->mode != WPAS_MODE_P2P_GO)
8390 continue;
8391 if (wpa_s->ap_iface == NULL)
8392 continue;
8393 break;
8394 }
8395 if (wpa_s == NULL) {
8396 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8397 return -1;
8398 }
8399
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008400 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008401 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008402 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008403 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8404 return -1;
8405 }
8406 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008407 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8408 wpa_s->p2pdev->p2p_oob_dev_pw,
8409 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8410 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008411 if (res)
8412 return res;
8413
8414 if (!tag) {
8415 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8416 return 0;
8417 }
8418
8419 if (!params->peer ||
8420 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8421 return 0;
8422
8423 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8424 " to join", MAC2STR(params->peer->p2p_device_addr));
8425
8426 wpa_s->global->p2p_invite_group = wpa_s;
8427 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008428 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008429 params->peer->p2p_device_addr,
8430 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008431 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008432
8433 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8434 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8435 ssid->ssid, ssid->ssid_len, ssid->frequency,
8436 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008437 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008438}
8439
8440
8441static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8442 struct p2p_nfc_params *params,
8443 int forced_freq)
8444{
8445 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8446 "connection handover");
8447 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8448 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008449 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008450 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8451 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008452}
8453
8454
8455static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8456 struct p2p_nfc_params *params,
8457 int forced_freq)
8458{
8459 int res;
8460
8461 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8462 "connection handover");
8463 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8464 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008465 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008466 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8467 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008468 if (res)
8469 return res;
8470
8471 res = wpas_p2p_listen(wpa_s, 60);
8472 if (res) {
8473 p2p_unauthorize(wpa_s->global->p2p,
8474 params->peer->p2p_device_addr);
8475 }
8476
8477 return res;
8478}
8479
8480
8481static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8482 const struct wpabuf *data,
8483 int sel, int tag, int forced_freq)
8484{
8485 const u8 *pos, *end;
8486 u16 len, id;
8487 struct p2p_nfc_params params;
8488 int res;
8489
8490 os_memset(&params, 0, sizeof(params));
8491 params.sel = sel;
8492
8493 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8494
8495 pos = wpabuf_head(data);
8496 end = pos + wpabuf_len(data);
8497
8498 if (end - pos < 2) {
8499 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8500 "attributes");
8501 return -1;
8502 }
8503 len = WPA_GET_BE16(pos);
8504 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008505 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008506 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8507 "attributes");
8508 return -1;
8509 }
8510 params.wsc_attr = pos;
8511 params.wsc_len = len;
8512 pos += len;
8513
8514 if (end - pos < 2) {
8515 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8516 "attributes");
8517 return -1;
8518 }
8519 len = WPA_GET_BE16(pos);
8520 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008521 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008522 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8523 "attributes");
8524 return -1;
8525 }
8526 params.p2p_attr = pos;
8527 params.p2p_len = len;
8528 pos += len;
8529
8530 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8531 params.wsc_attr, params.wsc_len);
8532 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8533 params.p2p_attr, params.p2p_len);
8534 if (pos < end) {
8535 wpa_hexdump(MSG_DEBUG,
8536 "P2P: Ignored extra data after P2P attributes",
8537 pos, end - pos);
8538 }
8539
8540 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8541 if (res)
8542 return res;
8543
8544 if (params.next_step == NO_ACTION)
8545 return 0;
8546
8547 if (params.next_step == BOTH_GO) {
8548 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8549 MAC2STR(params.peer->p2p_device_addr));
8550 return 0;
8551 }
8552
8553 if (params.next_step == PEER_CLIENT) {
8554 if (!is_zero_ether_addr(params.go_dev_addr)) {
8555 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8556 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8557 " ssid=\"%s\"",
8558 MAC2STR(params.peer->p2p_device_addr),
8559 params.go_freq,
8560 MAC2STR(params.go_dev_addr),
8561 wpa_ssid_txt(params.go_ssid,
8562 params.go_ssid_len));
8563 } else {
8564 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8565 "peer=" MACSTR " freq=%d",
8566 MAC2STR(params.peer->p2p_device_addr),
8567 params.go_freq);
8568 }
8569 return 0;
8570 }
8571
8572 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8573 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8574 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8575 return 0;
8576 }
8577
8578 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8579 wpa_s->p2p_oob_dev_pw = NULL;
8580
8581 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8582 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8583 "received");
8584 return -1;
8585 }
8586
8587 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8588 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8589 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8590 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8591 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8592 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8593 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8594
8595 if (tag) {
8596 if (id < 0x10) {
8597 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8598 "peer OOB Device Password Id %u", id);
8599 return -1;
8600 }
8601 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8602 "Device Password Id %u", id);
8603 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8604 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8605 params.oob_dev_pw_len -
8606 WPS_OOB_PUBKEY_HASH_LEN - 2);
8607 wpa_s->p2p_oob_dev_pw_id = id;
8608 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8609 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8610 params.oob_dev_pw_len -
8611 WPS_OOB_PUBKEY_HASH_LEN - 2);
8612 if (wpa_s->p2p_oob_dev_pw == NULL)
8613 return -1;
8614
8615 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8616 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8617 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8618 return -1;
8619 } else {
8620 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8621 "without Device Password");
8622 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8623 }
8624
8625 switch (params.next_step) {
8626 case NO_ACTION:
8627 case BOTH_GO:
8628 case PEER_CLIENT:
8629 /* already covered above */
8630 return 0;
8631 case JOIN_GROUP:
8632 return wpas_p2p_nfc_join_group(wpa_s, &params);
8633 case AUTH_JOIN:
8634 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8635 case INIT_GO_NEG:
8636 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8637 case RESP_GO_NEG:
8638 /* TODO: use own OOB Dev Pw */
8639 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8640 }
8641
8642 return -1;
8643}
8644
8645
8646int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8647 const struct wpabuf *data, int forced_freq)
8648{
8649 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8650 return -1;
8651
8652 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8653}
8654
8655
8656int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8657 const struct wpabuf *req,
8658 const struct wpabuf *sel, int forced_freq)
8659{
8660 struct wpabuf *tmp;
8661 int ret;
8662
8663 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8664 return -1;
8665
8666 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8667
8668 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8669 wpabuf_head(req), wpabuf_len(req));
8670 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8671 wpabuf_head(sel), wpabuf_len(sel));
8672 if (forced_freq)
8673 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8674 tmp = ndef_parse_p2p(init ? sel : req);
8675 if (tmp == NULL) {
8676 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8677 return -1;
8678 }
8679
8680 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8681 forced_freq);
8682 wpabuf_free(tmp);
8683
8684 return ret;
8685}
8686
8687
8688int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8689{
8690 const u8 *if_addr;
8691 int go_intent = wpa_s->conf->p2p_go_intent;
8692 struct wpa_supplicant *iface;
8693
8694 if (wpa_s->global->p2p == NULL)
8695 return -1;
8696
8697 if (!enabled) {
8698 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8699 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8700 {
8701 if (!iface->ap_iface)
8702 continue;
8703 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8704 }
8705 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8706 0, NULL);
8707 if (wpa_s->p2p_nfc_tag_enabled)
8708 wpas_p2p_remove_pending_group_interface(wpa_s);
8709 wpa_s->p2p_nfc_tag_enabled = 0;
8710 return 0;
8711 }
8712
8713 if (wpa_s->global->p2p_disabled)
8714 return -1;
8715
8716 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8717 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8718 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8719 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8720 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8721 "to allow static handover cases");
8722 return -1;
8723 }
8724
8725 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8726
8727 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8728 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8729 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8730 if (wpa_s->p2p_oob_dev_pw == NULL)
8731 return -1;
8732 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8733
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008734 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8735 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8736 /*
8737 * P2P Group Interface present and the command came on group
8738 * interface, so enable the token for the current interface.
8739 */
8740 wpa_s->create_p2p_iface = 0;
8741 } else {
8742 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8743 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008744
8745 if (wpa_s->create_p2p_iface) {
8746 enum wpa_driver_if_type iftype;
8747 /* Prepare to add a new interface for the group */
8748 iftype = WPA_IF_P2P_GROUP;
8749 if (go_intent == 15)
8750 iftype = WPA_IF_P2P_GO;
8751 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8752 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8753 "interface for the group");
8754 return -1;
8755 }
8756
8757 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008758 } else if (wpa_s->p2p_mgmt)
8759 if_addr = wpa_s->parent->own_addr;
8760 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008761 if_addr = wpa_s->own_addr;
8762
8763 wpa_s->p2p_nfc_tag_enabled = enabled;
8764
8765 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8766 struct hostapd_data *hapd;
8767 if (iface->ap_iface == NULL)
8768 continue;
8769 hapd = iface->ap_iface->bss[0];
8770 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8771 hapd->conf->wps_nfc_dh_pubkey =
8772 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8773 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8774 hapd->conf->wps_nfc_dh_privkey =
8775 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8776 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8777 hapd->conf->wps_nfc_dev_pw =
8778 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8779 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8780
8781 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8782 wpa_dbg(iface, MSG_DEBUG,
8783 "P2P: Failed to enable NFC Tag for GO");
8784 }
8785 }
8786 p2p_set_authorized_oob_dev_pw_id(
8787 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8788 if_addr);
8789
8790 return 0;
8791}
8792
8793#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008794
8795
8796static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8797 struct wpa_used_freq_data *freqs,
8798 unsigned int num)
8799{
8800 u8 curr_chan, cand, chan;
8801 unsigned int i;
8802
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008803 /*
8804 * If possible, optimize the Listen channel to be a channel that is
8805 * already used by one of the other interfaces.
8806 */
8807 if (!wpa_s->conf->p2p_optimize_listen_chan)
8808 return;
8809
8810 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8811 return;
8812
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008813 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8814 for (i = 0, cand = 0; i < num; i++) {
8815 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8816 if (curr_chan == chan) {
8817 cand = 0;
8818 break;
8819 }
8820
8821 if (chan == 1 || chan == 6 || chan == 11)
8822 cand = chan;
8823 }
8824
8825 if (cand) {
8826 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008827 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008828 cand);
8829 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8830 }
8831}
8832
8833
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008834static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008835{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008836 struct hostapd_config *conf;
8837 struct p2p_go_neg_results params;
8838 struct csa_settings csa_settings;
8839 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8840 int old_freq = current_ssid->frequency;
8841 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008842
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008843 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8844 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8845 return -1;
8846 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008847
8848 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008849 * TODO: This function may not always work correctly. For example,
8850 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008851 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008852 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008853 wpa_dbg(wpa_s, MSG_DEBUG,
8854 "P2P CSA: Failed to select new frequency for GO");
8855 return -1;
8856 }
8857
8858 if (current_ssid->frequency == params.freq) {
8859 wpa_dbg(wpa_s, MSG_DEBUG,
8860 "P2P CSA: Selected same frequency - not moving GO");
8861 return 0;
8862 }
8863
8864 conf = hostapd_config_defaults();
8865 if (!conf) {
8866 wpa_dbg(wpa_s, MSG_DEBUG,
8867 "P2P CSA: Failed to allocate default config");
8868 return -1;
8869 }
8870
8871 current_ssid->frequency = params.freq;
8872 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8873 wpa_dbg(wpa_s, MSG_DEBUG,
8874 "P2P CSA: Failed to create new GO config");
8875 ret = -1;
8876 goto out;
8877 }
8878
8879 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8880 wpa_dbg(wpa_s, MSG_DEBUG,
8881 "P2P CSA: CSA to a different band is not supported");
8882 ret = -1;
8883 goto out;
8884 }
8885
8886 os_memset(&csa_settings, 0, sizeof(csa_settings));
8887 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8888 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8889 csa_settings.freq_params.freq = params.freq;
8890 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8891 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8892 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8893
8894 if (conf->ieee80211ac) {
8895 int freq1 = 0, freq2 = 0;
8896 u8 chan, opclass;
8897
8898 if (ieee80211_freq_to_channel_ext(params.freq,
8899 conf->secondary_channel,
8900 conf->vht_oper_chwidth,
8901 &opclass, &chan) ==
8902 NUM_HOSTAPD_MODES) {
8903 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8904 ret = -1;
8905 goto out;
8906 }
8907
8908 if (conf->vht_oper_centr_freq_seg0_idx)
8909 freq1 = ieee80211_chan_to_freq(
8910 NULL, opclass,
8911 conf->vht_oper_centr_freq_seg0_idx);
8912
8913 if (conf->vht_oper_centr_freq_seg1_idx)
8914 freq2 = ieee80211_chan_to_freq(
8915 NULL, opclass,
8916 conf->vht_oper_centr_freq_seg1_idx);
8917
8918 if (freq1 < 0 || freq2 < 0) {
8919 wpa_dbg(wpa_s, MSG_DEBUG,
8920 "P2P CSA: Selected invalid VHT center freqs");
8921 ret = -1;
8922 goto out;
8923 }
8924
8925 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8926 csa_settings.freq_params.center_freq1 = freq1;
8927 csa_settings.freq_params.center_freq2 = freq2;
8928
8929 switch (conf->vht_oper_chwidth) {
8930 case VHT_CHANWIDTH_80MHZ:
8931 case VHT_CHANWIDTH_80P80MHZ:
8932 csa_settings.freq_params.bandwidth = 80;
8933 break;
8934 case VHT_CHANWIDTH_160MHZ:
8935 csa_settings.freq_params.bandwidth = 160;
8936 break;
8937 }
8938 }
8939
8940 ret = ap_switch_channel(wpa_s, &csa_settings);
8941out:
8942 current_ssid->frequency = old_freq;
8943 hostapd_config_free(conf);
8944 return ret;
8945}
8946
8947
8948static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8949{
8950 struct p2p_go_neg_results params;
8951 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8952
8953 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8954
8955 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8956 current_ssid->frequency);
8957
8958 /* Stop the AP functionality */
8959 /* TODO: Should do this in a way that does not indicated to possible
8960 * P2P Clients in the group that the group is terminated. */
8961 wpa_supplicant_ap_deinit(wpa_s);
8962
8963 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008964 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008965 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8966 wpas_p2p_group_delete(wpa_s,
8967 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8968 return;
8969 }
8970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8971 params.freq);
8972
8973 if (params.freq &&
8974 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8975 wpa_printf(MSG_DEBUG,
8976 "P2P: Selected freq (%u MHz) is not valid for P2P",
8977 params.freq);
8978 wpas_p2p_group_delete(wpa_s,
8979 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8980 return;
8981 }
8982
8983 /* Update the frequency */
8984 current_ssid->frequency = params.freq;
8985 wpa_s->connect_without_scan = current_ssid;
8986 wpa_s->reassociate = 1;
8987 wpa_s->disconnected = 0;
8988 wpa_supplicant_req_scan(wpa_s, 0, 0);
8989}
8990
8991
8992static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8993{
8994 struct wpa_supplicant *wpa_s = eloop_ctx;
8995
8996 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008997 return;
8998
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008999 wpas_p2p_go_update_common_freqs(wpa_s);
9000
9001 /* Do not move GO in the middle of a CSA */
9002 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9003 wpa_printf(MSG_DEBUG,
9004 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009005 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009006 }
9007
9008 /*
9009 * First, try a channel switch flow. If it is not supported or fails,
9010 * take down the GO and bring it up again.
9011 */
9012 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9013 wpas_p2p_move_go_no_csa(wpa_s);
9014}
9015
9016
9017static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9018{
9019 struct wpa_supplicant *wpa_s = eloop_ctx;
9020 struct wpa_used_freq_data *freqs = NULL;
9021 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009022
9023 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9024 if (!freqs)
9025 return;
9026
9027 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9028
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009029 /* Previous attempt to move a GO was not possible -- try again. */
9030 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9031 WPAS_P2P_CHANNEL_UPDATE_ANY);
9032
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009033 os_free(freqs);
9034}
9035
9036
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009037/*
9038 * Consider moving a GO from its currently used frequency:
9039 * 1. It is possible that due to regulatory consideration the frequency
9040 * can no longer be used and there is a need to evacuate the GO.
9041 * 2. It is possible that due to MCC considerations, it would be preferable
9042 * to move the GO to a channel that is currently used by some other
9043 * station interface.
9044 *
9045 * In case a frequency that became invalid is once again valid, cancel a
9046 * previously initiated GO frequency change.
9047 */
9048static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9049 struct wpa_used_freq_data *freqs,
9050 unsigned int num)
9051{
9052 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9053 unsigned int timeout;
9054 int freq;
9055
9056 wpas_p2p_go_update_common_freqs(wpa_s);
9057
9058 freq = wpa_s->current_ssid->frequency;
9059 for (i = 0, invalid_freq = 0; i < num; i++) {
9060 if (freqs[i].freq == freq) {
9061 flags = freqs[i].flags;
9062
9063 /* The channel is invalid, must change it */
9064 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
9065 wpa_dbg(wpa_s, MSG_DEBUG,
9066 "P2P: Freq=%d MHz no longer valid for GO",
9067 freq);
9068 invalid_freq = 1;
9069 }
9070 } else if (freqs[i].flags == 0) {
9071 /* Freq is not used by any other station interface */
9072 continue;
9073 } else if (!p2p_supported_freq(wpa_s->global->p2p,
9074 freqs[i].freq)) {
9075 /* Freq is not valid for P2P use cases */
9076 continue;
9077 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9078 P2P_GO_FREQ_MOVE_SCM) {
9079 policy_move = 1;
9080 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9081 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9082 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9083 policy_move = 1;
9084 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9085 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9086 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9087 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9088 policy_move = 1;
9089 } else if ((wpa_s->drv_flags &
9090 WPA_DRIVER_FLAGS_AP_CSA) &&
9091 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9092 u8 chan;
9093
9094 /*
9095 * We do not support CSA between bands, so move
9096 * GO only within the same band.
9097 */
9098 if (wpa_s->ap_iface->current_mode->mode ==
9099 ieee80211_freq_to_chan(freqs[i].freq,
9100 &chan))
9101 policy_move = 1;
9102 }
9103 }
9104 }
9105
9106 wpa_dbg(wpa_s, MSG_DEBUG,
9107 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9108 invalid_freq, policy_move, flags);
9109
9110 /*
9111 * The channel is valid, or we are going to have a policy move, so
9112 * cancel timeout.
9113 */
9114 if (!invalid_freq || policy_move) {
9115 wpa_dbg(wpa_s, MSG_DEBUG,
9116 "P2P: Cancel a GO move from freq=%d MHz", freq);
9117 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9118
9119 if (wpas_p2p_in_progress(wpa_s)) {
9120 wpa_dbg(wpa_s, MSG_DEBUG,
9121 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9122 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9123 wpa_s, NULL);
9124 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9125 wpas_p2p_reconsider_moving_go,
9126 wpa_s, NULL);
9127 return;
9128 }
9129 }
9130
9131 if (!invalid_freq && (!policy_move || flags != 0)) {
9132 wpa_dbg(wpa_s, MSG_DEBUG,
9133 "P2P: Not initiating a GO frequency change");
9134 return;
9135 }
9136
9137 /*
9138 * Do not consider moving GO if it is in the middle of a CSA. When the
9139 * CSA is finished this flow should be retriggered.
9140 */
9141 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9142 wpa_dbg(wpa_s, MSG_DEBUG,
9143 "P2P: Not initiating a GO frequency change - CSA is in progress");
9144 return;
9145 }
9146
9147 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9148 timeout = P2P_GO_FREQ_CHANGE_TIME;
9149 else
9150 timeout = 0;
9151
9152 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9153 freq, timeout);
9154 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9155 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9156}
9157
9158
9159static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9160 struct wpa_used_freq_data *freqs,
9161 unsigned int num,
9162 enum wpas_p2p_channel_update_trig trig)
9163{
9164 struct wpa_supplicant *ifs;
9165
9166 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9167 NULL);
9168
9169 /*
9170 * Travers all the radio interfaces, and for each GO interface, check
9171 * if there is a need to move the GO from the frequency it is using,
9172 * or in case the frequency is valid again, cancel the evacuation flow.
9173 */
9174 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9175 radio_list) {
9176 if (ifs->current_ssid == NULL ||
9177 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9178 continue;
9179
9180 /*
9181 * The GO was just started or completed channel switch, no need
9182 * to move it.
9183 */
9184 if (wpa_s == ifs &&
9185 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9186 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9187 wpa_dbg(wpa_s, MSG_DEBUG,
9188 "P2P: GO move - schedule re-consideration");
9189 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9190 wpas_p2p_reconsider_moving_go,
9191 wpa_s, NULL);
9192 continue;
9193 }
9194
9195 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9196 }
9197}
9198
9199
9200void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9201{
9202 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9203 return;
9204
9205 wpas_p2p_update_channel_list(wpa_s,
9206 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9207}
9208
9209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009210void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9211{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009212 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9213 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9214 "the management interface is being removed");
9215 wpas_p2p_deinit_global(wpa_s->global);
9216 }
9217}
9218
9219
9220void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9221{
9222 if (wpa_s->ap_iface->bss)
9223 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9224 wpas_p2p_group_deinit(wpa_s);
9225}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009226
9227
9228int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9229 unsigned int period, unsigned int interval,
9230 unsigned int count)
9231{
9232 struct p2p_data *p2p = wpa_s->global->p2p;
9233 u8 *device_types;
9234 size_t dev_types_len;
9235 struct wpabuf *buf;
9236 int ret;
9237
9238 if (wpa_s->p2p_lo_started) {
9239 wpa_dbg(wpa_s, MSG_DEBUG,
9240 "P2P Listen offload is already started");
9241 return 0;
9242 }
9243
9244 if (wpa_s->global->p2p == NULL ||
9245 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9246 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9247 return -1;
9248 }
9249
9250 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9251 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9252 freq);
9253 return -1;
9254 }
9255
9256 /* Get device type */
9257 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9258 WPS_DEV_TYPE_LEN;
9259 device_types = os_malloc(dev_types_len);
9260 if (!device_types)
9261 return -1;
9262 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9263 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9264 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9265
9266 /* Get Probe Response IE(s) */
9267 buf = p2p_build_probe_resp_template(p2p, freq);
9268 if (!buf) {
9269 os_free(device_types);
9270 return -1;
9271 }
9272
9273 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9274 device_types, dev_types_len,
9275 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9276 if (ret < 0)
9277 wpa_dbg(wpa_s, MSG_DEBUG,
9278 "P2P: Failed to start P2P listen offload");
9279
9280 os_free(device_types);
9281 wpabuf_free(buf);
9282
9283 if (ret == 0) {
9284 wpa_s->p2p_lo_started = 1;
9285
9286 /* Stop current P2P listen if any */
9287 wpas_stop_listen(wpa_s);
9288 }
9289
9290 return ret;
9291}
9292
9293
9294int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9295{
9296 int ret;
9297
9298 if (!wpa_s->p2p_lo_started)
9299 return 0;
9300
9301 ret = wpa_drv_p2p_lo_stop(wpa_s);
9302 if (ret < 0)
9303 wpa_dbg(wpa_s, MSG_DEBUG,
9304 "P2P: Failed to stop P2P listen offload");
9305
9306 wpa_s->p2p_lo_started = 0;
9307 return ret;
9308}