blob: 2da92bf460e29cadfc48b019ec03f40743179d2f [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{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 struct wpa_supplicant *wpa_s = ctx;
2294 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002295 char *wfd_dev_info_hex = NULL;
2296
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002297#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002298 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2299 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002300#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002301
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002302 if (info->p2ps_instance) {
2303 char str[256];
2304 const u8 *buf = wpabuf_head(info->p2ps_instance);
2305 size_t len = wpabuf_len(info->p2ps_instance);
2306
2307 while (len) {
2308 u32 id;
2309 u16 methods;
2310 u8 str_len;
2311
2312 if (len < 4 + 2 + 1)
2313 break;
2314 id = WPA_GET_LE32(buf);
2315 buf += sizeof(u32);
2316 methods = WPA_GET_BE16(buf);
2317 buf += sizeof(u16);
2318 str_len = *buf++;
2319 if (str_len > len - 4 - 2 - 1)
2320 break;
2321 os_memcpy(str, buf, str_len);
2322 str[str_len] = '\0';
2323 buf += str_len;
2324 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2325
2326 wpa_msg_global(wpa_s, MSG_INFO,
2327 P2P_EVENT_DEVICE_FOUND MACSTR
2328 " p2p_dev_addr=" MACSTR
2329 " pri_dev_type=%s name='%s'"
2330 " config_methods=0x%x"
2331 " dev_capab=0x%x"
2332 " group_capab=0x%x"
2333 " adv_id=%x asp_svc=%s%s",
2334 MAC2STR(addr),
2335 MAC2STR(info->p2p_device_addr),
2336 wps_dev_type_bin2str(
2337 info->pri_dev_type,
2338 devtype, sizeof(devtype)),
2339 info->device_name, methods,
2340 info->dev_capab, info->group_capab,
2341 id, str,
2342 info->vendor_elems ?
2343 " vendor_elems=1" : "");
2344 }
2345 goto done;
2346 }
2347
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002348 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2349 " p2p_dev_addr=" MACSTR
2350 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002351 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002352 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2353 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2354 sizeof(devtype)),
2355 info->device_name, info->config_methods,
2356 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002357 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002358 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002359 info->vendor_elems ? " vendor_elems=1" : "",
2360 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002362done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002363 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002364#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2367}
2368
2369
2370static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2371{
2372 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002374 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2375 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2378}
2379
2380
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002381static void wpas_find_stopped(void *ctx)
2382{
2383 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002384
2385 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2386 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2387
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002388 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002389 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002390}
2391
2392
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002393struct wpas_p2p_listen_work {
2394 unsigned int freq;
2395 unsigned int duration;
2396 struct wpabuf *probe_resp_ie;
2397};
2398
2399
2400static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2401{
2402 if (lwork == NULL)
2403 return;
2404 wpabuf_free(lwork->probe_resp_ie);
2405 os_free(lwork);
2406}
2407
2408
2409static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2410{
2411 struct wpas_p2p_listen_work *lwork;
2412
2413 if (!wpa_s->p2p_listen_work)
2414 return;
2415
2416 lwork = wpa_s->p2p_listen_work->ctx;
2417 wpas_p2p_listen_work_free(lwork);
2418 radio_work_done(wpa_s->p2p_listen_work);
2419 wpa_s->p2p_listen_work = NULL;
2420}
2421
2422
2423static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2424{
2425 struct wpa_supplicant *wpa_s = work->wpa_s;
2426 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002427 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002428
2429 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002430 if (work->started) {
2431 wpa_s->p2p_listen_work = NULL;
2432 wpas_stop_listen(wpa_s);
2433 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002434 wpas_p2p_listen_work_free(lwork);
2435 return;
2436 }
2437
2438 wpa_s->p2p_listen_work = work;
2439
2440 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2441
2442 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2443 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2444 "report received Probe Request frames");
2445 wpas_p2p_listen_work_done(wpa_s);
2446 return;
2447 }
2448
2449 wpa_s->pending_listen_freq = lwork->freq;
2450 wpa_s->pending_listen_duration = lwork->duration;
2451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002452 duration = lwork->duration;
2453#ifdef CONFIG_TESTING_OPTIONS
2454 if (wpa_s->extra_roc_dur) {
2455 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2456 duration, duration + wpa_s->extra_roc_dur);
2457 duration += wpa_s->extra_roc_dur;
2458 }
2459#endif /* CONFIG_TESTING_OPTIONS */
2460
2461 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002462 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2463 "to remain on channel (%u MHz) for Listen "
2464 "state", lwork->freq);
2465 wpas_p2p_listen_work_done(wpa_s);
2466 wpa_s->pending_listen_freq = 0;
2467 return;
2468 }
2469 wpa_s->off_channel_freq = 0;
2470 wpa_s->roc_waiting_drv_freq = lwork->freq;
2471}
2472
2473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474static int wpas_start_listen(void *ctx, unsigned int freq,
2475 unsigned int duration,
2476 const struct wpabuf *probe_resp_ie)
2477{
2478 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002479 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002481 if (wpa_s->p2p_listen_work) {
2482 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483 return -1;
2484 }
2485
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002486 lwork = os_zalloc(sizeof(*lwork));
2487 if (lwork == NULL)
2488 return -1;
2489 lwork->freq = freq;
2490 lwork->duration = duration;
2491 if (probe_resp_ie) {
2492 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2493 if (lwork->probe_resp_ie == NULL) {
2494 wpas_p2p_listen_work_free(lwork);
2495 return -1;
2496 }
2497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002499 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2500 lwork) < 0) {
2501 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 return -1;
2503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504
2505 return 0;
2506}
2507
2508
2509static void wpas_stop_listen(void *ctx)
2510{
2511 struct wpa_supplicant *wpa_s = ctx;
2512 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2513 wpa_drv_cancel_remain_on_channel(wpa_s);
2514 wpa_s->off_channel_freq = 0;
2515 wpa_s->roc_waiting_drv_freq = 0;
2516 }
2517 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002518
2519 /*
2520 * Don't cancel Probe Request RX reporting for a connected P2P Client
2521 * handling Probe Request frames.
2522 */
2523 if (!wpa_s->p2p_cli_probe)
2524 wpa_drv_probe_req_report(wpa_s, 0);
2525
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002526 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527}
2528
2529
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002530static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2531 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532{
2533 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002534 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2535 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536}
2537
2538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2540 const u8 *peer, const char *params,
2541 unsigned int generated_pin)
2542{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2544 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545}
2546
2547
2548static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2549 const u8 *peer, const char *params)
2550{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002551 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2552 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553}
2554
2555
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002556static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2557 const u8 *dev_addr, const u8 *pri_dev_type,
2558 const char *dev_name, u16 supp_config_methods,
2559 u8 dev_capab, u8 group_capab, const u8 *group_id,
2560 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561{
2562 struct wpa_supplicant *wpa_s = ctx;
2563 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002564 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 u8 empty_dev_type[8];
2566 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002567 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002568 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002569
2570 if (group_id) {
2571 for (group = wpa_s->global->ifaces; group; group = group->next)
2572 {
2573 struct wpa_ssid *s = group->current_ssid;
2574 if (s != NULL &&
2575 s->mode == WPAS_MODE_P2P_GO &&
2576 group_id_len - ETH_ALEN == s->ssid_len &&
2577 os_memcmp(group_id + ETH_ALEN, s->ssid,
2578 s->ssid_len) == 0)
2579 break;
2580 }
2581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582
2583 if (pri_dev_type == NULL) {
2584 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2585 pri_dev_type = empty_dev_type;
2586 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002587 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2588 " pri_dev_type=%s name='%s' config_methods=0x%x "
2589 "dev_capab=0x%x group_capab=0x%x%s%s",
2590 MAC2STR(dev_addr),
2591 wps_dev_type_bin2str(pri_dev_type, devtype,
2592 sizeof(devtype)),
2593 dev_name, supp_config_methods, dev_capab, group_capab,
2594 group ? " group=" : "",
2595 group ? group->ifname : "");
2596 if (os_snprintf_error(sizeof(params), res))
2597 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002601 if (wps_generate_pin(&generated_pin) < 0) {
2602 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2603 wpas_notify_p2p_provision_discovery(
2604 wpa_s, peer, 0 /* response */,
2605 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2606 return;
2607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 wpas_prov_disc_local_display(wpa_s, peer, params,
2609 generated_pin);
2610 } else if (config_methods & WPS_CONFIG_KEYPAD)
2611 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2612 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002613 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2614 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002615
2616 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2617 P2P_PROV_DISC_SUCCESS,
2618 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619}
2620
2621
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002622static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623{
2624 struct wpa_supplicant *wpa_s = ctx;
2625 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002626 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002628 if (wpa_s->pending_pd_before_join &&
2629 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2630 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2631 wpa_s->pending_pd_before_join = 0;
2632 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2633 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002634 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002635 return;
2636 }
2637
Dmitry Shmidt04949592012-07-19 12:16:46 -07002638 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002639 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2640 int res;
2641
2642 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2643 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2644 if (os_snprintf_error(sizeof(params), res))
2645 params[sizeof(params) - 1] = '\0';
2646 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002647 params[0] = '\0';
2648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002650 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002652 if (wps_generate_pin(&generated_pin) < 0) {
2653 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2654 wpas_notify_p2p_provision_discovery(
2655 wpa_s, peer, 0 /* response */,
2656 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2657 return;
2658 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002659 wpas_prov_disc_local_display(wpa_s, peer, params,
2660 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002662 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2663 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664
Jouni Malinen75ecf522011-06-27 15:19:46 -07002665 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2666 P2P_PROV_DISC_SUCCESS,
2667 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668}
2669
2670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002672 enum p2p_prov_disc_status status,
2673 u32 adv_id, const u8 *adv_mac,
2674 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002675{
2676 struct wpa_supplicant *wpa_s = ctx;
2677
Dmitry Shmidt04949592012-07-19 12:16:46 -07002678 if (wpa_s->p2p_fallback_to_go_neg) {
2679 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2680 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002681 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002682 P2P_EVENT_FALLBACK_TO_GO_NEG
2683 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002684 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2685 return;
2686 }
2687
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002688 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002689 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002690 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2691 "join-existing-group operation (no ACK for PD "
2692 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002693 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002694 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002695 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002696
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002697 if (adv_id && adv_mac && deferred_session_resp) {
2698 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2699 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2700 " deferred_session_resp='%s'",
2701 MAC2STR(peer), status, adv_id,
2702 deferred_session_resp);
2703 } else if (adv_id && adv_mac) {
2704 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2705 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2706 MAC2STR(peer), status, adv_id);
2707 } else {
2708 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2709 " p2p_dev_addr=" MACSTR " status=%d",
2710 MAC2STR(peer), status);
2711 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712
Jouni Malinen75ecf522011-06-27 15:19:46 -07002713 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2714 status, 0, 0);
2715}
2716
2717
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002718static int freq_included(struct wpa_supplicant *wpa_s,
2719 const struct p2p_channels *channels,
2720 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002721{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002722 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2723 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2724 return 1;
2725 return 0;
2726}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002728
2729static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2730{
2731 unsigned int num = P2P_MAX_CHANNELS;
2732 int *common_freqs;
2733 int ret;
2734
2735 p2p_go_dump_common_freqs(wpa_s);
2736 common_freqs = os_calloc(num, sizeof(int));
2737 if (!common_freqs)
2738 return;
2739
2740 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2741 if (ret < 0) {
2742 wpa_dbg(wpa_s, MSG_DEBUG,
2743 "P2P: Failed to get group common freqs");
2744 os_free(common_freqs);
2745 return;
2746 }
2747
2748 os_free(wpa_s->p2p_group_common_freqs);
2749 wpa_s->p2p_group_common_freqs = common_freqs;
2750 wpa_s->p2p_group_common_freqs_num = num;
2751 p2p_go_dump_common_freqs(wpa_s);
2752}
2753
2754
2755/*
2756 * Check if the given frequency is one of the possible operating frequencies
2757 * set after the completion of the GO Negotiation.
2758 */
2759static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2760{
2761 unsigned int i;
2762
2763 p2p_go_dump_common_freqs(wpa_s);
2764
2765 /* assume no restrictions */
2766 if (!wpa_s->p2p_group_common_freqs_num)
2767 return 1;
2768
2769 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2770 if (wpa_s->p2p_group_common_freqs[i] == freq)
2771 return 1;
2772 }
2773 return 0;
2774}
2775
2776
2777static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2778 struct sta_info *sta, void *ctx)
2779{
2780 int *ecsa_support = ctx;
2781
2782 *ecsa_support &= sta->ecsa_supported;
2783
2784 return 0;
2785}
2786
2787
2788/* Check if all the peers support eCSA */
2789static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2790{
2791 int ecsa_support = 1;
2792
2793 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2794 &ecsa_support);
2795
2796 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002797}
2798
2799
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002800/**
2801 * Pick the best frequency to use from all the currently used frequencies.
2802 */
2803static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2804 struct wpa_used_freq_data *freqs,
2805 unsigned int num)
2806{
2807 unsigned int i, c;
2808
2809 /* find a candidate freq that is supported by P2P */
2810 for (c = 0; c < num; c++)
2811 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2812 break;
2813
2814 if (c == num)
2815 return 0;
2816
2817 /* once we have a candidate, try to find a 'better' one */
2818 for (i = c + 1; i < num; i++) {
2819 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2820 continue;
2821
2822 /*
2823 * 1. Infrastructure station interfaces have higher preference.
2824 * 2. P2P Clients have higher preference.
2825 * 3. All others.
2826 */
2827 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2828 c = i;
2829 break;
2830 }
2831
2832 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2833 c = i;
2834 }
2835 return freqs[c].freq;
2836}
2837
2838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2840 const u8 *go_dev_addr, const u8 *ssid,
2841 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002842 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002843 const struct p2p_channels *channels,
2844 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845{
2846 struct wpa_supplicant *wpa_s = ctx;
2847 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002848 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002850 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851
2852 if (!persistent_group) {
2853 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002854 " to join an active group (SSID: %s)",
2855 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2857 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2858 == 0 ||
2859 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2860 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2861 "authorized invitation");
2862 goto accept_inv;
2863 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002864
2865#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002866 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2867 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002868 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002869 wpa_s->p2p_wps_method = WPS_NFC;
2870 wpa_s->pending_join_wps_method = WPS_NFC;
2871 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002872 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002873 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002874 bssid, ETH_ALEN);
2875 goto accept_inv;
2876 }
2877#endif /* CONFIG_WPS_NFC */
2878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 /*
2880 * Do not accept the invitation automatically; notify user and
2881 * request approval.
2882 */
2883 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2884 }
2885
2886 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2887 if (grp) {
2888 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2889 "running persistent group");
2890 if (*go)
2891 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2892 goto accept_inv;
2893 }
2894
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002895 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2896 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2897 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2898 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002899 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002900 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2902
2903 for (s = wpa_s->conf->ssid; s; s = s->next) {
2904 if (s->disabled == 2 &&
2905 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2906 s->ssid_len == ssid_len &&
2907 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2908 break;
2909 }
2910
2911 if (!s) {
2912 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2913 " requested reinvocation of an unknown group",
2914 MAC2STR(sa));
2915 return P2P_SC_FAIL_UNKNOWN_GROUP;
2916 }
2917
2918 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2919 *go = 1;
2920 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2921 wpa_printf(MSG_DEBUG, "P2P: The only available "
2922 "interface is already in use - reject "
2923 "invitation");
2924 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2925 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002926 if (wpa_s->p2p_mgmt)
2927 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2928 ETH_ALEN);
2929 else
2930 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 } else if (s->mode == WPAS_MODE_P2P_GO) {
2932 *go = 1;
2933 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2934 {
2935 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2936 "interface address for the group");
2937 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2938 }
2939 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2940 ETH_ALEN);
2941 }
2942
2943accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002944 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2945
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002946 best_freq = 0;
2947 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2948 sizeof(struct wpa_used_freq_data));
2949 if (freqs) {
2950 int num_channels = wpa_s->num_multichan_concurrent;
2951 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2952 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2953 os_free(freqs);
2954 }
2955
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002956 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002957 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002958 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002959 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002960
2961 if (wpa_s->num_multichan_concurrent < 2 ||
2962 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2963 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 -07002964 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 }
2967
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002968 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2969 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002970 if (*go == 0) {
2971 /* We are the client */
2972 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2973 "running a GO but we are capable of MCC, "
2974 "figure out the best channel to use");
2975 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002976 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002977 /* We are the GO, and *force_freq is not in the
2978 * intersection */
2979 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2980 "in intersection but we are capable of MCC, "
2981 "figure out the best channel to use",
2982 *force_freq);
2983 *force_freq = 0;
2984 }
2985 }
2986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 return P2P_SC_SUCCESS;
2988}
2989
2990
2991static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2992 const u8 *ssid, size_t ssid_len,
2993 const u8 *go_dev_addr, u8 status,
2994 int op_freq)
2995{
2996 struct wpa_supplicant *wpa_s = ctx;
2997 struct wpa_ssid *s;
2998
2999 for (s = wpa_s->conf->ssid; s; s = s->next) {
3000 if (s->disabled == 2 &&
3001 s->ssid_len == ssid_len &&
3002 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3003 break;
3004 }
3005
3006 if (status == P2P_SC_SUCCESS) {
3007 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003008 " was accepted; op_freq=%d MHz, SSID=%s",
3009 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003011 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003012 if (go) {
3013 wpa_msg_global(wpa_s, MSG_INFO,
3014 P2P_EVENT_INVITATION_ACCEPTED
3015 "sa=" MACSTR
3016 " persistent=%d freq=%d",
3017 MAC2STR(sa), s->id, op_freq);
3018 } else {
3019 wpa_msg_global(wpa_s, MSG_INFO,
3020 P2P_EVENT_INVITATION_ACCEPTED
3021 "sa=" MACSTR
3022 " persistent=%d",
3023 MAC2STR(sa), s->id);
3024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003026 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003027 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3028 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003030 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003031 wpa_msg_global(wpa_s, MSG_INFO,
3032 P2P_EVENT_INVITATION_ACCEPTED
3033 "sa=" MACSTR " go_dev_addr=" MACSTR
3034 " bssid=" MACSTR " unknown-network",
3035 MAC2STR(sa), MAC2STR(go_dev_addr),
3036 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003038 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003039 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 }
3041 return;
3042 }
3043
3044 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3045 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3046 " was rejected (status %u)", MAC2STR(sa), status);
3047 return;
3048 }
3049
3050 if (!s) {
3051 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003052 wpa_msg_global(wpa_s, MSG_INFO,
3053 P2P_EVENT_INVITATION_RECEIVED
3054 "sa=" MACSTR " go_dev_addr=" MACSTR
3055 " bssid=" MACSTR " unknown-network",
3056 MAC2STR(sa), MAC2STR(go_dev_addr),
3057 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003059 wpa_msg_global(wpa_s, MSG_INFO,
3060 P2P_EVENT_INVITATION_RECEIVED
3061 "sa=" MACSTR " go_dev_addr=" MACSTR
3062 " unknown-network",
3063 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003065 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3066 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 return;
3068 }
3069
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003070 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003071 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3072 "sa=" MACSTR " persistent=%d freq=%d",
3073 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003074 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003075 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3076 "sa=" MACSTR " persistent=%d",
3077 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003078 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003079 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3080 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081}
3082
3083
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003084static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3085 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003086 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003087{
3088 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003089 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003090
3091 if (ssid == NULL)
3092 return;
3093
3094 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003095 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003096 ETH_ALEN) == 0)
3097 break;
3098 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003099 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003100 if (ssid->mode != WPAS_MODE_P2P_GO &&
3101 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3102 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3103 "due to invitation result", ssid->id);
3104 wpas_notify_network_removed(wpa_s, ssid);
3105 wpa_config_remove_network(wpa_s->conf, ssid->id);
3106 return;
3107 }
3108 return; /* Peer not found in client list */
3109 }
3110
3111 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003112 "group %d client list%s",
3113 MAC2STR(peer), ssid->id,
3114 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003115 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3116 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3117 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003118 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003119 if (p2p_wpa_s->conf->update_config &&
3120 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003121 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003122}
3123
3124
3125static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3126 const u8 *peer)
3127{
3128 struct wpa_ssid *ssid;
3129
3130 wpa_s = wpa_s->global->p2p_invite_group;
3131 if (wpa_s == NULL)
3132 return; /* No known invitation group */
3133 ssid = wpa_s->current_ssid;
3134 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3135 !ssid->p2p_persistent_group)
3136 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003137 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003138 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003139 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003140}
3141
3142
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003143static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003144 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003145 const u8 *peer, int neg_freq,
3146 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147{
3148 struct wpa_supplicant *wpa_s = ctx;
3149 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003150 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151
3152 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003153 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3154 "status=%d " MACSTR,
3155 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003157 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3158 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003159 }
3160 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3161
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003162 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3163 status, MAC2STR(peer));
3164 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003165 struct wpa_supplicant *group_if =
3166 wpa_s->global->p2p_invite_group;
3167
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003168 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3169 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003170
3171 /*
3172 * Invitation to an active group. If this is successful and we
3173 * are the GO, set the client wait to postpone some concurrent
3174 * operations and to allow provisioning and connection to happen
3175 * more quickly.
3176 */
3177 if (status == P2P_SC_SUCCESS &&
3178 group_if && group_if->current_ssid &&
3179 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3180 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3181#ifdef CONFIG_TESTING_OPTIONS
3182 if (group_if->p2p_go_csa_on_inv) {
3183 wpa_printf(MSG_DEBUG,
3184 "Testing: force P2P GO CSA after invitation");
3185 eloop_cancel_timeout(
3186 wpas_p2p_reconsider_moving_go,
3187 wpa_s, NULL);
3188 eloop_register_timeout(
3189 0, 50000,
3190 wpas_p2p_reconsider_moving_go,
3191 wpa_s, NULL);
3192 }
3193#endif /* CONFIG_TESTING_OPTIONS */
3194 }
3195 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003198 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3199 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3200 "invitation exchange to indicate readiness for "
3201 "re-invocation");
3202 }
3203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003205 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3206 ssid = wpa_config_get_network(
3207 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003208 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 wpas_p2p_remove_pending_group_interface(wpa_s);
3211 return;
3212 }
3213
3214 ssid = wpa_config_get_network(wpa_s->conf,
3215 wpa_s->pending_invite_ssid_id);
3216 if (ssid == NULL) {
3217 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3218 "data matching with invitation");
3219 return;
3220 }
3221
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003222 /*
3223 * The peer could have missed our ctrl::ack frame for Invitation
3224 * Response and continue retransmitting the frame. To reduce the
3225 * likelihood of the peer not getting successful TX status for the
3226 * Invitation Response frame, wait a short time here before starting
3227 * the persistent group so that we will remain on the current channel to
3228 * acknowledge any possible retransmission from the peer.
3229 */
3230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3231 "starting persistent group");
3232 os_sleep(0, 50000);
3233
Dmitry Shmidt15907092014-03-25 10:42:57 -07003234 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003235 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003236 freq = neg_freq;
3237 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003238 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003239 freq = peer_oper_freq;
3240 else
3241 freq = 0;
3242
3243 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3244 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003246 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003247 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003248 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003249 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003250 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003251 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003252 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003253 ssid->mode == WPAS_MODE_P2P_GO ?
3254 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003255 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256}
3257
3258
Dmitry Shmidt04949592012-07-19 12:16:46 -07003259static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3260 unsigned int freq)
3261{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003262 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3263 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003264 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003265}
3266
3267
3268static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3269{
3270 reg->channel[reg->channels] = chan;
3271 reg->channels++;
3272}
3273
3274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003276 struct p2p_channels *chan,
3277 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278{
3279 int i, cla = 0;
3280
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003281 wpa_s->global->p2p_24ghz_social_channels = 1;
3282
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003283 os_memset(cli_chan, 0, sizeof(*cli_chan));
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3286 "band");
3287
3288 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3289 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003290 chan->reg_class[cla].channels = 0;
3291 for (i = 0; i < 11; i++) {
3292 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3293 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3294 }
3295 if (chan->reg_class[cla].channels)
3296 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297
3298 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3299 "band");
3300
3301 /* Operating class 115 - 5 GHz, channels 36-48 */
3302 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303 chan->reg_class[cla].channels = 0;
3304 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3305 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3306 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3307 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3308 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3309 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3310 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3311 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3312 if (chan->reg_class[cla].channels)
3313 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314
3315 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3316 "band");
3317
3318 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3319 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003320 chan->reg_class[cla].channels = 0;
3321 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3322 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3323 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3324 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3325 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3326 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3327 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3328 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3329 if (chan->reg_class[cla].channels)
3330 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331
3332 chan->reg_classes = cla;
3333 return 0;
3334}
3335
3336
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003337enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003338 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003339};
3340
Dmitry Shmidt04949592012-07-19 12:16:46 -07003341static int has_channel(struct wpa_global *global,
3342 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003343{
3344 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003345 unsigned int freq;
3346
3347 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3348 chan * 5;
3349 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003350 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351
3352 for (i = 0; i < mode->num_channels; i++) {
3353 if (mode->channels[i].chan == chan) {
3354 if (flags)
3355 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003356 if (mode->channels[i].flag &
3357 (HOSTAPD_CHAN_DISABLED |
3358 HOSTAPD_CHAN_RADAR))
3359 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003360 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3361 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003362 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 }
3364 }
3365
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003366 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003367}
3368
3369
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003370static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3371 struct hostapd_hw_modes *mode,
3372 u8 channel)
3373{
3374 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003375 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003376
3377 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3378 return 0;
3379
3380 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3381 /*
3382 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3383 * so the center channel is 6 channels away from the start/end.
3384 */
3385 if (channel >= center_channels[i] - 6 &&
3386 channel <= center_channels[i] + 6)
3387 return center_channels[i];
3388
3389 return 0;
3390}
3391
3392
3393static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3394 struct hostapd_hw_modes *mode,
3395 u8 channel, u8 bw)
3396{
3397 u8 center_chan;
3398 int i, flags;
3399 enum chan_allowed res, ret = ALLOWED;
3400
3401 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3402 if (!center_chan)
3403 return NOT_ALLOWED;
3404 if (center_chan >= 58 && center_chan <= 138)
3405 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3406
3407 /* check all the channels are available */
3408 for (i = 0; i < 4; i++) {
3409 int adj_chan = center_chan - 6 + i * 4;
3410
3411 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3412 if (res == NOT_ALLOWED)
3413 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003414 if (res == NO_IR)
3415 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003416
3417 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3418 return NOT_ALLOWED;
3419 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3420 return NOT_ALLOWED;
3421 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3422 return NOT_ALLOWED;
3423 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3424 return NOT_ALLOWED;
3425 }
3426
3427 return ret;
3428}
3429
3430
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003431static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3432 struct hostapd_hw_modes *mode,
3433 u8 channel)
3434{
3435 u8 center_channels[] = { 50, 114 };
3436 unsigned int i;
3437
3438 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3439 return 0;
3440
3441 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3442 /*
3443 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3444 * so the center channel is 14 channels away from the start/end.
3445 */
3446 if (channel >= center_channels[i] - 14 &&
3447 channel <= center_channels[i] + 14)
3448 return center_channels[i];
3449
3450 return 0;
3451}
3452
3453
3454static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3455 struct hostapd_hw_modes *mode,
3456 u8 channel, u8 bw)
3457{
3458 u8 center_chan;
3459 int i, flags;
3460 enum chan_allowed res, ret = ALLOWED;
3461
3462 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3463 if (!center_chan)
3464 return NOT_ALLOWED;
3465 /* VHT 160 MHz uses DFS channels in most countries. */
3466
3467 /* Check all the channels are available */
3468 for (i = 0; i < 8; i++) {
3469 int adj_chan = center_chan - 14 + i * 4;
3470
3471 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3472 if (res == NOT_ALLOWED)
3473 return NOT_ALLOWED;
3474
3475 if (res == NO_IR)
3476 ret = NO_IR;
3477
3478 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3479 return NOT_ALLOWED;
3480 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3481 return NOT_ALLOWED;
3482 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3483 return NOT_ALLOWED;
3484 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3485 return NOT_ALLOWED;
3486 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3487 return NOT_ALLOWED;
3488 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3489 return NOT_ALLOWED;
3490 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3491 return NOT_ALLOWED;
3492 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3493 return NOT_ALLOWED;
3494 }
3495
3496 return ret;
3497}
3498
3499
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003500static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3501 struct hostapd_hw_modes *mode,
3502 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003503{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003504 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003505 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003506
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003507 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3508 if (bw == BW40MINUS) {
3509 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3510 return NOT_ALLOWED;
3511 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3512 } else if (bw == BW40PLUS) {
3513 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3514 return NOT_ALLOWED;
3515 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3516 } else if (bw == BW80) {
3517 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003518 } else if (bw == BW160) {
3519 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 }
3521
3522 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3523 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003524 if (res == NO_IR || res2 == NO_IR)
3525 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003526 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003527}
3528
3529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003531 struct p2p_channels *chan,
3532 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003534 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003535 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003536
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003537 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3539 "of all supported channels; assume dualband "
3540 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003541 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 }
3543
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003544 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003546 for (op = 0; global_op_class[op].op_class; op++) {
3547 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003549 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003551 if (o->p2p == NO_P2P_SUPP)
3552 continue;
3553
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003554 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555 if (mode == NULL)
3556 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003557 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3558 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003559 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003560 enum chan_allowed res;
3561 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3562 if (res == ALLOWED) {
3563 if (reg == NULL) {
3564 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3565 o->op_class);
3566 reg = &chan->reg_class[cla];
3567 cla++;
3568 reg->reg_class = o->op_class;
3569 }
3570 reg->channel[reg->channels] = ch;
3571 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003572 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003573 wpa_s->conf->p2p_add_cli_chan) {
3574 if (cli_reg == NULL) {
3575 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3576 o->op_class);
3577 cli_reg = &cli_chan->reg_class[cli_cla];
3578 cli_cla++;
3579 cli_reg->reg_class = o->op_class;
3580 }
3581 cli_reg->channel[cli_reg->channels] = ch;
3582 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 }
3585 if (reg) {
3586 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3587 reg->channel, reg->channels);
3588 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003589 if (cli_reg) {
3590 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3591 cli_reg->channel, cli_reg->channels);
3592 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 }
3594
3595 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003596 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598 return 0;
3599}
3600
3601
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003602int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3603 struct hostapd_hw_modes *mode, u8 channel)
3604{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003605 int op;
3606 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003607
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003608 for (op = 0; global_op_class[op].op_class; op++) {
3609 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003610 u8 ch;
3611
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003612 if (o->p2p == NO_P2P_SUPP)
3613 continue;
3614
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003615 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3616 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003617 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3618 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003619 continue;
3620 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003621 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003622 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003623 }
3624 }
3625 return 0;
3626}
3627
3628
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003629int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3630 struct hostapd_hw_modes *mode, u8 channel)
3631{
3632 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3633 return 0;
3634
3635 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3636}
3637
3638
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003639int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3640 struct hostapd_hw_modes *mode, u8 channel)
3641{
3642 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3643 return 0;
3644 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3645}
3646
3647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3649 size_t buf_len)
3650{
3651 struct wpa_supplicant *wpa_s = ctx;
3652
3653 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3654 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3655 break;
3656 }
3657 if (wpa_s == NULL)
3658 return -1;
3659
3660 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3661}
3662
3663
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003664struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3665 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003666{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003667 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3668 struct wpa_ssid *s = wpa_s->current_ssid;
3669 if (s == NULL)
3670 continue;
3671 if (s->mode != WPAS_MODE_P2P_GO &&
3672 s->mode != WPAS_MODE_AP &&
3673 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3674 continue;
3675 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003676 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003677 continue;
3678 return wpa_s;
3679 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003680
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003681 return NULL;
3682
3683}
3684
3685
3686struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3687 const u8 *peer_dev_addr)
3688{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003689 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3690 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003691 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003692 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003693 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3694 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003695 }
3696
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003697 return NULL;
3698}
3699
3700
3701static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3702{
3703 struct wpa_supplicant *wpa_s = ctx;
3704
3705 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003706}
3707
3708
Dmitry Shmidt18463232014-01-24 12:29:41 -08003709static int wpas_is_concurrent_session_active(void *ctx)
3710{
3711 struct wpa_supplicant *wpa_s = ctx;
3712 struct wpa_supplicant *ifs;
3713
3714 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3715 if (ifs == wpa_s)
3716 continue;
3717 if (ifs->wpa_state > WPA_ASSOCIATED)
3718 return 1;
3719 }
3720 return 0;
3721}
3722
3723
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003724static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3725{
3726 struct wpa_supplicant *wpa_s = ctx;
3727 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3728}
3729
3730
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003731int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3732 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003733{
3734 struct wpa_interface iface;
3735 struct wpa_supplicant *p2pdev_wpa_s;
3736 char ifname[100];
3737 char force_name[100];
3738 int ret;
3739
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003740 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3741 wpa_s->ifname);
3742 if (os_snprintf_error(sizeof(ifname), ret))
3743 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003744 force_name[0] = '\0';
3745 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3746 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3747 force_name, wpa_s->pending_interface_addr, NULL);
3748 if (ret < 0) {
3749 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3750 return ret;
3751 }
3752 os_strlcpy(wpa_s->pending_interface_name, ifname,
3753 sizeof(wpa_s->pending_interface_name));
3754
3755 os_memset(&iface, 0, sizeof(iface));
3756 iface.p2p_mgmt = 1;
3757 iface.ifname = wpa_s->pending_interface_name;
3758 iface.driver = wpa_s->driver->name;
3759 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003760
3761 /*
3762 * If a P2P Device configuration file was given, use it as the interface
3763 * configuration file (instead of using parent's configuration file.
3764 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003765 if (conf_p2p_dev) {
3766 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003767 iface.ctrl_interface = NULL;
3768 } else {
3769 iface.confname = wpa_s->confname;
3770 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3771 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003772
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003773 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003774 if (!p2pdev_wpa_s) {
3775 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3776 return -1;
3777 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003778
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003779 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003780 wpa_s->pending_interface_name[0] = '\0';
3781 return 0;
3782}
3783
3784
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003785static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3786 const u8 *noa, size_t noa_len)
3787{
3788 struct wpa_supplicant *wpa_s, *intf = ctx;
3789 char hex[100];
3790
3791 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3792 if (wpa_s->waiting_presence_resp)
3793 break;
3794 }
3795 if (!wpa_s) {
3796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3797 return;
3798 }
3799 wpa_s->waiting_presence_resp = 0;
3800
3801 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3802 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3803 " status=%u noa=%s", MAC2STR(src), status, hex);
3804}
3805
3806
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003807static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3808 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003809 u8 *ret_ssid, size_t *ret_ssid_len,
3810 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003811{
3812 struct wpa_supplicant *wpa_s = ctx;
3813 struct wpa_ssid *s;
3814
3815 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3816 if (s) {
3817 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3818 *ret_ssid_len = s->ssid_len;
3819 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003820
3821 if (s->mode != WPAS_MODE_P2P_GO) {
3822 os_memset(intended_iface_addr, 0, ETH_ALEN);
3823 } else if (wpas_p2p_create_iface(wpa_s)) {
3824 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3825 return 0;
3826
3827 os_memcpy(intended_iface_addr,
3828 wpa_s->pending_interface_addr, ETH_ALEN);
3829 } else {
3830 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3831 ETH_ALEN);
3832 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003833 return 1;
3834 }
3835
3836 return 0;
3837}
3838
3839
3840static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003841 u8 *ssid, size_t *ssid_len, int *group_iface,
3842 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003843{
3844 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003845 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003846 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003847
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003848 /*
3849 * group_iface will be set to 1 only if a dedicated interface for P2P
3850 * role is required. First, we try to reuse an active GO. However,
3851 * if it is not present, we will try to reactivate an existing
3852 * persistent group and set group_iface to 1, so the caller will know
3853 * that the pending interface should be used.
3854 */
3855 *group_iface = 0;
3856
3857 if (freq)
3858 *freq = 0;
3859
3860 go = wpas_p2p_get_go_group(wpa_s);
3861 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003862 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003863 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003864 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003865 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3866 else
3867 return 0;
3868 } else {
3869 s = go->current_ssid;
3870 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3871 if (freq)
3872 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003873 }
3874
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003875 os_memcpy(ssid, s->ssid, s->ssid_len);
3876 *ssid_len = s->ssid_len;
3877
3878 return 1;
3879}
3880
3881
3882static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3883 const u8 *ssid, size_t ssid_len)
3884{
3885 struct wpa_supplicant *wpa_s = ctx;
3886 struct wpa_ssid *s;
3887 int save_config = 0;
3888 size_t i;
3889
3890 /* Start with our first choice of Persistent Groups */
3891 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3892 if (go && ssid && ssid_len &&
3893 s->ssid_len == ssid_len &&
3894 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3895 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3896 break;
3897
3898 /* Remove stale persistent group */
3899 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3900 wpa_config_remove_network(wpa_s->conf, s->id);
3901 save_config = 1;
3902 continue;
3903 }
3904
3905 for (i = 0; i < s->num_p2p_clients; i++) {
3906 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3907 peer, ETH_ALEN) != 0)
3908 continue;
3909
3910 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3911 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3912 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3913 break;
3914 }
3915 s->num_p2p_clients--;
3916 save_config = 1;
3917 }
3918
3919 if (save_config)
3920 p2p_config_write(wpa_s);
3921
3922 /* Return TRUE if valid SSID remains */
3923 return s != NULL;
3924}
3925
3926
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003927static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3928 const u8 *feat_cap, size_t feat_cap_len)
3929{
3930 static const char pref[] = " feature_cap=";
3931 int ret;
3932
3933 buf[0] = '\0';
3934
3935 /*
3936 * We expect a feature capability to contain at least one byte to be
3937 * reported. The string buffer provided by the caller function is
3938 * expected to be big enough to contain all bytes of the attribute for
3939 * known specifications. This function truncates the reported bytes if
3940 * the feature capability data exceeds the string buffer size.
3941 */
3942 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3943 return;
3944
3945 os_memcpy(buf, pref, sizeof(pref));
3946 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3947 buf_len - sizeof(pref) + 1,
3948 feat_cap, feat_cap_len);
3949
3950 if (ret != (2 * (int) feat_cap_len))
3951 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3952}
3953
3954
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003955static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3956 const u8 *adv_mac, const u8 *ses_mac,
3957 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3958 u8 conncap, int passwd_id,
3959 const u8 *persist_ssid,
3960 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003961 int prov_start, const char *session_info,
3962 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003963 unsigned int freq,
3964 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003965{
3966 struct wpa_supplicant *wpa_s = ctx;
3967 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003968 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003969 int save_config = 0;
3970 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003971 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003972
3973 if (!dev)
3974 return;
3975
3976 os_memset(mac, 0, ETH_ALEN);
3977 if (!adv_mac)
3978 adv_mac = mac;
3979 if (!ses_mac)
3980 ses_mac = mac;
3981 if (!grp_mac)
3982 grp_mac = mac;
3983
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003984 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3985 feat_cap, feat_cap_len);
3986
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003987 if (prov_start) {
3988 if (session_info == NULL) {
3989 wpa_msg_global(wpa_s, MSG_INFO,
3990 P2P_EVENT_P2PS_PROVISION_START MACSTR
3991 " adv_id=%x conncap=%x"
3992 " adv_mac=" MACSTR
3993 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003994 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003995 MAC2STR(dev), adv_id, conncap,
3996 MAC2STR(adv_mac),
3997 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003998 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003999 } else {
4000 wpa_msg_global(wpa_s, MSG_INFO,
4001 P2P_EVENT_P2PS_PROVISION_START MACSTR
4002 " adv_id=%x conncap=%x"
4003 " adv_mac=" MACSTR
4004 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004005 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004006 MAC2STR(dev), adv_id, conncap,
4007 MAC2STR(adv_mac),
4008 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004009 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004010 }
4011 return;
4012 }
4013
4014 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4015 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4016
4017 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4018 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4019 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4020
4021 if (persistent_go && !persistent_go->num_p2p_clients) {
4022 /* remove empty persistent GO */
4023 wpa_config_remove_network(wpa_s->conf,
4024 persistent_go->id);
4025 }
4026
4027 wpa_msg_global(wpa_s, MSG_INFO,
4028 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4029 " status=%d"
4030 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004031 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004032 MAC2STR(dev), status,
4033 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004034 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004035 return;
4036 }
4037
4038 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004039 if (persist_ssid && persist_ssid_size)
4040 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4041 persist_ssid_size);
4042
4043 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4044 is_zero_ether_addr(grp_mac)) {
4045 wpa_dbg(wpa_s, MSG_ERROR,
4046 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4047 return;
4048 }
4049
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004050 for (;;) {
4051 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4052 if (!stale)
4053 break;
4054
4055 if (s && s->ssid_len == stale->ssid_len &&
4056 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4057 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4058 break;
4059
4060 /* Remove stale persistent group */
4061 if (stale->mode != WPAS_MODE_P2P_GO ||
4062 stale->num_p2p_clients <= 1) {
4063 wpa_config_remove_network(wpa_s->conf, stale->id);
4064 } else {
4065 size_t i;
4066
4067 for (i = 0; i < stale->num_p2p_clients; i++) {
4068 if (os_memcmp(stale->p2p_client_list +
4069 i * ETH_ALEN,
4070 dev, ETH_ALEN) == 0) {
4071 os_memmove(stale->p2p_client_list +
4072 i * ETH_ALEN,
4073 stale->p2p_client_list +
4074 (i + 1) * ETH_ALEN,
4075 (stale->num_p2p_clients -
4076 i - 1) * ETH_ALEN);
4077 break;
4078 }
4079 }
4080 stale->num_p2p_clients--;
4081 }
4082 save_config = 1;
4083 }
4084
4085 if (save_config)
4086 p2p_config_write(wpa_s);
4087
4088 if (s) {
4089 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4090 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4091
4092 if (persistent_go && s != persistent_go &&
4093 !persistent_go->num_p2p_clients) {
4094 /* remove empty persistent GO */
4095 wpa_config_remove_network(wpa_s->conf,
4096 persistent_go->id);
4097 /* Save config */
4098 }
4099
4100 wpa_msg_global(wpa_s, MSG_INFO,
4101 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4102 " status=%d"
4103 " adv_id=%x adv_mac=" MACSTR
4104 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004105 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004106 MAC2STR(dev), status,
4107 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004108 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004109 return;
4110 }
4111
4112 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004113 /*
4114 * We need to copy the interface name. Simply saving a
4115 * pointer isn't enough, since if we use pending_interface_name
4116 * it will be overwritten when the group is added.
4117 */
4118 char go_ifname[100];
4119
4120 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004121 if (!go_wpa_s) {
4122 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004123 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004124
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004125 if (!wpas_p2p_create_iface(wpa_s))
4126 os_memcpy(go_ifname, wpa_s->ifname,
4127 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004128 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129 os_memcpy(go_ifname,
4130 wpa_s->pending_interface_name,
4131 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004132
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004133 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004134 wpas_p2ps_prov_complete(
4135 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4136 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004137 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004138 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4139 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004140 return;
4141 }
4142
4143 /* If PD Resp complete, start up the GO */
4144 if (response_done && persistent_go) {
4145 wpas_p2p_group_add_persistent(
4146 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004147 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004148 persistent_go->mode ==
4149 WPAS_MODE_P2P_GO ?
4150 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004151 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004152 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004153 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004154 }
4155
4156 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004157 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4158 ETH_ALEN);
4159 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004160 }
4161 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004162 os_memcpy(go_ifname, go_wpa_s->ifname,
4163 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004164
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004165 if (is_zero_ether_addr(grp_mac)) {
4166 wpa_dbg(go_wpa_s, MSG_DEBUG,
4167 "P2P: Setting PIN-1 for ANY");
4168 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4169 "12345670", NULL, 0,
4170 0);
4171 } else {
4172 wpa_dbg(go_wpa_s, MSG_DEBUG,
4173 "P2P: Setting PIN-1 for " MACSTR,
4174 MAC2STR(grp_mac));
4175 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4176 "12345670", NULL, 0,
4177 0);
4178 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004179
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004180 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4181 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004182 }
4183
4184 wpa_msg_global(wpa_s, MSG_INFO,
4185 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4186 " status=%d conncap=%x"
4187 " adv_id=%x adv_mac=" MACSTR
4188 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004189 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004190 MAC2STR(dev), status, conncap,
4191 adv_id, MAC2STR(adv_mac),
4192 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004193 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004194 return;
4195 }
4196
4197 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4198 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4199
4200 if (persistent_go && !persistent_go->num_p2p_clients) {
4201 /* remove empty persistent GO */
4202 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4203 }
4204
4205 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004206 char ssid_hex[32 * 2 + 1];
4207
4208 if (group_ssid)
4209 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4210 group_ssid, group_ssid_len);
4211 else
4212 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004213 wpa_msg_global(wpa_s, MSG_INFO,
4214 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4215 " status=%d conncap=%x"
4216 " adv_id=%x adv_mac=" MACSTR
4217 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004218 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004219 MAC2STR(dev), status, conncap,
4220 adv_id, MAC2STR(adv_mac),
4221 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004222 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4223 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004224 } else {
4225 wpa_msg_global(wpa_s, MSG_INFO,
4226 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4227 " status=%d conncap=%x"
4228 " adv_id=%x adv_mac=" MACSTR
4229 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004230 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004231 MAC2STR(dev), status, conncap,
4232 adv_id, MAC2STR(adv_mac),
4233 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004234 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004235 }
4236}
4237
4238
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004239static int _wpas_p2p_in_progress(void *ctx)
4240{
4241 struct wpa_supplicant *wpa_s = ctx;
4242 return wpas_p2p_in_progress(wpa_s);
4243}
4244
4245
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004246static int wpas_prov_disc_resp_cb(void *ctx)
4247{
4248 struct wpa_supplicant *wpa_s = ctx;
4249 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004250 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004251
4252 if (!wpa_s->global->pending_p2ps_group)
4253 return 0;
4254
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004255 freq = wpa_s->global->pending_p2ps_group_freq;
4256 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004257 wpa_s->global->pending_p2ps_group = 0;
4258
4259 if (wpas_p2p_get_go_group(wpa_s))
4260 return 0;
4261 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4262
4263 if (persistent_go) {
4264 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004265 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004266 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004267 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004268 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004269 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004270 }
4271
4272 return 1;
4273}
4274
4275
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004276static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4277 unsigned int *len,
4278 unsigned int *freq_list)
4279{
4280 struct wpa_supplicant *wpa_s = ctx;
4281
4282 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4283 WPA_IF_P2P_CLIENT, len, freq_list);
4284}
4285
4286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287/**
4288 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4289 * @global: Pointer to global data from wpa_supplicant_init()
4290 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4291 * Returns: 0 on success, -1 on failure
4292 */
4293int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4294{
4295 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296 int i;
4297
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004298 if (wpa_s->conf->p2p_disabled)
4299 return 0;
4300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004301 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4302 return 0;
4303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304 if (global->p2p)
4305 return 0;
4306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004307 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004308 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004309 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004310 p2p.p2p_scan = wpas_p2p_scan;
4311 p2p.send_action = wpas_send_action;
4312 p2p.send_action_done = wpas_send_action_done;
4313 p2p.go_neg_completed = wpas_go_neg_completed;
4314 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4315 p2p.dev_found = wpas_dev_found;
4316 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004317 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 p2p.start_listen = wpas_start_listen;
4319 p2p.stop_listen = wpas_stop_listen;
4320 p2p.send_probe_resp = wpas_send_probe_resp;
4321 p2p.sd_request = wpas_sd_request;
4322 p2p.sd_response = wpas_sd_response;
4323 p2p.prov_disc_req = wpas_prov_disc_req;
4324 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004325 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326 p2p.invitation_process = wpas_invitation_process;
4327 p2p.invitation_received = wpas_invitation_received;
4328 p2p.invitation_result = wpas_invitation_result;
4329 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004330 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004331 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004332 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004333 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004334 p2p.get_persistent_group = wpas_get_persistent_group;
4335 p2p.get_go_info = wpas_get_go_info;
4336 p2p.remove_stale_groups = wpas_remove_stale_groups;
4337 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4338 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4339 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004340 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341
4342 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004343 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 p2p.dev_name = wpa_s->conf->device_name;
4345 p2p.manufacturer = wpa_s->conf->manufacturer;
4346 p2p.model_name = wpa_s->conf->model_name;
4347 p2p.model_number = wpa_s->conf->model_number;
4348 p2p.serial_number = wpa_s->conf->serial_number;
4349 if (wpa_s->wps) {
4350 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4351 p2p.config_methods = wpa_s->wps->config_methods;
4352 }
4353
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004354 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4355 wpa_printf(MSG_ERROR,
4356 "P2P: Failed to configure supported channel list");
4357 return -1;
4358 }
4359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004360 if (wpa_s->conf->p2p_listen_reg_class &&
4361 wpa_s->conf->p2p_listen_channel) {
4362 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4363 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004364 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366 /*
4367 * Pick one of the social channels randomly as the listen
4368 * channel.
4369 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004370 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4371 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004372 wpa_printf(MSG_INFO,
4373 "P2P: No social channels supported by the driver - do not enable P2P");
4374 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004375 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004376 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004377 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004378 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4379 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004380
4381 if (wpa_s->conf->p2p_oper_reg_class &&
4382 wpa_s->conf->p2p_oper_channel) {
4383 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4384 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4385 p2p.cfg_op_channel = 1;
4386 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4387 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4388
4389 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004391 * Use random operation channel from 2.4 GHz band social
4392 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4393 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004395 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4396 &p2p.op_channel) != 0) {
4397 wpa_printf(MSG_ERROR,
4398 "P2P: Failed to select random social channel as operation channel");
4399 return -1;
4400 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004401 p2p.cfg_op_channel = 0;
4402 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4403 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4404 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004405
4406 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4407 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4408 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4409 }
4410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4412 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4413 p2p.country[2] = 0x04;
4414 } else
4415 os_memcpy(p2p.country, "XX\x04", 3);
4416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4418 WPS_DEV_TYPE_LEN);
4419
4420 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4421 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4422 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4423
4424 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4425 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4426
4427 p2p.max_peers = 100;
4428
4429 if (wpa_s->conf->p2p_ssid_postfix) {
4430 p2p.ssid_postfix_len =
4431 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4432 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4433 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4434 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4435 p2p.ssid_postfix_len);
4436 }
4437
4438 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4439
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004440 p2p.max_listen = wpa_s->max_remain_on_chan;
4441
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004442 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4443 wpa_s->conf->p2p_passphrase_len <= 63)
4444 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4445 else
4446 p2p.passphrase_len = 8;
4447
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 global->p2p = p2p_init(&p2p);
4449 if (global->p2p == NULL)
4450 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004451 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452
4453 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4454 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4455 continue;
4456 p2p_add_wps_vendor_extension(
4457 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4458 }
4459
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004460 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 return 0;
4463}
4464
4465
4466/**
4467 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4468 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4469 *
4470 * This function deinitialize per-interface P2P data.
4471 */
4472void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4473{
4474 if (wpa_s->driver && wpa_s->drv_priv)
4475 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004476
4477 if (wpa_s->go_params) {
4478 /* Clear any stored provisioning info */
4479 p2p_clear_provisioning_info(
4480 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004481 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004482 }
4483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484 os_free(wpa_s->go_params);
4485 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004486 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4488 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4489 wpa_s->p2p_long_listen = 0;
4490 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4491 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4492 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004493 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004494 wpas_p2p_listen_work_done(wpa_s);
4495 if (wpa_s->p2p_send_action_work) {
4496 os_free(wpa_s->p2p_send_action_work->ctx);
4497 radio_work_done(wpa_s->p2p_send_action_work);
4498 wpa_s->p2p_send_action_work = NULL;
4499 }
4500 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004501
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004502 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4503 wpa_s->p2p_oob_dev_pw = NULL;
4504
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004505 os_free(wpa_s->p2p_group_common_freqs);
4506 wpa_s->p2p_group_common_freqs = NULL;
4507 wpa_s->p2p_group_common_freqs_num = 0;
4508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004509 /* TODO: remove group interface from the driver if this wpa_s instance
4510 * is on top of a P2P group interface */
4511}
4512
4513
4514/**
4515 * wpas_p2p_deinit_global - Deinitialize global P2P module
4516 * @global: Pointer to global data from wpa_supplicant_init()
4517 *
4518 * This function deinitializes the global (per device) P2P module.
4519 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004520static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004521{
4522 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004524 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004525
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004526 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004527
4528 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4530 wpa_s = wpa_s->next;
4531 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 tmp = global->ifaces;
4533 while (tmp &&
4534 (tmp == wpa_s ||
4535 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4536 tmp = tmp->next;
4537 }
4538 if (tmp == NULL)
4539 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004540 /* Disconnect from the P2P group and deinit the interface */
4541 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 }
4543
4544 /*
4545 * Deinit GO data on any possibly remaining interface (if main
4546 * interface is used as GO).
4547 */
4548 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4549 if (wpa_s->ap_iface)
4550 wpas_p2p_group_deinit(wpa_s);
4551 }
4552
4553 p2p_deinit(global->p2p);
4554 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004555 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556}
4557
4558
4559static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4560{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004561 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004562 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563 if (wpa_s->drv_flags &
4564 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4565 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4566 return 1; /* P2P group requires a new interface in every case
4567 */
4568 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4569 return 0; /* driver does not support concurrent operations */
4570 if (wpa_s->global->ifaces->next)
4571 return 1; /* more that one interface already in use */
4572 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4573 return 1; /* this interface is already in use */
4574 return 0;
4575}
4576
4577
4578static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4579 const u8 *peer_addr,
4580 enum p2p_wps_method wps_method,
4581 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004582 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004583 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004585 if (persistent_group && wpa_s->conf->persistent_reconnect)
4586 persistent_group = 2;
4587
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004588 /*
4589 * Increase GO config timeout if HT40 is used since it takes some time
4590 * to scan channels for coex purposes before the BSS can be started.
4591 */
4592 p2p_set_config_timeout(wpa_s->global->p2p,
4593 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4596 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004597 persistent_group, ssid ? ssid->ssid : NULL,
4598 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004599 wpa_s->p2p_pd_before_go_neg, pref_freq,
4600 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4601 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602}
4603
4604
4605static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4606 const u8 *peer_addr,
4607 enum p2p_wps_method wps_method,
4608 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004609 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004610 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004612 if (persistent_group && wpa_s->conf->persistent_reconnect)
4613 persistent_group = 2;
4614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4616 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004617 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004618 ssid ? ssid->ssid_len : 0, pref_freq,
4619 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4620 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621}
4622
4623
4624static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4625{
4626 wpa_s->p2p_join_scan_count++;
4627 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4628 wpa_s->p2p_join_scan_count);
4629 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4630 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4631 " for join operationg - stop join attempt",
4632 MAC2STR(wpa_s->pending_join_iface_addr));
4633 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004634 if (wpa_s->p2p_auto_pd) {
4635 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004636 wpa_msg_global(wpa_s, MSG_INFO,
4637 P2P_EVENT_PROV_DISC_FAILURE
4638 " p2p_dev_addr=" MACSTR " status=N/A",
4639 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004640 return;
4641 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004642 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004643 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004644 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 }
4646}
4647
4648
Dmitry Shmidt04949592012-07-19 12:16:46 -07004649static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4650{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004651 int res;
4652 unsigned int num, i;
4653 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004654
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004655 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4656 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004657 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004658 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004659
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004660 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4661 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004662 if (!freqs)
4663 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004664
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004665 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4666 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004667
4668 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004669 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004670 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4671 freq);
4672 res = 0;
4673 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004674 }
4675 }
4676
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004677 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004678 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004679
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004680exit_free:
4681 os_free(freqs);
4682 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004683}
4684
4685
4686static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4687 const u8 *peer_dev_addr)
4688{
4689 struct wpa_bss *bss;
4690 int updated;
4691
4692 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4693 if (bss == NULL)
4694 return -1;
4695 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4696 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4697 "last scan");
4698 return 0;
4699 }
4700
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004701 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4702 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004703 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4704 "%ld.%06ld (%supdated in last scan)",
4705 bss->last_update.sec, bss->last_update.usec,
4706 updated ? "": "not ");
4707
4708 return updated;
4709}
4710
4711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4713 struct wpa_scan_results *scan_res)
4714{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004715 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 int freq;
4717 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4720
4721 if (wpa_s->global->p2p_disabled)
4722 return;
4723
Dmitry Shmidt04949592012-07-19 12:16:46 -07004724 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4725 scan_res ? (int) scan_res->num : -1,
4726 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727
4728 if (scan_res)
4729 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4730
Dmitry Shmidt04949592012-07-19 12:16:46 -07004731 if (wpa_s->p2p_auto_pd) {
4732 int join = wpas_p2p_peer_go(wpa_s,
4733 wpa_s->pending_join_dev_addr);
4734 if (join == 0 &&
4735 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4736 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004737 bss = wpa_bss_get_bssid_latest(
4738 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004739 if (bss) {
4740 freq = bss->freq;
4741 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4742 "the peer " MACSTR " at %d MHz",
4743 wpa_s->auto_pd_scan_retry,
4744 MAC2STR(wpa_s->
4745 pending_join_dev_addr),
4746 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004747 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004748 return;
4749 }
4750 }
4751
4752 if (join < 0)
4753 join = 0;
4754
4755 wpa_s->p2p_auto_pd = 0;
4756 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4757 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4758 MAC2STR(wpa_s->pending_join_dev_addr), join);
4759 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004760 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004761 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004762 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004763 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004764 wpa_msg_global(wpa_s, MSG_INFO,
4765 P2P_EVENT_PROV_DISC_FAILURE
4766 " p2p_dev_addr=" MACSTR " status=N/A",
4767 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004768 }
4769 return;
4770 }
4771
4772 if (wpa_s->p2p_auto_join) {
4773 int join = wpas_p2p_peer_go(wpa_s,
4774 wpa_s->pending_join_dev_addr);
4775 if (join < 0) {
4776 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4777 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004778 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004779 P2P_EVENT_FALLBACK_TO_GO_NEG
4780 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004781 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4782 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4783 wpa_s->p2p_persistent_group, 0, 0, 0,
4784 wpa_s->p2p_go_intent,
4785 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004786 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004787 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004788 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004789 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004790 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004791 wpa_s->p2p_go_max_oper_chwidth,
4792 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004793 return;
4794 }
4795
4796 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4797 "try to join the group", join ? "" :
4798 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004799 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004800 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004801 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004802 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004803 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004804 }
4805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004806 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4807 wpa_s->pending_join_iface_addr);
4808 if (freq < 0 &&
4809 p2p_get_interface_addr(wpa_s->global->p2p,
4810 wpa_s->pending_join_dev_addr,
4811 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004812 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4813 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4815 "address for join from " MACSTR " to " MACSTR
4816 " based on newly discovered P2P peer entry",
4817 MAC2STR(wpa_s->pending_join_iface_addr),
4818 MAC2STR(iface_addr));
4819 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4820 ETH_ALEN);
4821
4822 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4823 wpa_s->pending_join_iface_addr);
4824 }
4825 if (freq >= 0) {
4826 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4827 "from P2P peer table: %d MHz", freq);
4828 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004829 if (wpa_s->p2p_join_ssid_len) {
4830 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4831 MACSTR " and SSID %s",
4832 MAC2STR(wpa_s->pending_join_iface_addr),
4833 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4834 wpa_s->p2p_join_ssid_len));
4835 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4836 wpa_s->p2p_join_ssid,
4837 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004838 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004839 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4840 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4841 bss = wpa_bss_get_bssid_latest(wpa_s,
4842 wpa_s->pending_join_iface_addr);
4843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004845 u8 dev_addr[ETH_ALEN];
4846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 freq = bss->freq;
4848 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004849 "from BSS table: %d MHz (SSID %s)", freq,
4850 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004851 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4852 dev_addr) == 0 &&
4853 os_memcmp(wpa_s->pending_join_dev_addr,
4854 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4855 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4856 ETH_ALEN) != 0) {
4857 wpa_printf(MSG_DEBUG,
4858 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4859 MAC2STR(dev_addr),
4860 MAC2STR(wpa_s->pending_join_dev_addr));
4861 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4862 ETH_ALEN);
4863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 }
4865 if (freq > 0) {
4866 u16 method;
4867
Dmitry Shmidt04949592012-07-19 12:16:46 -07004868 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004869 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004870 P2P_EVENT_GROUP_FORMATION_FAILURE
4871 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004872 wpas_notify_p2p_group_formation_failure(
4873 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004874 return;
4875 }
4876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004877 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4878 "prior to joining an existing group (GO " MACSTR
4879 " freq=%u MHz)",
4880 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4881 wpa_s->pending_pd_before_join = 1;
4882
4883 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 case WPS_PIN_DISPLAY:
4885 method = WPS_CONFIG_KEYPAD;
4886 break;
4887 case WPS_PIN_KEYPAD:
4888 method = WPS_CONFIG_DISPLAY;
4889 break;
4890 case WPS_PBC:
4891 method = WPS_CONFIG_PUSHBUTTON;
4892 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004893 case WPS_P2PS:
4894 method = WPS_CONFIG_P2PS;
4895 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896 default:
4897 method = 0;
4898 break;
4899 }
4900
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004901 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4902 wpa_s->pending_join_dev_addr) ==
4903 method)) {
4904 /*
4905 * We have already performed provision discovery for
4906 * joining the group. Proceed directly to join
4907 * operation without duplicated provision discovery. */
4908 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4909 "with " MACSTR " already done - proceed to "
4910 "join",
4911 MAC2STR(wpa_s->pending_join_dev_addr));
4912 wpa_s->pending_pd_before_join = 0;
4913 goto start;
4914 }
4915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004917 wpa_s->pending_join_dev_addr,
4918 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004919 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004920 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4921 "Discovery Request before joining an "
4922 "existing group");
4923 wpa_s->pending_pd_before_join = 0;
4924 goto start;
4925 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 return;
4927 }
4928
4929 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4930 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4931 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4932 wpas_p2p_check_join_scan_limit(wpa_s);
4933 return;
4934
4935start:
4936 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004937 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
4938 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004939}
4940
4941
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004942static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4943 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945 int ret;
4946 struct wpa_driver_scan_params params;
4947 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004948 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004949 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004950 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951
4952 os_memset(&params, 0, sizeof(params));
4953
4954 /* P2P Wildcard SSID */
4955 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004956 if (ssid && ssid_len) {
4957 params.ssids[0].ssid = ssid;
4958 params.ssids[0].ssid_len = ssid_len;
4959 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4960 wpa_s->p2p_join_ssid_len = ssid_len;
4961 } else {
4962 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4963 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4964 wpa_s->p2p_join_ssid_len = 0;
4965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966
4967 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004968 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4969 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4970 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 if (wps_ie == NULL) {
4972 wpas_p2p_scan_res_join(wpa_s, NULL);
4973 return;
4974 }
4975
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004976 if (!freq) {
4977 int oper_freq;
4978 /*
4979 * If freq is not provided, check the operating freq of the GO
4980 * and use a single channel scan on if possible.
4981 */
4982 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4983 wpa_s->pending_join_iface_addr);
4984 if (oper_freq > 0)
4985 freq = oper_freq;
4986 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004987 if (freq > 0) {
4988 freqs[0] = freq;
4989 params.freqs = freqs;
4990 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004991
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004992 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4993 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4994 if (ies == NULL) {
4995 wpabuf_free(wps_ie);
4996 wpas_p2p_scan_res_join(wpa_s, NULL);
4997 return;
4998 }
4999 wpabuf_put_buf(ies, wps_ie);
5000 wpabuf_free(wps_ie);
5001
5002 bands = wpas_get_bands(wpa_s, freqs);
5003 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5004
5005 params.p2p_probe = 1;
5006 params.extra_ies = wpabuf_head(ies);
5007 params.extra_ies_len = wpabuf_len(ies);
5008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009 /*
5010 * Run a scan to update BSS table and start Provision Discovery once
5011 * the new scan results become available.
5012 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005013 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005014 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005015 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005016 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005017 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019
5020 wpabuf_free(ies);
5021
5022 if (ret) {
5023 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5024 "try again later");
5025 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5026 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5027 wpas_p2p_check_join_scan_limit(wpa_s);
5028 }
5029}
5030
5031
Dmitry Shmidt04949592012-07-19 12:16:46 -07005032static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5033{
5034 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005035 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005036}
5037
5038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005040 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005041 int auto_join, int op_freq,
5042 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043{
5044 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005045 MACSTR " dev " MACSTR " op_freq=%d)%s",
5046 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005047 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005048 if (ssid && ssid_len) {
5049 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5050 wpa_ssid_txt(ssid, ssid_len));
5051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005052
Dmitry Shmidt04949592012-07-19 12:16:46 -07005053 wpa_s->p2p_auto_pd = 0;
5054 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5056 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5057 wpa_s->pending_join_wps_method = wps_method;
5058
5059 /* Make sure we are not running find during connection establishment */
5060 wpas_p2p_stop_find(wpa_s);
5061
5062 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005063 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 return 0;
5065}
5066
5067
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005068static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5069 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070{
5071 struct wpa_supplicant *group;
5072 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074
5075 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5076 if (group == NULL)
5077 return -1;
5078 if (group != wpa_s) {
5079 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5080 sizeof(group->p2p_pin));
5081 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005082 } else {
5083 /*
5084 * Need to mark the current interface for p2p_group_formation
5085 * when a separate group interface is not used. This is needed
5086 * to allow p2p_cancel stop a pending p2p_connect-join.
5087 * wpas_p2p_init_group_interface() addresses this for the case
5088 * where a separate group interface is used.
5089 */
5090 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091 }
5092
5093 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005094 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095
5096 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005097 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005098 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5099 ETH_ALEN);
5100 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005101 if (freq && ssid && ssid_len) {
5102 res.freq = freq;
5103 res.ssid_len = ssid_len;
5104 os_memcpy(res.ssid, ssid, ssid_len);
5105 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005106 if (ssid && ssid_len) {
5107 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5108 ssid, ssid_len);
5109 } else {
5110 bss = wpa_bss_get_bssid_latest(
5111 wpa_s, wpa_s->pending_join_iface_addr);
5112 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005113 if (bss) {
5114 res.freq = bss->freq;
5115 res.ssid_len = bss->ssid_len;
5116 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5117 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5118 bss->freq,
5119 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005120 } else if (ssid && ssid_len) {
5121 res.ssid_len = ssid_len;
5122 os_memcpy(res.ssid, ssid, ssid_len);
5123 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5124 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005125 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126 }
5127
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005128 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5129 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5130 "starting client");
5131 wpa_drv_cancel_remain_on_channel(wpa_s);
5132 wpa_s->off_channel_freq = 0;
5133 wpa_s->roc_waiting_drv_freq = 0;
5134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 wpas_start_wps_enrollee(group, &res);
5136
5137 /*
5138 * Allow a longer timeout for join-a-running-group than normal 15
5139 * second group formation timeout since the GO may not have authorized
5140 * our connection yet.
5141 */
5142 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5143 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5144 wpa_s, NULL);
5145
5146 return 0;
5147}
5148
5149
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005150static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005151 int *force_freq, int *pref_freq, int go,
5152 unsigned int *pref_freq_list,
5153 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005154{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005155 struct wpa_used_freq_data *freqs;
5156 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005157 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5158
5159 max_pref_freq = *num_pref_freq;
5160 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005161
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005162 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5163 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005164 if (!freqs)
5165 return -1;
5166
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005167 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5168 wpa_s->num_multichan_concurrent);
5169
5170 /*
5171 * It is possible that the total number of used frequencies is bigger
5172 * than the number of frequencies used for P2P, so get the system wide
5173 * number of unused frequencies.
5174 */
5175 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5176
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005177 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005178 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5179 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005180
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005181 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005182 int ret;
5183 if (go)
5184 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5185 else
5186 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5187 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005188 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5189 ieee80211_is_dfs(freq)) {
5190 /*
5191 * If freq is a DFS channel and DFS is offloaded
5192 * to the driver, allow P2P GO to use it.
5193 */
5194 wpa_printf(MSG_DEBUG,
5195 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5196 freq);
5197 } else {
5198 wpa_printf(MSG_DEBUG,
5199 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5200 freq);
5201 res = -3;
5202 goto exit_free;
5203 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005204 }
5205
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005206 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005207 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005208 freq_in_use = 1;
5209 }
5210
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005211 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005212 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5213 freq);
5214 res = -2;
5215 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005216 }
5217 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5218 "requested channel (%u MHz)", freq);
5219 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005220 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005221 }
5222
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005223 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005224
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005225 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5226 enum wpa_driver_if_type iface_type;
5227
5228 if (go)
5229 iface_type = WPA_IF_P2P_GO;
5230 else
5231 iface_type = WPA_IF_P2P_CLIENT;
5232
5233 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5234 best_freq, go);
5235
5236 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5237 &max_pref_freq,
5238 pref_freq_list);
5239 if (!res && max_pref_freq > 0) {
5240 *num_pref_freq = max_pref_freq;
5241 i = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005242 while ((!p2p_supported_freq(wpa_s->global->p2p,
5243 pref_freq_list[i]) ||
5244 wpas_p2p_disallowed_freq(wpa_s->global,
5245 pref_freq_list[i])) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005246 i < *num_pref_freq) {
5247 wpa_printf(MSG_DEBUG,
5248 "P2P: preferred_freq_list[%d]=%d is disallowed",
5249 i, pref_freq_list[i]);
5250 i++;
5251 }
5252 if (i != *num_pref_freq) {
5253 best_freq = pref_freq_list[i];
5254 wpa_printf(MSG_DEBUG,
5255 "P2P: Using preferred_freq_list[%d]=%d",
5256 i, best_freq);
5257 } else {
5258 wpa_printf(MSG_DEBUG,
5259 "P2P: All driver preferred frequencies are disallowed for P2P use");
5260 *num_pref_freq = 0;
5261 }
5262 } else {
5263 wpa_printf(MSG_DEBUG,
5264 "P2P: No preferred frequency list available");
5265 }
5266 }
5267
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005268 /* We have a candidate frequency to use */
5269 if (best_freq > 0) {
5270 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005271 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005272 best_freq);
5273 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005274 } else {
5275 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 -07005276 best_freq);
5277 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005278 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005279 } else if (num_unused > 0) {
5280 wpa_printf(MSG_DEBUG,
5281 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5282 *force_freq = 0;
5283 } else {
5284 wpa_printf(MSG_DEBUG,
5285 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5286 res = -2;
5287 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005288 }
5289
5290exit_ok:
5291 res = 0;
5292exit_free:
5293 os_free(freqs);
5294 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005295}
5296
5297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298/**
5299 * wpas_p2p_connect - Request P2P Group Formation to be started
5300 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5301 * @peer_addr: Address of the peer P2P Device
5302 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5303 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005304 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 * @join: Whether to join an existing group (as a client) instead of starting
5306 * Group Owner negotiation; @peer_addr is BSSID in that case
5307 * @auth: Whether to only authorize the connection instead of doing that and
5308 * initiating Group Owner negotiation
5309 * @go_intent: GO Intent or -1 to use default
5310 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005311 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005312 * @persistent_id: Persistent group credentials to use for forcing GO
5313 * parameters or -1 to generate new values (SSID/passphrase)
5314 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5315 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005316 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005317 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005318 * @vht_chwidth: Channel width supported by GO operating with VHT support
5319 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005320 * @group_ssid: Specific Group SSID for join or %NULL if not set
5321 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005322 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5323 * failure, -2 on failure due to channel not currently available,
5324 * -3 if forced channel is not supported
5325 */
5326int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5327 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005328 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005329 int go_intent, int freq, unsigned int vht_center_freq2,
5330 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005331 unsigned int vht_chwidth, const u8 *group_ssid,
5332 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005334 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005335 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005337 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005338 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005339 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340
5341 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5342 return -1;
5343
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 if (persistent_id >= 0) {
5345 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5346 if (ssid == NULL || ssid->disabled != 2 ||
5347 ssid->mode != WPAS_MODE_P2P_GO)
5348 return -1;
5349 }
5350
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005351 os_free(wpa_s->global->add_psk);
5352 wpa_s->global->add_psk = NULL;
5353
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005354 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005355 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005356 wpa_s->global->pending_p2ps_group_freq = 0;
5357 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359 if (go_intent < 0)
5360 go_intent = wpa_s->conf->p2p_go_intent;
5361
5362 if (!auth)
5363 wpa_s->p2p_long_listen = 0;
5364
5365 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005366 wpa_s->p2p_persistent_group = !!persistent_group;
5367 wpa_s->p2p_persistent_id = persistent_id;
5368 wpa_s->p2p_go_intent = go_intent;
5369 wpa_s->p2p_connect_freq = freq;
5370 wpa_s->p2p_fallback_to_go_neg = 0;
5371 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005372 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005373 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005374 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5375 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005376
5377 if (pin)
5378 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5379 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005380 if (wps_generate_pin((unsigned int *) &ret) < 0)
5381 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005382 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5383 "%08d", ret);
5384 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5385 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005386 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5387 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005388 } else if (wps_method == WPS_P2PS) {
5389 /* Force the P2Ps default PIN to be used */
5390 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391 } else
5392 wpa_s->p2p_pin[0] = '\0';
5393
Dmitry Shmidt04949592012-07-19 12:16:46 -07005394 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005395 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5396 if (auth) {
5397 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5398 "connect a running group from " MACSTR,
5399 MAC2STR(peer_addr));
5400 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5401 return ret;
5402 }
5403 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5404 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5405 iface_addr) < 0) {
5406 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5407 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5408 dev_addr);
5409 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005410 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005411 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005412 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5413 "%ld.%06ld",
5414 wpa_s->p2p_auto_started.sec,
5415 wpa_s->p2p_auto_started.usec);
5416 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005417 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005418 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005419 auto_join, freq,
5420 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005421 return -1;
5422 return ret;
5423 }
5424
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005425 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005426 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005427 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005428 if (res)
5429 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005430 wpas_p2p_set_own_freq_preference(wpa_s,
5431 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005433 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5436
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005437 if (wpa_s->create_p2p_iface) {
5438 /* Prepare to add a new interface for the group */
5439 iftype = WPA_IF_P2P_GROUP;
5440 if (go_intent == 15)
5441 iftype = WPA_IF_P2P_GO;
5442 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5443 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5444 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005448 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005449 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005450 if (wpa_s->p2p_mgmt)
5451 if_addr = wpa_s->parent->own_addr;
5452 else
5453 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005454 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456
5457 if (auth) {
5458 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005459 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005460 force_freq, persistent_group, ssid,
5461 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 return -1;
5463 return ret;
5464 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465
5466 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5467 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005468 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005469 if (wpa_s->create_p2p_iface)
5470 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 return -1;
5472 }
5473 return ret;
5474}
5475
5476
5477/**
5478 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5479 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5480 * @freq: Frequency of the channel in MHz
5481 * @duration: Duration of the stay on the channel in milliseconds
5482 *
5483 * This callback is called when the driver indicates that it has started the
5484 * requested remain-on-channel duration.
5485 */
5486void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5487 unsigned int freq, unsigned int duration)
5488{
5489 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5490 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005491 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)",
5492 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5493 wpa_s->roc_waiting_drv_freq, freq, duration);
5494 if (wpa_s->off_channel_freq &&
5495 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5497 wpa_s->pending_listen_duration);
5498 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005499 } else {
5500 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5501 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5502 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503 }
5504}
5505
5506
Jithu Jance57cea1a2014-08-20 10:22:04 -07005507int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508{
5509 /* Limit maximum Listen state time based on driver limitation. */
5510 if (timeout > wpa_s->max_remain_on_chan)
5511 timeout = wpa_s->max_remain_on_chan;
5512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 return p2p_listen(wpa_s->global->p2p, timeout);
5514}
5515
5516
5517/**
5518 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5519 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5520 * @freq: Frequency of the channel in MHz
5521 *
5522 * This callback is called when the driver indicates that a remain-on-channel
5523 * operation has been completed, i.e., the duration on the requested channel
5524 * has timed out.
5525 */
5526void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5527 unsigned int freq)
5528{
5529 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5530 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005531 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005532 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5534 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005535 if (wpa_s->p2p_long_listen > 0)
5536 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5538 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005539 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005540 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005541 if (wpa_s->p2p_long_listen > 0) {
5542 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5543 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005544 } else {
5545 /*
5546 * When listen duration is over, stop listen & update p2p_state
5547 * to IDLE.
5548 */
5549 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550 }
5551}
5552
5553
5554/**
5555 * wpas_p2p_group_remove - Remove a P2P group
5556 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5557 * @ifname: Network interface name of the group interface or "*" to remove all
5558 * groups
5559 * Returns: 0 on success, -1 on failure
5560 *
5561 * This function is used to remove a P2P group. This can be used to disconnect
5562 * from a group in which the local end is a P2P Client or to end a P2P Group in
5563 * case the local end is the Group Owner. If a virtual network interface was
5564 * created for this group, that interface will be removed. Otherwise, only the
5565 * configured P2P group network will be removed from the interface.
5566 */
5567int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5568{
5569 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005570 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571
5572 if (os_strcmp(ifname, "*") == 0) {
5573 struct wpa_supplicant *prev;
5574 wpa_s = global->ifaces;
5575 while (wpa_s) {
5576 prev = wpa_s;
5577 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005578 if (prev->p2p_group_interface !=
5579 NOT_P2P_GROUP_INTERFACE ||
5580 (prev->current_ssid &&
5581 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005582 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583 }
5584 return 0;
5585 }
5586
5587 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5588 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5589 break;
5590 }
5591
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005592 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593}
5594
5595
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005596static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5597{
5598 unsigned int r;
5599
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005600 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5601 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5602 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5603 int res;
5604
5605 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5606 &size, pref_freq_list);
5607 if (!res && size > 0) {
5608 i = 0;
5609 while (wpas_p2p_disallowed_freq(wpa_s->global,
5610 pref_freq_list[i]) &&
5611 i < size) {
5612 wpa_printf(MSG_DEBUG,
5613 "P2P: preferred_freq_list[%d]=%d is disallowed",
5614 i, pref_freq_list[i]);
5615 i++;
5616 }
5617 if (i != size) {
5618 freq = pref_freq_list[i];
5619 wpa_printf(MSG_DEBUG,
5620 "P2P: Using preferred_freq_list[%d]=%d",
5621 i, freq);
5622 } else {
5623 wpa_printf(MSG_DEBUG,
5624 "P2P: All driver preferred frequencies are disallowed for P2P use");
5625 }
5626 } else {
5627 wpa_printf(MSG_DEBUG,
5628 "P2P: No preferred frequency list available");
5629 }
5630 }
5631
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005632 if (freq == 2) {
5633 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5634 "band");
5635 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005636 p2p_supported_freq_go(wpa_s->global->p2p,
5637 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005638 freq = wpa_s->best_24_freq;
5639 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5640 "channel: %d MHz", freq);
5641 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005642 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5643 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005644 freq = 2412 + (r % 3) * 25;
5645 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5646 "channel: %d MHz", freq);
5647 }
5648 }
5649
5650 if (freq == 5) {
5651 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5652 "band");
5653 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005654 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005655 wpa_s->best_5_freq)) {
5656 freq = wpa_s->best_5_freq;
5657 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5658 "channel: %d MHz", freq);
5659 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005660 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5661 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005662 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005663 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005664 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5665 "5 GHz channel for P2P group");
5666 return -1;
5667 }
5668 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5669 "channel: %d MHz", freq);
5670 }
5671 }
5672
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005673 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005674 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5675 ieee80211_is_dfs(freq)) {
5676 /*
5677 * If freq is a DFS channel and DFS is offloaded to the
5678 * driver, allow P2P GO to use it.
5679 */
5680 wpa_printf(MSG_DEBUG, "P2P: "
5681 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5682 __func__, freq);
5683 return freq;
5684 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005685 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5686 "(%u MHz) is not supported for P2P uses",
5687 freq);
5688 return -1;
5689 }
5690
5691 return freq;
5692}
5693
5694
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005695static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5696 const struct p2p_channels *channels,
5697 int freq)
5698{
5699 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5700 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5701 freq_included(wpa_s, channels, freq))
5702 return 1;
5703 return 0;
5704}
5705
5706
5707static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5708 struct p2p_go_neg_results *params,
5709 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005710{
5711 unsigned int i, r;
5712
5713 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005714 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005715 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005716
5717 for (i = 0; i < 3; i++) {
5718 params->freq = 2412 + ((r + i) % 3) * 25;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005719 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005720 goto out;
5721 }
5722
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005723 /* try all other channels in operating class 81 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005724 for (i = 0; i < 11; i++) {
5725 params->freq = 2412 + i * 5;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005726
5727 /* skip social channels; covered in the previous loop */
5728 if (params->freq == 2412 ||
5729 params->freq == 2437 ||
5730 params->freq == 2462)
5731 continue;
5732
5733 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005734 goto out;
5735 }
5736
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005737 /* try all channels in operating class 115 */
5738 for (i = 0; i < 4; i++) {
5739 params->freq = 5180 + i * 20;
5740 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005741 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005742 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5743 goto out;
5744 }
5745
5746 /* try all channels in operating class 124 */
5747 for (i = 0; i < 4; i++) {
5748 params->freq = 5745 + i * 20;
5749 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005750 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005751 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5752 goto out;
5753 }
5754
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005755 /* try social channel class 180 channel 2 */
5756 params->freq = 58320 + 1 * 2160;
5757 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005758 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005759 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5760 goto out;
5761
5762 /* try all channels in reg. class 180 */
5763 for (i = 0; i < 4; i++) {
5764 params->freq = 58320 + i * 2160;
5765 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005766 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005767 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5768 goto out;
5769 }
5770
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005771 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005772 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005773 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005774out:
5775 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5776 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005777}
5778
5779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005780static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5781 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005782 int freq, int vht_center_freq2, int ht40,
5783 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005784 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005786 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005787 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005788 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005789 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005790 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791
5792 os_memset(params, 0, sizeof(*params));
5793 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005794 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005795 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005796 params->max_oper_chwidth = max_oper_chwidth;
5797 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005799 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5800 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005801 if (!freqs)
5802 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005803
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005804 num = get_shared_radio_freqs_data(wpa_s, freqs,
5805 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005806
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005807 if (wpa_s->current_ssid &&
5808 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5809 wpa_s->wpa_state == WPA_COMPLETED) {
5810 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5811 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005812
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005813 /*
5814 * If the frequency selection is done for an active P2P GO that
5815 * is not sharing a frequency, allow to select a new frequency
5816 * even if there are no unused frequencies as we are about to
5817 * move the P2P GO so its frequency can be re-used.
5818 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005819 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005820 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5821 freqs[i].flags == 0) {
5822 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005823 break;
5824 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005825 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005826 }
5827
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005828 /* try using the forced freq */
5829 if (freq) {
5830 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5831 wpa_printf(MSG_DEBUG,
5832 "P2P: Forced GO freq %d MHz not accepted",
5833 freq);
5834 goto fail;
5835 }
5836
5837 for (i = 0; i < num; i++) {
5838 if (freqs[i].freq == freq) {
5839 wpa_printf(MSG_DEBUG,
5840 "P2P: forced freq (%d MHz) is also shared",
5841 freq);
5842 params->freq = freq;
5843 goto success;
5844 }
5845 }
5846
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005847 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005848 wpa_printf(MSG_DEBUG,
5849 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5850 freq);
5851 goto fail;
5852 }
5853
5854 wpa_printf(MSG_DEBUG,
5855 "P2P: force GO freq (%d MHz) on a free channel",
5856 freq);
5857 params->freq = freq;
5858 goto success;
5859 }
5860
5861 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005862 if (num &&
5863 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005864 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5865 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5866 wpa_printf(MSG_DEBUG,
5867 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005868 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005869 params->freq = cand;
5870 goto success;
5871 }
5872
5873 /* try using one of the shared freqs */
5874 for (i = 0; i < num; i++) {
5875 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5876 freqs[i].freq)) {
5877 wpa_printf(MSG_DEBUG,
5878 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005879 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005880 params->freq = freqs[i].freq;
5881 goto success;
5882 }
5883 }
5884 }
5885
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005886 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005887 wpa_printf(MSG_DEBUG,
5888 "P2P: Cannot force GO on any of the channels we are already using");
5889 goto fail;
5890 }
5891
5892 /* try using the setting from the configuration file */
5893 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5894 wpa_s->conf->p2p_oper_channel >= 1 &&
5895 wpa_s->conf->p2p_oper_channel <= 11 &&
5896 wpas_p2p_supported_freq_go(
5897 wpa_s, channels,
5898 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5899 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5900 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5901 "frequency %d MHz", params->freq);
5902 goto success;
5903 }
5904
5905 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5906 wpa_s->conf->p2p_oper_reg_class == 116 ||
5907 wpa_s->conf->p2p_oper_reg_class == 117 ||
5908 wpa_s->conf->p2p_oper_reg_class == 124 ||
5909 wpa_s->conf->p2p_oper_reg_class == 125 ||
5910 wpa_s->conf->p2p_oper_reg_class == 126 ||
5911 wpa_s->conf->p2p_oper_reg_class == 127) &&
5912 wpas_p2p_supported_freq_go(wpa_s, channels,
5913 5000 +
5914 5 * wpa_s->conf->p2p_oper_channel)) {
5915 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5916 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5917 "frequency %d MHz", params->freq);
5918 goto success;
5919 }
5920
5921 /* Try using best channels */
5922 if (wpa_s->conf->p2p_oper_channel == 0 &&
5923 wpa_s->best_overall_freq > 0 &&
5924 wpas_p2p_supported_freq_go(wpa_s, channels,
5925 wpa_s->best_overall_freq)) {
5926 params->freq = wpa_s->best_overall_freq;
5927 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5928 "channel %d MHz", params->freq);
5929 goto success;
5930 }
5931
5932 if (wpa_s->conf->p2p_oper_channel == 0 &&
5933 wpa_s->best_24_freq > 0 &&
5934 wpas_p2p_supported_freq_go(wpa_s, channels,
5935 wpa_s->best_24_freq)) {
5936 params->freq = wpa_s->best_24_freq;
5937 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5938 "channel %d MHz", params->freq);
5939 goto success;
5940 }
5941
5942 if (wpa_s->conf->p2p_oper_channel == 0 &&
5943 wpa_s->best_5_freq > 0 &&
5944 wpas_p2p_supported_freq_go(wpa_s, channels,
5945 wpa_s->best_5_freq)) {
5946 params->freq = wpa_s->best_5_freq;
5947 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5948 "channel %d MHz", params->freq);
5949 goto success;
5950 }
5951
5952 /* try using preferred channels */
5953 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5954 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5955 params->freq = cand;
5956 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5957 "channels", params->freq);
5958 goto success;
5959 }
5960
5961 /* Try using one of the group common freqs */
5962 if (wpa_s->p2p_group_common_freqs) {
5963 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5964 cand = wpa_s->p2p_group_common_freqs[i];
5965 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5966 params->freq = cand;
5967 wpa_printf(MSG_DEBUG,
5968 "P2P: Use freq %d MHz common with the peer",
5969 params->freq);
5970 goto success;
5971 }
5972 }
5973 }
5974
5975 /* no preference, select some channel */
5976 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5977
5978 if (params->freq == 0) {
5979 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5980 goto fail;
5981 }
5982
5983success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005984 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005985 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005986fail:
5987 os_free(freqs);
5988 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005989}
5990
5991
5992static struct wpa_supplicant *
5993wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5994 int go)
5995{
5996 struct wpa_supplicant *group_wpa_s;
5997
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005998 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005999 if (wpa_s->p2p_mgmt) {
6000 /*
6001 * We may be called on the p2p_dev interface which
6002 * cannot be used for group operations, so always use
6003 * the primary interface.
6004 */
6005 wpa_s->parent->p2pdev = wpa_s;
6006 wpa_s = wpa_s->parent;
6007 }
6008 wpa_dbg(wpa_s, MSG_DEBUG,
6009 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006010 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006011 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006012 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006013 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006014 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015
6016 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006017 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006018 wpa_msg_global(wpa_s, MSG_ERROR,
6019 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006020 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006021 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6023 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006024 wpa_msg_global(wpa_s, MSG_ERROR,
6025 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026 wpas_p2p_remove_pending_group_interface(wpa_s);
6027 return NULL;
6028 }
6029
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006030 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6031 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006032 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033 return group_wpa_s;
6034}
6035
6036
6037/**
6038 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6039 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6040 * @persistent_group: Whether to create a persistent group
6041 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006042 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006043 * @ht40: Start GO with 40 MHz channel width
6044 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006045 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046 * Returns: 0 on success, -1 on failure
6047 *
6048 * This function creates a new P2P group with the local end as the Group Owner,
6049 * i.e., without using Group Owner Negotiation.
6050 */
6051int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006052 int freq, int vht_center_freq2, int ht40, int vht,
6053 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006054{
6055 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056
6057 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6058 return -1;
6059
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006060 os_free(wpa_s->global->add_psk);
6061 wpa_s->global->add_psk = NULL;
6062
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006063 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006064 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006065 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006066
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006067 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6068 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006069 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006070
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006071 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6072 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006073 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006074 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006075 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006076 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6077 ieee80211_is_dfs(params.freq)) {
6078 /*
6079 * If freq is a DFS channel and DFS is offloaded to the
6080 * driver, allow P2P GO to use it.
6081 */
6082 wpa_printf(MSG_DEBUG,
6083 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
6084 __func__, params.freq);
6085 } else {
6086 wpa_printf(MSG_DEBUG,
6087 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
6088 params.freq);
6089 return -1;
6090 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006092 p2p_go_params(wpa_s->global->p2p, &params);
6093 params.persistent_group = persistent_group;
6094
6095 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6096 if (wpa_s == NULL)
6097 return -1;
6098 wpas_start_wps_go(wpa_s, &params, 0);
6099
6100 return 0;
6101}
6102
6103
6104static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006105 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006106 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107{
6108 struct wpa_ssid *ssid;
6109
6110 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6111 if (wpa_s == NULL)
6112 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006113 if (force_scan)
6114 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006115 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006116
6117 wpa_supplicant_ap_deinit(wpa_s);
6118
6119 ssid = wpa_config_add_network(wpa_s->conf);
6120 if (ssid == NULL)
6121 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006122 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006123 wpa_config_set_network_defaults(ssid);
6124 ssid->temporary = 1;
6125 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006126 ssid->pbss = params->pbss;
6127 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6128 WPA_CIPHER_CCMP;
6129 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6131 ssid->ssid = os_malloc(params->ssid_len);
6132 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133 wpa_config_remove_network(wpa_s->conf, ssid->id);
6134 return -1;
6135 }
6136 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6137 ssid->ssid_len = params->ssid_len;
6138 ssid->p2p_group = 1;
6139 ssid->export_keys = 1;
6140 if (params->psk_set) {
6141 os_memcpy(ssid->psk, params->psk, 32);
6142 ssid->psk_set = 1;
6143 }
6144 if (params->passphrase)
6145 ssid->passphrase = os_strdup(params->passphrase);
6146
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006147 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006148 wpa_s->p2p_in_invitation = 1;
6149 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006150 wpa_s->p2p_go_group_formation_completed = 0;
6151 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006152
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006153 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006154 NULL);
6155 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6156 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006157 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006158 wpa_supplicant_select_network(wpa_s, ssid);
6159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006160 return 0;
6161}
6162
6163
6164int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6165 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006166 int force_freq, int neg_freq,
6167 int vht_center_freq2, int ht40,
6168 int vht, int max_oper_chwidth,
6169 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006170 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006171{
6172 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006173 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174
6175 if (ssid->disabled != 2 || ssid->ssid == NULL)
6176 return -1;
6177
6178 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6179 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6180 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6181 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006182 if (go == 0 &&
6183 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006184 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006185 /*
6186 * This can happen if Invitation Response frame was lost
6187 * and the peer (GO of a persistent group) tries to
6188 * invite us again. Reschedule the timeout to avoid
6189 * terminating the wait for the connection too early
6190 * since we now know that the peer is still trying to
6191 * invite us instead of having already started the GO.
6192 */
6193 wpa_printf(MSG_DEBUG,
6194 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6195 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6196 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006197 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199 return 0;
6200 }
6201
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006202 os_free(wpa_s->global->add_psk);
6203 wpa_s->global->add_psk = NULL;
6204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006205 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006206 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006207
Dmitry Shmidt04949592012-07-19 12:16:46 -07006208 wpa_s->p2p_fallback_to_go_neg = 0;
6209
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006210 if (ssid->mode == WPAS_MODE_P2P_GO) {
6211 if (force_freq > 0) {
6212 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6213 if (freq < 0)
6214 return -1;
6215 } else {
6216 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6217 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006218 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006219 freq = 0;
6220 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006221 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006222 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006223 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006224 struct os_reltime now;
6225 struct wpa_bss *bss =
6226 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006227
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006228 os_get_reltime(&now);
6229 if (bss &&
6230 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006231 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006232 freq = bss->freq;
6233 else
6234 freq = 0;
6235 }
6236
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006237 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6238 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006239 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006240 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006241 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006242
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006243 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6244 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006245 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006246
6247 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006248 params.psk_set = ssid->psk_set;
6249 if (params.psk_set)
6250 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006251 if (ssid->passphrase) {
6252 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6253 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6254 "persistent group");
6255 return -1;
6256 }
6257 os_strlcpy(params.passphrase, ssid->passphrase,
6258 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006260 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6261 params.ssid_len = ssid->ssid_len;
6262 params.persistent_group = 1;
6263
6264 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6265 if (wpa_s == NULL)
6266 return -1;
6267
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006268 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6269
Dmitry Shmidt56052862013-10-04 10:23:25 -07006270 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006271 wpas_start_wps_go(wpa_s, &params, 0);
6272
6273 return 0;
6274}
6275
6276
6277static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6278 struct wpabuf *proberesp_ies)
6279{
6280 struct wpa_supplicant *wpa_s = ctx;
6281 if (wpa_s->ap_iface) {
6282 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006283 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6284 wpabuf_free(beacon_ies);
6285 wpabuf_free(proberesp_ies);
6286 return;
6287 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006288 if (beacon_ies) {
6289 wpabuf_free(hapd->p2p_beacon_ie);
6290 hapd->p2p_beacon_ie = beacon_ies;
6291 }
6292 wpabuf_free(hapd->p2p_probe_resp_ie);
6293 hapd->p2p_probe_resp_ie = proberesp_ies;
6294 } else {
6295 wpabuf_free(beacon_ies);
6296 wpabuf_free(proberesp_ies);
6297 }
6298 wpa_supplicant_ap_update_beacon(wpa_s);
6299}
6300
6301
6302static void wpas_p2p_idle_update(void *ctx, int idle)
6303{
6304 struct wpa_supplicant *wpa_s = ctx;
6305 if (!wpa_s->ap_iface)
6306 return;
6307 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006308 if (idle) {
6309 if (wpa_s->global->p2p_fail_on_wps_complete &&
6310 wpa_s->p2p_in_provisioning) {
6311 wpas_p2p_grpform_fail_after_wps(wpa_s);
6312 return;
6313 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006314 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006315 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006316 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6317}
6318
6319
6320struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006321 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322{
6323 struct p2p_group *group;
6324 struct p2p_group_config *cfg;
6325
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006326 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6327 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006328 return NULL;
6329
6330 cfg = os_zalloc(sizeof(*cfg));
6331 if (cfg == NULL)
6332 return NULL;
6333
Dmitry Shmidt04949592012-07-19 12:16:46 -07006334 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006335 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006336 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006337 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6339 if (wpa_s->max_stations &&
6340 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6341 cfg->max_clients = wpa_s->max_stations;
6342 else
6343 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006344 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6345 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006346 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347 cfg->cb_ctx = wpa_s;
6348 cfg->ie_update = wpas_p2p_ie_update;
6349 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006350 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6351 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006352
6353 group = p2p_group_init(wpa_s->global->p2p, cfg);
6354 if (group == NULL)
6355 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006356 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 p2p_group_notif_formation_done(group);
6358 wpa_s->p2p_group = group;
6359 return group;
6360}
6361
6362
6363void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6364 int registrar)
6365{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006366 struct wpa_ssid *ssid = wpa_s->current_ssid;
6367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006368 if (!wpa_s->p2p_in_provisioning) {
6369 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6370 "provisioning not in progress");
6371 return;
6372 }
6373
Dmitry Shmidt04949592012-07-19 12:16:46 -07006374 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6375 u8 go_dev_addr[ETH_ALEN];
6376 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6377 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6378 ssid->ssid_len);
6379 /* Clear any stored provisioning info */
6380 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6381 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006382
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006383 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006384 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006385 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006386 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6387 /*
6388 * Use a separate timeout for initial data connection to
6389 * complete to allow the group to be removed automatically if
6390 * something goes wrong in this step before the P2P group idle
6391 * timeout mechanism is taken into use.
6392 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006393 wpa_dbg(wpa_s, MSG_DEBUG,
6394 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6395 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006396 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6397 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006398 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006399 /* Complete group formation on successful data connection. */
6400 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006401 } else if (ssid) {
6402 /*
6403 * Use a separate timeout for initial data connection to
6404 * complete to allow the group to be removed automatically if
6405 * the client does not complete data connection successfully.
6406 */
6407 wpa_dbg(wpa_s, MSG_DEBUG,
6408 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6409 P2P_MAX_INITIAL_CONN_WAIT_GO);
6410 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6411 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006412 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006413 /*
6414 * Complete group formation on first successful data connection
6415 */
6416 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006417 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006418 if (wpa_s->global->p2p)
6419 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006420 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006421}
6422
6423
Jouni Malinen75ecf522011-06-27 15:19:46 -07006424void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6425 struct wps_event_fail *fail)
6426{
6427 if (!wpa_s->p2p_in_provisioning) {
6428 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6429 "provisioning not in progress");
6430 return;
6431 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006432
6433 if (wpa_s->go_params) {
6434 p2p_clear_provisioning_info(
6435 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006436 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006437 }
6438
Jouni Malinen75ecf522011-06-27 15:19:46 -07006439 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006440
6441 if (wpa_s == wpa_s->global->p2p_group_formation) {
6442 /*
6443 * Allow some time for the failed WPS negotiation exchange to
6444 * complete, but remove the group since group formation cannot
6445 * succeed after provisioning failure.
6446 */
6447 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6448 wpa_s->global->p2p_fail_on_wps_complete = 1;
6449 eloop_deplete_timeout(0, 50000,
6450 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006451 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006452 }
6453}
6454
6455
6456int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6457{
6458 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6459 !wpa_s->p2p_in_provisioning)
6460 return 0;
6461
6462 wpas_p2p_grpform_fail_after_wps(wpa_s);
6463
6464 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006465}
6466
6467
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006468int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006469 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006470 enum wpas_p2p_prov_disc_use use,
6471 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006472{
6473 u16 config_methods;
6474
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006475 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006476 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006477 wpa_s->p2p_fallback_to_go_neg = 0;
6478 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006479 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6480 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006481 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6482 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6483
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006484 wpa_printf(MSG_DEBUG,
6485 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6486 __func__, p2ps_prov->conncap,
6487 p2ps_prov->adv_id, p2ps_prov->conncap,
6488 p2ps_prov->status, p2ps_prov->info);
6489
6490 config_methods = 0;
6491 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006492 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006493 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006494 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006495 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6496 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006497 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006498 else {
6499 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006500 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006501 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006502 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006503
Dmitry Shmidt04949592012-07-19 12:16:46 -07006504 if (use == WPAS_P2P_PD_AUTO) {
6505 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6506 wpa_s->pending_pd_config_methods = config_methods;
6507 wpa_s->p2p_auto_pd = 1;
6508 wpa_s->p2p_auto_join = 0;
6509 wpa_s->pending_pd_before_join = 0;
6510 wpa_s->auto_pd_scan_retry = 0;
6511 wpas_p2p_stop_find(wpa_s);
6512 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006513 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006514 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6515 wpa_s->p2p_auto_started.sec,
6516 wpa_s->p2p_auto_started.usec);
6517 wpas_p2p_join_scan(wpa_s, NULL);
6518 return 0;
6519 }
6520
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006521 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6522 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006526 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006527 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006528 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006529}
6530
6531
6532int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6533 char *end)
6534{
6535 return p2p_scan_result_text(ies, ies_len, buf, end);
6536}
6537
6538
6539static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6540{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006541 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542 return;
6543
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006544 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006545 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006546 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6547 wpa_s, NULL);
6548 offchannel_send_action_done(wpa_s);
6549 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006550
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006551 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6552 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006553 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006554}
6555
6556
6557int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6558 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006559 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006560 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006561 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006562{
6563 wpas_p2p_clear_pending_action_tx(wpa_s);
6564 wpa_s->p2p_long_listen = 0;
6565
Dmitry Shmidt04949592012-07-19 12:16:46 -07006566 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6567 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568 return -1;
6569
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006570 wpa_supplicant_cancel_sched_scan(wpa_s);
6571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006572 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006573 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006574 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006575}
6576
6577
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006578static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6579 struct wpa_scan_results *scan_res)
6580{
6581 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6582
6583 if (wpa_s->p2p_scan_work) {
6584 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6585 wpa_s->p2p_scan_work = NULL;
6586 radio_work_done(work);
6587 }
6588
6589 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6590 return;
6591
6592 /*
6593 * Indicate that results have been processed so that the P2P module can
6594 * continue pending tasks.
6595 */
6596 p2p_scan_res_handled(wpa_s->global->p2p);
6597}
6598
6599
6600static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006601{
6602 wpas_p2p_clear_pending_action_tx(wpa_s);
6603 wpa_s->p2p_long_listen = 0;
6604 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6605 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006606
6607 if (wpa_s->global->p2p)
6608 p2p_stop_find(wpa_s->global->p2p);
6609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006610 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6611 wpa_printf(MSG_DEBUG,
6612 "P2P: Do not consider the scan results after stop_find");
6613 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6614 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006615}
6616
6617
6618void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6619{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006620 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006621 if (!wpa_s->global->pending_group_iface_for_p2ps)
6622 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006623}
6624
6625
6626static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6627{
6628 struct wpa_supplicant *wpa_s = eloop_ctx;
6629 wpa_s->p2p_long_listen = 0;
6630}
6631
6632
6633int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6634{
6635 int res;
6636
6637 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6638 return -1;
6639
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006640 if (wpa_s->p2p_lo_started) {
6641 wpa_printf(MSG_DEBUG,
6642 "P2P: Cannot start P2P listen, it is offloaded");
6643 return -1;
6644 }
6645
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006646 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006647 wpas_p2p_clear_pending_action_tx(wpa_s);
6648
6649 if (timeout == 0) {
6650 /*
6651 * This is a request for unlimited Listen state. However, at
6652 * least for now, this is mapped to a Listen state for one
6653 * hour.
6654 */
6655 timeout = 3600;
6656 }
6657 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6658 wpa_s->p2p_long_listen = 0;
6659
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006660 /*
6661 * Stop previous find/listen operation to avoid trying to request a new
6662 * remain-on-channel operation while the driver is still running the
6663 * previous one.
6664 */
6665 if (wpa_s->global->p2p)
6666 p2p_stop_find(wpa_s->global->p2p);
6667
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006668 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6669 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6670 wpa_s->p2p_long_listen = timeout * 1000;
6671 eloop_register_timeout(timeout, 0,
6672 wpas_p2p_long_listen_timeout,
6673 wpa_s, NULL);
6674 }
6675
6676 return res;
6677}
6678
6679
6680int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6681 u8 *buf, size_t len, int p2p_group)
6682{
6683 struct wpabuf *p2p_ie;
6684 int ret;
6685
6686 if (wpa_s->global->p2p_disabled)
6687 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006688 /*
6689 * Advertize mandatory cross connection capability even on
6690 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6691 */
6692 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006693 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006694 if (wpa_s->global->p2p == NULL)
6695 return -1;
6696 if (bss == NULL)
6697 return -1;
6698
6699 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6700 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6701 p2p_group, p2p_ie);
6702 wpabuf_free(p2p_ie);
6703
6704 return ret;
6705}
6706
6707
6708int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006709 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006710 const u8 *ie, size_t ie_len,
6711 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006712{
6713 if (wpa_s->global->p2p_disabled)
6714 return 0;
6715 if (wpa_s->global->p2p == NULL)
6716 return 0;
6717
Dmitry Shmidt04949592012-07-19 12:16:46 -07006718 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006719 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006720 case P2P_PREQ_NOT_P2P:
6721 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6722 ssi_signal);
6723 /* fall through */
6724 case P2P_PREQ_MALFORMED:
6725 case P2P_PREQ_NOT_LISTEN:
6726 case P2P_PREQ_NOT_PROCESSED:
6727 default: /* make gcc happy */
6728 return 0;
6729 case P2P_PREQ_PROCESSED:
6730 return 1;
6731 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006732}
6733
6734
6735void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6736 const u8 *sa, const u8 *bssid,
6737 u8 category, const u8 *data, size_t len, int freq)
6738{
6739 if (wpa_s->global->p2p_disabled)
6740 return;
6741 if (wpa_s->global->p2p == NULL)
6742 return;
6743
6744 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6745 freq);
6746}
6747
6748
6749void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6750{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006751 unsigned int bands;
6752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006753 if (wpa_s->global->p2p_disabled)
6754 return;
6755 if (wpa_s->global->p2p == NULL)
6756 return;
6757
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006758 bands = wpas_get_bands(wpa_s, NULL);
6759 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006760}
6761
6762
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006763static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006764{
6765 p2p_group_deinit(wpa_s->p2p_group);
6766 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006767
6768 wpa_s->ap_configured_cb = NULL;
6769 wpa_s->ap_configured_cb_ctx = NULL;
6770 wpa_s->ap_configured_cb_data = NULL;
6771 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006772}
6773
6774
6775int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6776{
6777 wpa_s->p2p_long_listen = 0;
6778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006779 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6780 return -1;
6781
6782 return p2p_reject(wpa_s->global->p2p, addr);
6783}
6784
6785
6786/* Invite to reinvoke a persistent group */
6787int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006788 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006789 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6790 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791{
6792 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006793 u8 *bssid = NULL;
6794 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006795 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006796 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006797 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006798
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006799 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006800 if (peer_addr)
6801 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6802 else
6803 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006804
Jouni Malinen31be0a42012-08-31 21:20:51 +03006805 wpa_s->p2p_persistent_go_freq = freq;
6806 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006807 wpa_s->p2p_go_vht = !!vht;
6808 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
6809 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006810 if (ssid->mode == WPAS_MODE_P2P_GO) {
6811 role = P2P_INVITE_ROLE_GO;
6812 if (peer_addr == NULL) {
6813 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6814 "address in invitation command");
6815 return -1;
6816 }
6817 if (wpas_p2p_create_iface(wpa_s)) {
6818 if (wpas_p2p_add_group_interface(wpa_s,
6819 WPA_IF_P2P_GO) < 0) {
6820 wpa_printf(MSG_ERROR, "P2P: Failed to "
6821 "allocate a new interface for the "
6822 "group");
6823 return -1;
6824 }
6825 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006826 } else if (wpa_s->p2p_mgmt)
6827 bssid = wpa_s->parent->own_addr;
6828 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006829 bssid = wpa_s->own_addr;
6830 } else {
6831 role = P2P_INVITE_ROLE_CLIENT;
6832 peer_addr = ssid->bssid;
6833 }
6834 wpa_s->pending_invite_ssid_id = ssid->id;
6835
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006836 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006837 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006838 role == P2P_INVITE_ROLE_GO,
6839 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006840 if (res)
6841 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006843 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6844 return -1;
6845
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006846 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6847
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006848 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6849 no_pref_freq_given && pref_freq > 0 &&
6850 wpa_s->num_multichan_concurrent > 1 &&
6851 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6852 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6853 pref_freq);
6854 pref_freq = 0;
6855 }
6856
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006857 /*
6858 * Stop any find/listen operations before invitation and possibly
6859 * connection establishment.
6860 */
6861 wpas_p2p_stop_find_oper(wpa_s);
6862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006863 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006864 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006865 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006866}
6867
6868
6869/* Invite to join an active group */
6870int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6871 const u8 *peer_addr, const u8 *go_dev_addr)
6872{
6873 struct wpa_global *global = wpa_s->global;
6874 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006875 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006877 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006878 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006879 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006880 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881
Jouni Malinen31be0a42012-08-31 21:20:51 +03006882 wpa_s->p2p_persistent_go_freq = 0;
6883 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006884 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006885 wpa_s->p2p_go_vht_center_freq2 = 0;
6886 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006888 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6889 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6890 break;
6891 }
6892 if (wpa_s == NULL) {
6893 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6894 return -1;
6895 }
6896
6897 ssid = wpa_s->current_ssid;
6898 if (ssid == NULL) {
6899 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6900 "invitation");
6901 return -1;
6902 }
6903
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006904 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006905 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006906 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006907 ssid->ssid, ssid->ssid_len);
6908
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006909 if (ssid->mode == WPAS_MODE_P2P_GO) {
6910 role = P2P_INVITE_ROLE_ACTIVE_GO;
6911 bssid = wpa_s->own_addr;
6912 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006913 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006914 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006915 } else {
6916 role = P2P_INVITE_ROLE_CLIENT;
6917 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6918 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6919 "invite to current group");
6920 return -1;
6921 }
6922 bssid = wpa_s->bssid;
6923 if (go_dev_addr == NULL &&
6924 !is_zero_ether_addr(wpa_s->go_dev_addr))
6925 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006926 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6927 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006929 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006931 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6932 return -1;
6933
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006934 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006935 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006936 role == P2P_INVITE_ROLE_ACTIVE_GO,
6937 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006938 if (res)
6939 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006940 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006941
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006942 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006943 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006944 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006945}
6946
6947
6948void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6949{
6950 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006951 u8 go_dev_addr[ETH_ALEN];
6952 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006953 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006954 u8 ip[3 * 4];
6955 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006956
Dmitry Shmidt04949592012-07-19 12:16:46 -07006957 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6958 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006959 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006960 }
6961
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006962 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006963 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006964
6965 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006966 if (!wpa_s->p2p_go_group_formation_completed &&
6967 wpa_s->global->p2p_group_formation == wpa_s) {
6968 wpa_dbg(wpa_s, MSG_DEBUG,
6969 "P2P: Marking group formation completed on client on data connection");
6970 wpa_s->p2p_go_group_formation_completed = 1;
6971 wpa_s->global->p2p_group_formation = NULL;
6972 wpa_s->p2p_in_provisioning = 0;
6973 wpa_s->p2p_in_invitation = 0;
6974 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006976 os_memset(go_dev_addr, 0, ETH_ALEN);
6977 if (ssid->bssid_set)
6978 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6979 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6980 ssid->ssid_len);
6981 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6982
6983 if (wpa_s->global->p2p_group_formation == wpa_s)
6984 wpa_s->global->p2p_group_formation = NULL;
6985
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006986 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6987 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006988
6989 ip_addr[0] = '\0';
6990 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006991 int res;
6992
6993 res = os_snprintf(ip_addr, sizeof(ip_addr),
6994 " ip_addr=%u.%u.%u.%u "
6995 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6996 ip[0], ip[1], ip[2], ip[3],
6997 ip[4], ip[5], ip[6], ip[7],
6998 ip[8], ip[9], ip[10], ip[11]);
6999 if (os_snprintf_error(sizeof(ip_addr), res))
7000 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007001 }
7002
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007003 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7004 ssid->passphrase == NULL && ssid->psk_set ?
7005 ssid->psk : NULL,
7006 ssid->passphrase, go_dev_addr, persistent,
7007 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007008
7009 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007010 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7011 ssid, go_dev_addr);
7012
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007013 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007014}
7015
7016
7017int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7018 u32 interval1, u32 duration2, u32 interval2)
7019{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007020 int ret;
7021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7023 return -1;
7024
7025 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7026 wpa_s->current_ssid == NULL ||
7027 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7028 return -1;
7029
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007030 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7031 wpa_s->own_addr, wpa_s->assoc_freq,
7032 duration1, interval1, duration2, interval2);
7033 if (ret == 0)
7034 wpa_s->waiting_presence_resp = 1;
7035
7036 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007037}
7038
7039
7040int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7041 unsigned int interval)
7042{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007043 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7044 return -1;
7045
7046 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7047}
7048
7049
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007050static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7051{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007052 if (wpa_s->current_ssid == NULL) {
7053 /*
7054 * current_ssid can be cleared when P2P client interface gets
7055 * disconnected, so assume this interface was used as P2P
7056 * client.
7057 */
7058 return 1;
7059 }
7060 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007061 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7062}
7063
7064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007065static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7066{
7067 struct wpa_supplicant *wpa_s = eloop_ctx;
7068
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007069 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007070 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7071 "disabled");
7072 return;
7073 }
7074
Dmitry Shmidt04949592012-07-19 12:16:46 -07007075 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7076 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007077 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007078}
7079
7080
7081static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7082{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007083 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084
Dmitry Shmidt04949592012-07-19 12:16:46 -07007085 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7086 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7089 return;
7090
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007091 timeout = wpa_s->conf->p2p_group_idle;
7092 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7093 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7094 timeout = P2P_MAX_CLIENT_IDLE;
7095
7096 if (timeout == 0)
7097 return;
7098
Dmitry Shmidt04949592012-07-19 12:16:46 -07007099 if (timeout < 0) {
7100 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7101 timeout = 0; /* special client mode no-timeout */
7102 else
7103 return;
7104 }
7105
7106 if (wpa_s->p2p_in_provisioning) {
7107 /*
7108 * Use the normal group formation timeout during the
7109 * provisioning phase to avoid terminating this process too
7110 * early due to group idle timeout.
7111 */
7112 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7113 "during provisioning");
7114 return;
7115 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307116
Dmitry Shmidt04949592012-07-19 12:16:46 -07007117 if (wpa_s->show_group_started) {
7118 /*
7119 * Use the normal group formation timeout between the end of
7120 * the provisioning phase and completion of 4-way handshake to
7121 * avoid terminating this process too early due to group idle
7122 * timeout.
7123 */
7124 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7125 "while waiting for initial 4-way handshake to "
7126 "complete");
7127 return;
7128 }
7129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007131 timeout);
7132 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7133 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007134}
7135
7136
Jouni Malinen2b89da82012-08-31 22:04:41 +03007137/* Returns 1 if the interface was removed */
7138int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7139 u16 reason_code, const u8 *ie, size_t ie_len,
7140 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007141{
7142 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007143 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007144
Dmitry Shmidt04949592012-07-19 12:16:46 -07007145 if (!locally_generated)
7146 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7147 ie_len);
7148
7149 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7150 wpa_s->current_ssid &&
7151 wpa_s->current_ssid->p2p_group &&
7152 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7153 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7154 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007155 if (wpas_p2p_group_delete(wpa_s,
7156 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7157 > 0)
7158 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007159 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007160
7161 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007162}
7163
7164
7165void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007166 u16 reason_code, const u8 *ie, size_t ie_len,
7167 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007168{
7169 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7170 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007171
Dmitry Shmidt04949592012-07-19 12:16:46 -07007172 if (!locally_generated)
7173 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7174 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007175}
7176
7177
7178void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7179{
7180 struct p2p_data *p2p = wpa_s->global->p2p;
7181
7182 if (p2p == NULL)
7183 return;
7184
7185 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7186 return;
7187
7188 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7189 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7190
7191 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7192 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7193
7194 if (wpa_s->wps &&
7195 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7196 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7197
7198 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7199 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7200
7201 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7202 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7203 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7204 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7205 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7206 }
7207
7208 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7209 p2p_set_sec_dev_types(p2p,
7210 (void *) wpa_s->conf->sec_device_type,
7211 wpa_s->conf->num_sec_device_types);
7212
7213 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7214 int i;
7215 p2p_remove_wps_vendor_extensions(p2p);
7216 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7217 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7218 continue;
7219 p2p_add_wps_vendor_extension(
7220 p2p, wpa_s->conf->wps_vendor_ext[i]);
7221 }
7222 }
7223
7224 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7225 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7226 char country[3];
7227 country[0] = wpa_s->conf->country[0];
7228 country[1] = wpa_s->conf->country[1];
7229 country[2] = 0x04;
7230 p2p_set_country(p2p, country);
7231 }
7232
7233 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7234 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7235 wpa_s->conf->p2p_ssid_postfix ?
7236 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7237 0);
7238 }
7239
7240 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7241 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007242
7243 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7244 u8 reg_class, channel;
7245 int ret;
7246 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007247 u8 channel_forced;
7248
Jouni Malinen75ecf522011-06-27 15:19:46 -07007249 if (wpa_s->conf->p2p_listen_reg_class &&
7250 wpa_s->conf->p2p_listen_channel) {
7251 reg_class = wpa_s->conf->p2p_listen_reg_class;
7252 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007253 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007254 } else {
7255 reg_class = 81;
7256 /*
7257 * Pick one of the social channels randomly as the
7258 * listen channel.
7259 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007260 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7261 channel = 1;
7262 else
7263 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007264 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007265 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007266 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7267 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007268 if (ret)
7269 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7270 "failed: %d", ret);
7271 }
7272 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7273 u8 op_reg_class, op_channel, cfg_op_channel;
7274 int ret = 0;
7275 unsigned int r;
7276 if (wpa_s->conf->p2p_oper_reg_class &&
7277 wpa_s->conf->p2p_oper_channel) {
7278 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7279 op_channel = wpa_s->conf->p2p_oper_channel;
7280 cfg_op_channel = 1;
7281 } else {
7282 op_reg_class = 81;
7283 /*
7284 * Use random operation channel from (1, 6, 11)
7285 *if no other preference is indicated.
7286 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007287 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7288 op_channel = 1;
7289 else
7290 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007291 cfg_op_channel = 0;
7292 }
7293 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7294 cfg_op_channel);
7295 if (ret)
7296 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7297 "failed: %d", ret);
7298 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007299
7300 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7301 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7302 wpa_s->conf->p2p_pref_chan) < 0) {
7303 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7304 "update failed");
7305 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007306
7307 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7308 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7309 "update failed");
7310 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007311 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007312
7313 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7314 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007315}
7316
7317
7318int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7319 int duration)
7320{
7321 if (!wpa_s->ap_iface)
7322 return -1;
7323 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7324 duration);
7325}
7326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007327
7328int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7329{
7330 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7331 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007332
7333 wpa_s->global->cross_connection = enabled;
7334 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7335
7336 if (!enabled) {
7337 struct wpa_supplicant *iface;
7338
7339 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7340 {
7341 if (iface->cross_connect_enabled == 0)
7342 continue;
7343
7344 iface->cross_connect_enabled = 0;
7345 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007346 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007347 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7348 iface->ifname,
7349 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 }
7351 }
7352
7353 return 0;
7354}
7355
7356
7357static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7358{
7359 struct wpa_supplicant *iface;
7360
7361 if (!uplink->global->cross_connection)
7362 return;
7363
7364 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7365 if (!iface->cross_connect_enabled)
7366 continue;
7367 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7368 0)
7369 continue;
7370 if (iface->ap_iface == NULL)
7371 continue;
7372 if (iface->cross_connect_in_use)
7373 continue;
7374
7375 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007376 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007377 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7378 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007379 }
7380}
7381
7382
7383static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7384{
7385 struct wpa_supplicant *iface;
7386
7387 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7388 if (!iface->cross_connect_enabled)
7389 continue;
7390 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7391 0)
7392 continue;
7393 if (!iface->cross_connect_in_use)
7394 continue;
7395
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007396 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007397 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7398 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007399 iface->cross_connect_in_use = 0;
7400 }
7401}
7402
7403
7404void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7405{
7406 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7407 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7408 wpa_s->cross_connect_disallowed)
7409 wpas_p2p_disable_cross_connect(wpa_s);
7410 else
7411 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007412 if (!wpa_s->ap_iface &&
7413 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7414 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007415}
7416
7417
7418void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7419{
7420 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007421 if (!wpa_s->ap_iface &&
7422 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7423 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007424 wpas_p2p_set_group_idle_timeout(wpa_s);
7425}
7426
7427
7428static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7429{
7430 struct wpa_supplicant *iface;
7431
7432 if (!wpa_s->global->cross_connection)
7433 return;
7434
7435 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7436 if (iface == wpa_s)
7437 continue;
7438 if (iface->drv_flags &
7439 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7440 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007441 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7442 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007443 continue;
7444
7445 wpa_s->cross_connect_enabled = 1;
7446 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7447 sizeof(wpa_s->cross_connect_uplink));
7448 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7449 "%s to %s whenever uplink is available",
7450 wpa_s->ifname, wpa_s->cross_connect_uplink);
7451
7452 if (iface->ap_iface || iface->current_ssid == NULL ||
7453 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7454 iface->cross_connect_disallowed ||
7455 iface->wpa_state != WPA_COMPLETED)
7456 break;
7457
7458 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007459 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007460 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7461 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007462 break;
7463 }
7464}
7465
7466
7467int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7468{
7469 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7470 !wpa_s->p2p_in_provisioning)
7471 return 0; /* not P2P client operation */
7472
7473 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7474 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007475 if (wpa_s != wpa_s->p2pdev)
7476 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007477 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478 return 1;
7479}
7480
7481
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007482void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7483{
7484 struct wpa_supplicant *wpa_s = eloop_ctx;
7485 wpas_p2p_notif_pbc_overlap(wpa_s);
7486}
7487
7488
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007489void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7490 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007491{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007492 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007493 struct wpa_used_freq_data *freqs = NULL;
7494 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007495
7496 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7497 return;
7498
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007499 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7500 if (!freqs)
7501 return;
7502
7503 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007505 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007506 os_memset(&cli_chan, 0, sizeof(cli_chan));
7507 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007508 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7509 "channel list");
7510 return;
7511 }
7512
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007513 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007514
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007515 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007516
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007517 /*
7518 * The used frequencies map changed, so it is possible that a GO is
7519 * using a channel that is no longer valid for P2P use. It is also
7520 * possible that due to policy consideration, it would be preferable to
7521 * move it to a frequency already used by other station interfaces.
7522 */
7523 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7524
7525 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007526}
7527
7528
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007529static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7530 struct wpa_scan_results *scan_res)
7531{
7532 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7533}
7534
7535
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7537{
7538 struct wpa_global *global = wpa_s->global;
7539 int found = 0;
7540 const u8 *peer;
7541
7542 if (global->p2p == NULL)
7543 return -1;
7544
7545 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7546
7547 if (wpa_s->pending_interface_name[0] &&
7548 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7549 found = 1;
7550
7551 peer = p2p_get_go_neg_peer(global->p2p);
7552 if (peer) {
7553 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7554 MACSTR, MAC2STR(peer));
7555 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007556 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007557 }
7558
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007559 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7560 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7561 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7562 found = 1;
7563 }
7564
7565 if (wpa_s->pending_pd_before_join) {
7566 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7567 wpa_s->pending_pd_before_join = 0;
7568 found = 1;
7569 }
7570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007571 wpas_p2p_stop_find(wpa_s);
7572
7573 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7574 if (wpa_s == global->p2p_group_formation &&
7575 (wpa_s->p2p_in_provisioning ||
7576 wpa_s->parent->pending_interface_type ==
7577 WPA_IF_P2P_CLIENT)) {
7578 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7579 "formation found - cancelling",
7580 wpa_s->ifname);
7581 found = 1;
7582 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007583 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007584 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007585 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007586 break;
7587 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007588 wpas_p2p_group_delete(wpa_s,
7589 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007590 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007591 } else if (wpa_s->p2p_in_invitation) {
7592 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7593 wpa_s->ifname);
7594 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007595 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007596 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007597 }
7598 }
7599
7600 if (!found) {
7601 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7602 return -1;
7603 }
7604
7605 return 0;
7606}
7607
7608
7609void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7610{
7611 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7612 return;
7613
7614 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7615 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007616 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007617}
7618
7619
7620void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7621 int freq_24, int freq_5, int freq_overall)
7622{
7623 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007624 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007625 return;
7626 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7627}
7628
7629
7630int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7631{
7632 u8 peer[ETH_ALEN];
7633 struct p2p_data *p2p = wpa_s->global->p2p;
7634
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007635 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007636 return -1;
7637
7638 if (hwaddr_aton(addr, peer))
7639 return -1;
7640
7641 return p2p_unauthorize(p2p, peer);
7642}
7643
7644
7645/**
7646 * wpas_p2p_disconnect - Disconnect from a P2P Group
7647 * @wpa_s: Pointer to wpa_supplicant data
7648 * Returns: 0 on success, -1 on failure
7649 *
7650 * This can be used to disconnect from a group in which the local end is a P2P
7651 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7652 * virtual network interface was created for this group, that interface will be
7653 * removed. Otherwise, only the configured P2P group network will be removed
7654 * from the interface.
7655 */
7656int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7657{
7658
7659 if (wpa_s == NULL)
7660 return -1;
7661
Jouni Malinen2b89da82012-08-31 22:04:41 +03007662 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7663 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007664}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007665
7666
7667int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7668{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007669 int ret;
7670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007671 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7672 return 0;
7673
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007674 ret = p2p_in_progress(wpa_s->global->p2p);
7675 if (ret == 0) {
7676 /*
7677 * Check whether there is an ongoing WPS provisioning step (or
7678 * other parts of group formation) on another interface since
7679 * p2p_in_progress() does not report this to avoid issues for
7680 * scans during such provisioning step.
7681 */
7682 if (wpa_s->global->p2p_group_formation &&
7683 wpa_s->global->p2p_group_formation != wpa_s) {
7684 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7685 "in group formation",
7686 wpa_s->global->p2p_group_formation->ifname);
7687 ret = 1;
7688 }
7689 }
7690
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007691 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007692 struct os_reltime now;
7693 os_get_reltime(&now);
7694 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7695 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007696 /* Wait for the first client has expired */
7697 wpa_s->global->p2p_go_wait_client.sec = 0;
7698 } else {
7699 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7700 ret = 1;
7701 }
7702 }
7703
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007704 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007705}
7706
7707
7708void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7709 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007710{
7711 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7712 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007713 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007714 /**
7715 * Remove the network by scheduling the group formation
7716 * timeout to happen immediately. The teardown code
7717 * needs to be scheduled to run asynch later so that we
7718 * don't delete data from under ourselves unexpectedly.
7719 * Calling wpas_p2p_group_formation_timeout directly
7720 * causes a series of crashes in WPS failure scenarios.
7721 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007722 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7723 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007724 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007725 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007726 }
7727}
7728
7729
7730struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007731 const u8 *addr, const u8 *ssid,
7732 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007733{
7734 struct wpa_ssid *s;
7735 size_t i;
7736
7737 for (s = wpa_s->conf->ssid; s; s = s->next) {
7738 if (s->disabled != 2)
7739 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007740 if (ssid &&
7741 (ssid_len != s->ssid_len ||
7742 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7743 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007744 if (addr == NULL) {
7745 if (s->mode == WPAS_MODE_P2P_GO)
7746 return s;
7747 continue;
7748 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007749 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7750 return s; /* peer is GO in the persistent group */
7751 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7752 continue;
7753 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007754 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007755 addr, ETH_ALEN) == 0)
7756 return s; /* peer is P2P client in persistent
7757 * group */
7758 }
7759 }
7760
7761 return NULL;
7762}
7763
7764
7765void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7766 const u8 *addr)
7767{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007768 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007769 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007770 /*
7771 * This can happen if WPS provisioning step is not terminated
7772 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7773 * peer was able to connect, there is no need to time out group
7774 * formation after this, though. In addition, this is used with
7775 * the initial connection wait on the GO as a separate formation
7776 * timeout and as such, expected to be hit after the initial WPS
7777 * provisioning step.
7778 */
7779 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007780
7781 if (!wpa_s->p2p_go_group_formation_completed &&
7782 !wpa_s->group_formation_reported) {
7783 /*
7784 * GO has not yet notified group formation success since
7785 * the WPS step was not completed cleanly. Do that
7786 * notification now since the P2P Client was able to
7787 * connect and as such, must have received the
7788 * credential from the WPS step.
7789 */
7790 if (wpa_s->global->p2p)
7791 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007792 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007793 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007794 }
7795 if (!wpa_s->p2p_go_group_formation_completed) {
7796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7797 wpa_s->p2p_go_group_formation_completed = 1;
7798 wpa_s->global->p2p_group_formation = NULL;
7799 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007800 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007801 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007802 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007803 if (addr == NULL)
7804 return;
7805 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7806}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007807
Dmitry Shmidt04949592012-07-19 12:16:46 -07007808
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007809static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7810 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007811{
7812 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007813 int ret = 0;
7814
Dmitry Shmidt04949592012-07-19 12:16:46 -07007815 if (wpa_s->global->p2p_group_formation)
7816 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007817 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007818 offchannel_send_action_done(wpa_s);
7819 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007820 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007821 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7822 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7823 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7824 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007825 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007826 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007827 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007828 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007829 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007830 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007831 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007832}
7833
7834
7835int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7836{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007837 int res;
7838
Dmitry Shmidt04949592012-07-19 12:16:46 -07007839 if (!wpa_s->p2p_fallback_to_go_neg ||
7840 wpa_s->p2p_in_provisioning <= 5)
7841 return 0;
7842
7843 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7844 return 0; /* peer operating as a GO */
7845
7846 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7847 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007848 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007849 "reason=GO-not-found");
7850 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007851
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007852 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007853}
7854
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007855
7856unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7857{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007858 struct wpa_supplicant *ifs;
7859
7860 if (wpa_s->wpa_state > WPA_SCANNING) {
7861 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7862 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007863 wpa_s->conf->p2p_search_delay);
7864 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007865 }
7866
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007867 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7868 radio_list) {
7869 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007870 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7871 "delay due to concurrent operation on "
7872 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007873 wpa_s->conf->p2p_search_delay,
7874 ifs->ifname);
7875 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007876 }
7877 }
7878
7879 return 0;
7880}
7881
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007882
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007883static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7884 struct wpa_ssid *s, const u8 *addr,
7885 int iface_addr)
7886{
7887 struct psk_list_entry *psk, *tmp;
7888 int changed = 0;
7889
7890 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7891 list) {
7892 if ((iface_addr && !psk->p2p &&
7893 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7894 (!iface_addr && psk->p2p &&
7895 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7896 wpa_dbg(wpa_s, MSG_DEBUG,
7897 "P2P: Remove persistent group PSK list entry for "
7898 MACSTR " p2p=%u",
7899 MAC2STR(psk->addr), psk->p2p);
7900 dl_list_del(&psk->list);
7901 os_free(psk);
7902 changed++;
7903 }
7904 }
7905
7906 return changed;
7907}
7908
7909
7910void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7911 const u8 *p2p_dev_addr,
7912 const u8 *psk, size_t psk_len)
7913{
7914 struct wpa_ssid *ssid = wpa_s->current_ssid;
7915 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007916 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007917
7918 if (psk_len != sizeof(p->psk))
7919 return;
7920
7921 if (p2p_dev_addr) {
7922 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7923 " p2p_dev_addr=" MACSTR,
7924 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7925 if (is_zero_ether_addr(p2p_dev_addr))
7926 p2p_dev_addr = NULL;
7927 } else {
7928 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7929 MAC2STR(mac_addr));
7930 }
7931
7932 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7933 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7934 /* To be added to persistent group once created */
7935 if (wpa_s->global->add_psk == NULL) {
7936 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7937 if (wpa_s->global->add_psk == NULL)
7938 return;
7939 }
7940 p = wpa_s->global->add_psk;
7941 if (p2p_dev_addr) {
7942 p->p2p = 1;
7943 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7944 } else {
7945 p->p2p = 0;
7946 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7947 }
7948 os_memcpy(p->psk, psk, psk_len);
7949 return;
7950 }
7951
7952 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7953 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7954 return;
7955 }
7956
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007957 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007958 ssid->ssid_len);
7959 if (!persistent) {
7960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7961 return;
7962 }
7963
7964 p = os_zalloc(sizeof(*p));
7965 if (p == NULL)
7966 return;
7967 if (p2p_dev_addr) {
7968 p->p2p = 1;
7969 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7970 } else {
7971 p->p2p = 0;
7972 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7973 }
7974 os_memcpy(p->psk, psk, psk_len);
7975
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007976 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7977 (last = dl_list_last(&persistent->psk_list,
7978 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007979 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7980 MACSTR " (p2p=%u) to make room for a new one",
7981 MAC2STR(last->addr), last->p2p);
7982 dl_list_del(&last->list);
7983 os_free(last);
7984 }
7985
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007986 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007987 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7988 p2p_dev_addr == NULL);
7989 if (p2p_dev_addr) {
7990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7991 MACSTR, MAC2STR(p2p_dev_addr));
7992 } else {
7993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7994 MAC2STR(mac_addr));
7995 }
7996 dl_list_add(&persistent->psk_list, &p->list);
7997
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007998 if (wpa_s->p2pdev->conf->update_config &&
7999 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008000 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008001}
8002
8003
8004static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8005 struct wpa_ssid *s, const u8 *addr,
8006 int iface_addr)
8007{
8008 int res;
8009
8010 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008011 if (res > 0 && wpa_s->conf->update_config &&
8012 wpa_config_write(wpa_s->confname, wpa_s->conf))
8013 wpa_dbg(wpa_s, MSG_DEBUG,
8014 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008015}
8016
8017
8018static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8019 const u8 *peer, int iface_addr)
8020{
8021 struct hostapd_data *hapd;
8022 struct hostapd_wpa_psk *psk, *prev, *rem;
8023 struct sta_info *sta;
8024
8025 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8026 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8027 return;
8028
8029 /* Remove per-station PSK entry */
8030 hapd = wpa_s->ap_iface->bss[0];
8031 prev = NULL;
8032 psk = hapd->conf->ssid.wpa_psk;
8033 while (psk) {
8034 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8035 (!iface_addr &&
8036 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8037 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8038 MACSTR " iface_addr=%d",
8039 MAC2STR(peer), iface_addr);
8040 if (prev)
8041 prev->next = psk->next;
8042 else
8043 hapd->conf->ssid.wpa_psk = psk->next;
8044 rem = psk;
8045 psk = psk->next;
8046 os_free(rem);
8047 } else {
8048 prev = psk;
8049 psk = psk->next;
8050 }
8051 }
8052
8053 /* Disconnect from group */
8054 if (iface_addr)
8055 sta = ap_get_sta(hapd, peer);
8056 else
8057 sta = ap_get_sta_p2p(hapd, peer);
8058 if (sta) {
8059 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8060 " (iface_addr=%d) from group",
8061 MAC2STR(peer), iface_addr);
8062 hostapd_drv_sta_deauth(hapd, sta->addr,
8063 WLAN_REASON_DEAUTH_LEAVING);
8064 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8065 }
8066}
8067
8068
8069void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8070 int iface_addr)
8071{
8072 struct wpa_ssid *s;
8073 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008074 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008075
8076 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8077
8078 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008079 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008080 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8081 continue;
8082 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008083 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8084 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008085 }
8086
8087 /* Remove from any operating group */
8088 for (w = wpa_s->global->ifaces; w; w = w->next)
8089 wpas_p2p_remove_client_go(w, peer, iface_addr);
8090}
8091
8092
8093static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8094{
8095 struct wpa_supplicant *wpa_s = eloop_ctx;
8096 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8097}
8098
8099
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008100static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8101{
8102 struct wpa_supplicant *wpa_s = eloop_ctx;
8103
8104 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8105 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8106}
8107
8108
8109int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8110 struct wpa_ssid *ssid)
8111{
8112 struct wpa_supplicant *iface;
8113
8114 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8115 if (!iface->current_ssid ||
8116 iface->current_ssid->frequency == freq ||
8117 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8118 !iface->current_ssid->p2p_group))
8119 continue;
8120
8121 /* Remove the connection with least priority */
8122 if (!wpas_is_p2p_prioritized(iface)) {
8123 /* STA connection has priority over existing
8124 * P2P connection, so remove the interface. */
8125 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8126 eloop_register_timeout(0, 0,
8127 wpas_p2p_group_freq_conflict,
8128 iface, NULL);
8129 /* If connection in progress is P2P connection, do not
8130 * proceed for the connection. */
8131 if (wpa_s == iface)
8132 return -1;
8133 else
8134 return 0;
8135 } else {
8136 /* P2P connection has priority, disable the STA network
8137 */
8138 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8139 ssid);
8140 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8141 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8142 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8143 ETH_ALEN);
8144 /* If P2P connection is in progress, continue
8145 * connecting...*/
8146 if (wpa_s == iface)
8147 return 0;
8148 else
8149 return -1;
8150 }
8151 }
8152
8153 return 0;
8154}
8155
8156
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008157int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8158{
8159 struct wpa_ssid *ssid = wpa_s->current_ssid;
8160
8161 if (ssid == NULL || !ssid->p2p_group)
8162 return 0;
8163
8164 if (wpa_s->p2p_last_4way_hs_fail &&
8165 wpa_s->p2p_last_4way_hs_fail == ssid) {
8166 u8 go_dev_addr[ETH_ALEN];
8167 struct wpa_ssid *persistent;
8168
8169 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8170 ssid->ssid,
8171 ssid->ssid_len) <= 0) {
8172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8173 goto disconnect;
8174 }
8175
8176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8177 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008178 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008179 ssid->ssid,
8180 ssid->ssid_len);
8181 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8183 goto disconnect;
8184 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008185 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008186 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8187 persistent->id);
8188 disconnect:
8189 wpa_s->p2p_last_4way_hs_fail = NULL;
8190 /*
8191 * Remove the group from a timeout to avoid issues with caller
8192 * continuing to use the interface if this is on a P2P group
8193 * interface.
8194 */
8195 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8196 wpa_s, NULL);
8197 return 1;
8198 }
8199
8200 wpa_s->p2p_last_4way_hs_fail = ssid;
8201 return 0;
8202}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008203
8204
8205#ifdef CONFIG_WPS_NFC
8206
8207static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8208 struct wpabuf *p2p)
8209{
8210 struct wpabuf *ret;
8211 size_t wsc_len;
8212
8213 if (p2p == NULL) {
8214 wpabuf_free(wsc);
8215 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8216 return NULL;
8217 }
8218
8219 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8220 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8221 if (ret == NULL) {
8222 wpabuf_free(wsc);
8223 wpabuf_free(p2p);
8224 return NULL;
8225 }
8226
8227 wpabuf_put_be16(ret, wsc_len);
8228 if (wsc)
8229 wpabuf_put_buf(ret, wsc);
8230 wpabuf_put_be16(ret, wpabuf_len(p2p));
8231 wpabuf_put_buf(ret, p2p);
8232
8233 wpabuf_free(wsc);
8234 wpabuf_free(p2p);
8235 wpa_hexdump_buf(MSG_DEBUG,
8236 "P2P: Generated NFC connection handover message", ret);
8237
8238 if (ndef && ret) {
8239 struct wpabuf *tmp;
8240 tmp = ndef_build_p2p(ret);
8241 wpabuf_free(ret);
8242 if (tmp == NULL) {
8243 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8244 return NULL;
8245 }
8246 ret = tmp;
8247 }
8248
8249 return ret;
8250}
8251
8252
8253static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8254 struct wpa_ssid **ssid, u8 *go_dev_addr)
8255{
8256 struct wpa_supplicant *iface;
8257
8258 if (go_dev_addr)
8259 os_memset(go_dev_addr, 0, ETH_ALEN);
8260 if (ssid)
8261 *ssid = NULL;
8262 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8263 if (iface->wpa_state < WPA_ASSOCIATING ||
8264 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8265 !iface->current_ssid->p2p_group ||
8266 iface->current_ssid->mode != WPAS_MODE_INFRA)
8267 continue;
8268 if (ssid)
8269 *ssid = iface->current_ssid;
8270 if (go_dev_addr)
8271 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8272 return iface->assoc_freq;
8273 }
8274 return 0;
8275}
8276
8277
8278struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8279 int ndef)
8280{
8281 struct wpabuf *wsc, *p2p;
8282 struct wpa_ssid *ssid;
8283 u8 go_dev_addr[ETH_ALEN];
8284 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8285
8286 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8287 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8288 return NULL;
8289 }
8290
8291 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8292 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8293 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8294 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8295 return NULL;
8296 }
8297
8298 if (cli_freq == 0) {
8299 wsc = wps_build_nfc_handover_req_p2p(
8300 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8301 } else
8302 wsc = NULL;
8303 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8304 go_dev_addr, ssid ? ssid->ssid : NULL,
8305 ssid ? ssid->ssid_len : 0);
8306
8307 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8308}
8309
8310
8311struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8312 int ndef, int tag)
8313{
8314 struct wpabuf *wsc, *p2p;
8315 struct wpa_ssid *ssid;
8316 u8 go_dev_addr[ETH_ALEN];
8317 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8318
8319 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8320 return NULL;
8321
8322 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8323 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8324 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8325 return NULL;
8326
8327 if (cli_freq == 0) {
8328 wsc = wps_build_nfc_handover_sel_p2p(
8329 wpa_s->parent->wps,
8330 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8331 DEV_PW_NFC_CONNECTION_HANDOVER,
8332 wpa_s->conf->wps_nfc_dh_pubkey,
8333 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8334 } else
8335 wsc = NULL;
8336 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8337 go_dev_addr, ssid ? ssid->ssid : NULL,
8338 ssid ? ssid->ssid_len : 0);
8339
8340 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8341}
8342
8343
8344static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8345 struct p2p_nfc_params *params)
8346{
8347 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8348 "connection handover (freq=%d)",
8349 params->go_freq);
8350
8351 if (params->go_freq && params->go_ssid_len) {
8352 wpa_s->p2p_wps_method = WPS_NFC;
8353 wpa_s->pending_join_wps_method = WPS_NFC;
8354 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8355 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8356 ETH_ALEN);
8357 return wpas_p2p_join_start(wpa_s, params->go_freq,
8358 params->go_ssid,
8359 params->go_ssid_len);
8360 }
8361
8362 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8363 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008364 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008365 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8366 params->go_ssid_len ? params->go_ssid : NULL,
8367 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008368}
8369
8370
8371static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8372 struct p2p_nfc_params *params, int tag)
8373{
8374 int res, persistent;
8375 struct wpa_ssid *ssid;
8376
8377 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8378 "connection handover");
8379 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8380 ssid = wpa_s->current_ssid;
8381 if (ssid == NULL)
8382 continue;
8383 if (ssid->mode != WPAS_MODE_P2P_GO)
8384 continue;
8385 if (wpa_s->ap_iface == NULL)
8386 continue;
8387 break;
8388 }
8389 if (wpa_s == NULL) {
8390 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8391 return -1;
8392 }
8393
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008394 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008395 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008396 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008397 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8398 return -1;
8399 }
8400 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008401 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8402 wpa_s->p2pdev->p2p_oob_dev_pw,
8403 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8404 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008405 if (res)
8406 return res;
8407
8408 if (!tag) {
8409 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8410 return 0;
8411 }
8412
8413 if (!params->peer ||
8414 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8415 return 0;
8416
8417 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8418 " to join", MAC2STR(params->peer->p2p_device_addr));
8419
8420 wpa_s->global->p2p_invite_group = wpa_s;
8421 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008422 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008423 params->peer->p2p_device_addr,
8424 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008425 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008426
8427 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8428 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8429 ssid->ssid, ssid->ssid_len, ssid->frequency,
8430 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008431 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008432}
8433
8434
8435static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8436 struct p2p_nfc_params *params,
8437 int forced_freq)
8438{
8439 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8440 "connection handover");
8441 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8442 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008443 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008444 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8445 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008446}
8447
8448
8449static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8450 struct p2p_nfc_params *params,
8451 int forced_freq)
8452{
8453 int res;
8454
8455 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8456 "connection handover");
8457 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8458 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008459 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008460 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8461 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008462 if (res)
8463 return res;
8464
8465 res = wpas_p2p_listen(wpa_s, 60);
8466 if (res) {
8467 p2p_unauthorize(wpa_s->global->p2p,
8468 params->peer->p2p_device_addr);
8469 }
8470
8471 return res;
8472}
8473
8474
8475static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8476 const struct wpabuf *data,
8477 int sel, int tag, int forced_freq)
8478{
8479 const u8 *pos, *end;
8480 u16 len, id;
8481 struct p2p_nfc_params params;
8482 int res;
8483
8484 os_memset(&params, 0, sizeof(params));
8485 params.sel = sel;
8486
8487 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8488
8489 pos = wpabuf_head(data);
8490 end = pos + wpabuf_len(data);
8491
8492 if (end - pos < 2) {
8493 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8494 "attributes");
8495 return -1;
8496 }
8497 len = WPA_GET_BE16(pos);
8498 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008499 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008500 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8501 "attributes");
8502 return -1;
8503 }
8504 params.wsc_attr = pos;
8505 params.wsc_len = len;
8506 pos += len;
8507
8508 if (end - pos < 2) {
8509 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8510 "attributes");
8511 return -1;
8512 }
8513 len = WPA_GET_BE16(pos);
8514 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008515 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008516 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8517 "attributes");
8518 return -1;
8519 }
8520 params.p2p_attr = pos;
8521 params.p2p_len = len;
8522 pos += len;
8523
8524 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8525 params.wsc_attr, params.wsc_len);
8526 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8527 params.p2p_attr, params.p2p_len);
8528 if (pos < end) {
8529 wpa_hexdump(MSG_DEBUG,
8530 "P2P: Ignored extra data after P2P attributes",
8531 pos, end - pos);
8532 }
8533
8534 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8535 if (res)
8536 return res;
8537
8538 if (params.next_step == NO_ACTION)
8539 return 0;
8540
8541 if (params.next_step == BOTH_GO) {
8542 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8543 MAC2STR(params.peer->p2p_device_addr));
8544 return 0;
8545 }
8546
8547 if (params.next_step == PEER_CLIENT) {
8548 if (!is_zero_ether_addr(params.go_dev_addr)) {
8549 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8550 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8551 " ssid=\"%s\"",
8552 MAC2STR(params.peer->p2p_device_addr),
8553 params.go_freq,
8554 MAC2STR(params.go_dev_addr),
8555 wpa_ssid_txt(params.go_ssid,
8556 params.go_ssid_len));
8557 } else {
8558 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8559 "peer=" MACSTR " freq=%d",
8560 MAC2STR(params.peer->p2p_device_addr),
8561 params.go_freq);
8562 }
8563 return 0;
8564 }
8565
8566 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8567 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8568 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8569 return 0;
8570 }
8571
8572 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8573 wpa_s->p2p_oob_dev_pw = NULL;
8574
8575 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8576 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8577 "received");
8578 return -1;
8579 }
8580
8581 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8582 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8583 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8584 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8585 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8586 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8587 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8588
8589 if (tag) {
8590 if (id < 0x10) {
8591 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8592 "peer OOB Device Password Id %u", id);
8593 return -1;
8594 }
8595 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8596 "Device Password Id %u", id);
8597 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8598 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8599 params.oob_dev_pw_len -
8600 WPS_OOB_PUBKEY_HASH_LEN - 2);
8601 wpa_s->p2p_oob_dev_pw_id = id;
8602 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8603 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8604 params.oob_dev_pw_len -
8605 WPS_OOB_PUBKEY_HASH_LEN - 2);
8606 if (wpa_s->p2p_oob_dev_pw == NULL)
8607 return -1;
8608
8609 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8610 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8611 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8612 return -1;
8613 } else {
8614 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8615 "without Device Password");
8616 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8617 }
8618
8619 switch (params.next_step) {
8620 case NO_ACTION:
8621 case BOTH_GO:
8622 case PEER_CLIENT:
8623 /* already covered above */
8624 return 0;
8625 case JOIN_GROUP:
8626 return wpas_p2p_nfc_join_group(wpa_s, &params);
8627 case AUTH_JOIN:
8628 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8629 case INIT_GO_NEG:
8630 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8631 case RESP_GO_NEG:
8632 /* TODO: use own OOB Dev Pw */
8633 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8634 }
8635
8636 return -1;
8637}
8638
8639
8640int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8641 const struct wpabuf *data, int forced_freq)
8642{
8643 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8644 return -1;
8645
8646 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8647}
8648
8649
8650int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8651 const struct wpabuf *req,
8652 const struct wpabuf *sel, int forced_freq)
8653{
8654 struct wpabuf *tmp;
8655 int ret;
8656
8657 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8658 return -1;
8659
8660 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8661
8662 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8663 wpabuf_head(req), wpabuf_len(req));
8664 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8665 wpabuf_head(sel), wpabuf_len(sel));
8666 if (forced_freq)
8667 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8668 tmp = ndef_parse_p2p(init ? sel : req);
8669 if (tmp == NULL) {
8670 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8671 return -1;
8672 }
8673
8674 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8675 forced_freq);
8676 wpabuf_free(tmp);
8677
8678 return ret;
8679}
8680
8681
8682int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8683{
8684 const u8 *if_addr;
8685 int go_intent = wpa_s->conf->p2p_go_intent;
8686 struct wpa_supplicant *iface;
8687
8688 if (wpa_s->global->p2p == NULL)
8689 return -1;
8690
8691 if (!enabled) {
8692 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8693 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8694 {
8695 if (!iface->ap_iface)
8696 continue;
8697 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8698 }
8699 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8700 0, NULL);
8701 if (wpa_s->p2p_nfc_tag_enabled)
8702 wpas_p2p_remove_pending_group_interface(wpa_s);
8703 wpa_s->p2p_nfc_tag_enabled = 0;
8704 return 0;
8705 }
8706
8707 if (wpa_s->global->p2p_disabled)
8708 return -1;
8709
8710 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8711 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8712 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8713 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8714 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8715 "to allow static handover cases");
8716 return -1;
8717 }
8718
8719 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8720
8721 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8722 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8723 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8724 if (wpa_s->p2p_oob_dev_pw == NULL)
8725 return -1;
8726 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8727
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008728 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8729 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8730 /*
8731 * P2P Group Interface present and the command came on group
8732 * interface, so enable the token for the current interface.
8733 */
8734 wpa_s->create_p2p_iface = 0;
8735 } else {
8736 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8737 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008738
8739 if (wpa_s->create_p2p_iface) {
8740 enum wpa_driver_if_type iftype;
8741 /* Prepare to add a new interface for the group */
8742 iftype = WPA_IF_P2P_GROUP;
8743 if (go_intent == 15)
8744 iftype = WPA_IF_P2P_GO;
8745 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8746 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8747 "interface for the group");
8748 return -1;
8749 }
8750
8751 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008752 } else if (wpa_s->p2p_mgmt)
8753 if_addr = wpa_s->parent->own_addr;
8754 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008755 if_addr = wpa_s->own_addr;
8756
8757 wpa_s->p2p_nfc_tag_enabled = enabled;
8758
8759 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8760 struct hostapd_data *hapd;
8761 if (iface->ap_iface == NULL)
8762 continue;
8763 hapd = iface->ap_iface->bss[0];
8764 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8765 hapd->conf->wps_nfc_dh_pubkey =
8766 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8767 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8768 hapd->conf->wps_nfc_dh_privkey =
8769 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8770 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8771 hapd->conf->wps_nfc_dev_pw =
8772 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8773 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8774
8775 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8776 wpa_dbg(iface, MSG_DEBUG,
8777 "P2P: Failed to enable NFC Tag for GO");
8778 }
8779 }
8780 p2p_set_authorized_oob_dev_pw_id(
8781 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8782 if_addr);
8783
8784 return 0;
8785}
8786
8787#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008788
8789
8790static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8791 struct wpa_used_freq_data *freqs,
8792 unsigned int num)
8793{
8794 u8 curr_chan, cand, chan;
8795 unsigned int i;
8796
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008797 /*
8798 * If possible, optimize the Listen channel to be a channel that is
8799 * already used by one of the other interfaces.
8800 */
8801 if (!wpa_s->conf->p2p_optimize_listen_chan)
8802 return;
8803
8804 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8805 return;
8806
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008807 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8808 for (i = 0, cand = 0; i < num; i++) {
8809 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8810 if (curr_chan == chan) {
8811 cand = 0;
8812 break;
8813 }
8814
8815 if (chan == 1 || chan == 6 || chan == 11)
8816 cand = chan;
8817 }
8818
8819 if (cand) {
8820 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008821 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008822 cand);
8823 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8824 }
8825}
8826
8827
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008828static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008829{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008830 struct hostapd_config *conf;
8831 struct p2p_go_neg_results params;
8832 struct csa_settings csa_settings;
8833 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8834 int old_freq = current_ssid->frequency;
8835 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008836
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008837 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8838 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8839 return -1;
8840 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008841
8842 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008843 * TODO: This function may not always work correctly. For example,
8844 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008845 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008846 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008847 wpa_dbg(wpa_s, MSG_DEBUG,
8848 "P2P CSA: Failed to select new frequency for GO");
8849 return -1;
8850 }
8851
8852 if (current_ssid->frequency == params.freq) {
8853 wpa_dbg(wpa_s, MSG_DEBUG,
8854 "P2P CSA: Selected same frequency - not moving GO");
8855 return 0;
8856 }
8857
8858 conf = hostapd_config_defaults();
8859 if (!conf) {
8860 wpa_dbg(wpa_s, MSG_DEBUG,
8861 "P2P CSA: Failed to allocate default config");
8862 return -1;
8863 }
8864
8865 current_ssid->frequency = params.freq;
8866 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8867 wpa_dbg(wpa_s, MSG_DEBUG,
8868 "P2P CSA: Failed to create new GO config");
8869 ret = -1;
8870 goto out;
8871 }
8872
8873 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8874 wpa_dbg(wpa_s, MSG_DEBUG,
8875 "P2P CSA: CSA to a different band is not supported");
8876 ret = -1;
8877 goto out;
8878 }
8879
8880 os_memset(&csa_settings, 0, sizeof(csa_settings));
8881 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8882 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8883 csa_settings.freq_params.freq = params.freq;
8884 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8885 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8886 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8887
8888 if (conf->ieee80211ac) {
8889 int freq1 = 0, freq2 = 0;
8890 u8 chan, opclass;
8891
8892 if (ieee80211_freq_to_channel_ext(params.freq,
8893 conf->secondary_channel,
8894 conf->vht_oper_chwidth,
8895 &opclass, &chan) ==
8896 NUM_HOSTAPD_MODES) {
8897 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8898 ret = -1;
8899 goto out;
8900 }
8901
8902 if (conf->vht_oper_centr_freq_seg0_idx)
8903 freq1 = ieee80211_chan_to_freq(
8904 NULL, opclass,
8905 conf->vht_oper_centr_freq_seg0_idx);
8906
8907 if (conf->vht_oper_centr_freq_seg1_idx)
8908 freq2 = ieee80211_chan_to_freq(
8909 NULL, opclass,
8910 conf->vht_oper_centr_freq_seg1_idx);
8911
8912 if (freq1 < 0 || freq2 < 0) {
8913 wpa_dbg(wpa_s, MSG_DEBUG,
8914 "P2P CSA: Selected invalid VHT center freqs");
8915 ret = -1;
8916 goto out;
8917 }
8918
8919 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8920 csa_settings.freq_params.center_freq1 = freq1;
8921 csa_settings.freq_params.center_freq2 = freq2;
8922
8923 switch (conf->vht_oper_chwidth) {
8924 case VHT_CHANWIDTH_80MHZ:
8925 case VHT_CHANWIDTH_80P80MHZ:
8926 csa_settings.freq_params.bandwidth = 80;
8927 break;
8928 case VHT_CHANWIDTH_160MHZ:
8929 csa_settings.freq_params.bandwidth = 160;
8930 break;
8931 }
8932 }
8933
8934 ret = ap_switch_channel(wpa_s, &csa_settings);
8935out:
8936 current_ssid->frequency = old_freq;
8937 hostapd_config_free(conf);
8938 return ret;
8939}
8940
8941
8942static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8943{
8944 struct p2p_go_neg_results params;
8945 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8946
8947 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8948
8949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8950 current_ssid->frequency);
8951
8952 /* Stop the AP functionality */
8953 /* TODO: Should do this in a way that does not indicated to possible
8954 * P2P Clients in the group that the group is terminated. */
8955 wpa_supplicant_ap_deinit(wpa_s);
8956
8957 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008958 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8960 wpas_p2p_group_delete(wpa_s,
8961 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8962 return;
8963 }
8964 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8965 params.freq);
8966
8967 if (params.freq &&
8968 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8969 wpa_printf(MSG_DEBUG,
8970 "P2P: Selected freq (%u MHz) is not valid for P2P",
8971 params.freq);
8972 wpas_p2p_group_delete(wpa_s,
8973 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8974 return;
8975 }
8976
8977 /* Update the frequency */
8978 current_ssid->frequency = params.freq;
8979 wpa_s->connect_without_scan = current_ssid;
8980 wpa_s->reassociate = 1;
8981 wpa_s->disconnected = 0;
8982 wpa_supplicant_req_scan(wpa_s, 0, 0);
8983}
8984
8985
8986static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8987{
8988 struct wpa_supplicant *wpa_s = eloop_ctx;
8989
8990 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008991 return;
8992
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008993 wpas_p2p_go_update_common_freqs(wpa_s);
8994
8995 /* Do not move GO in the middle of a CSA */
8996 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8997 wpa_printf(MSG_DEBUG,
8998 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008999 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009000 }
9001
9002 /*
9003 * First, try a channel switch flow. If it is not supported or fails,
9004 * take down the GO and bring it up again.
9005 */
9006 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9007 wpas_p2p_move_go_no_csa(wpa_s);
9008}
9009
9010
9011static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9012{
9013 struct wpa_supplicant *wpa_s = eloop_ctx;
9014 struct wpa_used_freq_data *freqs = NULL;
9015 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009016
9017 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9018 if (!freqs)
9019 return;
9020
9021 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9022
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009023 /* Previous attempt to move a GO was not possible -- try again. */
9024 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9025 WPAS_P2P_CHANNEL_UPDATE_ANY);
9026
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009027 os_free(freqs);
9028}
9029
9030
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009031/*
9032 * Consider moving a GO from its currently used frequency:
9033 * 1. It is possible that due to regulatory consideration the frequency
9034 * can no longer be used and there is a need to evacuate the GO.
9035 * 2. It is possible that due to MCC considerations, it would be preferable
9036 * to move the GO to a channel that is currently used by some other
9037 * station interface.
9038 *
9039 * In case a frequency that became invalid is once again valid, cancel a
9040 * previously initiated GO frequency change.
9041 */
9042static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9043 struct wpa_used_freq_data *freqs,
9044 unsigned int num)
9045{
9046 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9047 unsigned int timeout;
9048 int freq;
9049
9050 wpas_p2p_go_update_common_freqs(wpa_s);
9051
9052 freq = wpa_s->current_ssid->frequency;
9053 for (i = 0, invalid_freq = 0; i < num; i++) {
9054 if (freqs[i].freq == freq) {
9055 flags = freqs[i].flags;
9056
9057 /* The channel is invalid, must change it */
9058 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
9059 wpa_dbg(wpa_s, MSG_DEBUG,
9060 "P2P: Freq=%d MHz no longer valid for GO",
9061 freq);
9062 invalid_freq = 1;
9063 }
9064 } else if (freqs[i].flags == 0) {
9065 /* Freq is not used by any other station interface */
9066 continue;
9067 } else if (!p2p_supported_freq(wpa_s->global->p2p,
9068 freqs[i].freq)) {
9069 /* Freq is not valid for P2P use cases */
9070 continue;
9071 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9072 P2P_GO_FREQ_MOVE_SCM) {
9073 policy_move = 1;
9074 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9075 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9076 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9077 policy_move = 1;
9078 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9079 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9080 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9081 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9082 policy_move = 1;
9083 } else if ((wpa_s->drv_flags &
9084 WPA_DRIVER_FLAGS_AP_CSA) &&
9085 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9086 u8 chan;
9087
9088 /*
9089 * We do not support CSA between bands, so move
9090 * GO only within the same band.
9091 */
9092 if (wpa_s->ap_iface->current_mode->mode ==
9093 ieee80211_freq_to_chan(freqs[i].freq,
9094 &chan))
9095 policy_move = 1;
9096 }
9097 }
9098 }
9099
9100 wpa_dbg(wpa_s, MSG_DEBUG,
9101 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9102 invalid_freq, policy_move, flags);
9103
9104 /*
9105 * The channel is valid, or we are going to have a policy move, so
9106 * cancel timeout.
9107 */
9108 if (!invalid_freq || policy_move) {
9109 wpa_dbg(wpa_s, MSG_DEBUG,
9110 "P2P: Cancel a GO move from freq=%d MHz", freq);
9111 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9112
9113 if (wpas_p2p_in_progress(wpa_s)) {
9114 wpa_dbg(wpa_s, MSG_DEBUG,
9115 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9116 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9117 wpa_s, NULL);
9118 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9119 wpas_p2p_reconsider_moving_go,
9120 wpa_s, NULL);
9121 return;
9122 }
9123 }
9124
9125 if (!invalid_freq && (!policy_move || flags != 0)) {
9126 wpa_dbg(wpa_s, MSG_DEBUG,
9127 "P2P: Not initiating a GO frequency change");
9128 return;
9129 }
9130
9131 /*
9132 * Do not consider moving GO if it is in the middle of a CSA. When the
9133 * CSA is finished this flow should be retriggered.
9134 */
9135 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9136 wpa_dbg(wpa_s, MSG_DEBUG,
9137 "P2P: Not initiating a GO frequency change - CSA is in progress");
9138 return;
9139 }
9140
9141 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9142 timeout = P2P_GO_FREQ_CHANGE_TIME;
9143 else
9144 timeout = 0;
9145
9146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9147 freq, timeout);
9148 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9149 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9150}
9151
9152
9153static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9154 struct wpa_used_freq_data *freqs,
9155 unsigned int num,
9156 enum wpas_p2p_channel_update_trig trig)
9157{
9158 struct wpa_supplicant *ifs;
9159
9160 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9161 NULL);
9162
9163 /*
9164 * Travers all the radio interfaces, and for each GO interface, check
9165 * if there is a need to move the GO from the frequency it is using,
9166 * or in case the frequency is valid again, cancel the evacuation flow.
9167 */
9168 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9169 radio_list) {
9170 if (ifs->current_ssid == NULL ||
9171 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9172 continue;
9173
9174 /*
9175 * The GO was just started or completed channel switch, no need
9176 * to move it.
9177 */
9178 if (wpa_s == ifs &&
9179 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9180 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9181 wpa_dbg(wpa_s, MSG_DEBUG,
9182 "P2P: GO move - schedule re-consideration");
9183 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9184 wpas_p2p_reconsider_moving_go,
9185 wpa_s, NULL);
9186 continue;
9187 }
9188
9189 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9190 }
9191}
9192
9193
9194void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9195{
9196 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9197 return;
9198
9199 wpas_p2p_update_channel_list(wpa_s,
9200 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9201}
9202
9203
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009204void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9205{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009206 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9207 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9208 "the management interface is being removed");
9209 wpas_p2p_deinit_global(wpa_s->global);
9210 }
9211}
9212
9213
9214void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9215{
9216 if (wpa_s->ap_iface->bss)
9217 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9218 wpas_p2p_group_deinit(wpa_s);
9219}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009220
9221
9222int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9223 unsigned int period, unsigned int interval,
9224 unsigned int count)
9225{
9226 struct p2p_data *p2p = wpa_s->global->p2p;
9227 u8 *device_types;
9228 size_t dev_types_len;
9229 struct wpabuf *buf;
9230 int ret;
9231
9232 if (wpa_s->p2p_lo_started) {
9233 wpa_dbg(wpa_s, MSG_DEBUG,
9234 "P2P Listen offload is already started");
9235 return 0;
9236 }
9237
9238 if (wpa_s->global->p2p == NULL ||
9239 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9240 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9241 return -1;
9242 }
9243
9244 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9245 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9246 freq);
9247 return -1;
9248 }
9249
9250 /* Get device type */
9251 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9252 WPS_DEV_TYPE_LEN;
9253 device_types = os_malloc(dev_types_len);
9254 if (!device_types)
9255 return -1;
9256 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9257 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9258 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9259
9260 /* Get Probe Response IE(s) */
9261 buf = p2p_build_probe_resp_template(p2p, freq);
9262 if (!buf) {
9263 os_free(device_types);
9264 return -1;
9265 }
9266
9267 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9268 device_types, dev_types_len,
9269 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9270 if (ret < 0)
9271 wpa_dbg(wpa_s, MSG_DEBUG,
9272 "P2P: Failed to start P2P listen offload");
9273
9274 os_free(device_types);
9275 wpabuf_free(buf);
9276
9277 if (ret == 0) {
9278 wpa_s->p2p_lo_started = 1;
9279
9280 /* Stop current P2P listen if any */
9281 wpas_stop_listen(wpa_s);
9282 }
9283
9284 return ret;
9285}
9286
9287
9288int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9289{
9290 int ret;
9291
9292 if (!wpa_s->p2p_lo_started)
9293 return 0;
9294
9295 ret = wpa_drv_p2p_lo_stop(wpa_s);
9296 if (ret < 0)
9297 wpa_dbg(wpa_s, MSG_DEBUG,
9298 "P2P: Failed to stop P2P listen offload");
9299
9300 wpa_s->p2p_lo_started = 0;
9301 return ret;
9302}