blob: f58808fae1f7fa8ab1a6a7d1ae22419f4c0ca036 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800115 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118};
119
Jouni Malinendc7b7132012-09-14 12:53:47 -0700120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122static struct wpa_supplicant *
123wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800125static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800127static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800131static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800144static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800145static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800147static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800148static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700149static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700150static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800151static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700153static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160static void
161wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167/*
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
170 */
171static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172{
173 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
177 if (!freqs)
178 return -1;
179
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
182 os_free(freqs);
183
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187}
188
189
190/*
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
193 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194static unsigned int
195wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700199 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 unsigned int num, i, j;
201
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700211
212 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800220
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700221 return j;
222}
223
224
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700225static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
226 int freq)
227{
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
229 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700230
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
233
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
238 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800240 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242}
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247{
248 size_t i;
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
254 }
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
257 return;
258
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
261
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700265 const u8 *ies;
266 size_t ies_len;
267
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700271
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
279 ies = ies + ies_len;
280 ies_len = bss->beacon_ie_len;
281 }
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800285 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700286 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291}
292
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
295{
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
298 int ret;
299
300 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 return;
308 }
309
310 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800311 if (ret == 0)
312 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800313 wpa_scan_free_params(params);
314 work->ctx = NULL;
315 if (ret) {
316 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 return;
319 }
320
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800321 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 os_get_reltime(&wpa_s->scan_trigger_time);
323 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
324 wpa_s->own_scan_requested = 1;
325 wpa_s->p2p_scan_work = work;
326}
327
328
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800329static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
330 int freq)
331{
332 if (wpa_s->global->p2p_24ghz_social_channels &&
333 (freq == 2412 || freq == 2437 || freq == 2462)) {
334 /*
335 * Search all social channels regardless of whether these have
336 * been disabled for P2P operating channel use to avoid missing
337 * peers.
338 */
339 return 1;
340 }
341 return p2p_supported_freq(wpa_s->global->p2p, freq);
342}
343
344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
346 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700347 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348{
349 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800350 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700352 unsigned int num_channels = 0;
353 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700355 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800356 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
358 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
359 return -1;
360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 if (wpa_s->p2p_scan_work) {
362 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
363 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700364 }
365
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800366 params = os_zalloc(sizeof(*params));
367 if (params == NULL)
368 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
370 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800371 params->num_ssids = 1;
372 n = os_malloc(P2P_WILDCARD_SSID_LEN);
373 if (n == NULL)
374 goto fail;
375 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
376 params->ssids[0].ssid = n;
377 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378
379 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
381 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 num_req_dev_types, req_dev_types);
383 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800384 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 switch (type) {
387 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700388 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
389 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 if (params->freqs == NULL)
391 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700392 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393 if (wpas_p2p_search_social_channel(
394 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700395 params->freqs[num_channels++] =
396 social_channels_freq[i];
397 }
398 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 break;
400 case P2P_SCAN_FULL:
401 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800402 case P2P_SCAN_SPECIFIC:
403 params->freqs = os_calloc(2, sizeof(int));
404 if (params->freqs == NULL)
405 goto fail;
406 params->freqs[0] = freq;
407 params->freqs[1] = 0;
408 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700410 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
411 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800412 if (params->freqs == NULL)
413 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700414 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800415 if (wpas_p2p_search_social_channel(
416 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700417 params->freqs[num_channels++] =
418 social_channels_freq[i];
419 }
420 if (p2p_supported_freq(wpa_s->global->p2p, freq))
421 params->freqs[num_channels++] = freq;
422 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 break;
424 }
425
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800426 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
427 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
428 if (ies == NULL) {
429 wpabuf_free(wps_ie);
430 goto fail;
431 }
432 wpabuf_put_buf(ies, wps_ie);
433 wpabuf_free(wps_ie);
434
435 bands = wpas_get_bands(wpa_s, params->freqs);
436 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
437
438 params->p2p_probe = 1;
439 n = os_malloc(wpabuf_len(ies));
440 if (n == NULL) {
441 wpabuf_free(ies);
442 goto fail;
443 }
444 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
445 params->extra_ies = n;
446 params->extra_ies_len = wpabuf_len(ies);
447 wpabuf_free(ies);
448
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800449 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800450 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
451 params) < 0)
452 goto fail;
453 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800455fail:
456 wpa_scan_free_params(params);
457 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458}
459
460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
462{
463 switch (p2p_group_interface) {
464 case P2P_GROUP_INTERFACE_PENDING:
465 return WPA_IF_P2P_GROUP;
466 case P2P_GROUP_INTERFACE_GO:
467 return WPA_IF_P2P_GO;
468 case P2P_GROUP_INTERFACE_CLIENT:
469 return WPA_IF_P2P_CLIENT;
470 }
471
472 return WPA_IF_P2P_GROUP;
473}
474
475
476static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
477 const u8 *ssid,
478 size_t ssid_len, int *go)
479{
480 struct wpa_ssid *s;
481
482 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
483 for (s = wpa_s->conf->ssid; s; s = s->next) {
484 if (s->disabled != 0 || !s->p2p_group ||
485 s->ssid_len != ssid_len ||
486 os_memcmp(ssid, s->ssid, ssid_len) != 0)
487 continue;
488 if (s->mode == WPAS_MODE_P2P_GO &&
489 s != wpa_s->current_ssid)
490 continue;
491 if (go)
492 *go = s->mode == WPAS_MODE_P2P_GO;
493 return wpa_s;
494 }
495 }
496
497 return NULL;
498}
499
500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800501static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
502{
503 struct wpa_supplicant *wpa_s = eloop_ctx;
504 wpa_printf(MSG_DEBUG,
505 "P2P: Complete previously requested removal of %s",
506 wpa_s->ifname);
507 wpas_p2p_disconnect(wpa_s);
508}
509
510
511static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
512 struct wpa_supplicant *calling_wpa_s)
513{
514 if (calling_wpa_s == wpa_s && wpa_s &&
515 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
516 /*
517 * The calling wpa_s instance is going to be removed. Do that
518 * from an eloop callback to keep the instance available until
519 * the caller has returned. This my be needed, e.g., to provide
520 * control interface responses on the per-interface socket.
521 */
522 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
523 wpa_s, NULL) < 0)
524 return -1;
525 return 0;
526 }
527
528 return wpas_p2p_disconnect(wpa_s);
529}
530
531
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800532/* Determine total number of clients in active groups where we are the GO */
533static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
534{
535 unsigned int count = 0;
536 struct wpa_ssid *s;
537
538 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
539 for (s = wpa_s->conf->ssid; s; s = s->next) {
540 wpa_printf(MSG_DEBUG,
541 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
542 wpa_s, s, s->disabled, s->p2p_group,
543 s->mode);
544 if (!s->disabled && s->p2p_group &&
545 s->mode == WPAS_MODE_P2P_GO) {
546 count += p2p_get_group_num_members(
547 wpa_s->p2p_group);
548 }
549 }
550 }
551
552 return count;
553}
554
555
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800556static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
557{
558 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
559 !wpa_s->current_ssid->disabled &&
560 wpa_s->current_ssid->p2p_group &&
561 wpa_s->current_ssid->p2p_persistent_group;
562}
563
564
565static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
566{
567 return p2p_is_active_persistent_group(wpa_s) &&
568 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
569}
570
571
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800572/* Find an interface for a P2P group where we are the GO */
573static struct wpa_supplicant *
574wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
575{
576 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800577
578 if (!wpa_s)
579 return NULL;
580
581 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800582 if (!p2p_is_active_persistent_go(wpa_s))
583 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800584
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800585 /* Prefer a group with connected clients */
586 if (p2p_get_group_num_members(wpa_s->p2p_group))
587 return wpa_s;
588 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800589 }
590
591 /* No group with connected clients, so pick the one without (if any) */
592 return save;
593}
594
595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800596static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800597{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800598 return p2p_is_active_persistent_group(wpa_s) &&
599 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
600}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800601
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800603/* Find an interface for a P2P group where we are the P2P Client */
604static struct wpa_supplicant *
605wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
606{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800608 if (p2p_is_active_persistent_cli(wpa_s))
609 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800610 }
611
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800612 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613}
614
615
616/* Find a persistent group where we are the GO */
617static struct wpa_ssid *
618wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
619{
620 struct wpa_ssid *s;
621
622 for (s = wpa_s->conf->ssid; s; s = s->next) {
623 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
624 return s;
625 }
626
627 return NULL;
628}
629
630
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800631static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
632 unsigned int *force_freq,
633 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800634{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800636 struct wpa_ssid *s;
637 u8 conncap = P2PS_SETUP_NONE;
638 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800639 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640 struct wpa_ssid *persistent_go;
641 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800642 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800643
644 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646 if (force_freq)
647 *force_freq = 0;
648 if (pref_freq)
649 *pref_freq = 0;
650
651 size = P2P_MAX_PREF_CHANNELS;
652 if (force_freq && pref_freq &&
653 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
654 (int *) pref_freq, 0, pref_freq_list, &size))
655 wpas_p2p_set_own_freq_preference(wpa_s,
656 *force_freq ? *force_freq :
657 *pref_freq);
658
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800659 /*
660 * For non-concurrent capable devices:
661 * If persistent_go, then no new.
662 * If GO, then no client.
663 * If client, then no GO.
664 */
665 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800666 if (go_wpa_s)
667 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800668 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
670 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800671
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 wpa_printf(MSG_DEBUG,
673 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
674 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800675
676 /* If not concurrent, restrict our choices */
677 if (p2p_no_group_iface) {
678 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
679
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800680 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681 return P2PS_SETUP_NONE;
682
683 if (go_wpa_s) {
684 if (role == P2PS_SETUP_CLIENT ||
685 incoming == P2PS_SETUP_GROUP_OWNER ||
686 p2p_client_limit_reached(go_wpa_s->p2p_group))
687 return P2PS_SETUP_NONE;
688
689 return P2PS_SETUP_GROUP_OWNER;
690 }
691
692 if (persistent_go) {
693 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
694 if (!incoming)
695 return P2PS_SETUP_GROUP_OWNER |
696 P2PS_SETUP_CLIENT;
697 if (incoming == P2PS_SETUP_NEW) {
698 u8 r;
699
700 if (os_get_random(&r, sizeof(r)) < 0 ||
701 (r & 1))
702 return P2PS_SETUP_CLIENT;
703 return P2PS_SETUP_GROUP_OWNER;
704 }
705 }
706 }
707 }
708
709 /* If a required role has been specified, handle it here */
710 if (role && role != P2PS_SETUP_NEW) {
711 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800712 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
713 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
714 /*
715 * Peer has an active GO, so if the role allows it and
716 * we do not have any active roles, become client.
717 */
718 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
719 !cli_wpa_s)
720 return P2PS_SETUP_CLIENT;
721
722 /* fall through */
723
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 case P2PS_SETUP_NONE:
725 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 conncap = role;
727 goto grp_owner;
728
729 case P2PS_SETUP_GROUP_OWNER:
730 /*
731 * Must be a complimentary role - cannot be a client to
732 * more than one peer.
733 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800734 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735 return P2PS_SETUP_NONE;
736
737 return P2PS_SETUP_CLIENT;
738
739 case P2PS_SETUP_CLIENT:
740 /* Must be a complimentary role */
741 if (incoming != role) {
742 conncap = P2PS_SETUP_GROUP_OWNER;
743 goto grp_owner;
744 }
745
746 default:
747 return P2PS_SETUP_NONE;
748 }
749 }
750
751 /*
752 * For now, we only will support ownership of one group, and being a
753 * client of one group. Therefore, if we have either an existing GO
754 * group, or an existing client group, we will not do a new GO
755 * negotiation, but rather try to re-use the existing groups.
756 */
757 switch (incoming) {
758 case P2PS_SETUP_NONE:
759 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800760 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800761 conncap = P2PS_SETUP_GROUP_OWNER;
762 else if (!owned_members)
763 conncap = P2PS_SETUP_NEW;
764 else if (incoming == P2PS_SETUP_NONE)
765 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
766 else
767 conncap = P2PS_SETUP_CLIENT;
768 break;
769
770 case P2PS_SETUP_CLIENT:
771 conncap = P2PS_SETUP_GROUP_OWNER;
772 break;
773
774 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800775 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800776 conncap = P2PS_SETUP_CLIENT;
777 break;
778
779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
780 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800781 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800782 conncap = P2PS_SETUP_GROUP_OWNER;
783 else {
784 u8 r;
785
786 if (os_get_random(&r, sizeof(r)) < 0 ||
787 (r & 1))
788 conncap = P2PS_SETUP_CLIENT;
789 else
790 conncap = P2PS_SETUP_GROUP_OWNER;
791 }
792 break;
793
794 default:
795 return P2PS_SETUP_NONE;
796 }
797
798grp_owner:
799 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
800 (!incoming && (conncap & P2PS_SETUP_NEW))) {
801 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
802 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800803
804 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800805 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800806 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800807 wpa_s->p2p_mgmt ?
808 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800809 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800810 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800811 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800812 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 wpa_printf(MSG_ERROR,
814 "P2P: Failed to allocate a new interface for the group");
815 return P2PS_SETUP_NONE;
816 }
817 wpa_s->global->pending_group_iface_for_p2ps = 1;
818 p2p_set_intended_addr(wpa_s->global->p2p,
819 wpa_s->pending_interface_addr);
820 }
821 }
822
823 return conncap;
824}
825
826
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700827static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
828 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829{
830 struct wpa_ssid *ssid;
831 char *gtype;
832 const char *reason;
833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 ssid = wpa_s->current_ssid;
835 if (ssid == NULL) {
836 /*
837 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700838 * pending P2P group interface waiting for provisioning or a
839 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 */
841 ssid = wpa_s->conf->ssid;
842 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700843 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 break;
845 ssid = ssid->next;
846 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300847 if (ssid == NULL &&
848 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
849 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700850 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
851 "not found");
852 return -1;
853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 }
855 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
856 gtype = "GO";
857 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
858 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
859 wpa_s->reassociate = 0;
860 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 gtype = "client";
862 } else
863 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700864
865 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
866 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
867
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800868 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700869 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
871 wpa_s, NULL)) {
872 wpa_printf(MSG_DEBUG,
873 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
874 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
875 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
876 wpa_s, NULL);
877 }
878 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (wpa_s->cross_connect_in_use) {
881 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800882 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700883 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
884 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700886 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 case P2P_GROUP_REMOVAL_REQUESTED:
888 reason = " reason=REQUESTED";
889 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700890 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
891 reason = " reason=FORMATION_FAILED";
892 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
894 reason = " reason=IDLE";
895 break;
896 case P2P_GROUP_REMOVAL_UNAVAILABLE:
897 reason = " reason=UNAVAILABLE";
898 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700899 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
900 reason = " reason=GO_ENDING_SESSION";
901 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700902 case P2P_GROUP_REMOVAL_PSK_FAILURE:
903 reason = " reason=PSK_FAILURE";
904 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800905 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
906 reason = " reason=FREQ_CONFLICT";
907 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 default:
909 reason = "";
910 break;
911 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800913 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700914 P2P_EVENT_GROUP_REMOVED "%s %s%s",
915 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800918 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
919 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700920 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
921 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800922 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800923 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800924 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
925 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700926 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800927 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700928 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700929
Dmitry Shmidt15907092014-03-25 10:42:57 -0700930 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800931 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
932 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700933
Dmitry Shmidt96571392013-10-14 12:54:46 -0700934 /*
935 * Make sure wait for the first client does not remain active after the
936 * group has been removed.
937 */
938 wpa_s->global->p2p_go_wait_client.sec = 0;
939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
941 struct wpa_global *global;
942 char *ifname;
943 enum wpa_driver_if_type type;
944 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
945 wpa_s->ifname);
946 global = wpa_s->global;
947 ifname = os_strdup(wpa_s->ifname);
948 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800949 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700950 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_s = global->ifaces;
952 if (wpa_s && ifname)
953 wpa_drv_if_remove(wpa_s, type, ifname);
954 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300955 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 }
957
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800958 /*
959 * The primary interface was used for P2P group operations, so
960 * need to reset its p2pdev.
961 */
962 wpa_s->p2pdev = wpa_s->parent;
963
Dmitry Shmidt96571392013-10-14 12:54:46 -0700964 if (!wpa_s->p2p_go_group_formation_completed) {
965 wpa_s->global->p2p_group_formation = NULL;
966 wpa_s->p2p_in_provisioning = 0;
967 }
968
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800969 wpa_s->show_group_started = 0;
970 os_free(wpa_s->go_params);
971 wpa_s->go_params = NULL;
972
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 os_free(wpa_s->p2p_group_common_freqs);
974 wpa_s->p2p_group_common_freqs = NULL;
975 wpa_s->p2p_group_common_freqs_num = 0;
976
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800977 wpa_s->waiting_presence_resp = 0;
978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
980 if (ssid && (ssid->p2p_group ||
981 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
982 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
983 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700984 if (ssid == wpa_s->current_ssid) {
985 wpa_sm_set_config(wpa_s->wpa, NULL);
986 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700988 }
989 /*
990 * Networks objects created during any P2P activities are not
991 * exposed out as they might/will confuse certain non-P2P aware
992 * applications since these network objects won't behave like
993 * regular ones.
994 *
995 * Likewise, we don't send out network removed signals for such
996 * network objects.
997 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 wpa_config_remove_network(wpa_s->conf, id);
999 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001000 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 } else {
1002 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1003 "found");
1004 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 if (wpa_s->ap_iface)
1006 wpa_supplicant_ap_deinit(wpa_s);
1007 else
1008 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001009
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001010 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1011
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013}
1014
1015
1016static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1017 u8 *go_dev_addr,
1018 const u8 *ssid, size_t ssid_len)
1019{
1020 struct wpa_bss *bss;
1021 const u8 *bssid;
1022 struct wpabuf *p2p;
1023 u8 group_capab;
1024 const u8 *addr;
1025
1026 if (wpa_s->go_params)
1027 bssid = wpa_s->go_params->peer_interface_addr;
1028 else
1029 bssid = wpa_s->bssid;
1030
1031 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001032 if (bss == NULL && wpa_s->go_params &&
1033 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1034 bss = wpa_bss_get_p2p_dev_addr(
1035 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 if (bss == NULL) {
1037 u8 iface_addr[ETH_ALEN];
1038 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1039 iface_addr) == 0)
1040 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1041 }
1042 if (bss == NULL) {
1043 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1044 "group is persistent - BSS " MACSTR " not found",
1045 MAC2STR(bssid));
1046 return 0;
1047 }
1048
1049 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001050 if (p2p == NULL)
1051 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1052 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 if (p2p == NULL) {
1054 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1055 "group is persistent - BSS " MACSTR
1056 " did not include P2P IE", MAC2STR(bssid));
1057 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1058 (u8 *) (bss + 1), bss->ie_len);
1059 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1060 ((u8 *) bss + 1) + bss->ie_len,
1061 bss->beacon_ie_len);
1062 return 0;
1063 }
1064
1065 group_capab = p2p_get_group_capab(p2p);
1066 addr = p2p_get_go_dev_addr(p2p);
1067 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1068 "group_capab=0x%x", group_capab);
1069 if (addr) {
1070 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1071 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1072 MAC2STR(addr));
1073 } else
1074 os_memset(go_dev_addr, 0, ETH_ALEN);
1075 wpabuf_free(p2p);
1076
1077 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1078 "go_dev_addr=" MACSTR,
1079 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1080
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001081 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082}
1083
1084
Jouni Malinen75ecf522011-06-27 15:19:46 -07001085static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1086 struct wpa_ssid *ssid,
1087 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088{
1089 struct wpa_ssid *s;
1090 int changed = 0;
1091
1092 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1093 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1094 for (s = wpa_s->conf->ssid; s; s = s->next) {
1095 if (s->disabled == 2 &&
1096 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1097 s->ssid_len == ssid->ssid_len &&
1098 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1099 break;
1100 }
1101
1102 if (s) {
1103 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1104 "entry");
1105 if (ssid->passphrase && !s->passphrase)
1106 changed = 1;
1107 else if (ssid->passphrase && s->passphrase &&
1108 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1109 changed = 1;
1110 } else {
1111 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1112 "entry");
1113 changed = 1;
1114 s = wpa_config_add_network(wpa_s->conf);
1115 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001116 return -1;
1117
1118 /*
1119 * Instead of network_added we emit persistent_group_added
1120 * notification. Also to keep the defense checks in
1121 * persistent_group obj registration method, we set the
1122 * relevant flags in s to designate it as a persistent group.
1123 */
1124 s->p2p_group = 1;
1125 s->p2p_persistent_group = 1;
1126 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_config_set_network_defaults(s);
1128 }
1129
1130 s->p2p_group = 1;
1131 s->p2p_persistent_group = 1;
1132 s->disabled = 2;
1133 s->bssid_set = 1;
1134 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1135 s->mode = ssid->mode;
1136 s->auth_alg = WPA_AUTH_ALG_OPEN;
1137 s->key_mgmt = WPA_KEY_MGMT_PSK;
1138 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001139 s->pbss = ssid->pbss;
1140 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 s->export_keys = 1;
1142 if (ssid->passphrase) {
1143 os_free(s->passphrase);
1144 s->passphrase = os_strdup(ssid->passphrase);
1145 }
1146 if (ssid->psk_set) {
1147 s->psk_set = 1;
1148 os_memcpy(s->psk, ssid->psk, 32);
1149 }
1150 if (s->passphrase && !s->psk_set)
1151 wpa_config_update_psk(s);
1152 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1153 os_free(s->ssid);
1154 s->ssid = os_malloc(ssid->ssid_len);
1155 }
1156 if (s->ssid) {
1157 s->ssid_len = ssid->ssid_len;
1158 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1159 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001160 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1161 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1162 wpa_s->global->add_psk = NULL;
1163 changed = 1;
1164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 if (changed && wpa_s->conf->update_config &&
1167 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1168 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1169 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001170
1171 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172}
1173
1174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1176 const u8 *addr)
1177{
1178 struct wpa_ssid *ssid, *s;
1179 u8 *n;
1180 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001181 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001182 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183
1184 ssid = wpa_s->current_ssid;
1185 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1186 !ssid->p2p_persistent_group)
1187 return;
1188
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1191 continue;
1192
1193 if (s->ssid_len == ssid->ssid_len &&
1194 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1195 break;
1196 }
1197
1198 if (s == NULL)
1199 return;
1200
1201 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001202 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001203 ETH_ALEN) != 0)
1204 continue;
1205
1206 if (i == s->num_p2p_clients - 1)
1207 return; /* already the most recent entry */
1208
1209 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001210 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1211 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1212 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001213 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001214 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1215 ETH_ALEN);
1216 os_memset(s->p2p_client_list +
1217 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1218 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001219 found = 1;
1220 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 }
1222
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001223 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1224 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001225 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 if (n == NULL)
1227 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001228 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1229 ETH_ALEN);
1230 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1231 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001232 s->p2p_client_list = n;
1233 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001234 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001235 /* Not enough room for an additional entry - drop the oldest
1236 * entry */
1237 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001238 s->p2p_client_list + 2 * ETH_ALEN,
1239 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001241 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001243 os_memset(s->p2p_client_list +
1244 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1245 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001248 if (p2p_wpa_s->conf->update_config &&
1249 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251}
1252
1253
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001254static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1255 int go, struct wpa_ssid *ssid, int freq,
1256 const u8 *psk, const char *passphrase,
1257 const u8 *go_dev_addr, int persistent,
1258 const char *extra)
1259{
1260 const char *ssid_txt;
1261 char psk_txt[65];
1262
1263 if (psk)
1264 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1265 else
1266 psk_txt[0] = '\0';
1267
1268 if (ssid)
1269 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1270 else
1271 ssid_txt = "";
1272
1273 if (passphrase && passphrase[0] == '\0')
1274 passphrase = NULL;
1275
1276 /*
1277 * Include PSK/passphrase only in the control interface message and
1278 * leave it out from the debug log entry.
1279 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001280 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001281 P2P_EVENT_GROUP_STARTED
1282 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1283 MACSTR "%s%s",
1284 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1285 psk ? " psk=" : "", psk_txt,
1286 passphrase ? " passphrase=\"" : "",
1287 passphrase ? passphrase : "",
1288 passphrase ? "\"" : "",
1289 MAC2STR(go_dev_addr),
1290 persistent ? " [PERSISTENT]" : "", extra);
1291 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1292 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1293 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1294 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1295 extra);
1296}
1297
1298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001300 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301{
1302 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 int client;
1304 int persistent;
1305 u8 go_dev_addr[ETH_ALEN];
1306
1307 /*
1308 * This callback is likely called for the main interface. Update wpa_s
1309 * to use the group interface if a new interface was created for the
1310 * group.
1311 */
1312 if (wpa_s->global->p2p_group_formation)
1313 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001314 if (wpa_s->p2p_go_group_formation_completed) {
1315 wpa_s->global->p2p_group_formation = NULL;
1316 wpa_s->p2p_in_provisioning = 0;
1317 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001318 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320
1321 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001322 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001323 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001324 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001325 if (already_deleted)
1326 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001327 wpas_p2p_group_delete(wpa_s,
1328 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return;
1330 }
1331
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001332 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001333 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334
1335 ssid = wpa_s->current_ssid;
1336 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1337 ssid->mode = WPAS_MODE_P2P_GO;
1338 p2p_group_notif_formation_done(wpa_s->p2p_group);
1339 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1340 }
1341
1342 persistent = 0;
1343 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 client = ssid->mode == WPAS_MODE_INFRA;
1345 if (ssid->mode == WPAS_MODE_P2P_GO) {
1346 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001347 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1348 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 } else
1350 persistent = wpas_p2p_persistent_group(wpa_s,
1351 go_dev_addr,
1352 ssid->ssid,
1353 ssid->ssid_len);
1354 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 client = wpa_s->p2p_group_interface ==
1356 P2P_GROUP_INTERFACE_CLIENT;
1357 os_memset(go_dev_addr, 0, ETH_ALEN);
1358 }
1359
1360 wpa_s->show_group_started = 0;
1361 if (client) {
1362 /*
1363 * Indicate event only after successfully completed 4-way
1364 * handshake, i.e., when the interface is ready for data
1365 * packets.
1366 */
1367 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001369 wpas_p2p_group_started(wpa_s, 1, ssid,
1370 ssid ? ssid->frequency : 0,
1371 ssid && ssid->passphrase == NULL &&
1372 ssid->psk_set ? ssid->psk : NULL,
1373 ssid ? ssid->passphrase : NULL,
1374 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 wpas_p2p_cross_connect_setup(wpa_s);
1376 wpas_p2p_set_group_idle_timeout(wpa_s);
1377 }
1378
1379 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001380 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1381 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001382 else {
1383 os_free(wpa_s->global->add_psk);
1384 wpa_s->global->add_psk = NULL;
1385 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001386
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001387 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001388 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001389 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391}
1392
1393
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001394struct send_action_work {
1395 unsigned int freq;
1396 u8 dst[ETH_ALEN];
1397 u8 src[ETH_ALEN];
1398 u8 bssid[ETH_ALEN];
1399 size_t len;
1400 unsigned int wait_time;
1401 u8 buf[0];
1402};
1403
1404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001405static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1406{
1407 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1408
1409 wpa_printf(MSG_DEBUG,
1410 "P2P: Free Action frame radio work @%p (freq=%u dst="
1411 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1412 wpa_s->p2p_send_action_work, awork->freq,
1413 MAC2STR(awork->dst), MAC2STR(awork->src),
1414 MAC2STR(awork->bssid), awork->wait_time);
1415 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1416 awork->buf, awork->len);
1417 os_free(awork);
1418 wpa_s->p2p_send_action_work->ctx = NULL;
1419 radio_work_done(wpa_s->p2p_send_action_work);
1420 wpa_s->p2p_send_action_work = NULL;
1421}
1422
1423
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001424static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1425 void *timeout_ctx)
1426{
1427 struct wpa_supplicant *wpa_s = eloop_ctx;
1428
1429 if (!wpa_s->p2p_send_action_work)
1430 return;
1431
1432 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001433 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001434}
1435
1436
Dmitry Shmidt03658832014-08-13 11:03:49 -07001437static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001439 if (wpa_s->p2p_send_action_work) {
1440 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001441
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001442 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001443 wpa_printf(MSG_DEBUG,
1444 "P2P: Clear Action TX work @%p (wait_time=%u)",
1445 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001446 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001447 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001448 } else {
1449 /*
1450 * In theory, this should not be needed, but number of
1451 * places in the P2P code is still using non-zero wait
1452 * time for the last Action frame in the sequence and
1453 * some of these do not call send_action_done().
1454 */
1455 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1456 wpa_s, NULL);
1457 eloop_register_timeout(
1458 0, awork->wait_time * 1000,
1459 wpas_p2p_send_action_work_timeout,
1460 wpa_s, NULL);
1461 }
1462 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001463}
1464
1465
1466static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1467 unsigned int freq,
1468 const u8 *dst, const u8 *src,
1469 const u8 *bssid,
1470 const u8 *data, size_t data_len,
1471 enum offchannel_send_action_result
1472 result)
1473{
1474 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1475
1476 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1479 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001481 switch (result) {
1482 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1483 res = P2P_SEND_ACTION_SUCCESS;
1484 break;
1485 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1486 res = P2P_SEND_ACTION_NO_ACK;
1487 break;
1488 case OFFCHANNEL_SEND_ACTION_FAILED:
1489 res = P2P_SEND_ACTION_FAILED;
1490 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 }
1492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001495 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1496 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001498 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1499 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001501 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1502 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001503 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001504 P2P_EVENT_FALLBACK_TO_GO_NEG
1505 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001506 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1507 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 }
1509}
1510
1511
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001512static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1513{
1514 struct wpa_supplicant *wpa_s = work->wpa_s;
1515 struct send_action_work *awork = work->ctx;
1516
1517 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001518 if (work->started) {
1519 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1520 wpa_s, NULL);
1521 wpa_s->p2p_send_action_work = NULL;
1522 offchannel_send_action_done(wpa_s);
1523 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 os_free(awork);
1525 return;
1526 }
1527
1528 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1529 awork->bssid, awork->buf, awork->len,
1530 awork->wait_time,
1531 wpas_p2p_send_action_tx_status, 1) < 0) {
1532 os_free(awork);
1533 radio_work_done(work);
1534 return;
1535 }
1536 wpa_s->p2p_send_action_work = work;
1537}
1538
1539
1540static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1541 unsigned int freq, const u8 *dst,
1542 const u8 *src, const u8 *bssid, const u8 *buf,
1543 size_t len, unsigned int wait_time)
1544{
1545 struct send_action_work *awork;
1546
1547 if (wpa_s->p2p_send_action_work) {
1548 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1549 return -1;
1550 }
1551
1552 awork = os_zalloc(sizeof(*awork) + len);
1553 if (awork == NULL)
1554 return -1;
1555
1556 awork->freq = freq;
1557 os_memcpy(awork->dst, dst, ETH_ALEN);
1558 os_memcpy(awork->src, src, ETH_ALEN);
1559 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1560 awork->len = len;
1561 awork->wait_time = wait_time;
1562 os_memcpy(awork->buf, buf, len);
1563
1564 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1565 wpas_send_action_cb, awork) < 0) {
1566 os_free(awork);
1567 return -1;
1568 }
1569
1570 return 0;
1571}
1572
1573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1575 const u8 *src, const u8 *bssid, const u8 *buf,
1576 size_t len, unsigned int wait_time)
1577{
1578 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001579 int listen_freq = -1, send_freq = -1;
1580
1581 if (wpa_s->p2p_listen_work)
1582 listen_freq = wpa_s->p2p_listen_work->freq;
1583 if (wpa_s->p2p_send_action_work)
1584 send_freq = wpa_s->p2p_send_action_work->freq;
1585 if (listen_freq != (int) freq && send_freq != (int) freq) {
1586 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1587 listen_freq, send_freq);
1588 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1589 len, wait_time);
1590 }
1591
1592 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1594 wait_time,
1595 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596}
1597
1598
1599static void wpas_send_action_done(void *ctx)
1600{
1601 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001602
1603 if (wpa_s->p2p_send_action_work) {
1604 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1605 wpa_s, NULL);
1606 os_free(wpa_s->p2p_send_action_work->ctx);
1607 radio_work_done(wpa_s->p2p_send_action_work);
1608 wpa_s->p2p_send_action_work = NULL;
1609 }
1610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612}
1613
1614
1615static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1616 struct p2p_go_neg_results *params)
1617{
1618 if (wpa_s->go_params == NULL) {
1619 wpa_s->go_params = os_malloc(sizeof(*params));
1620 if (wpa_s->go_params == NULL)
1621 return -1;
1622 }
1623 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1624 return 0;
1625}
1626
1627
1628static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1629 struct p2p_go_neg_results *res)
1630{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001631 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001632 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1633 " dev_addr " MACSTR " wps_method %d",
1634 MAC2STR(res->peer_interface_addr),
1635 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1637 res->ssid, res->ssid_len);
1638 wpa_supplicant_ap_deinit(wpa_s);
1639 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001640 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001642#ifdef CONFIG_WPS_NFC
1643 } else if (res->wps_method == WPS_NFC) {
1644 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1645 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001646 wpa_s->p2pdev->p2p_oob_dev_pw,
1647 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1648 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001649 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001650 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 NULL,
1652 NULL, 0, 0);
1653#endif /* CONFIG_WPS_NFC */
1654 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001656 if (wpa_s->p2p_wps_method == WPS_P2PS)
1657 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1659 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1660 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1661 wpa_s->p2p_pin, 1, dev_pw_id);
1662 }
1663}
1664
1665
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001666static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1667 struct wpa_ssid *ssid)
1668{
1669 struct wpa_ssid *persistent;
1670 struct psk_list_entry *psk;
1671 struct hostapd_data *hapd;
1672
1673 if (!wpa_s->ap_iface)
1674 return;
1675
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001676 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001677 ssid->ssid_len);
1678 if (persistent == NULL)
1679 return;
1680
1681 hapd = wpa_s->ap_iface->bss[0];
1682
1683 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1684 list) {
1685 struct hostapd_wpa_psk *hpsk;
1686
1687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1688 MACSTR " psk=%d",
1689 MAC2STR(psk->addr), psk->p2p);
1690 hpsk = os_zalloc(sizeof(*hpsk));
1691 if (hpsk == NULL)
1692 break;
1693 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1694 if (psk->p2p)
1695 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1696 else
1697 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1698 hpsk->next = hapd->conf->ssid.wpa_psk;
1699 hapd->conf->ssid.wpa_psk = hpsk;
1700 }
1701}
1702
1703
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001704static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1705{
1706 unsigned int i;
1707
1708 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1709 wpa_s->p2p_group_common_freqs_num);
1710
1711 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1712 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1713 i, wpa_s->p2p_group_common_freqs[i]);
1714}
1715
1716
1717static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1718 struct p2p_go_neg_results *params)
1719{
1720 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1721
1722 wpa_s->p2p_group_common_freqs_num = 0;
1723 os_free(wpa_s->p2p_group_common_freqs);
1724 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1725 if (!wpa_s->p2p_group_common_freqs)
1726 return;
1727
1728 for (i = 0; i < len; i++) {
1729 if (!wpa_s->go_params->freq_list[i])
1730 break;
1731 wpa_s->p2p_group_common_freqs[i] =
1732 wpa_s->go_params->freq_list[i];
1733 }
1734 wpa_s->p2p_group_common_freqs_num = i;
1735}
1736
1737
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001738static void p2p_config_write(struct wpa_supplicant *wpa_s)
1739{
1740#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001741 if (wpa_s->p2pdev->conf->update_config &&
1742 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001743 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1744#endif /* CONFIG_NO_CONFIG_WRITE */
1745}
1746
1747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748static void p2p_go_configured(void *ctx, void *data)
1749{
1750 struct wpa_supplicant *wpa_s = ctx;
1751 struct p2p_go_neg_results *params = data;
1752 struct wpa_ssid *ssid;
1753
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001754 wpa_s->ap_configured_cb = NULL;
1755 wpa_s->ap_configured_cb_ctx = NULL;
1756 wpa_s->ap_configured_cb_data = NULL;
1757 if (!wpa_s->go_params) {
1758 wpa_printf(MSG_ERROR,
1759 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1760 return;
1761 }
1762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001763 p2p_go_save_group_common_freqs(wpa_s, params);
1764 p2p_go_dump_common_freqs(wpa_s);
1765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 ssid = wpa_s->current_ssid;
1767 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1768 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1769 if (wpa_s->global->p2p_group_formation == wpa_s)
1770 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001771 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1772 params->passphrase[0] == '\0' ?
1773 params->psk : NULL,
1774 params->passphrase,
1775 wpa_s->global->p2p_dev_addr,
1776 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001777 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001779 if (wpa_s->p2pdev->p2ps_method_config_any) {
1780 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001781 wpa_dbg(wpa_s, MSG_DEBUG,
1782 "P2PS: Setting default PIN for ANY");
1783 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1784 "12345670", NULL, 0,
1785 0);
1786 } else {
1787 wpa_dbg(wpa_s, MSG_DEBUG,
1788 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001789 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001790 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001791 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001792 "12345670", NULL, 0, 0);
1793 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001794 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001795 }
1796
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001797 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001798 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001799 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001800 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001801 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001802 wpas_p2p_add_psk_list(wpa_s, ssid);
1803 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001804
1805 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001806 params->persistent_group, 0,
1807 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 wpas_p2p_cross_connect_setup(wpa_s);
1809 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001810
1811 if (wpa_s->p2p_first_connection_timeout) {
1812 wpa_dbg(wpa_s, MSG_DEBUG,
1813 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1814 wpa_s->p2p_first_connection_timeout);
1815 wpa_s->p2p_go_group_formation_completed = 0;
1816 wpa_s->global->p2p_group_formation = wpa_s;
1817 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001818 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001819 eloop_register_timeout(
1820 wpa_s->p2p_first_connection_timeout, 0,
1821 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001822 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001823 }
1824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return;
1826 }
1827
1828 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1829 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1830 params->peer_interface_addr)) {
1831 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1832 "filtering");
1833 return;
1834 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001835 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001837 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001838#ifdef CONFIG_WPS_NFC
1839 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001840 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001841 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001842 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001843 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1844 return;
1845 }
1846 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001847 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1848 wpa_s->p2pdev->p2p_oob_dev_pw,
1849 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1850 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001851#endif /* CONFIG_WPS_NFC */
1852 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001854 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 os_free(wpa_s->go_params);
1856 wpa_s->go_params = NULL;
1857}
1858
1859
1860static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1861 struct p2p_go_neg_results *params,
1862 int group_formation)
1863{
1864 struct wpa_ssid *ssid;
1865
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001866 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1867 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1869 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872
1873 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001874 if (ssid == NULL) {
1875 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 wpa_s->p2p_go_group_formation_completed = 0;
1881 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 wpa_config_set_network_defaults(ssid);
1885 ssid->temporary = 1;
1886 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001887 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1889 WPAS_MODE_P2P_GO;
1890 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001891 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001892 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001893 ssid->max_oper_chwidth = params->max_oper_chwidth;
1894 ssid->vht_center_freq2 = params->vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 ssid->ssid = os_zalloc(params->ssid_len + 1);
1896 if (ssid->ssid) {
1897 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1898 ssid->ssid_len = params->ssid_len;
1899 }
1900 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1901 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1902 ssid->proto = WPA_PROTO_RSN;
1903 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904 ssid->group_cipher = WPA_CIPHER_CCMP;
1905 if (params->freq > 56160) {
1906 /*
1907 * Enable GCMP instead of CCMP as pairwise_cipher and
1908 * group_cipher in 60 GHz.
1909 */
1910 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1911 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001912 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1913 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001914 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001915 if (os_strlen(params->passphrase) > 0) {
1916 ssid->passphrase = os_strdup(params->passphrase);
1917 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001918 wpa_msg_global(wpa_s, MSG_ERROR,
1919 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001920 wpa_config_remove_network(wpa_s->conf, ssid->id);
1921 return;
1922 }
1923 } else
1924 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001925 ssid->psk_set = params->psk_set;
1926 if (ssid->psk_set)
1927 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001928 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001929 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001930 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931
1932 wpa_s->ap_configured_cb = p2p_go_configured;
1933 wpa_s->ap_configured_cb_ctx = wpa_s;
1934 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001935 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001936 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 wpa_s->reassociate = 1;
1938 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001939 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1940 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 wpa_supplicant_req_scan(wpa_s, 0, 0);
1942}
1943
1944
1945static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1946 const struct wpa_supplicant *src)
1947{
1948 struct wpa_config *d;
1949 const struct wpa_config *s;
1950
1951 d = dst->conf;
1952 s = src->conf;
1953
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001954#define C(n) \
1955do { \
1956 if (s->n && !d->n) \
1957 d->n = os_strdup(s->n); \
1958} while (0)
1959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 C(device_name);
1961 C(manufacturer);
1962 C(model_name);
1963 C(model_number);
1964 C(serial_number);
1965 C(config_methods);
1966#undef C
1967
1968 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1969 os_memcpy(d->sec_device_type, s->sec_device_type,
1970 sizeof(d->sec_device_type));
1971 d->num_sec_device_types = s->num_sec_device_types;
1972
1973 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001974 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001976 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001977 d->max_num_sta = s->max_num_sta;
1978 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001979 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001980 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001981 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001982 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001983 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001984 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001985 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001986
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001987 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
1988 !d->wps_nfc_pw_from_config) {
1989 wpabuf_free(d->wps_nfc_dh_privkey);
1990 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001991 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1992 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1993 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001994 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995}
1996
1997
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001998static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1999 char *ifname, size_t len)
2000{
2001 char *ifname_ptr = wpa_s->ifname;
2002
2003 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2004 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2005 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2006 }
2007
2008 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2009 if (os_strlen(ifname) >= IFNAMSIZ &&
2010 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002011 int res;
2012
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002013 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002014 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2015 if (os_snprintf_error(len, res) && len)
2016 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002017 }
2018}
2019
2020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2022 enum wpa_driver_if_type type)
2023{
2024 char ifname[120], force_ifname[120];
2025
2026 if (wpa_s->pending_interface_name[0]) {
2027 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2028 "- skip creation of a new one");
2029 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2030 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2031 "unknown?! ifname='%s'",
2032 wpa_s->pending_interface_name);
2033 return -1;
2034 }
2035 return 0;
2036 }
2037
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 force_ifname[0] = '\0';
2040
2041 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2042 ifname);
2043 wpa_s->p2p_group_idx++;
2044
2045 wpa_s->pending_interface_type = type;
2046 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2047 wpa_s->pending_interface_addr, NULL) < 0) {
2048 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2049 "interface");
2050 return -1;
2051 }
2052
2053 if (force_ifname[0]) {
2054 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2055 force_ifname);
2056 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2057 sizeof(wpa_s->pending_interface_name));
2058 } else
2059 os_strlcpy(wpa_s->pending_interface_name, ifname,
2060 sizeof(wpa_s->pending_interface_name));
2061 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2062 MACSTR, wpa_s->pending_interface_name,
2063 MAC2STR(wpa_s->pending_interface_addr));
2064
2065 return 0;
2066}
2067
2068
2069static void wpas_p2p_remove_pending_group_interface(
2070 struct wpa_supplicant *wpa_s)
2071{
2072 if (!wpa_s->pending_interface_name[0] ||
2073 is_zero_ether_addr(wpa_s->pending_interface_addr))
2074 return; /* No pending virtual interface */
2075
2076 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2077 wpa_s->pending_interface_name);
2078 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2079 wpa_s->pending_interface_name);
2080 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2081 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002082 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083}
2084
2085
2086static struct wpa_supplicant *
2087wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2088{
2089 struct wpa_interface iface;
2090 struct wpa_supplicant *group_wpa_s;
2091
2092 if (!wpa_s->pending_interface_name[0]) {
2093 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2094 if (!wpas_p2p_create_iface(wpa_s))
2095 return NULL;
2096 /*
2097 * Something has forced us to remove the pending interface; try
2098 * to create a new one and hope for the best that we will get
2099 * the same local address.
2100 */
2101 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2102 WPA_IF_P2P_CLIENT) < 0)
2103 return NULL;
2104 }
2105
2106 os_memset(&iface, 0, sizeof(iface));
2107 iface.ifname = wpa_s->pending_interface_name;
2108 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002109 if (wpa_s->conf->ctrl_interface == NULL &&
2110 wpa_s->parent != wpa_s &&
2111 wpa_s->p2p_mgmt &&
2112 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2113 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2114 else
2115 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002117 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 if (group_wpa_s == NULL) {
2119 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2120 "wpa_supplicant interface");
2121 return NULL;
2122 }
2123 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2125 P2P_GROUP_INTERFACE_CLIENT;
2126 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002127 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128
2129 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2130
2131 return group_wpa_s;
2132}
2133
2134
2135static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2136 void *timeout_ctx)
2137{
2138 struct wpa_supplicant *wpa_s = eloop_ctx;
2139 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002140 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002141}
2142
2143
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002144static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2145 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002146{
2147 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002148 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 if (wpa_s->global->p2p)
2150 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002151 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152}
2153
2154
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002155static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2156{
2157 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002159 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002160 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002161 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002162 wpa_s->global->p2p_fail_on_wps_complete = 0;
2163}
2164
2165
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002166void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2167{
2168 if (wpa_s->global->p2p_group_formation != wpa_s)
2169 return;
2170 /* Speed up group formation timeout since this cannot succeed */
2171 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002172 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002173 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002174 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002175}
2176
2177
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002178static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179{
2180 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002181 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182
2183 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2184 wpa_drv_cancel_remain_on_channel(wpa_s);
2185 wpa_s->off_channel_freq = 0;
2186 wpa_s->roc_waiting_drv_freq = 0;
2187 }
2188
2189 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002190 wpa_msg_global(wpa_s, MSG_INFO,
2191 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2192 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002193 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpas_p2p_remove_pending_group_interface(wpa_s);
2195 return;
2196 }
2197
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002198 if (!res->role_go) {
2199 /* Inform driver of the operating channel of GO. */
2200 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2201 }
2202
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203 if (wpa_s->p2p_go_ht40)
2204 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002205 if (wpa_s->p2p_go_vht)
2206 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002207 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2208 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002209
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002210 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2211 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2212 " wps_method=%s",
2213 res->role_go ? "GO" : "client", res->freq, res->ht40,
2214 MAC2STR(res->peer_device_addr),
2215 MAC2STR(res->peer_interface_addr),
2216 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218
Dmitry Shmidt04949592012-07-19 12:16:46 -07002219 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2220 struct wpa_ssid *ssid;
2221 ssid = wpa_config_get_network(wpa_s->conf,
2222 wpa_s->p2p_persistent_id);
2223 if (ssid && ssid->disabled == 2 &&
2224 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2225 size_t len = os_strlen(ssid->passphrase);
2226 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2227 "on requested persistent group");
2228 os_memcpy(res->passphrase, ssid->passphrase, len);
2229 res->passphrase[len] = '\0';
2230 }
2231 }
2232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002234 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2236 if (group_wpa_s == NULL) {
2237 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002238 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2239 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002240 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 return;
2242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2244 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002246 group_wpa_s = wpa_s->parent;
2247 wpa_s->global->p2p_group_formation = group_wpa_s;
2248 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002249 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002252 group_wpa_s->p2p_in_provisioning = 1;
2253 group_wpa_s->p2pdev = wpa_s;
2254 if (group_wpa_s != wpa_s) {
2255 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2256 sizeof(group_wpa_s->p2p_pin));
2257 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2258 }
2259 if (res->role_go) {
2260 wpas_start_wps_go(group_wpa_s, res, 1);
2261 } else {
2262 os_get_reltime(&group_wpa_s->scan_min_time);
2263 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 }
2265
2266 wpa_s->p2p_long_listen = 0;
2267 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2268
2269 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2270 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2271 (res->peer_config_timeout % 100) * 10000,
2272 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2273}
2274
2275
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002276static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2277 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278{
2279 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002280 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002281 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2282 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002284 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285}
2286
2287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002288static void wpas_dev_found(void *ctx, const u8 *addr,
2289 const struct p2p_peer_info *info,
2290 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291{
Roshan Piusfd2fd662017-01-23 13:41:57 -08002292 u8 *wfd_dev_info = NULL;
2293 u8 wfd_dev_info_len = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002294#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 struct wpa_supplicant *wpa_s = ctx;
2296 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002297 char *wfd_dev_info_hex = NULL;
2298
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002299#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002300 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2301 WFD_SUBELEM_DEVICE_INFO);
Roshan Piusfd2fd662017-01-23 13:41:57 -08002302 if (wfd_dev_info_hex) {
2303 wfd_dev_info_len = strlen(wfd_dev_info_hex) / 2;
2304 wfd_dev_info = os_zalloc(wfd_dev_info_len);
2305 // Only used for notification, so not handling error.
2306 hexstr2bin(wfd_dev_info_hex, wfd_dev_info, wfd_dev_info_len);
2307 }
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002308#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002309
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002310 if (info->p2ps_instance) {
2311 char str[256];
2312 const u8 *buf = wpabuf_head(info->p2ps_instance);
2313 size_t len = wpabuf_len(info->p2ps_instance);
2314
2315 while (len) {
2316 u32 id;
2317 u16 methods;
2318 u8 str_len;
2319
2320 if (len < 4 + 2 + 1)
2321 break;
2322 id = WPA_GET_LE32(buf);
2323 buf += sizeof(u32);
2324 methods = WPA_GET_BE16(buf);
2325 buf += sizeof(u16);
2326 str_len = *buf++;
2327 if (str_len > len - 4 - 2 - 1)
2328 break;
2329 os_memcpy(str, buf, str_len);
2330 str[str_len] = '\0';
2331 buf += str_len;
2332 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2333
2334 wpa_msg_global(wpa_s, MSG_INFO,
2335 P2P_EVENT_DEVICE_FOUND MACSTR
2336 " p2p_dev_addr=" MACSTR
2337 " pri_dev_type=%s name='%s'"
2338 " config_methods=0x%x"
2339 " dev_capab=0x%x"
2340 " group_capab=0x%x"
2341 " adv_id=%x asp_svc=%s%s",
2342 MAC2STR(addr),
2343 MAC2STR(info->p2p_device_addr),
2344 wps_dev_type_bin2str(
2345 info->pri_dev_type,
2346 devtype, sizeof(devtype)),
2347 info->device_name, methods,
2348 info->dev_capab, info->group_capab,
2349 id, str,
2350 info->vendor_elems ?
2351 " vendor_elems=1" : "");
2352 }
2353 goto done;
2354 }
2355
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002356 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2357 " p2p_dev_addr=" MACSTR
2358 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002359 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002360 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2361 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2362 sizeof(devtype)),
2363 info->device_name, info->config_methods,
2364 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002365 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002366 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002367 info->vendor_elems ? " vendor_elems=1" : "",
2368 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002370done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002371 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002372#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002373
Roshan Piusfd2fd662017-01-23 13:41:57 -08002374 wpas_notify_p2p_device_found(ctx, addr, info, wfd_dev_info,
2375 wfd_dev_info_len, new_device);
2376 os_free(wfd_dev_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377}
2378
2379
2380static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2381{
2382 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002383
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002384 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2385 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2388}
2389
2390
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002391static void wpas_find_stopped(void *ctx)
2392{
2393 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002394
2395 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2396 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2397
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002398 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002399 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002400}
2401
2402
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002403struct wpas_p2p_listen_work {
2404 unsigned int freq;
2405 unsigned int duration;
2406 struct wpabuf *probe_resp_ie;
2407};
2408
2409
2410static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2411{
2412 if (lwork == NULL)
2413 return;
2414 wpabuf_free(lwork->probe_resp_ie);
2415 os_free(lwork);
2416}
2417
2418
2419static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2420{
2421 struct wpas_p2p_listen_work *lwork;
2422
2423 if (!wpa_s->p2p_listen_work)
2424 return;
2425
2426 lwork = wpa_s->p2p_listen_work->ctx;
2427 wpas_p2p_listen_work_free(lwork);
2428 radio_work_done(wpa_s->p2p_listen_work);
2429 wpa_s->p2p_listen_work = NULL;
2430}
2431
2432
2433static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2434{
2435 struct wpa_supplicant *wpa_s = work->wpa_s;
2436 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002437 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002438
2439 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002440 if (work->started) {
2441 wpa_s->p2p_listen_work = NULL;
2442 wpas_stop_listen(wpa_s);
2443 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002444 wpas_p2p_listen_work_free(lwork);
2445 return;
2446 }
2447
2448 wpa_s->p2p_listen_work = work;
2449
2450 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2451
2452 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2453 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2454 "report received Probe Request frames");
2455 wpas_p2p_listen_work_done(wpa_s);
2456 return;
2457 }
2458
2459 wpa_s->pending_listen_freq = lwork->freq;
2460 wpa_s->pending_listen_duration = lwork->duration;
2461
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002462 duration = lwork->duration;
2463#ifdef CONFIG_TESTING_OPTIONS
2464 if (wpa_s->extra_roc_dur) {
2465 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2466 duration, duration + wpa_s->extra_roc_dur);
2467 duration += wpa_s->extra_roc_dur;
2468 }
2469#endif /* CONFIG_TESTING_OPTIONS */
2470
2471 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002472 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2473 "to remain on channel (%u MHz) for Listen "
2474 "state", lwork->freq);
2475 wpas_p2p_listen_work_done(wpa_s);
2476 wpa_s->pending_listen_freq = 0;
2477 return;
2478 }
2479 wpa_s->off_channel_freq = 0;
2480 wpa_s->roc_waiting_drv_freq = lwork->freq;
2481}
2482
2483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484static int wpas_start_listen(void *ctx, unsigned int freq,
2485 unsigned int duration,
2486 const struct wpabuf *probe_resp_ie)
2487{
2488 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002489 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002491 if (wpa_s->p2p_listen_work) {
2492 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 return -1;
2494 }
2495
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002496 lwork = os_zalloc(sizeof(*lwork));
2497 if (lwork == NULL)
2498 return -1;
2499 lwork->freq = freq;
2500 lwork->duration = duration;
2501 if (probe_resp_ie) {
2502 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2503 if (lwork->probe_resp_ie == NULL) {
2504 wpas_p2p_listen_work_free(lwork);
2505 return -1;
2506 }
2507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002509 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2510 lwork) < 0) {
2511 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512 return -1;
2513 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514
2515 return 0;
2516}
2517
2518
2519static void wpas_stop_listen(void *ctx)
2520{
2521 struct wpa_supplicant *wpa_s = ctx;
2522 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2523 wpa_drv_cancel_remain_on_channel(wpa_s);
2524 wpa_s->off_channel_freq = 0;
2525 wpa_s->roc_waiting_drv_freq = 0;
2526 }
2527 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002528
2529 /*
2530 * Don't cancel Probe Request RX reporting for a connected P2P Client
2531 * handling Probe Request frames.
2532 */
2533 if (!wpa_s->p2p_cli_probe)
2534 wpa_drv_probe_req_report(wpa_s, 0);
2535
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002536 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537}
2538
2539
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002540static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2541 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542{
2543 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002544 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2545 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002546}
2547
2548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2550 const u8 *peer, const char *params,
2551 unsigned int generated_pin)
2552{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002553 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2554 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555}
2556
2557
2558static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2559 const u8 *peer, const char *params)
2560{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002561 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2562 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563}
2564
2565
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002566static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2567 const u8 *dev_addr, const u8 *pri_dev_type,
2568 const char *dev_name, u16 supp_config_methods,
2569 u8 dev_capab, u8 group_capab, const u8 *group_id,
2570 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571{
2572 struct wpa_supplicant *wpa_s = ctx;
2573 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002574 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575 u8 empty_dev_type[8];
2576 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002578 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002579
2580 if (group_id) {
2581 for (group = wpa_s->global->ifaces; group; group = group->next)
2582 {
2583 struct wpa_ssid *s = group->current_ssid;
2584 if (s != NULL &&
2585 s->mode == WPAS_MODE_P2P_GO &&
2586 group_id_len - ETH_ALEN == s->ssid_len &&
2587 os_memcmp(group_id + ETH_ALEN, s->ssid,
2588 s->ssid_len) == 0)
2589 break;
2590 }
2591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592
2593 if (pri_dev_type == NULL) {
2594 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2595 pri_dev_type = empty_dev_type;
2596 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002597 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2598 " pri_dev_type=%s name='%s' config_methods=0x%x "
2599 "dev_capab=0x%x group_capab=0x%x%s%s",
2600 MAC2STR(dev_addr),
2601 wps_dev_type_bin2str(pri_dev_type, devtype,
2602 sizeof(devtype)),
2603 dev_name, supp_config_methods, dev_capab, group_capab,
2604 group ? " group=" : "",
2605 group ? group->ifname : "");
2606 if (os_snprintf_error(sizeof(params), res))
2607 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002610 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002611 if (wps_generate_pin(&generated_pin) < 0) {
2612 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2613 wpas_notify_p2p_provision_discovery(
2614 wpa_s, peer, 0 /* response */,
2615 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2616 return;
2617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 wpas_prov_disc_local_display(wpa_s, peer, params,
2619 generated_pin);
2620 } else if (config_methods & WPS_CONFIG_KEYPAD)
2621 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2622 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002623 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2624 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002625
2626 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2627 P2P_PROV_DISC_SUCCESS,
2628 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629}
2630
2631
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002632static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002633{
2634 struct wpa_supplicant *wpa_s = ctx;
2635 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002636 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002638 if (wpa_s->pending_pd_before_join &&
2639 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2640 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2641 wpa_s->pending_pd_before_join = 0;
2642 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2643 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002644 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002645 return;
2646 }
2647
Dmitry Shmidt04949592012-07-19 12:16:46 -07002648 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002649 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2650 int res;
2651
2652 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2653 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2654 if (os_snprintf_error(sizeof(params), res))
2655 params[sizeof(params) - 1] = '\0';
2656 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002657 params[0] = '\0';
2658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002660 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002662 if (wps_generate_pin(&generated_pin) < 0) {
2663 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2664 wpas_notify_p2p_provision_discovery(
2665 wpa_s, peer, 0 /* response */,
2666 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2667 return;
2668 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002669 wpas_prov_disc_local_display(wpa_s, peer, params,
2670 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002672 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2673 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674
Jouni Malinen75ecf522011-06-27 15:19:46 -07002675 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2676 P2P_PROV_DISC_SUCCESS,
2677 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678}
2679
2680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002681static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002682 enum p2p_prov_disc_status status,
2683 u32 adv_id, const u8 *adv_mac,
2684 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002685{
2686 struct wpa_supplicant *wpa_s = ctx;
2687
Dmitry Shmidt04949592012-07-19 12:16:46 -07002688 if (wpa_s->p2p_fallback_to_go_neg) {
2689 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2690 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002691 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002692 P2P_EVENT_FALLBACK_TO_GO_NEG
2693 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002694 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2695 return;
2696 }
2697
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002698 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002699 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002700 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2701 "join-existing-group operation (no ACK for PD "
2702 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002703 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002704 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002705 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002706
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002707 if (adv_id && adv_mac && deferred_session_resp) {
2708 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2709 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2710 " deferred_session_resp='%s'",
2711 MAC2STR(peer), status, adv_id,
2712 deferred_session_resp);
2713 } else if (adv_id && adv_mac) {
2714 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2715 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2716 MAC2STR(peer), status, adv_id);
2717 } else {
2718 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2719 " p2p_dev_addr=" MACSTR " status=%d",
2720 MAC2STR(peer), status);
2721 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002722
Jouni Malinen75ecf522011-06-27 15:19:46 -07002723 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2724 status, 0, 0);
2725}
2726
2727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002728static int freq_included(struct wpa_supplicant *wpa_s,
2729 const struct p2p_channels *channels,
2730 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002731{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002732 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2733 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2734 return 1;
2735 return 0;
2736}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002737
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002738
2739static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2740{
2741 unsigned int num = P2P_MAX_CHANNELS;
2742 int *common_freqs;
2743 int ret;
2744
2745 p2p_go_dump_common_freqs(wpa_s);
2746 common_freqs = os_calloc(num, sizeof(int));
2747 if (!common_freqs)
2748 return;
2749
2750 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2751 if (ret < 0) {
2752 wpa_dbg(wpa_s, MSG_DEBUG,
2753 "P2P: Failed to get group common freqs");
2754 os_free(common_freqs);
2755 return;
2756 }
2757
2758 os_free(wpa_s->p2p_group_common_freqs);
2759 wpa_s->p2p_group_common_freqs = common_freqs;
2760 wpa_s->p2p_group_common_freqs_num = num;
2761 p2p_go_dump_common_freqs(wpa_s);
2762}
2763
2764
2765/*
2766 * Check if the given frequency is one of the possible operating frequencies
2767 * set after the completion of the GO Negotiation.
2768 */
2769static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2770{
2771 unsigned int i;
2772
2773 p2p_go_dump_common_freqs(wpa_s);
2774
2775 /* assume no restrictions */
2776 if (!wpa_s->p2p_group_common_freqs_num)
2777 return 1;
2778
2779 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2780 if (wpa_s->p2p_group_common_freqs[i] == freq)
2781 return 1;
2782 }
2783 return 0;
2784}
2785
2786
2787static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2788 struct sta_info *sta, void *ctx)
2789{
2790 int *ecsa_support = ctx;
2791
2792 *ecsa_support &= sta->ecsa_supported;
2793
2794 return 0;
2795}
2796
2797
2798/* Check if all the peers support eCSA */
2799static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2800{
2801 int ecsa_support = 1;
2802
2803 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2804 &ecsa_support);
2805
2806 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002807}
2808
2809
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002810/**
2811 * Pick the best frequency to use from all the currently used frequencies.
2812 */
2813static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2814 struct wpa_used_freq_data *freqs,
2815 unsigned int num)
2816{
2817 unsigned int i, c;
2818
2819 /* find a candidate freq that is supported by P2P */
2820 for (c = 0; c < num; c++)
2821 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2822 break;
2823
2824 if (c == num)
2825 return 0;
2826
2827 /* once we have a candidate, try to find a 'better' one */
2828 for (i = c + 1; i < num; i++) {
2829 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2830 continue;
2831
2832 /*
2833 * 1. Infrastructure station interfaces have higher preference.
2834 * 2. P2P Clients have higher preference.
2835 * 3. All others.
2836 */
2837 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2838 c = i;
2839 break;
2840 }
2841
2842 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2843 c = i;
2844 }
2845 return freqs[c].freq;
2846}
2847
2848
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2850 const u8 *go_dev_addr, const u8 *ssid,
2851 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002852 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002853 const struct p2p_channels *channels,
2854 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855{
2856 struct wpa_supplicant *wpa_s = ctx;
2857 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002858 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002860 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861
2862 if (!persistent_group) {
2863 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002864 " to join an active group (SSID: %s)",
2865 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2867 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2868 == 0 ||
2869 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2870 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2871 "authorized invitation");
2872 goto accept_inv;
2873 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002874
2875#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002876 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2877 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002878 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002879 wpa_s->p2p_wps_method = WPS_NFC;
2880 wpa_s->pending_join_wps_method = WPS_NFC;
2881 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002882 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002883 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002884 bssid, ETH_ALEN);
2885 goto accept_inv;
2886 }
2887#endif /* CONFIG_WPS_NFC */
2888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002889 /*
2890 * Do not accept the invitation automatically; notify user and
2891 * request approval.
2892 */
2893 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2894 }
2895
2896 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2897 if (grp) {
2898 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2899 "running persistent group");
2900 if (*go)
2901 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2902 goto accept_inv;
2903 }
2904
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002905 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2906 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2907 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2908 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002909 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002910 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2912
2913 for (s = wpa_s->conf->ssid; s; s = s->next) {
2914 if (s->disabled == 2 &&
2915 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2916 s->ssid_len == ssid_len &&
2917 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2918 break;
2919 }
2920
2921 if (!s) {
2922 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2923 " requested reinvocation of an unknown group",
2924 MAC2STR(sa));
2925 return P2P_SC_FAIL_UNKNOWN_GROUP;
2926 }
2927
2928 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2929 *go = 1;
2930 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2931 wpa_printf(MSG_DEBUG, "P2P: The only available "
2932 "interface is already in use - reject "
2933 "invitation");
2934 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2935 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002936 if (wpa_s->p2p_mgmt)
2937 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2938 ETH_ALEN);
2939 else
2940 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 } else if (s->mode == WPAS_MODE_P2P_GO) {
2942 *go = 1;
2943 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2944 {
2945 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2946 "interface address for the group");
2947 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2948 }
2949 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2950 ETH_ALEN);
2951 }
2952
2953accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002954 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2955
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002956 best_freq = 0;
2957 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2958 sizeof(struct wpa_used_freq_data));
2959 if (freqs) {
2960 int num_channels = wpa_s->num_multichan_concurrent;
2961 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2962 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2963 os_free(freqs);
2964 }
2965
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002966 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002967 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002968 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002969 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002970
2971 if (wpa_s->num_multichan_concurrent < 2 ||
2972 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2973 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002974 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 }
2977
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002978 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2979 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002980 if (*go == 0) {
2981 /* We are the client */
2982 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2983 "running a GO but we are capable of MCC, "
2984 "figure out the best channel to use");
2985 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002986 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002987 /* We are the GO, and *force_freq is not in the
2988 * intersection */
2989 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2990 "in intersection but we are capable of MCC, "
2991 "figure out the best channel to use",
2992 *force_freq);
2993 *force_freq = 0;
2994 }
2995 }
2996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 return P2P_SC_SUCCESS;
2998}
2999
3000
3001static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3002 const u8 *ssid, size_t ssid_len,
3003 const u8 *go_dev_addr, u8 status,
3004 int op_freq)
3005{
3006 struct wpa_supplicant *wpa_s = ctx;
3007 struct wpa_ssid *s;
3008
3009 for (s = wpa_s->conf->ssid; s; s = s->next) {
3010 if (s->disabled == 2 &&
3011 s->ssid_len == ssid_len &&
3012 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3013 break;
3014 }
3015
3016 if (status == P2P_SC_SUCCESS) {
3017 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003018 " was accepted; op_freq=%d MHz, SSID=%s",
3019 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003021 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003022 if (go) {
3023 wpa_msg_global(wpa_s, MSG_INFO,
3024 P2P_EVENT_INVITATION_ACCEPTED
3025 "sa=" MACSTR
3026 " persistent=%d freq=%d",
3027 MAC2STR(sa), s->id, op_freq);
3028 } else {
3029 wpa_msg_global(wpa_s, MSG_INFO,
3030 P2P_EVENT_INVITATION_ACCEPTED
3031 "sa=" MACSTR
3032 " persistent=%d",
3033 MAC2STR(sa), s->id);
3034 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003035 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003036 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003037 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3038 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003040 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003041 wpa_msg_global(wpa_s, MSG_INFO,
3042 P2P_EVENT_INVITATION_ACCEPTED
3043 "sa=" MACSTR " go_dev_addr=" MACSTR
3044 " bssid=" MACSTR " unknown-network",
3045 MAC2STR(sa), MAC2STR(go_dev_addr),
3046 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003048 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003049 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050 }
3051 return;
3052 }
3053
3054 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3055 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3056 " was rejected (status %u)", MAC2STR(sa), status);
3057 return;
3058 }
3059
3060 if (!s) {
3061 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003062 wpa_msg_global(wpa_s, MSG_INFO,
3063 P2P_EVENT_INVITATION_RECEIVED
3064 "sa=" MACSTR " go_dev_addr=" MACSTR
3065 " bssid=" MACSTR " unknown-network",
3066 MAC2STR(sa), MAC2STR(go_dev_addr),
3067 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003069 wpa_msg_global(wpa_s, MSG_INFO,
3070 P2P_EVENT_INVITATION_RECEIVED
3071 "sa=" MACSTR " go_dev_addr=" MACSTR
3072 " unknown-network",
3073 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003075 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3076 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003077 return;
3078 }
3079
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003080 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003081 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3082 "sa=" MACSTR " persistent=%d freq=%d",
3083 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003084 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003085 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3086 "sa=" MACSTR " persistent=%d",
3087 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003088 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003089 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3090 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091}
3092
3093
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003094static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3095 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003096 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003097{
3098 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003099 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003100
3101 if (ssid == NULL)
3102 return;
3103
3104 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003105 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003106 ETH_ALEN) == 0)
3107 break;
3108 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003109 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003110 if (ssid->mode != WPAS_MODE_P2P_GO &&
3111 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3112 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3113 "due to invitation result", ssid->id);
3114 wpas_notify_network_removed(wpa_s, ssid);
3115 wpa_config_remove_network(wpa_s->conf, ssid->id);
3116 return;
3117 }
3118 return; /* Peer not found in client list */
3119 }
3120
3121 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003122 "group %d client list%s",
3123 MAC2STR(peer), ssid->id,
3124 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003125 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3126 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3127 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003128 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003129 if (p2p_wpa_s->conf->update_config &&
3130 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003131 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003132}
3133
3134
3135static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3136 const u8 *peer)
3137{
3138 struct wpa_ssid *ssid;
3139
3140 wpa_s = wpa_s->global->p2p_invite_group;
3141 if (wpa_s == NULL)
3142 return; /* No known invitation group */
3143 ssid = wpa_s->current_ssid;
3144 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3145 !ssid->p2p_persistent_group)
3146 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003147 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003148 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003149 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003150}
3151
3152
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003153static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003154 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003155 const u8 *peer, int neg_freq,
3156 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003157{
3158 struct wpa_supplicant *wpa_s = ctx;
3159 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003160 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161
3162 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003163 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3164 "status=%d " MACSTR,
3165 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003167 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3168 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 }
3170 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3171
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003172 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3173 status, MAC2STR(peer));
3174 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003175 struct wpa_supplicant *group_if =
3176 wpa_s->global->p2p_invite_group;
3177
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003178 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3179 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003180
3181 /*
3182 * Invitation to an active group. If this is successful and we
3183 * are the GO, set the client wait to postpone some concurrent
3184 * operations and to allow provisioning and connection to happen
3185 * more quickly.
3186 */
3187 if (status == P2P_SC_SUCCESS &&
3188 group_if && group_if->current_ssid &&
3189 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3190 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3191#ifdef CONFIG_TESTING_OPTIONS
3192 if (group_if->p2p_go_csa_on_inv) {
3193 wpa_printf(MSG_DEBUG,
3194 "Testing: force P2P GO CSA after invitation");
3195 eloop_cancel_timeout(
3196 wpas_p2p_reconsider_moving_go,
3197 wpa_s, NULL);
3198 eloop_register_timeout(
3199 0, 50000,
3200 wpas_p2p_reconsider_moving_go,
3201 wpa_s, NULL);
3202 }
3203#endif /* CONFIG_TESTING_OPTIONS */
3204 }
3205 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003208 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3209 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3210 "invitation exchange to indicate readiness for "
3211 "re-invocation");
3212 }
3213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003215 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3216 ssid = wpa_config_get_network(
3217 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003218 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 wpas_p2p_remove_pending_group_interface(wpa_s);
3221 return;
3222 }
3223
3224 ssid = wpa_config_get_network(wpa_s->conf,
3225 wpa_s->pending_invite_ssid_id);
3226 if (ssid == NULL) {
3227 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3228 "data matching with invitation");
3229 return;
3230 }
3231
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003232 /*
3233 * The peer could have missed our ctrl::ack frame for Invitation
3234 * Response and continue retransmitting the frame. To reduce the
3235 * likelihood of the peer not getting successful TX status for the
3236 * Invitation Response frame, wait a short time here before starting
3237 * the persistent group so that we will remain on the current channel to
3238 * acknowledge any possible retransmission from the peer.
3239 */
3240 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3241 "starting persistent group");
3242 os_sleep(0, 50000);
3243
Dmitry Shmidt15907092014-03-25 10:42:57 -07003244 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003245 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003246 freq = neg_freq;
3247 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003248 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003249 freq = peer_oper_freq;
3250 else
3251 freq = 0;
3252
3253 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3254 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003256 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003257 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003258 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003259 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003260 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003261 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003262 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003263 ssid->mode == WPAS_MODE_P2P_GO ?
3264 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003265 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266}
3267
3268
Dmitry Shmidt04949592012-07-19 12:16:46 -07003269static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3270 unsigned int freq)
3271{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003272 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3273 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003274 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003275}
3276
3277
3278static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3279{
3280 reg->channel[reg->channels] = chan;
3281 reg->channels++;
3282}
3283
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003286 struct p2p_channels *chan,
3287 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288{
3289 int i, cla = 0;
3290
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003291 wpa_s->global->p2p_24ghz_social_channels = 1;
3292
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003293 os_memset(cli_chan, 0, sizeof(*cli_chan));
3294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3296 "band");
3297
3298 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3299 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003300 chan->reg_class[cla].channels = 0;
3301 for (i = 0; i < 11; i++) {
3302 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3303 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3304 }
3305 if (chan->reg_class[cla].channels)
3306 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307
3308 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3309 "band");
3310
3311 /* Operating class 115 - 5 GHz, channels 36-48 */
3312 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 chan->reg_class[cla].channels = 0;
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3316 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3317 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3318 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3319 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3320 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3321 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3322 if (chan->reg_class[cla].channels)
3323 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324
3325 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3326 "band");
3327
3328 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3329 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003330 chan->reg_class[cla].channels = 0;
3331 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3332 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3333 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3334 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3335 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3336 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3337 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3338 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3339 if (chan->reg_class[cla].channels)
3340 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003341
3342 chan->reg_classes = cla;
3343 return 0;
3344}
3345
3346
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003347enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003348 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003349};
3350
Dmitry Shmidt04949592012-07-19 12:16:46 -07003351static int has_channel(struct wpa_global *global,
3352 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353{
3354 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003355 unsigned int freq;
3356
3357 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3358 chan * 5;
3359 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003360 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003361
3362 for (i = 0; i < mode->num_channels; i++) {
3363 if (mode->channels[i].chan == chan) {
3364 if (flags)
3365 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003366 if (mode->channels[i].flag &
3367 (HOSTAPD_CHAN_DISABLED |
3368 HOSTAPD_CHAN_RADAR))
3369 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003370 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3371 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003372 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 }
3374 }
3375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003376 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377}
3378
3379
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003380static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3381 struct hostapd_hw_modes *mode,
3382 u8 channel)
3383{
3384 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003385 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003386
3387 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3388 return 0;
3389
3390 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3391 /*
3392 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3393 * so the center channel is 6 channels away from the start/end.
3394 */
3395 if (channel >= center_channels[i] - 6 &&
3396 channel <= center_channels[i] + 6)
3397 return center_channels[i];
3398
3399 return 0;
3400}
3401
3402
3403static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3404 struct hostapd_hw_modes *mode,
3405 u8 channel, u8 bw)
3406{
3407 u8 center_chan;
3408 int i, flags;
3409 enum chan_allowed res, ret = ALLOWED;
3410
3411 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3412 if (!center_chan)
3413 return NOT_ALLOWED;
3414 if (center_chan >= 58 && center_chan <= 138)
3415 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3416
3417 /* check all the channels are available */
3418 for (i = 0; i < 4; i++) {
3419 int adj_chan = center_chan - 6 + i * 4;
3420
3421 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3422 if (res == NOT_ALLOWED)
3423 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003424 if (res == NO_IR)
3425 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003426
3427 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3428 return NOT_ALLOWED;
3429 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3430 return NOT_ALLOWED;
3431 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3432 return NOT_ALLOWED;
3433 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3434 return NOT_ALLOWED;
3435 }
3436
3437 return ret;
3438}
3439
3440
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003441static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3442 struct hostapd_hw_modes *mode,
3443 u8 channel)
3444{
3445 u8 center_channels[] = { 50, 114 };
3446 unsigned int i;
3447
3448 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3449 return 0;
3450
3451 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3452 /*
3453 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3454 * so the center channel is 14 channels away from the start/end.
3455 */
3456 if (channel >= center_channels[i] - 14 &&
3457 channel <= center_channels[i] + 14)
3458 return center_channels[i];
3459
3460 return 0;
3461}
3462
3463
3464static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3465 struct hostapd_hw_modes *mode,
3466 u8 channel, u8 bw)
3467{
3468 u8 center_chan;
3469 int i, flags;
3470 enum chan_allowed res, ret = ALLOWED;
3471
3472 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3473 if (!center_chan)
3474 return NOT_ALLOWED;
3475 /* VHT 160 MHz uses DFS channels in most countries. */
3476
3477 /* Check all the channels are available */
3478 for (i = 0; i < 8; i++) {
3479 int adj_chan = center_chan - 14 + i * 4;
3480
3481 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3482 if (res == NOT_ALLOWED)
3483 return NOT_ALLOWED;
3484
3485 if (res == NO_IR)
3486 ret = NO_IR;
3487
3488 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3489 return NOT_ALLOWED;
3490 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3491 return NOT_ALLOWED;
3492 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3493 return NOT_ALLOWED;
3494 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3495 return NOT_ALLOWED;
3496 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3497 return NOT_ALLOWED;
3498 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3499 return NOT_ALLOWED;
3500 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3501 return NOT_ALLOWED;
3502 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3503 return NOT_ALLOWED;
3504 }
3505
3506 return ret;
3507}
3508
3509
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003510static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3511 struct hostapd_hw_modes *mode,
3512 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003513{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003514 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003515 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003516
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003517 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3518 if (bw == BW40MINUS) {
3519 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3520 return NOT_ALLOWED;
3521 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3522 } else if (bw == BW40PLUS) {
3523 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3524 return NOT_ALLOWED;
3525 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3526 } else if (bw == BW80) {
3527 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003528 } else if (bw == BW160) {
3529 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003530 }
3531
3532 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3533 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003534 if (res == NO_IR || res2 == NO_IR)
3535 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003536 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003537}
3538
3539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003541 struct p2p_channels *chan,
3542 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003544 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003545 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003547 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003548 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3549 "of all supported channels; assume dualband "
3550 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003551 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 }
3553
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003554 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003556 for (op = 0; global_op_class[op].op_class; op++) {
3557 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003558 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003559 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003561 if (o->p2p == NO_P2P_SUPP)
3562 continue;
3563
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003564 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003565 if (mode == NULL)
3566 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003567 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3568 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003569 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003570 enum chan_allowed res;
3571 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3572 if (res == ALLOWED) {
3573 if (reg == NULL) {
3574 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3575 o->op_class);
3576 reg = &chan->reg_class[cla];
3577 cla++;
3578 reg->reg_class = o->op_class;
3579 }
3580 reg->channel[reg->channels] = ch;
3581 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003582 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003583 wpa_s->conf->p2p_add_cli_chan) {
3584 if (cli_reg == NULL) {
3585 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3586 o->op_class);
3587 cli_reg = &cli_chan->reg_class[cli_cla];
3588 cli_cla++;
3589 cli_reg->reg_class = o->op_class;
3590 }
3591 cli_reg->channel[cli_reg->channels] = ch;
3592 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594 }
3595 if (reg) {
3596 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3597 reg->channel, reg->channels);
3598 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003599 if (cli_reg) {
3600 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3601 cli_reg->channel, cli_reg->channels);
3602 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 }
3604
3605 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003606 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003608 return 0;
3609}
3610
3611
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003612int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3613 struct hostapd_hw_modes *mode, u8 channel)
3614{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003615 int op;
3616 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003617
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003618 for (op = 0; global_op_class[op].op_class; op++) {
3619 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003620 u8 ch;
3621
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003622 if (o->p2p == NO_P2P_SUPP)
3623 continue;
3624
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003625 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3626 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003627 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3628 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003629 continue;
3630 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003631 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003632 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003633 }
3634 }
3635 return 0;
3636}
3637
3638
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003639int wpas_p2p_get_vht80_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, BW80))
3643 return 0;
3644
3645 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3646}
3647
3648
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003649int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3650 struct hostapd_hw_modes *mode, u8 channel)
3651{
3652 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3653 return 0;
3654 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3655}
3656
3657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3659 size_t buf_len)
3660{
3661 struct wpa_supplicant *wpa_s = ctx;
3662
3663 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3664 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3665 break;
3666 }
3667 if (wpa_s == NULL)
3668 return -1;
3669
3670 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3671}
3672
3673
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003674struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3675 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003676{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003677 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3678 struct wpa_ssid *s = wpa_s->current_ssid;
3679 if (s == NULL)
3680 continue;
3681 if (s->mode != WPAS_MODE_P2P_GO &&
3682 s->mode != WPAS_MODE_AP &&
3683 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3684 continue;
3685 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003686 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003687 continue;
3688 return wpa_s;
3689 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003690
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003691 return NULL;
3692
3693}
3694
3695
3696struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3697 const u8 *peer_dev_addr)
3698{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003699 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3700 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003701 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003702 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003703 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3704 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003705 }
3706
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003707 return NULL;
3708}
3709
3710
3711static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3712{
3713 struct wpa_supplicant *wpa_s = ctx;
3714
3715 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003716}
3717
3718
Dmitry Shmidt18463232014-01-24 12:29:41 -08003719static int wpas_is_concurrent_session_active(void *ctx)
3720{
3721 struct wpa_supplicant *wpa_s = ctx;
3722 struct wpa_supplicant *ifs;
3723
3724 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3725 if (ifs == wpa_s)
3726 continue;
3727 if (ifs->wpa_state > WPA_ASSOCIATED)
3728 return 1;
3729 }
3730 return 0;
3731}
3732
3733
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003734static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3735{
3736 struct wpa_supplicant *wpa_s = ctx;
3737 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3738}
3739
3740
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003741int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3742 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003743{
3744 struct wpa_interface iface;
3745 struct wpa_supplicant *p2pdev_wpa_s;
3746 char ifname[100];
3747 char force_name[100];
3748 int ret;
3749
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003750 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3751 wpa_s->ifname);
3752 if (os_snprintf_error(sizeof(ifname), ret))
3753 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003754 force_name[0] = '\0';
3755 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3756 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3757 force_name, wpa_s->pending_interface_addr, NULL);
3758 if (ret < 0) {
3759 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3760 return ret;
3761 }
3762 os_strlcpy(wpa_s->pending_interface_name, ifname,
3763 sizeof(wpa_s->pending_interface_name));
3764
3765 os_memset(&iface, 0, sizeof(iface));
3766 iface.p2p_mgmt = 1;
3767 iface.ifname = wpa_s->pending_interface_name;
3768 iface.driver = wpa_s->driver->name;
3769 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003770
3771 /*
3772 * If a P2P Device configuration file was given, use it as the interface
3773 * configuration file (instead of using parent's configuration file.
3774 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003775 if (conf_p2p_dev) {
3776 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003777 iface.ctrl_interface = NULL;
3778 } else {
3779 iface.confname = wpa_s->confname;
3780 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3781 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003782
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003783 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003784 if (!p2pdev_wpa_s) {
3785 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3786 return -1;
3787 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003788
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003789 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003790 wpa_s->pending_interface_name[0] = '\0';
3791 return 0;
3792}
3793
3794
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003795static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3796 const u8 *noa, size_t noa_len)
3797{
3798 struct wpa_supplicant *wpa_s, *intf = ctx;
3799 char hex[100];
3800
3801 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3802 if (wpa_s->waiting_presence_resp)
3803 break;
3804 }
3805 if (!wpa_s) {
3806 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3807 return;
3808 }
3809 wpa_s->waiting_presence_resp = 0;
3810
3811 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3812 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3813 " status=%u noa=%s", MAC2STR(src), status, hex);
3814}
3815
3816
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003817static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3818 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003819 u8 *ret_ssid, size_t *ret_ssid_len,
3820 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003821{
3822 struct wpa_supplicant *wpa_s = ctx;
3823 struct wpa_ssid *s;
3824
3825 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3826 if (s) {
3827 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3828 *ret_ssid_len = s->ssid_len;
3829 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003830
3831 if (s->mode != WPAS_MODE_P2P_GO) {
3832 os_memset(intended_iface_addr, 0, ETH_ALEN);
3833 } else if (wpas_p2p_create_iface(wpa_s)) {
3834 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3835 return 0;
3836
3837 os_memcpy(intended_iface_addr,
3838 wpa_s->pending_interface_addr, ETH_ALEN);
3839 } else {
3840 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3841 ETH_ALEN);
3842 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003843 return 1;
3844 }
3845
3846 return 0;
3847}
3848
3849
3850static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003851 u8 *ssid, size_t *ssid_len, int *group_iface,
3852 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003853{
3854 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003855 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003856 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003857
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003858 /*
3859 * group_iface will be set to 1 only if a dedicated interface for P2P
3860 * role is required. First, we try to reuse an active GO. However,
3861 * if it is not present, we will try to reactivate an existing
3862 * persistent group and set group_iface to 1, so the caller will know
3863 * that the pending interface should be used.
3864 */
3865 *group_iface = 0;
3866
3867 if (freq)
3868 *freq = 0;
3869
3870 go = wpas_p2p_get_go_group(wpa_s);
3871 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003872 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003873 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003874 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003875 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3876 else
3877 return 0;
3878 } else {
3879 s = go->current_ssid;
3880 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3881 if (freq)
3882 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003883 }
3884
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003885 os_memcpy(ssid, s->ssid, s->ssid_len);
3886 *ssid_len = s->ssid_len;
3887
3888 return 1;
3889}
3890
3891
3892static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3893 const u8 *ssid, size_t ssid_len)
3894{
3895 struct wpa_supplicant *wpa_s = ctx;
3896 struct wpa_ssid *s;
3897 int save_config = 0;
3898 size_t i;
3899
3900 /* Start with our first choice of Persistent Groups */
3901 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3902 if (go && ssid && ssid_len &&
3903 s->ssid_len == ssid_len &&
3904 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3905 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3906 break;
3907
3908 /* Remove stale persistent group */
3909 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3910 wpa_config_remove_network(wpa_s->conf, s->id);
3911 save_config = 1;
3912 continue;
3913 }
3914
3915 for (i = 0; i < s->num_p2p_clients; i++) {
3916 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3917 peer, ETH_ALEN) != 0)
3918 continue;
3919
3920 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3921 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3922 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3923 break;
3924 }
3925 s->num_p2p_clients--;
3926 save_config = 1;
3927 }
3928
3929 if (save_config)
3930 p2p_config_write(wpa_s);
3931
3932 /* Return TRUE if valid SSID remains */
3933 return s != NULL;
3934}
3935
3936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003937static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3938 const u8 *feat_cap, size_t feat_cap_len)
3939{
3940 static const char pref[] = " feature_cap=";
3941 int ret;
3942
3943 buf[0] = '\0';
3944
3945 /*
3946 * We expect a feature capability to contain at least one byte to be
3947 * reported. The string buffer provided by the caller function is
3948 * expected to be big enough to contain all bytes of the attribute for
3949 * known specifications. This function truncates the reported bytes if
3950 * the feature capability data exceeds the string buffer size.
3951 */
3952 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3953 return;
3954
3955 os_memcpy(buf, pref, sizeof(pref));
3956 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3957 buf_len - sizeof(pref) + 1,
3958 feat_cap, feat_cap_len);
3959
3960 if (ret != (2 * (int) feat_cap_len))
3961 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3962}
3963
3964
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003965static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3966 const u8 *adv_mac, const u8 *ses_mac,
3967 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3968 u8 conncap, int passwd_id,
3969 const u8 *persist_ssid,
3970 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003971 int prov_start, const char *session_info,
3972 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003973 unsigned int freq,
3974 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003975{
3976 struct wpa_supplicant *wpa_s = ctx;
3977 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003978 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003979 int save_config = 0;
3980 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003981 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003982
3983 if (!dev)
3984 return;
3985
3986 os_memset(mac, 0, ETH_ALEN);
3987 if (!adv_mac)
3988 adv_mac = mac;
3989 if (!ses_mac)
3990 ses_mac = mac;
3991 if (!grp_mac)
3992 grp_mac = mac;
3993
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003994 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3995 feat_cap, feat_cap_len);
3996
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003997 if (prov_start) {
3998 if (session_info == NULL) {
3999 wpa_msg_global(wpa_s, MSG_INFO,
4000 P2P_EVENT_P2PS_PROVISION_START MACSTR
4001 " adv_id=%x conncap=%x"
4002 " adv_mac=" MACSTR
4003 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004004 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004005 MAC2STR(dev), adv_id, conncap,
4006 MAC2STR(adv_mac),
4007 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004008 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004009 } else {
4010 wpa_msg_global(wpa_s, MSG_INFO,
4011 P2P_EVENT_P2PS_PROVISION_START MACSTR
4012 " adv_id=%x conncap=%x"
4013 " adv_mac=" MACSTR
4014 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004015 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004016 MAC2STR(dev), adv_id, conncap,
4017 MAC2STR(adv_mac),
4018 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004019 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004020 }
4021 return;
4022 }
4023
4024 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4025 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4026
4027 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4028 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4029 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4030
4031 if (persistent_go && !persistent_go->num_p2p_clients) {
4032 /* remove empty persistent GO */
4033 wpa_config_remove_network(wpa_s->conf,
4034 persistent_go->id);
4035 }
4036
4037 wpa_msg_global(wpa_s, MSG_INFO,
4038 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4039 " status=%d"
4040 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004041 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004042 MAC2STR(dev), status,
4043 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004044 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004045 return;
4046 }
4047
4048 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004049 if (persist_ssid && persist_ssid_size)
4050 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4051 persist_ssid_size);
4052
4053 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4054 is_zero_ether_addr(grp_mac)) {
4055 wpa_dbg(wpa_s, MSG_ERROR,
4056 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4057 return;
4058 }
4059
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004060 for (;;) {
4061 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4062 if (!stale)
4063 break;
4064
4065 if (s && s->ssid_len == stale->ssid_len &&
4066 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4067 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4068 break;
4069
4070 /* Remove stale persistent group */
4071 if (stale->mode != WPAS_MODE_P2P_GO ||
4072 stale->num_p2p_clients <= 1) {
4073 wpa_config_remove_network(wpa_s->conf, stale->id);
4074 } else {
4075 size_t i;
4076
4077 for (i = 0; i < stale->num_p2p_clients; i++) {
4078 if (os_memcmp(stale->p2p_client_list +
4079 i * ETH_ALEN,
4080 dev, ETH_ALEN) == 0) {
4081 os_memmove(stale->p2p_client_list +
4082 i * ETH_ALEN,
4083 stale->p2p_client_list +
4084 (i + 1) * ETH_ALEN,
4085 (stale->num_p2p_clients -
4086 i - 1) * ETH_ALEN);
4087 break;
4088 }
4089 }
4090 stale->num_p2p_clients--;
4091 }
4092 save_config = 1;
4093 }
4094
4095 if (save_config)
4096 p2p_config_write(wpa_s);
4097
4098 if (s) {
4099 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4100 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4101
4102 if (persistent_go && s != persistent_go &&
4103 !persistent_go->num_p2p_clients) {
4104 /* remove empty persistent GO */
4105 wpa_config_remove_network(wpa_s->conf,
4106 persistent_go->id);
4107 /* Save config */
4108 }
4109
4110 wpa_msg_global(wpa_s, MSG_INFO,
4111 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4112 " status=%d"
4113 " adv_id=%x adv_mac=" MACSTR
4114 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004115 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004116 MAC2STR(dev), status,
4117 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004118 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004119 return;
4120 }
4121
4122 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004123 /*
4124 * We need to copy the interface name. Simply saving a
4125 * pointer isn't enough, since if we use pending_interface_name
4126 * it will be overwritten when the group is added.
4127 */
4128 char go_ifname[100];
4129
4130 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004131 if (!go_wpa_s) {
4132 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004133 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004134
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004135 if (!wpas_p2p_create_iface(wpa_s))
4136 os_memcpy(go_ifname, wpa_s->ifname,
4137 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004138 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004139 os_memcpy(go_ifname,
4140 wpa_s->pending_interface_name,
4141 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004142
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004143 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004144 wpas_p2ps_prov_complete(
4145 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4146 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004147 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004148 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4149 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004150 return;
4151 }
4152
4153 /* If PD Resp complete, start up the GO */
4154 if (response_done && persistent_go) {
4155 wpas_p2p_group_add_persistent(
4156 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004157 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004158 persistent_go->mode ==
4159 WPAS_MODE_P2P_GO ?
4160 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004161 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004162 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004163 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004164 }
4165
4166 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004167 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4168 ETH_ALEN);
4169 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004170 }
4171 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004172 os_memcpy(go_ifname, go_wpa_s->ifname,
4173 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004174
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004175 if (is_zero_ether_addr(grp_mac)) {
4176 wpa_dbg(go_wpa_s, MSG_DEBUG,
4177 "P2P: Setting PIN-1 for ANY");
4178 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4179 "12345670", NULL, 0,
4180 0);
4181 } else {
4182 wpa_dbg(go_wpa_s, MSG_DEBUG,
4183 "P2P: Setting PIN-1 for " MACSTR,
4184 MAC2STR(grp_mac));
4185 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4186 "12345670", NULL, 0,
4187 0);
4188 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004189
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004190 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4191 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004192 }
4193
4194 wpa_msg_global(wpa_s, MSG_INFO,
4195 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4196 " status=%d conncap=%x"
4197 " adv_id=%x adv_mac=" MACSTR
4198 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004199 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004200 MAC2STR(dev), status, conncap,
4201 adv_id, MAC2STR(adv_mac),
4202 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004203 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004204 return;
4205 }
4206
4207 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4208 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4209
4210 if (persistent_go && !persistent_go->num_p2p_clients) {
4211 /* remove empty persistent GO */
4212 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4213 }
4214
4215 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004216 char ssid_hex[32 * 2 + 1];
4217
4218 if (group_ssid)
4219 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4220 group_ssid, group_ssid_len);
4221 else
4222 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004223 wpa_msg_global(wpa_s, MSG_INFO,
4224 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4225 " status=%d conncap=%x"
4226 " adv_id=%x adv_mac=" MACSTR
4227 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004228 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004229 MAC2STR(dev), status, conncap,
4230 adv_id, MAC2STR(adv_mac),
4231 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004232 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4233 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004234 } else {
4235 wpa_msg_global(wpa_s, MSG_INFO,
4236 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4237 " status=%d conncap=%x"
4238 " adv_id=%x adv_mac=" MACSTR
4239 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004240 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004241 MAC2STR(dev), status, conncap,
4242 adv_id, MAC2STR(adv_mac),
4243 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004244 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004245 }
4246}
4247
4248
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004249static int _wpas_p2p_in_progress(void *ctx)
4250{
4251 struct wpa_supplicant *wpa_s = ctx;
4252 return wpas_p2p_in_progress(wpa_s);
4253}
4254
4255
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004256static int wpas_prov_disc_resp_cb(void *ctx)
4257{
4258 struct wpa_supplicant *wpa_s = ctx;
4259 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004260 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004261
4262 if (!wpa_s->global->pending_p2ps_group)
4263 return 0;
4264
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004265 freq = wpa_s->global->pending_p2ps_group_freq;
4266 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004267 wpa_s->global->pending_p2ps_group = 0;
4268
4269 if (wpas_p2p_get_go_group(wpa_s))
4270 return 0;
4271 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4272
4273 if (persistent_go) {
4274 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004275 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004276 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004277 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004278 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004279 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004280 }
4281
4282 return 1;
4283}
4284
4285
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004286static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4287 unsigned int *len,
4288 unsigned int *freq_list)
4289{
4290 struct wpa_supplicant *wpa_s = ctx;
4291
4292 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4293 WPA_IF_P2P_CLIENT, len, freq_list);
4294}
4295
4296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297/**
4298 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4299 * @global: Pointer to global data from wpa_supplicant_init()
4300 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4301 * Returns: 0 on success, -1 on failure
4302 */
4303int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4304{
4305 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306 int i;
4307
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004308 if (wpa_s->conf->p2p_disabled)
4309 return 0;
4310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004311 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4312 return 0;
4313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314 if (global->p2p)
4315 return 0;
4316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004319 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004320 p2p.p2p_scan = wpas_p2p_scan;
4321 p2p.send_action = wpas_send_action;
4322 p2p.send_action_done = wpas_send_action_done;
4323 p2p.go_neg_completed = wpas_go_neg_completed;
4324 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4325 p2p.dev_found = wpas_dev_found;
4326 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004327 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004328 p2p.start_listen = wpas_start_listen;
4329 p2p.stop_listen = wpas_stop_listen;
4330 p2p.send_probe_resp = wpas_send_probe_resp;
4331 p2p.sd_request = wpas_sd_request;
4332 p2p.sd_response = wpas_sd_response;
4333 p2p.prov_disc_req = wpas_prov_disc_req;
4334 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004335 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 p2p.invitation_process = wpas_invitation_process;
4337 p2p.invitation_received = wpas_invitation_received;
4338 p2p.invitation_result = wpas_invitation_result;
4339 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004340 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004341 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004342 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004343 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004344 p2p.get_persistent_group = wpas_get_persistent_group;
4345 p2p.get_go_info = wpas_get_go_info;
4346 p2p.remove_stale_groups = wpas_remove_stale_groups;
4347 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4348 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4349 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004350 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004351
4352 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004353 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354 p2p.dev_name = wpa_s->conf->device_name;
4355 p2p.manufacturer = wpa_s->conf->manufacturer;
4356 p2p.model_name = wpa_s->conf->model_name;
4357 p2p.model_number = wpa_s->conf->model_number;
4358 p2p.serial_number = wpa_s->conf->serial_number;
4359 if (wpa_s->wps) {
4360 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4361 p2p.config_methods = wpa_s->wps->config_methods;
4362 }
4363
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004364 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4365 wpa_printf(MSG_ERROR,
4366 "P2P: Failed to configure supported channel list");
4367 return -1;
4368 }
4369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 if (wpa_s->conf->p2p_listen_reg_class &&
4371 wpa_s->conf->p2p_listen_channel) {
4372 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4373 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004374 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004375 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376 /*
4377 * Pick one of the social channels randomly as the listen
4378 * channel.
4379 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004380 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4381 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004382 wpa_printf(MSG_INFO,
4383 "P2P: No social channels supported by the driver - do not enable P2P");
4384 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004385 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004386 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004388 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4389 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390
4391 if (wpa_s->conf->p2p_oper_reg_class &&
4392 wpa_s->conf->p2p_oper_channel) {
4393 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4394 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4395 p2p.cfg_op_channel = 1;
4396 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4397 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4398
4399 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004401 * Use random operation channel from 2.4 GHz band social
4402 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4403 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004405 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4406 &p2p.op_channel) != 0) {
4407 wpa_printf(MSG_ERROR,
4408 "P2P: Failed to select random social channel as operation channel");
4409 return -1;
4410 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004411 p2p.cfg_op_channel = 0;
4412 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4413 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4414 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004415
4416 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4417 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4418 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4419 }
4420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004421 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4422 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4423 p2p.country[2] = 0x04;
4424 } else
4425 os_memcpy(p2p.country, "XX\x04", 3);
4426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004427 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4428 WPS_DEV_TYPE_LEN);
4429
4430 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4431 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4432 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4433
4434 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4435 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4436
4437 p2p.max_peers = 100;
4438
4439 if (wpa_s->conf->p2p_ssid_postfix) {
4440 p2p.ssid_postfix_len =
4441 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4442 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4443 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4444 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4445 p2p.ssid_postfix_len);
4446 }
4447
4448 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4449
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004450 p2p.max_listen = wpa_s->max_remain_on_chan;
4451
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004452 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4453 wpa_s->conf->p2p_passphrase_len <= 63)
4454 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4455 else
4456 p2p.passphrase_len = 8;
4457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 global->p2p = p2p_init(&p2p);
4459 if (global->p2p == NULL)
4460 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004461 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462
4463 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4464 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4465 continue;
4466 p2p_add_wps_vendor_extension(
4467 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4468 }
4469
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004470 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 return 0;
4473}
4474
4475
4476/**
4477 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4478 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4479 *
4480 * This function deinitialize per-interface P2P data.
4481 */
4482void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4483{
4484 if (wpa_s->driver && wpa_s->drv_priv)
4485 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004486
4487 if (wpa_s->go_params) {
4488 /* Clear any stored provisioning info */
4489 p2p_clear_provisioning_info(
4490 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004491 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004492 }
4493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004494 os_free(wpa_s->go_params);
4495 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004496 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4498 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4499 wpa_s->p2p_long_listen = 0;
4500 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4501 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4502 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004503 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004504 wpas_p2p_listen_work_done(wpa_s);
4505 if (wpa_s->p2p_send_action_work) {
4506 os_free(wpa_s->p2p_send_action_work->ctx);
4507 radio_work_done(wpa_s->p2p_send_action_work);
4508 wpa_s->p2p_send_action_work = NULL;
4509 }
4510 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004511
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004512 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4513 wpa_s->p2p_oob_dev_pw = NULL;
4514
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004515 os_free(wpa_s->p2p_group_common_freqs);
4516 wpa_s->p2p_group_common_freqs = NULL;
4517 wpa_s->p2p_group_common_freqs_num = 0;
4518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519 /* TODO: remove group interface from the driver if this wpa_s instance
4520 * is on top of a P2P group interface */
4521}
4522
4523
4524/**
4525 * wpas_p2p_deinit_global - Deinitialize global P2P module
4526 * @global: Pointer to global data from wpa_supplicant_init()
4527 *
4528 * This function deinitializes the global (per device) P2P module.
4529 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004530static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531{
4532 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004534 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004535
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004536 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004537
4538 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004539 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4540 wpa_s = wpa_s->next;
4541 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004542 tmp = global->ifaces;
4543 while (tmp &&
4544 (tmp == wpa_s ||
4545 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4546 tmp = tmp->next;
4547 }
4548 if (tmp == NULL)
4549 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004550 /* Disconnect from the P2P group and deinit the interface */
4551 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552 }
4553
4554 /*
4555 * Deinit GO data on any possibly remaining interface (if main
4556 * interface is used as GO).
4557 */
4558 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4559 if (wpa_s->ap_iface)
4560 wpas_p2p_group_deinit(wpa_s);
4561 }
4562
4563 p2p_deinit(global->p2p);
4564 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004565 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566}
4567
4568
4569static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4570{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004571 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004572 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004573 if (wpa_s->drv_flags &
4574 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4575 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4576 return 1; /* P2P group requires a new interface in every case
4577 */
4578 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4579 return 0; /* driver does not support concurrent operations */
4580 if (wpa_s->global->ifaces->next)
4581 return 1; /* more that one interface already in use */
4582 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4583 return 1; /* this interface is already in use */
4584 return 0;
4585}
4586
4587
4588static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4589 const u8 *peer_addr,
4590 enum p2p_wps_method wps_method,
4591 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004592 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004593 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004594{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004595 if (persistent_group && wpa_s->conf->persistent_reconnect)
4596 persistent_group = 2;
4597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004598 /*
4599 * Increase GO config timeout if HT40 is used since it takes some time
4600 * to scan channels for coex purposes before the BSS can be started.
4601 */
4602 p2p_set_config_timeout(wpa_s->global->p2p,
4603 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4606 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004607 persistent_group, ssid ? ssid->ssid : NULL,
4608 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004609 wpa_s->p2p_pd_before_go_neg, pref_freq,
4610 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4611 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612}
4613
4614
4615static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4616 const u8 *peer_addr,
4617 enum p2p_wps_method wps_method,
4618 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004619 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004620 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004621{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004622 if (persistent_group && wpa_s->conf->persistent_reconnect)
4623 persistent_group = 2;
4624
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4626 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004627 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004628 ssid ? ssid->ssid_len : 0, pref_freq,
4629 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4630 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631}
4632
4633
4634static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4635{
4636 wpa_s->p2p_join_scan_count++;
4637 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4638 wpa_s->p2p_join_scan_count);
4639 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4640 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4641 " for join operationg - stop join attempt",
4642 MAC2STR(wpa_s->pending_join_iface_addr));
4643 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004644 if (wpa_s->p2p_auto_pd) {
4645 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004646 wpa_msg_global(wpa_s, MSG_INFO,
4647 P2P_EVENT_PROV_DISC_FAILURE
4648 " p2p_dev_addr=" MACSTR " status=N/A",
4649 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004650 return;
4651 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004652 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004653 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004654 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 }
4656}
4657
4658
Dmitry Shmidt04949592012-07-19 12:16:46 -07004659static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4660{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004661 int res;
4662 unsigned int num, i;
4663 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004664
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004665 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4666 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004667 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004668 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004669
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004670 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4671 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004672 if (!freqs)
4673 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004674
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004675 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4676 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004677
4678 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004679 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004680 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4681 freq);
4682 res = 0;
4683 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004684 }
4685 }
4686
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004687 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004688 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004689
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004690exit_free:
4691 os_free(freqs);
4692 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004693}
4694
4695
4696static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4697 const u8 *peer_dev_addr)
4698{
4699 struct wpa_bss *bss;
4700 int updated;
4701
4702 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4703 if (bss == NULL)
4704 return -1;
4705 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4706 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4707 "last scan");
4708 return 0;
4709 }
4710
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004711 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4712 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004713 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4714 "%ld.%06ld (%supdated in last scan)",
4715 bss->last_update.sec, bss->last_update.usec,
4716 updated ? "": "not ");
4717
4718 return updated;
4719}
4720
4721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4723 struct wpa_scan_results *scan_res)
4724{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004725 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 int freq;
4727 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4730
4731 if (wpa_s->global->p2p_disabled)
4732 return;
4733
Dmitry Shmidt04949592012-07-19 12:16:46 -07004734 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4735 scan_res ? (int) scan_res->num : -1,
4736 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004737
4738 if (scan_res)
4739 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4740
Dmitry Shmidt04949592012-07-19 12:16:46 -07004741 if (wpa_s->p2p_auto_pd) {
4742 int join = wpas_p2p_peer_go(wpa_s,
4743 wpa_s->pending_join_dev_addr);
4744 if (join == 0 &&
4745 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4746 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004747 bss = wpa_bss_get_bssid_latest(
4748 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004749 if (bss) {
4750 freq = bss->freq;
4751 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4752 "the peer " MACSTR " at %d MHz",
4753 wpa_s->auto_pd_scan_retry,
4754 MAC2STR(wpa_s->
4755 pending_join_dev_addr),
4756 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004757 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004758 return;
4759 }
4760 }
4761
4762 if (join < 0)
4763 join = 0;
4764
4765 wpa_s->p2p_auto_pd = 0;
4766 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4767 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4768 MAC2STR(wpa_s->pending_join_dev_addr), join);
4769 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004770 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004771 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004772 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004773 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004774 wpa_msg_global(wpa_s, MSG_INFO,
4775 P2P_EVENT_PROV_DISC_FAILURE
4776 " p2p_dev_addr=" MACSTR " status=N/A",
4777 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004778 }
4779 return;
4780 }
4781
4782 if (wpa_s->p2p_auto_join) {
4783 int join = wpas_p2p_peer_go(wpa_s,
4784 wpa_s->pending_join_dev_addr);
4785 if (join < 0) {
4786 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4787 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004788 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004789 P2P_EVENT_FALLBACK_TO_GO_NEG
4790 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004791 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4792 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4793 wpa_s->p2p_persistent_group, 0, 0, 0,
4794 wpa_s->p2p_go_intent,
4795 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004796 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004797 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004798 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004799 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004800 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004801 wpa_s->p2p_go_max_oper_chwidth,
4802 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004803 return;
4804 }
4805
4806 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4807 "try to join the group", join ? "" :
4808 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004809 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004810 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004811 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004812 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004813 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004814 }
4815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4817 wpa_s->pending_join_iface_addr);
4818 if (freq < 0 &&
4819 p2p_get_interface_addr(wpa_s->global->p2p,
4820 wpa_s->pending_join_dev_addr,
4821 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004822 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4823 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4825 "address for join from " MACSTR " to " MACSTR
4826 " based on newly discovered P2P peer entry",
4827 MAC2STR(wpa_s->pending_join_iface_addr),
4828 MAC2STR(iface_addr));
4829 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4830 ETH_ALEN);
4831
4832 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4833 wpa_s->pending_join_iface_addr);
4834 }
4835 if (freq >= 0) {
4836 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4837 "from P2P peer table: %d MHz", freq);
4838 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004839 if (wpa_s->p2p_join_ssid_len) {
4840 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4841 MACSTR " and SSID %s",
4842 MAC2STR(wpa_s->pending_join_iface_addr),
4843 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4844 wpa_s->p2p_join_ssid_len));
4845 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4846 wpa_s->p2p_join_ssid,
4847 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004848 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004849 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4850 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4851 bss = wpa_bss_get_bssid_latest(wpa_s,
4852 wpa_s->pending_join_iface_addr);
4853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004854 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004855 u8 dev_addr[ETH_ALEN];
4856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 freq = bss->freq;
4858 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004859 "from BSS table: %d MHz (SSID %s)", freq,
4860 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004861 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4862 dev_addr) == 0 &&
4863 os_memcmp(wpa_s->pending_join_dev_addr,
4864 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4865 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4866 ETH_ALEN) != 0) {
4867 wpa_printf(MSG_DEBUG,
4868 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4869 MAC2STR(dev_addr),
4870 MAC2STR(wpa_s->pending_join_dev_addr));
4871 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4872 ETH_ALEN);
4873 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004874 }
4875 if (freq > 0) {
4876 u16 method;
4877
Dmitry Shmidt04949592012-07-19 12:16:46 -07004878 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004879 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004880 P2P_EVENT_GROUP_FORMATION_FAILURE
4881 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004882 wpas_notify_p2p_group_formation_failure(
4883 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004884 return;
4885 }
4886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4888 "prior to joining an existing group (GO " MACSTR
4889 " freq=%u MHz)",
4890 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4891 wpa_s->pending_pd_before_join = 1;
4892
4893 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894 case WPS_PIN_DISPLAY:
4895 method = WPS_CONFIG_KEYPAD;
4896 break;
4897 case WPS_PIN_KEYPAD:
4898 method = WPS_CONFIG_DISPLAY;
4899 break;
4900 case WPS_PBC:
4901 method = WPS_CONFIG_PUSHBUTTON;
4902 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004903 case WPS_P2PS:
4904 method = WPS_CONFIG_P2PS;
4905 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906 default:
4907 method = 0;
4908 break;
4909 }
4910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004911 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4912 wpa_s->pending_join_dev_addr) ==
4913 method)) {
4914 /*
4915 * We have already performed provision discovery for
4916 * joining the group. Proceed directly to join
4917 * operation without duplicated provision discovery. */
4918 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4919 "with " MACSTR " already done - proceed to "
4920 "join",
4921 MAC2STR(wpa_s->pending_join_dev_addr));
4922 wpa_s->pending_pd_before_join = 0;
4923 goto start;
4924 }
4925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004927 wpa_s->pending_join_dev_addr,
4928 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004929 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004930 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4931 "Discovery Request before joining an "
4932 "existing group");
4933 wpa_s->pending_pd_before_join = 0;
4934 goto start;
4935 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004936 return;
4937 }
4938
4939 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4940 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4941 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4942 wpas_p2p_check_join_scan_limit(wpa_s);
4943 return;
4944
4945start:
4946 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004947 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
4948 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949}
4950
4951
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004952static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4953 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004954{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 int ret;
4956 struct wpa_driver_scan_params params;
4957 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004958 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004959 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004960 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004961
4962 os_memset(&params, 0, sizeof(params));
4963
4964 /* P2P Wildcard SSID */
4965 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004966 if (ssid && ssid_len) {
4967 params.ssids[0].ssid = ssid;
4968 params.ssids[0].ssid_len = ssid_len;
4969 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4970 wpa_s->p2p_join_ssid_len = ssid_len;
4971 } else {
4972 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4973 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4974 wpa_s->p2p_join_ssid_len = 0;
4975 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004976
4977 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004978 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4979 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4980 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 if (wps_ie == NULL) {
4982 wpas_p2p_scan_res_join(wpa_s, NULL);
4983 return;
4984 }
4985
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004986 if (!freq) {
4987 int oper_freq;
4988 /*
4989 * If freq is not provided, check the operating freq of the GO
4990 * and use a single channel scan on if possible.
4991 */
4992 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4993 wpa_s->pending_join_iface_addr);
4994 if (oper_freq > 0)
4995 freq = oper_freq;
4996 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004997 if (freq > 0) {
4998 freqs[0] = freq;
4999 params.freqs = freqs;
5000 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005002 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5003 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
5004 if (ies == NULL) {
5005 wpabuf_free(wps_ie);
5006 wpas_p2p_scan_res_join(wpa_s, NULL);
5007 return;
5008 }
5009 wpabuf_put_buf(ies, wps_ie);
5010 wpabuf_free(wps_ie);
5011
5012 bands = wpas_get_bands(wpa_s, freqs);
5013 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5014
5015 params.p2p_probe = 1;
5016 params.extra_ies = wpabuf_head(ies);
5017 params.extra_ies_len = wpabuf_len(ies);
5018
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 /*
5020 * Run a scan to update BSS table and start Provision Discovery once
5021 * the new scan results become available.
5022 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005023 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005024 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005025 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005026 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005027 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005028 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005029
5030 wpabuf_free(ies);
5031
5032 if (ret) {
5033 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5034 "try again later");
5035 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5036 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5037 wpas_p2p_check_join_scan_limit(wpa_s);
5038 }
5039}
5040
5041
Dmitry Shmidt04949592012-07-19 12:16:46 -07005042static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5043{
5044 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005045 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005046}
5047
5048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005050 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005051 int auto_join, int op_freq,
5052 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053{
5054 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005055 MACSTR " dev " MACSTR " op_freq=%d)%s",
5056 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005057 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005058 if (ssid && ssid_len) {
5059 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5060 wpa_ssid_txt(ssid, ssid_len));
5061 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062
Dmitry Shmidt04949592012-07-19 12:16:46 -07005063 wpa_s->p2p_auto_pd = 0;
5064 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5066 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5067 wpa_s->pending_join_wps_method = wps_method;
5068
5069 /* Make sure we are not running find during connection establishment */
5070 wpas_p2p_stop_find(wpa_s);
5071
5072 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005073 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 return 0;
5075}
5076
5077
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005078static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5079 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080{
5081 struct wpa_supplicant *group;
5082 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005083 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005084
5085 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5086 if (group == NULL)
5087 return -1;
5088 if (group != wpa_s) {
5089 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5090 sizeof(group->p2p_pin));
5091 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005092 } else {
5093 /*
5094 * Need to mark the current interface for p2p_group_formation
5095 * when a separate group interface is not used. This is needed
5096 * to allow p2p_cancel stop a pending p2p_connect-join.
5097 * wpas_p2p_init_group_interface() addresses this for the case
5098 * where a separate group interface is used.
5099 */
5100 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005101 }
5102
5103 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005104 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105
5106 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005107 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5109 ETH_ALEN);
5110 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005111 if (freq && ssid && ssid_len) {
5112 res.freq = freq;
5113 res.ssid_len = ssid_len;
5114 os_memcpy(res.ssid, ssid, ssid_len);
5115 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005116 if (ssid && ssid_len) {
5117 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5118 ssid, ssid_len);
5119 } else {
5120 bss = wpa_bss_get_bssid_latest(
5121 wpa_s, wpa_s->pending_join_iface_addr);
5122 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005123 if (bss) {
5124 res.freq = bss->freq;
5125 res.ssid_len = bss->ssid_len;
5126 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5127 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5128 bss->freq,
5129 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005130 } else if (ssid && ssid_len) {
5131 res.ssid_len = ssid_len;
5132 os_memcpy(res.ssid, ssid, ssid_len);
5133 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5134 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005135 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005136 }
5137
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005138 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5139 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5140 "starting client");
5141 wpa_drv_cancel_remain_on_channel(wpa_s);
5142 wpa_s->off_channel_freq = 0;
5143 wpa_s->roc_waiting_drv_freq = 0;
5144 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005145 wpas_start_wps_enrollee(group, &res);
5146
5147 /*
5148 * Allow a longer timeout for join-a-running-group than normal 15
5149 * second group formation timeout since the GO may not have authorized
5150 * our connection yet.
5151 */
5152 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5153 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5154 wpa_s, NULL);
5155
5156 return 0;
5157}
5158
5159
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005160static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005161 int *force_freq, int *pref_freq, int go,
5162 unsigned int *pref_freq_list,
5163 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005164{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005165 struct wpa_used_freq_data *freqs;
5166 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005167 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5168
5169 max_pref_freq = *num_pref_freq;
5170 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005171
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005172 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5173 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005174 if (!freqs)
5175 return -1;
5176
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005177 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5178 wpa_s->num_multichan_concurrent);
5179
5180 /*
5181 * It is possible that the total number of used frequencies is bigger
5182 * than the number of frequencies used for P2P, so get the system wide
5183 * number of unused frequencies.
5184 */
5185 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5186
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005187 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005188 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5189 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005190
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005191 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005192 int ret;
5193 if (go)
5194 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5195 else
5196 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5197 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005198 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5199 ieee80211_is_dfs(freq)) {
5200 /*
5201 * If freq is a DFS channel and DFS is offloaded
5202 * to the driver, allow P2P GO to use it.
5203 */
5204 wpa_printf(MSG_DEBUG,
5205 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5206 freq);
5207 } else {
5208 wpa_printf(MSG_DEBUG,
5209 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5210 freq);
5211 res = -3;
5212 goto exit_free;
5213 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005214 }
5215
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005216 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005217 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005218 freq_in_use = 1;
5219 }
5220
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005221 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005222 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5223 freq);
5224 res = -2;
5225 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005226 }
5227 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5228 "requested channel (%u MHz)", freq);
5229 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005230 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005231 }
5232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005233 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005234
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005235 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5236 enum wpa_driver_if_type iface_type;
5237
5238 if (go)
5239 iface_type = WPA_IF_P2P_GO;
5240 else
5241 iface_type = WPA_IF_P2P_CLIENT;
5242
5243 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5244 best_freq, go);
5245
5246 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5247 &max_pref_freq,
5248 pref_freq_list);
5249 if (!res && max_pref_freq > 0) {
5250 *num_pref_freq = max_pref_freq;
5251 i = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005252 while ((!p2p_supported_freq(wpa_s->global->p2p,
5253 pref_freq_list[i]) ||
5254 wpas_p2p_disallowed_freq(wpa_s->global,
5255 pref_freq_list[i])) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005256 i < *num_pref_freq) {
5257 wpa_printf(MSG_DEBUG,
5258 "P2P: preferred_freq_list[%d]=%d is disallowed",
5259 i, pref_freq_list[i]);
5260 i++;
5261 }
5262 if (i != *num_pref_freq) {
5263 best_freq = pref_freq_list[i];
5264 wpa_printf(MSG_DEBUG,
5265 "P2P: Using preferred_freq_list[%d]=%d",
5266 i, best_freq);
5267 } else {
5268 wpa_printf(MSG_DEBUG,
5269 "P2P: All driver preferred frequencies are disallowed for P2P use");
5270 *num_pref_freq = 0;
5271 }
5272 } else {
5273 wpa_printf(MSG_DEBUG,
5274 "P2P: No preferred frequency list available");
5275 }
5276 }
5277
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005278 /* We have a candidate frequency to use */
5279 if (best_freq > 0) {
5280 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005281 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005282 best_freq);
5283 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005284 } else {
5285 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 -07005286 best_freq);
5287 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005288 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005289 } else if (num_unused > 0) {
5290 wpa_printf(MSG_DEBUG,
5291 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5292 *force_freq = 0;
5293 } else {
5294 wpa_printf(MSG_DEBUG,
5295 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5296 res = -2;
5297 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005298 }
5299
5300exit_ok:
5301 res = 0;
5302exit_free:
5303 os_free(freqs);
5304 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005305}
5306
5307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005308/**
5309 * wpas_p2p_connect - Request P2P Group Formation to be started
5310 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5311 * @peer_addr: Address of the peer P2P Device
5312 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5313 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005314 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005315 * @join: Whether to join an existing group (as a client) instead of starting
5316 * Group Owner negotiation; @peer_addr is BSSID in that case
5317 * @auth: Whether to only authorize the connection instead of doing that and
5318 * initiating Group Owner negotiation
5319 * @go_intent: GO Intent or -1 to use default
5320 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005321 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005322 * @persistent_id: Persistent group credentials to use for forcing GO
5323 * parameters or -1 to generate new values (SSID/passphrase)
5324 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5325 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005326 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005327 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005328 * @vht_chwidth: Channel width supported by GO operating with VHT support
5329 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005330 * @group_ssid: Specific Group SSID for join or %NULL if not set
5331 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005332 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5333 * failure, -2 on failure due to channel not currently available,
5334 * -3 if forced channel is not supported
5335 */
5336int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5337 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005338 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005339 int go_intent, int freq, unsigned int vht_center_freq2,
5340 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005341 unsigned int vht_chwidth, const u8 *group_ssid,
5342 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005344 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005345 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005347 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005348 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005349 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350
5351 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5352 return -1;
5353
Dmitry Shmidt04949592012-07-19 12:16:46 -07005354 if (persistent_id >= 0) {
5355 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5356 if (ssid == NULL || ssid->disabled != 2 ||
5357 ssid->mode != WPAS_MODE_P2P_GO)
5358 return -1;
5359 }
5360
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005361 os_free(wpa_s->global->add_psk);
5362 wpa_s->global->add_psk = NULL;
5363
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005364 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005365 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005366 wpa_s->global->pending_p2ps_group_freq = 0;
5367 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005369 if (go_intent < 0)
5370 go_intent = wpa_s->conf->p2p_go_intent;
5371
5372 if (!auth)
5373 wpa_s->p2p_long_listen = 0;
5374
5375 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005376 wpa_s->p2p_persistent_group = !!persistent_group;
5377 wpa_s->p2p_persistent_id = persistent_id;
5378 wpa_s->p2p_go_intent = go_intent;
5379 wpa_s->p2p_connect_freq = freq;
5380 wpa_s->p2p_fallback_to_go_neg = 0;
5381 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005382 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005383 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005384 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5385 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005386
5387 if (pin)
5388 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5389 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005390 if (wps_generate_pin((unsigned int *) &ret) < 0)
5391 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005392 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5393 "%08d", ret);
5394 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5395 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005396 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5397 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005398 } else if (wps_method == WPS_P2PS) {
5399 /* Force the P2Ps default PIN to be used */
5400 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401 } else
5402 wpa_s->p2p_pin[0] = '\0';
5403
Dmitry Shmidt04949592012-07-19 12:16:46 -07005404 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005405 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5406 if (auth) {
5407 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5408 "connect a running group from " MACSTR,
5409 MAC2STR(peer_addr));
5410 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5411 return ret;
5412 }
5413 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5414 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5415 iface_addr) < 0) {
5416 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5417 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5418 dev_addr);
5419 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005420 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005421 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005422 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5423 "%ld.%06ld",
5424 wpa_s->p2p_auto_started.sec,
5425 wpa_s->p2p_auto_started.usec);
5426 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005427 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005428 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005429 auto_join, freq,
5430 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 return -1;
5432 return ret;
5433 }
5434
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005435 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005436 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005437 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005438 if (res)
5439 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005440 wpas_p2p_set_own_freq_preference(wpa_s,
5441 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005443 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005447 if (wpa_s->create_p2p_iface) {
5448 /* Prepare to add a new interface for the group */
5449 iftype = WPA_IF_P2P_GROUP;
5450 if (go_intent == 15)
5451 iftype = WPA_IF_P2P_GO;
5452 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5453 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5454 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005457
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005458 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005459 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005460 if (wpa_s->p2p_mgmt)
5461 if_addr = wpa_s->parent->own_addr;
5462 else
5463 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005464 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466
5467 if (auth) {
5468 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005469 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005470 force_freq, persistent_group, ssid,
5471 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 return -1;
5473 return ret;
5474 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005475
5476 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5477 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005478 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005479 if (wpa_s->create_p2p_iface)
5480 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005481 return -1;
5482 }
5483 return ret;
5484}
5485
5486
5487/**
5488 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5489 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5490 * @freq: Frequency of the channel in MHz
5491 * @duration: Duration of the stay on the channel in milliseconds
5492 *
5493 * This callback is called when the driver indicates that it has started the
5494 * requested remain-on-channel duration.
5495 */
5496void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5497 unsigned int freq, unsigned int duration)
5498{
5499 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5500 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005501 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)",
5502 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5503 wpa_s->roc_waiting_drv_freq, freq, duration);
5504 if (wpa_s->off_channel_freq &&
5505 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005506 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5507 wpa_s->pending_listen_duration);
5508 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005509 } else {
5510 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5511 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5512 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005513 }
5514}
5515
5516
Jithu Jance57cea1a2014-08-20 10:22:04 -07005517int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005518{
5519 /* Limit maximum Listen state time based on driver limitation. */
5520 if (timeout > wpa_s->max_remain_on_chan)
5521 timeout = wpa_s->max_remain_on_chan;
5522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005523 return p2p_listen(wpa_s->global->p2p, timeout);
5524}
5525
5526
5527/**
5528 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5529 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5530 * @freq: Frequency of the channel in MHz
5531 *
5532 * This callback is called when the driver indicates that a remain-on-channel
5533 * operation has been completed, i.e., the duration on the requested channel
5534 * has timed out.
5535 */
5536void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5537 unsigned int freq)
5538{
5539 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5540 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005541 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005542 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5544 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005545 if (wpa_s->p2p_long_listen > 0)
5546 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005547 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5548 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005549 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005551 if (wpa_s->p2p_long_listen > 0) {
5552 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5553 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005554 } else {
5555 /*
5556 * When listen duration is over, stop listen & update p2p_state
5557 * to IDLE.
5558 */
5559 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560 }
5561}
5562
5563
5564/**
5565 * wpas_p2p_group_remove - Remove a P2P group
5566 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5567 * @ifname: Network interface name of the group interface or "*" to remove all
5568 * groups
5569 * Returns: 0 on success, -1 on failure
5570 *
5571 * This function is used to remove a P2P group. This can be used to disconnect
5572 * from a group in which the local end is a P2P Client or to end a P2P Group in
5573 * case the local end is the Group Owner. If a virtual network interface was
5574 * created for this group, that interface will be removed. Otherwise, only the
5575 * configured P2P group network will be removed from the interface.
5576 */
5577int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5578{
5579 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005580 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005581
5582 if (os_strcmp(ifname, "*") == 0) {
5583 struct wpa_supplicant *prev;
5584 wpa_s = global->ifaces;
5585 while (wpa_s) {
5586 prev = wpa_s;
5587 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005588 if (prev->p2p_group_interface !=
5589 NOT_P2P_GROUP_INTERFACE ||
5590 (prev->current_ssid &&
5591 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005592 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 }
5594 return 0;
5595 }
5596
5597 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5598 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5599 break;
5600 }
5601
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005602 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603}
5604
5605
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005606static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5607{
5608 unsigned int r;
5609
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005610 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5611 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5612 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5613 int res;
5614
5615 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5616 &size, pref_freq_list);
5617 if (!res && size > 0) {
5618 i = 0;
5619 while (wpas_p2p_disallowed_freq(wpa_s->global,
5620 pref_freq_list[i]) &&
5621 i < size) {
5622 wpa_printf(MSG_DEBUG,
5623 "P2P: preferred_freq_list[%d]=%d is disallowed",
5624 i, pref_freq_list[i]);
5625 i++;
5626 }
5627 if (i != size) {
5628 freq = pref_freq_list[i];
5629 wpa_printf(MSG_DEBUG,
5630 "P2P: Using preferred_freq_list[%d]=%d",
5631 i, freq);
5632 } else {
5633 wpa_printf(MSG_DEBUG,
5634 "P2P: All driver preferred frequencies are disallowed for P2P use");
5635 }
5636 } else {
5637 wpa_printf(MSG_DEBUG,
5638 "P2P: No preferred frequency list available");
5639 }
5640 }
5641
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005642 if (freq == 2) {
5643 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5644 "band");
5645 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005646 p2p_supported_freq_go(wpa_s->global->p2p,
5647 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005648 freq = wpa_s->best_24_freq;
5649 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5650 "channel: %d MHz", freq);
5651 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005652 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5653 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005654 freq = 2412 + (r % 3) * 25;
5655 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5656 "channel: %d MHz", freq);
5657 }
5658 }
5659
5660 if (freq == 5) {
5661 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5662 "band");
5663 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005664 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005665 wpa_s->best_5_freq)) {
5666 freq = wpa_s->best_5_freq;
5667 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5668 "channel: %d MHz", freq);
5669 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005670 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5671 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005672 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005673 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005674 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5675 "5 GHz channel for P2P group");
5676 return -1;
5677 }
5678 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5679 "channel: %d MHz", freq);
5680 }
5681 }
5682
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005683 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005684 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5685 ieee80211_is_dfs(freq)) {
5686 /*
5687 * If freq is a DFS channel and DFS is offloaded to the
5688 * driver, allow P2P GO to use it.
5689 */
5690 wpa_printf(MSG_DEBUG, "P2P: "
5691 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5692 __func__, freq);
5693 return freq;
5694 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005695 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5696 "(%u MHz) is not supported for P2P uses",
5697 freq);
5698 return -1;
5699 }
5700
5701 return freq;
5702}
5703
5704
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005705static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5706 const struct p2p_channels *channels,
5707 int freq)
5708{
5709 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5710 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5711 freq_included(wpa_s, channels, freq))
5712 return 1;
5713 return 0;
5714}
5715
5716
5717static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5718 struct p2p_go_neg_results *params,
5719 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005720{
5721 unsigned int i, r;
5722
5723 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005724 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005725 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005726
5727 for (i = 0; i < 3; i++) {
5728 params->freq = 2412 + ((r + i) % 3) * 25;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005729 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005730 goto out;
5731 }
5732
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005733 /* try all other channels in operating class 81 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005734 for (i = 0; i < 11; i++) {
5735 params->freq = 2412 + i * 5;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005736
5737 /* skip social channels; covered in the previous loop */
5738 if (params->freq == 2412 ||
5739 params->freq == 2437 ||
5740 params->freq == 2462)
5741 continue;
5742
5743 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005744 goto out;
5745 }
5746
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005747 /* try all channels in operating class 115 */
5748 for (i = 0; i < 4; i++) {
5749 params->freq = 5180 + i * 20;
5750 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005751 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005752 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5753 goto out;
5754 }
5755
5756 /* try all channels in operating class 124 */
5757 for (i = 0; i < 4; i++) {
5758 params->freq = 5745 + i * 20;
5759 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005760 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005761 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5762 goto out;
5763 }
5764
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005765 /* try social channel class 180 channel 2 */
5766 params->freq = 58320 + 1 * 2160;
5767 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005768 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005769 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5770 goto out;
5771
5772 /* try all channels in reg. class 180 */
5773 for (i = 0; i < 4; i++) {
5774 params->freq = 58320 + i * 2160;
5775 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005776 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005777 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5778 goto out;
5779 }
5780
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005781 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005782 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005783 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005784out:
5785 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5786 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005787}
5788
5789
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005790static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5791 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005792 int freq, int vht_center_freq2, int ht40,
5793 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005794 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005796 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005797 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005798 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005799 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005800 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005801
5802 os_memset(params, 0, sizeof(*params));
5803 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005804 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005805 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005806 params->max_oper_chwidth = max_oper_chwidth;
5807 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005809 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5810 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005811 if (!freqs)
5812 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005814 num = get_shared_radio_freqs_data(wpa_s, freqs,
5815 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005816
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005817 if (wpa_s->current_ssid &&
5818 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5819 wpa_s->wpa_state == WPA_COMPLETED) {
5820 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5821 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005822
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005823 /*
5824 * If the frequency selection is done for an active P2P GO that
5825 * is not sharing a frequency, allow to select a new frequency
5826 * even if there are no unused frequencies as we are about to
5827 * move the P2P GO so its frequency can be re-used.
5828 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005829 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005830 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5831 freqs[i].flags == 0) {
5832 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005833 break;
5834 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005835 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005836 }
5837
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005838 /* try using the forced freq */
5839 if (freq) {
5840 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5841 wpa_printf(MSG_DEBUG,
5842 "P2P: Forced GO freq %d MHz not accepted",
5843 freq);
5844 goto fail;
5845 }
5846
5847 for (i = 0; i < num; i++) {
5848 if (freqs[i].freq == freq) {
5849 wpa_printf(MSG_DEBUG,
5850 "P2P: forced freq (%d MHz) is also shared",
5851 freq);
5852 params->freq = freq;
5853 goto success;
5854 }
5855 }
5856
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005857 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005858 wpa_printf(MSG_DEBUG,
5859 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5860 freq);
5861 goto fail;
5862 }
5863
5864 wpa_printf(MSG_DEBUG,
5865 "P2P: force GO freq (%d MHz) on a free channel",
5866 freq);
5867 params->freq = freq;
5868 goto success;
5869 }
5870
5871 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005872 if (num &&
5873 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005874 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5875 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5876 wpa_printf(MSG_DEBUG,
5877 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005878 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005879 params->freq = cand;
5880 goto success;
5881 }
5882
5883 /* try using one of the shared freqs */
5884 for (i = 0; i < num; i++) {
5885 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5886 freqs[i].freq)) {
5887 wpa_printf(MSG_DEBUG,
5888 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005889 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005890 params->freq = freqs[i].freq;
5891 goto success;
5892 }
5893 }
5894 }
5895
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005896 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005897 wpa_printf(MSG_DEBUG,
5898 "P2P: Cannot force GO on any of the channels we are already using");
5899 goto fail;
5900 }
5901
5902 /* try using the setting from the configuration file */
5903 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5904 wpa_s->conf->p2p_oper_channel >= 1 &&
5905 wpa_s->conf->p2p_oper_channel <= 11 &&
5906 wpas_p2p_supported_freq_go(
5907 wpa_s, channels,
5908 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5909 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5910 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5911 "frequency %d MHz", params->freq);
5912 goto success;
5913 }
5914
5915 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5916 wpa_s->conf->p2p_oper_reg_class == 116 ||
5917 wpa_s->conf->p2p_oper_reg_class == 117 ||
5918 wpa_s->conf->p2p_oper_reg_class == 124 ||
5919 wpa_s->conf->p2p_oper_reg_class == 125 ||
5920 wpa_s->conf->p2p_oper_reg_class == 126 ||
5921 wpa_s->conf->p2p_oper_reg_class == 127) &&
5922 wpas_p2p_supported_freq_go(wpa_s, channels,
5923 5000 +
5924 5 * wpa_s->conf->p2p_oper_channel)) {
5925 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5926 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5927 "frequency %d MHz", params->freq);
5928 goto success;
5929 }
5930
5931 /* Try using best channels */
5932 if (wpa_s->conf->p2p_oper_channel == 0 &&
5933 wpa_s->best_overall_freq > 0 &&
5934 wpas_p2p_supported_freq_go(wpa_s, channels,
5935 wpa_s->best_overall_freq)) {
5936 params->freq = wpa_s->best_overall_freq;
5937 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5938 "channel %d MHz", params->freq);
5939 goto success;
5940 }
5941
5942 if (wpa_s->conf->p2p_oper_channel == 0 &&
5943 wpa_s->best_24_freq > 0 &&
5944 wpas_p2p_supported_freq_go(wpa_s, channels,
5945 wpa_s->best_24_freq)) {
5946 params->freq = wpa_s->best_24_freq;
5947 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5948 "channel %d MHz", params->freq);
5949 goto success;
5950 }
5951
5952 if (wpa_s->conf->p2p_oper_channel == 0 &&
5953 wpa_s->best_5_freq > 0 &&
5954 wpas_p2p_supported_freq_go(wpa_s, channels,
5955 wpa_s->best_5_freq)) {
5956 params->freq = wpa_s->best_5_freq;
5957 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5958 "channel %d MHz", params->freq);
5959 goto success;
5960 }
5961
5962 /* try using preferred channels */
5963 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5964 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5965 params->freq = cand;
5966 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5967 "channels", params->freq);
5968 goto success;
5969 }
5970
5971 /* Try using one of the group common freqs */
5972 if (wpa_s->p2p_group_common_freqs) {
5973 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5974 cand = wpa_s->p2p_group_common_freqs[i];
5975 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5976 params->freq = cand;
5977 wpa_printf(MSG_DEBUG,
5978 "P2P: Use freq %d MHz common with the peer",
5979 params->freq);
5980 goto success;
5981 }
5982 }
5983 }
5984
5985 /* no preference, select some channel */
5986 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5987
5988 if (params->freq == 0) {
5989 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5990 goto fail;
5991 }
5992
5993success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005994 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005995 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005996fail:
5997 os_free(freqs);
5998 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005999}
6000
6001
6002static struct wpa_supplicant *
6003wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6004 int go)
6005{
6006 struct wpa_supplicant *group_wpa_s;
6007
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006008 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006009 if (wpa_s->p2p_mgmt) {
6010 /*
6011 * We may be called on the p2p_dev interface which
6012 * cannot be used for group operations, so always use
6013 * the primary interface.
6014 */
6015 wpa_s->parent->p2pdev = wpa_s;
6016 wpa_s = wpa_s->parent;
6017 }
6018 wpa_dbg(wpa_s, MSG_DEBUG,
6019 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006020 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006021 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006022 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006023 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006025
6026 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006027 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006028 wpa_msg_global(wpa_s, MSG_ERROR,
6029 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006030 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006031 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6033 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006034 wpa_msg_global(wpa_s, MSG_ERROR,
6035 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036 wpas_p2p_remove_pending_group_interface(wpa_s);
6037 return NULL;
6038 }
6039
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006040 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6041 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006042 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043 return group_wpa_s;
6044}
6045
6046
6047/**
6048 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6049 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6050 * @persistent_group: Whether to create a persistent group
6051 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006052 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006053 * @ht40: Start GO with 40 MHz channel width
6054 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006055 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 * Returns: 0 on success, -1 on failure
6057 *
6058 * This function creates a new P2P group with the local end as the Group Owner,
6059 * i.e., without using Group Owner Negotiation.
6060 */
6061int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006062 int freq, int vht_center_freq2, int ht40, int vht,
6063 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006064{
6065 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066
6067 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6068 return -1;
6069
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006070 os_free(wpa_s->global->add_psk);
6071 wpa_s->global->add_psk = NULL;
6072
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006073 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006074 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006075 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006076
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006077 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6078 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006080
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006081 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6082 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006083 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006084 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006085 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006086 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6087 ieee80211_is_dfs(params.freq)) {
6088 /*
6089 * If freq is a DFS channel and DFS is offloaded to the
6090 * driver, allow P2P GO to use it.
6091 */
6092 wpa_printf(MSG_DEBUG,
6093 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
6094 __func__, params.freq);
6095 } else {
6096 wpa_printf(MSG_DEBUG,
6097 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
6098 params.freq);
6099 return -1;
6100 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102 p2p_go_params(wpa_s->global->p2p, &params);
6103 params.persistent_group = persistent_group;
6104
6105 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6106 if (wpa_s == NULL)
6107 return -1;
6108 wpas_start_wps_go(wpa_s, &params, 0);
6109
6110 return 0;
6111}
6112
6113
6114static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006115 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006116 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117{
6118 struct wpa_ssid *ssid;
6119
6120 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6121 if (wpa_s == NULL)
6122 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006123 if (force_scan)
6124 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006125 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126
6127 wpa_supplicant_ap_deinit(wpa_s);
6128
6129 ssid = wpa_config_add_network(wpa_s->conf);
6130 if (ssid == NULL)
6131 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006132 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006133 wpa_config_set_network_defaults(ssid);
6134 ssid->temporary = 1;
6135 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006136 ssid->pbss = params->pbss;
6137 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6138 WPA_CIPHER_CCMP;
6139 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006140 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6141 ssid->ssid = os_malloc(params->ssid_len);
6142 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006143 wpa_config_remove_network(wpa_s->conf, ssid->id);
6144 return -1;
6145 }
6146 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6147 ssid->ssid_len = params->ssid_len;
6148 ssid->p2p_group = 1;
6149 ssid->export_keys = 1;
6150 if (params->psk_set) {
6151 os_memcpy(ssid->psk, params->psk, 32);
6152 ssid->psk_set = 1;
6153 }
6154 if (params->passphrase)
6155 ssid->passphrase = os_strdup(params->passphrase);
6156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006157 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006158 wpa_s->p2p_in_invitation = 1;
6159 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006160 wpa_s->p2p_go_group_formation_completed = 0;
6161 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006162
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006163 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006164 NULL);
6165 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6166 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006167 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006168 wpa_supplicant_select_network(wpa_s, ssid);
6169
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170 return 0;
6171}
6172
6173
6174int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6175 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006176 int force_freq, int neg_freq,
6177 int vht_center_freq2, int ht40,
6178 int vht, int max_oper_chwidth,
6179 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006180 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181{
6182 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006183 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006184
6185 if (ssid->disabled != 2 || ssid->ssid == NULL)
6186 return -1;
6187
6188 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6189 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6190 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6191 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006192 if (go == 0 &&
6193 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006194 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006195 /*
6196 * This can happen if Invitation Response frame was lost
6197 * and the peer (GO of a persistent group) tries to
6198 * invite us again. Reschedule the timeout to avoid
6199 * terminating the wait for the connection too early
6200 * since we now know that the peer is still trying to
6201 * invite us instead of having already started the GO.
6202 */
6203 wpa_printf(MSG_DEBUG,
6204 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6205 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6206 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006207 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006208 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006209 return 0;
6210 }
6211
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006212 os_free(wpa_s->global->add_psk);
6213 wpa_s->global->add_psk = NULL;
6214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006216 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006217
Dmitry Shmidt04949592012-07-19 12:16:46 -07006218 wpa_s->p2p_fallback_to_go_neg = 0;
6219
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006220 if (ssid->mode == WPAS_MODE_P2P_GO) {
6221 if (force_freq > 0) {
6222 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6223 if (freq < 0)
6224 return -1;
6225 } else {
6226 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6227 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006228 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006229 freq = 0;
6230 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006231 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006232 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006233 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006234 struct os_reltime now;
6235 struct wpa_bss *bss =
6236 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006237
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006238 os_get_reltime(&now);
6239 if (bss &&
6240 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006241 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006242 freq = bss->freq;
6243 else
6244 freq = 0;
6245 }
6246
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006247 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6248 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006249 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006250 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006251 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006252
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006253 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6254 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006255 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256
6257 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006258 params.psk_set = ssid->psk_set;
6259 if (params.psk_set)
6260 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006261 if (ssid->passphrase) {
6262 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6263 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6264 "persistent group");
6265 return -1;
6266 }
6267 os_strlcpy(params.passphrase, ssid->passphrase,
6268 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006269 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6271 params.ssid_len = ssid->ssid_len;
6272 params.persistent_group = 1;
6273
6274 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6275 if (wpa_s == NULL)
6276 return -1;
6277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006278 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6279
Dmitry Shmidt56052862013-10-04 10:23:25 -07006280 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281 wpas_start_wps_go(wpa_s, &params, 0);
6282
6283 return 0;
6284}
6285
6286
6287static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6288 struct wpabuf *proberesp_ies)
6289{
6290 struct wpa_supplicant *wpa_s = ctx;
6291 if (wpa_s->ap_iface) {
6292 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006293 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6294 wpabuf_free(beacon_ies);
6295 wpabuf_free(proberesp_ies);
6296 return;
6297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298 if (beacon_ies) {
6299 wpabuf_free(hapd->p2p_beacon_ie);
6300 hapd->p2p_beacon_ie = beacon_ies;
6301 }
6302 wpabuf_free(hapd->p2p_probe_resp_ie);
6303 hapd->p2p_probe_resp_ie = proberesp_ies;
6304 } else {
6305 wpabuf_free(beacon_ies);
6306 wpabuf_free(proberesp_ies);
6307 }
6308 wpa_supplicant_ap_update_beacon(wpa_s);
6309}
6310
6311
6312static void wpas_p2p_idle_update(void *ctx, int idle)
6313{
6314 struct wpa_supplicant *wpa_s = ctx;
6315 if (!wpa_s->ap_iface)
6316 return;
6317 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006318 if (idle) {
6319 if (wpa_s->global->p2p_fail_on_wps_complete &&
6320 wpa_s->p2p_in_provisioning) {
6321 wpas_p2p_grpform_fail_after_wps(wpa_s);
6322 return;
6323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006324 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006325 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6327}
6328
6329
6330struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006331 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006332{
6333 struct p2p_group *group;
6334 struct p2p_group_config *cfg;
6335
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006336 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6337 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006338 return NULL;
6339
6340 cfg = os_zalloc(sizeof(*cfg));
6341 if (cfg == NULL)
6342 return NULL;
6343
Dmitry Shmidt04949592012-07-19 12:16:46 -07006344 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006345 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006346 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006347 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006348 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6349 if (wpa_s->max_stations &&
6350 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6351 cfg->max_clients = wpa_s->max_stations;
6352 else
6353 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006354 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6355 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006356 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 cfg->cb_ctx = wpa_s;
6358 cfg->ie_update = wpas_p2p_ie_update;
6359 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006360 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6361 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006362
6363 group = p2p_group_init(wpa_s->global->p2p, cfg);
6364 if (group == NULL)
6365 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006366 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006367 p2p_group_notif_formation_done(group);
6368 wpa_s->p2p_group = group;
6369 return group;
6370}
6371
6372
6373void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6374 int registrar)
6375{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006376 struct wpa_ssid *ssid = wpa_s->current_ssid;
6377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006378 if (!wpa_s->p2p_in_provisioning) {
6379 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6380 "provisioning not in progress");
6381 return;
6382 }
6383
Dmitry Shmidt04949592012-07-19 12:16:46 -07006384 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6385 u8 go_dev_addr[ETH_ALEN];
6386 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6387 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6388 ssid->ssid_len);
6389 /* Clear any stored provisioning info */
6390 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6391 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006392
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006393 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006394 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006395 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006396 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6397 /*
6398 * Use a separate timeout for initial data connection to
6399 * complete to allow the group to be removed automatically if
6400 * something goes wrong in this step before the P2P group idle
6401 * timeout mechanism is taken into use.
6402 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006403 wpa_dbg(wpa_s, MSG_DEBUG,
6404 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6405 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006406 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6407 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006408 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006409 /* Complete group formation on successful data connection. */
6410 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006411 } else if (ssid) {
6412 /*
6413 * Use a separate timeout for initial data connection to
6414 * complete to allow the group to be removed automatically if
6415 * the client does not complete data connection successfully.
6416 */
6417 wpa_dbg(wpa_s, MSG_DEBUG,
6418 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6419 P2P_MAX_INITIAL_CONN_WAIT_GO);
6420 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6421 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006422 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006423 /*
6424 * Complete group formation on first successful data connection
6425 */
6426 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006428 if (wpa_s->global->p2p)
6429 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006430 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431}
6432
6433
Jouni Malinen75ecf522011-06-27 15:19:46 -07006434void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6435 struct wps_event_fail *fail)
6436{
6437 if (!wpa_s->p2p_in_provisioning) {
6438 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6439 "provisioning not in progress");
6440 return;
6441 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006442
6443 if (wpa_s->go_params) {
6444 p2p_clear_provisioning_info(
6445 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006446 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006447 }
6448
Jouni Malinen75ecf522011-06-27 15:19:46 -07006449 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006450
6451 if (wpa_s == wpa_s->global->p2p_group_formation) {
6452 /*
6453 * Allow some time for the failed WPS negotiation exchange to
6454 * complete, but remove the group since group formation cannot
6455 * succeed after provisioning failure.
6456 */
6457 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6458 wpa_s->global->p2p_fail_on_wps_complete = 1;
6459 eloop_deplete_timeout(0, 50000,
6460 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006461 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006462 }
6463}
6464
6465
6466int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6467{
6468 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6469 !wpa_s->p2p_in_provisioning)
6470 return 0;
6471
6472 wpas_p2p_grpform_fail_after_wps(wpa_s);
6473
6474 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006475}
6476
6477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006478int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006479 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006480 enum wpas_p2p_prov_disc_use use,
6481 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006482{
6483 u16 config_methods;
6484
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006485 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006486 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006487 wpa_s->p2p_fallback_to_go_neg = 0;
6488 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006489 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6490 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006491 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6492 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6493
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006494 wpa_printf(MSG_DEBUG,
6495 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6496 __func__, p2ps_prov->conncap,
6497 p2ps_prov->adv_id, p2ps_prov->conncap,
6498 p2ps_prov->status, p2ps_prov->info);
6499
6500 config_methods = 0;
6501 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006502 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006503 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006504 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006505 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6506 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006508 else {
6509 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006510 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006511 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513
Dmitry Shmidt04949592012-07-19 12:16:46 -07006514 if (use == WPAS_P2P_PD_AUTO) {
6515 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6516 wpa_s->pending_pd_config_methods = config_methods;
6517 wpa_s->p2p_auto_pd = 1;
6518 wpa_s->p2p_auto_join = 0;
6519 wpa_s->pending_pd_before_join = 0;
6520 wpa_s->auto_pd_scan_retry = 0;
6521 wpas_p2p_stop_find(wpa_s);
6522 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006523 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006524 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6525 wpa_s->p2p_auto_started.sec,
6526 wpa_s->p2p_auto_started.usec);
6527 wpas_p2p_join_scan(wpa_s, NULL);
6528 return 0;
6529 }
6530
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006531 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6532 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006533 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006534 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006536 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006537 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006538 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006539}
6540
6541
6542int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6543 char *end)
6544{
6545 return p2p_scan_result_text(ies, ies_len, buf, end);
6546}
6547
6548
6549static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6550{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006551 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006552 return;
6553
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006554 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006555 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006556 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6557 wpa_s, NULL);
6558 offchannel_send_action_done(wpa_s);
6559 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006560
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006561 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6562 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006563 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006564}
6565
6566
6567int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6568 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006569 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006570 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006571 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006572{
6573 wpas_p2p_clear_pending_action_tx(wpa_s);
6574 wpa_s->p2p_long_listen = 0;
6575
Dmitry Shmidt04949592012-07-19 12:16:46 -07006576 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6577 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578 return -1;
6579
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006580 wpa_supplicant_cancel_sched_scan(wpa_s);
6581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006582 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006583 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006584 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006585}
6586
6587
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006588static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6589 struct wpa_scan_results *scan_res)
6590{
6591 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6592
6593 if (wpa_s->p2p_scan_work) {
6594 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6595 wpa_s->p2p_scan_work = NULL;
6596 radio_work_done(work);
6597 }
6598
6599 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6600 return;
6601
6602 /*
6603 * Indicate that results have been processed so that the P2P module can
6604 * continue pending tasks.
6605 */
6606 p2p_scan_res_handled(wpa_s->global->p2p);
6607}
6608
6609
6610static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611{
6612 wpas_p2p_clear_pending_action_tx(wpa_s);
6613 wpa_s->p2p_long_listen = 0;
6614 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6615 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006616
6617 if (wpa_s->global->p2p)
6618 p2p_stop_find(wpa_s->global->p2p);
6619
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006620 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6621 wpa_printf(MSG_DEBUG,
6622 "P2P: Do not consider the scan results after stop_find");
6623 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6624 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006625}
6626
6627
6628void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6629{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006630 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006631 if (!wpa_s->global->pending_group_iface_for_p2ps)
6632 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006633}
6634
6635
6636static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6637{
6638 struct wpa_supplicant *wpa_s = eloop_ctx;
6639 wpa_s->p2p_long_listen = 0;
6640}
6641
6642
6643int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6644{
6645 int res;
6646
6647 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6648 return -1;
6649
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006650 if (wpa_s->p2p_lo_started) {
6651 wpa_printf(MSG_DEBUG,
6652 "P2P: Cannot start P2P listen, it is offloaded");
6653 return -1;
6654 }
6655
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006656 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006657 wpas_p2p_clear_pending_action_tx(wpa_s);
6658
6659 if (timeout == 0) {
6660 /*
6661 * This is a request for unlimited Listen state. However, at
6662 * least for now, this is mapped to a Listen state for one
6663 * hour.
6664 */
6665 timeout = 3600;
6666 }
6667 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6668 wpa_s->p2p_long_listen = 0;
6669
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006670 /*
6671 * Stop previous find/listen operation to avoid trying to request a new
6672 * remain-on-channel operation while the driver is still running the
6673 * previous one.
6674 */
6675 if (wpa_s->global->p2p)
6676 p2p_stop_find(wpa_s->global->p2p);
6677
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006678 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6679 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6680 wpa_s->p2p_long_listen = timeout * 1000;
6681 eloop_register_timeout(timeout, 0,
6682 wpas_p2p_long_listen_timeout,
6683 wpa_s, NULL);
6684 }
6685
6686 return res;
6687}
6688
6689
6690int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6691 u8 *buf, size_t len, int p2p_group)
6692{
6693 struct wpabuf *p2p_ie;
6694 int ret;
6695
6696 if (wpa_s->global->p2p_disabled)
6697 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006698 /*
6699 * Advertize mandatory cross connection capability even on
6700 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6701 */
6702 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006703 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006704 if (wpa_s->global->p2p == NULL)
6705 return -1;
6706 if (bss == NULL)
6707 return -1;
6708
6709 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6710 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6711 p2p_group, p2p_ie);
6712 wpabuf_free(p2p_ie);
6713
6714 return ret;
6715}
6716
6717
6718int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006719 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006720 const u8 *ie, size_t ie_len,
6721 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006722{
6723 if (wpa_s->global->p2p_disabled)
6724 return 0;
6725 if (wpa_s->global->p2p == NULL)
6726 return 0;
6727
Dmitry Shmidt04949592012-07-19 12:16:46 -07006728 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006729 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006730 case P2P_PREQ_NOT_P2P:
6731 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6732 ssi_signal);
6733 /* fall through */
6734 case P2P_PREQ_MALFORMED:
6735 case P2P_PREQ_NOT_LISTEN:
6736 case P2P_PREQ_NOT_PROCESSED:
6737 default: /* make gcc happy */
6738 return 0;
6739 case P2P_PREQ_PROCESSED:
6740 return 1;
6741 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006742}
6743
6744
6745void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6746 const u8 *sa, const u8 *bssid,
6747 u8 category, const u8 *data, size_t len, int freq)
6748{
6749 if (wpa_s->global->p2p_disabled)
6750 return;
6751 if (wpa_s->global->p2p == NULL)
6752 return;
6753
6754 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6755 freq);
6756}
6757
6758
6759void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6760{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006761 unsigned int bands;
6762
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006763 if (wpa_s->global->p2p_disabled)
6764 return;
6765 if (wpa_s->global->p2p == NULL)
6766 return;
6767
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006768 bands = wpas_get_bands(wpa_s, NULL);
6769 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006770}
6771
6772
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006773static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006774{
6775 p2p_group_deinit(wpa_s->p2p_group);
6776 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006777
6778 wpa_s->ap_configured_cb = NULL;
6779 wpa_s->ap_configured_cb_ctx = NULL;
6780 wpa_s->ap_configured_cb_data = NULL;
6781 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006782}
6783
6784
6785int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6786{
6787 wpa_s->p2p_long_listen = 0;
6788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006789 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6790 return -1;
6791
6792 return p2p_reject(wpa_s->global->p2p, addr);
6793}
6794
6795
6796/* Invite to reinvoke a persistent group */
6797int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006798 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006799 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6800 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801{
6802 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006803 u8 *bssid = NULL;
6804 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006805 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006806 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006807 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006808
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006809 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006810 if (peer_addr)
6811 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6812 else
6813 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006814
Jouni Malinen31be0a42012-08-31 21:20:51 +03006815 wpa_s->p2p_persistent_go_freq = freq;
6816 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006817 wpa_s->p2p_go_vht = !!vht;
6818 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
6819 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006820 if (ssid->mode == WPAS_MODE_P2P_GO) {
6821 role = P2P_INVITE_ROLE_GO;
6822 if (peer_addr == NULL) {
6823 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6824 "address in invitation command");
6825 return -1;
6826 }
6827 if (wpas_p2p_create_iface(wpa_s)) {
6828 if (wpas_p2p_add_group_interface(wpa_s,
6829 WPA_IF_P2P_GO) < 0) {
6830 wpa_printf(MSG_ERROR, "P2P: Failed to "
6831 "allocate a new interface for the "
6832 "group");
6833 return -1;
6834 }
6835 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006836 } else if (wpa_s->p2p_mgmt)
6837 bssid = wpa_s->parent->own_addr;
6838 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006839 bssid = wpa_s->own_addr;
6840 } else {
6841 role = P2P_INVITE_ROLE_CLIENT;
6842 peer_addr = ssid->bssid;
6843 }
6844 wpa_s->pending_invite_ssid_id = ssid->id;
6845
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006846 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006847 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006848 role == P2P_INVITE_ROLE_GO,
6849 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006850 if (res)
6851 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6854 return -1;
6855
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006856 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6857
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006858 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6859 no_pref_freq_given && pref_freq > 0 &&
6860 wpa_s->num_multichan_concurrent > 1 &&
6861 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6862 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6863 pref_freq);
6864 pref_freq = 0;
6865 }
6866
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006867 /*
6868 * Stop any find/listen operations before invitation and possibly
6869 * connection establishment.
6870 */
6871 wpas_p2p_stop_find_oper(wpa_s);
6872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006873 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006874 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006875 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006876}
6877
6878
6879/* Invite to join an active group */
6880int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6881 const u8 *peer_addr, const u8 *go_dev_addr)
6882{
6883 struct wpa_global *global = wpa_s->global;
6884 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006885 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006886 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006887 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006888 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006889 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006890 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006891
Jouni Malinen31be0a42012-08-31 21:20:51 +03006892 wpa_s->p2p_persistent_go_freq = 0;
6893 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006894 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006895 wpa_s->p2p_go_vht_center_freq2 = 0;
6896 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006898 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6899 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6900 break;
6901 }
6902 if (wpa_s == NULL) {
6903 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6904 return -1;
6905 }
6906
6907 ssid = wpa_s->current_ssid;
6908 if (ssid == NULL) {
6909 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6910 "invitation");
6911 return -1;
6912 }
6913
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006914 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006915 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006916 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006917 ssid->ssid, ssid->ssid_len);
6918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006919 if (ssid->mode == WPAS_MODE_P2P_GO) {
6920 role = P2P_INVITE_ROLE_ACTIVE_GO;
6921 bssid = wpa_s->own_addr;
6922 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006923 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006924 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006925 } else {
6926 role = P2P_INVITE_ROLE_CLIENT;
6927 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6928 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6929 "invite to current group");
6930 return -1;
6931 }
6932 bssid = wpa_s->bssid;
6933 if (go_dev_addr == NULL &&
6934 !is_zero_ether_addr(wpa_s->go_dev_addr))
6935 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006936 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6937 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006939 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6942 return -1;
6943
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006944 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006945 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006946 role == P2P_INVITE_ROLE_ACTIVE_GO,
6947 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006948 if (res)
6949 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006950 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006953 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006954 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955}
6956
6957
6958void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6959{
6960 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961 u8 go_dev_addr[ETH_ALEN];
6962 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006963 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006964 u8 ip[3 * 4];
6965 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006966
Dmitry Shmidt04949592012-07-19 12:16:46 -07006967 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6968 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006969 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006970 }
6971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006973 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974
6975 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006976 if (!wpa_s->p2p_go_group_formation_completed &&
6977 wpa_s->global->p2p_group_formation == wpa_s) {
6978 wpa_dbg(wpa_s, MSG_DEBUG,
6979 "P2P: Marking group formation completed on client on data connection");
6980 wpa_s->p2p_go_group_formation_completed = 1;
6981 wpa_s->global->p2p_group_formation = NULL;
6982 wpa_s->p2p_in_provisioning = 0;
6983 wpa_s->p2p_in_invitation = 0;
6984 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006985
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006986 os_memset(go_dev_addr, 0, ETH_ALEN);
6987 if (ssid->bssid_set)
6988 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6989 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6990 ssid->ssid_len);
6991 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6992
6993 if (wpa_s->global->p2p_group_formation == wpa_s)
6994 wpa_s->global->p2p_group_formation = NULL;
6995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006996 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6997 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006998
6999 ip_addr[0] = '\0';
7000 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007001 int res;
7002
7003 res = os_snprintf(ip_addr, sizeof(ip_addr),
7004 " ip_addr=%u.%u.%u.%u "
7005 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7006 ip[0], ip[1], ip[2], ip[3],
7007 ip[4], ip[5], ip[6], ip[7],
7008 ip[8], ip[9], ip[10], ip[11]);
7009 if (os_snprintf_error(sizeof(ip_addr), res))
7010 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007011 }
7012
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007013 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7014 ssid->passphrase == NULL && ssid->psk_set ?
7015 ssid->psk : NULL,
7016 ssid->passphrase, go_dev_addr, persistent,
7017 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007018
7019 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007020 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7021 ssid, go_dev_addr);
7022
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007023 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007024}
7025
7026
7027int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7028 u32 interval1, u32 duration2, u32 interval2)
7029{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007030 int ret;
7031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007032 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7033 return -1;
7034
7035 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7036 wpa_s->current_ssid == NULL ||
7037 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7038 return -1;
7039
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007040 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7041 wpa_s->own_addr, wpa_s->assoc_freq,
7042 duration1, interval1, duration2, interval2);
7043 if (ret == 0)
7044 wpa_s->waiting_presence_resp = 1;
7045
7046 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007047}
7048
7049
7050int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7051 unsigned int interval)
7052{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7054 return -1;
7055
7056 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7057}
7058
7059
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007060static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7061{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007062 if (wpa_s->current_ssid == NULL) {
7063 /*
7064 * current_ssid can be cleared when P2P client interface gets
7065 * disconnected, so assume this interface was used as P2P
7066 * client.
7067 */
7068 return 1;
7069 }
7070 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007071 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7072}
7073
7074
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007075static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7076{
7077 struct wpa_supplicant *wpa_s = eloop_ctx;
7078
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007079 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7081 "disabled");
7082 return;
7083 }
7084
Dmitry Shmidt04949592012-07-19 12:16:46 -07007085 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7086 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007087 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007088}
7089
7090
7091static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7092{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007093 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007094
Dmitry Shmidt04949592012-07-19 12:16:46 -07007095 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7096 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007098 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7099 return;
7100
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007101 timeout = wpa_s->conf->p2p_group_idle;
7102 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7103 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7104 timeout = P2P_MAX_CLIENT_IDLE;
7105
7106 if (timeout == 0)
7107 return;
7108
Dmitry Shmidt04949592012-07-19 12:16:46 -07007109 if (timeout < 0) {
7110 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7111 timeout = 0; /* special client mode no-timeout */
7112 else
7113 return;
7114 }
7115
7116 if (wpa_s->p2p_in_provisioning) {
7117 /*
7118 * Use the normal group formation timeout during the
7119 * provisioning phase to avoid terminating this process too
7120 * early due to group idle timeout.
7121 */
7122 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7123 "during provisioning");
7124 return;
7125 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307126
Dmitry Shmidt04949592012-07-19 12:16:46 -07007127 if (wpa_s->show_group_started) {
7128 /*
7129 * Use the normal group formation timeout between the end of
7130 * the provisioning phase and completion of 4-way handshake to
7131 * avoid terminating this process too early due to group idle
7132 * timeout.
7133 */
7134 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7135 "while waiting for initial 4-way handshake to "
7136 "complete");
7137 return;
7138 }
7139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007141 timeout);
7142 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7143 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007144}
7145
7146
Jouni Malinen2b89da82012-08-31 22:04:41 +03007147/* Returns 1 if the interface was removed */
7148int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7149 u16 reason_code, const u8 *ie, size_t ie_len,
7150 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007151{
7152 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007153 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007154
Dmitry Shmidt04949592012-07-19 12:16:46 -07007155 if (!locally_generated)
7156 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7157 ie_len);
7158
7159 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7160 wpa_s->current_ssid &&
7161 wpa_s->current_ssid->p2p_group &&
7162 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7163 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7164 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007165 if (wpas_p2p_group_delete(wpa_s,
7166 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7167 > 0)
7168 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007169 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007170
7171 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007172}
7173
7174
7175void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007176 u16 reason_code, const u8 *ie, size_t ie_len,
7177 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007178{
7179 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7180 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007181
Dmitry Shmidt04949592012-07-19 12:16:46 -07007182 if (!locally_generated)
7183 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7184 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007185}
7186
7187
7188void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7189{
7190 struct p2p_data *p2p = wpa_s->global->p2p;
7191
7192 if (p2p == NULL)
7193 return;
7194
7195 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7196 return;
7197
7198 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7199 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7200
7201 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7202 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7203
7204 if (wpa_s->wps &&
7205 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7206 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7207
7208 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7209 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7210
7211 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7212 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7213 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7214 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7215 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7216 }
7217
7218 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7219 p2p_set_sec_dev_types(p2p,
7220 (void *) wpa_s->conf->sec_device_type,
7221 wpa_s->conf->num_sec_device_types);
7222
7223 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7224 int i;
7225 p2p_remove_wps_vendor_extensions(p2p);
7226 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7227 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7228 continue;
7229 p2p_add_wps_vendor_extension(
7230 p2p, wpa_s->conf->wps_vendor_ext[i]);
7231 }
7232 }
7233
7234 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7235 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7236 char country[3];
7237 country[0] = wpa_s->conf->country[0];
7238 country[1] = wpa_s->conf->country[1];
7239 country[2] = 0x04;
7240 p2p_set_country(p2p, country);
7241 }
7242
7243 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7244 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7245 wpa_s->conf->p2p_ssid_postfix ?
7246 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7247 0);
7248 }
7249
7250 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7251 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007252
7253 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7254 u8 reg_class, channel;
7255 int ret;
7256 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007257 u8 channel_forced;
7258
Jouni Malinen75ecf522011-06-27 15:19:46 -07007259 if (wpa_s->conf->p2p_listen_reg_class &&
7260 wpa_s->conf->p2p_listen_channel) {
7261 reg_class = wpa_s->conf->p2p_listen_reg_class;
7262 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007263 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007264 } else {
7265 reg_class = 81;
7266 /*
7267 * Pick one of the social channels randomly as the
7268 * listen channel.
7269 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007270 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7271 channel = 1;
7272 else
7273 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007274 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007275 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007276 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7277 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007278 if (ret)
7279 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7280 "failed: %d", ret);
7281 }
7282 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7283 u8 op_reg_class, op_channel, cfg_op_channel;
7284 int ret = 0;
7285 unsigned int r;
7286 if (wpa_s->conf->p2p_oper_reg_class &&
7287 wpa_s->conf->p2p_oper_channel) {
7288 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7289 op_channel = wpa_s->conf->p2p_oper_channel;
7290 cfg_op_channel = 1;
7291 } else {
7292 op_reg_class = 81;
7293 /*
7294 * Use random operation channel from (1, 6, 11)
7295 *if no other preference is indicated.
7296 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007297 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7298 op_channel = 1;
7299 else
7300 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007301 cfg_op_channel = 0;
7302 }
7303 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7304 cfg_op_channel);
7305 if (ret)
7306 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7307 "failed: %d", ret);
7308 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007309
7310 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7311 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7312 wpa_s->conf->p2p_pref_chan) < 0) {
7313 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7314 "update failed");
7315 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007316
7317 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7318 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7319 "update failed");
7320 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007321 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007322
7323 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7324 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007325}
7326
7327
7328int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7329 int duration)
7330{
7331 if (!wpa_s->ap_iface)
7332 return -1;
7333 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7334 duration);
7335}
7336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007337
7338int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7339{
7340 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7341 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007342
7343 wpa_s->global->cross_connection = enabled;
7344 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7345
7346 if (!enabled) {
7347 struct wpa_supplicant *iface;
7348
7349 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7350 {
7351 if (iface->cross_connect_enabled == 0)
7352 continue;
7353
7354 iface->cross_connect_enabled = 0;
7355 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007356 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007357 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7358 iface->ifname,
7359 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 }
7361 }
7362
7363 return 0;
7364}
7365
7366
7367static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7368{
7369 struct wpa_supplicant *iface;
7370
7371 if (!uplink->global->cross_connection)
7372 return;
7373
7374 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7375 if (!iface->cross_connect_enabled)
7376 continue;
7377 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7378 0)
7379 continue;
7380 if (iface->ap_iface == NULL)
7381 continue;
7382 if (iface->cross_connect_in_use)
7383 continue;
7384
7385 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007386 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007387 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7388 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007389 }
7390}
7391
7392
7393static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7394{
7395 struct wpa_supplicant *iface;
7396
7397 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7398 if (!iface->cross_connect_enabled)
7399 continue;
7400 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7401 0)
7402 continue;
7403 if (!iface->cross_connect_in_use)
7404 continue;
7405
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007406 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007407 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7408 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007409 iface->cross_connect_in_use = 0;
7410 }
7411}
7412
7413
7414void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7415{
7416 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7417 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7418 wpa_s->cross_connect_disallowed)
7419 wpas_p2p_disable_cross_connect(wpa_s);
7420 else
7421 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007422 if (!wpa_s->ap_iface &&
7423 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7424 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007425}
7426
7427
7428void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7429{
7430 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007431 if (!wpa_s->ap_iface &&
7432 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7433 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007434 wpas_p2p_set_group_idle_timeout(wpa_s);
7435}
7436
7437
7438static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7439{
7440 struct wpa_supplicant *iface;
7441
7442 if (!wpa_s->global->cross_connection)
7443 return;
7444
7445 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7446 if (iface == wpa_s)
7447 continue;
7448 if (iface->drv_flags &
7449 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7450 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007451 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7452 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007453 continue;
7454
7455 wpa_s->cross_connect_enabled = 1;
7456 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7457 sizeof(wpa_s->cross_connect_uplink));
7458 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7459 "%s to %s whenever uplink is available",
7460 wpa_s->ifname, wpa_s->cross_connect_uplink);
7461
7462 if (iface->ap_iface || iface->current_ssid == NULL ||
7463 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7464 iface->cross_connect_disallowed ||
7465 iface->wpa_state != WPA_COMPLETED)
7466 break;
7467
7468 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007469 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007470 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7471 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007472 break;
7473 }
7474}
7475
7476
7477int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7478{
7479 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7480 !wpa_s->p2p_in_provisioning)
7481 return 0; /* not P2P client operation */
7482
7483 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7484 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007485 if (wpa_s != wpa_s->p2pdev)
7486 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007487 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007488 return 1;
7489}
7490
7491
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007492void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7493{
7494 struct wpa_supplicant *wpa_s = eloop_ctx;
7495 wpas_p2p_notif_pbc_overlap(wpa_s);
7496}
7497
7498
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007499void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7500 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007501{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007502 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007503 struct wpa_used_freq_data *freqs = NULL;
7504 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007505
7506 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7507 return;
7508
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007509 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7510 if (!freqs)
7511 return;
7512
7513 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007515 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007516 os_memset(&cli_chan, 0, sizeof(cli_chan));
7517 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007518 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7519 "channel list");
7520 return;
7521 }
7522
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007523 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007524
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007525 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007526
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007527 /*
7528 * The used frequencies map changed, so it is possible that a GO is
7529 * using a channel that is no longer valid for P2P use. It is also
7530 * possible that due to policy consideration, it would be preferable to
7531 * move it to a frequency already used by other station interfaces.
7532 */
7533 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7534
7535 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007536}
7537
7538
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007539static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7540 struct wpa_scan_results *scan_res)
7541{
7542 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7543}
7544
7545
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007546int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7547{
7548 struct wpa_global *global = wpa_s->global;
7549 int found = 0;
7550 const u8 *peer;
7551
7552 if (global->p2p == NULL)
7553 return -1;
7554
7555 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7556
7557 if (wpa_s->pending_interface_name[0] &&
7558 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7559 found = 1;
7560
7561 peer = p2p_get_go_neg_peer(global->p2p);
7562 if (peer) {
7563 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7564 MACSTR, MAC2STR(peer));
7565 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007566 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007567 }
7568
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007569 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7570 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7571 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7572 found = 1;
7573 }
7574
7575 if (wpa_s->pending_pd_before_join) {
7576 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7577 wpa_s->pending_pd_before_join = 0;
7578 found = 1;
7579 }
7580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007581 wpas_p2p_stop_find(wpa_s);
7582
7583 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7584 if (wpa_s == global->p2p_group_formation &&
7585 (wpa_s->p2p_in_provisioning ||
7586 wpa_s->parent->pending_interface_type ==
7587 WPA_IF_P2P_CLIENT)) {
7588 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7589 "formation found - cancelling",
7590 wpa_s->ifname);
7591 found = 1;
7592 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007593 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007594 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007595 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007596 break;
7597 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007598 wpas_p2p_group_delete(wpa_s,
7599 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007600 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007601 } else if (wpa_s->p2p_in_invitation) {
7602 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7603 wpa_s->ifname);
7604 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007605 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007606 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007607 }
7608 }
7609
7610 if (!found) {
7611 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7612 return -1;
7613 }
7614
7615 return 0;
7616}
7617
7618
7619void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7620{
7621 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7622 return;
7623
7624 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7625 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007626 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007627}
7628
7629
7630void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7631 int freq_24, int freq_5, int freq_overall)
7632{
7633 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007634 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007635 return;
7636 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7637}
7638
7639
7640int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7641{
7642 u8 peer[ETH_ALEN];
7643 struct p2p_data *p2p = wpa_s->global->p2p;
7644
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007645 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007646 return -1;
7647
7648 if (hwaddr_aton(addr, peer))
7649 return -1;
7650
7651 return p2p_unauthorize(p2p, peer);
7652}
7653
7654
7655/**
7656 * wpas_p2p_disconnect - Disconnect from a P2P Group
7657 * @wpa_s: Pointer to wpa_supplicant data
7658 * Returns: 0 on success, -1 on failure
7659 *
7660 * This can be used to disconnect from a group in which the local end is a P2P
7661 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7662 * virtual network interface was created for this group, that interface will be
7663 * removed. Otherwise, only the configured P2P group network will be removed
7664 * from the interface.
7665 */
7666int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7667{
7668
7669 if (wpa_s == NULL)
7670 return -1;
7671
Jouni Malinen2b89da82012-08-31 22:04:41 +03007672 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7673 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007674}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007675
7676
7677int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7678{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007679 int ret;
7680
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007681 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7682 return 0;
7683
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007684 ret = p2p_in_progress(wpa_s->global->p2p);
7685 if (ret == 0) {
7686 /*
7687 * Check whether there is an ongoing WPS provisioning step (or
7688 * other parts of group formation) on another interface since
7689 * p2p_in_progress() does not report this to avoid issues for
7690 * scans during such provisioning step.
7691 */
7692 if (wpa_s->global->p2p_group_formation &&
7693 wpa_s->global->p2p_group_formation != wpa_s) {
7694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7695 "in group formation",
7696 wpa_s->global->p2p_group_formation->ifname);
7697 ret = 1;
7698 }
7699 }
7700
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007701 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007702 struct os_reltime now;
7703 os_get_reltime(&now);
7704 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7705 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007706 /* Wait for the first client has expired */
7707 wpa_s->global->p2p_go_wait_client.sec = 0;
7708 } else {
7709 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7710 ret = 1;
7711 }
7712 }
7713
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007714 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007715}
7716
7717
7718void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7719 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007720{
7721 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7722 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007723 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007724 /**
7725 * Remove the network by scheduling the group formation
7726 * timeout to happen immediately. The teardown code
7727 * needs to be scheduled to run asynch later so that we
7728 * don't delete data from under ourselves unexpectedly.
7729 * Calling wpas_p2p_group_formation_timeout directly
7730 * causes a series of crashes in WPS failure scenarios.
7731 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007732 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7733 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007734 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007735 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007736 }
7737}
7738
7739
7740struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007741 const u8 *addr, const u8 *ssid,
7742 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007743{
7744 struct wpa_ssid *s;
7745 size_t i;
7746
7747 for (s = wpa_s->conf->ssid; s; s = s->next) {
7748 if (s->disabled != 2)
7749 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007750 if (ssid &&
7751 (ssid_len != s->ssid_len ||
7752 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7753 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007754 if (addr == NULL) {
7755 if (s->mode == WPAS_MODE_P2P_GO)
7756 return s;
7757 continue;
7758 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007759 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7760 return s; /* peer is GO in the persistent group */
7761 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7762 continue;
7763 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007764 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007765 addr, ETH_ALEN) == 0)
7766 return s; /* peer is P2P client in persistent
7767 * group */
7768 }
7769 }
7770
7771 return NULL;
7772}
7773
7774
7775void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7776 const u8 *addr)
7777{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007778 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007779 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007780 /*
7781 * This can happen if WPS provisioning step is not terminated
7782 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7783 * peer was able to connect, there is no need to time out group
7784 * formation after this, though. In addition, this is used with
7785 * the initial connection wait on the GO as a separate formation
7786 * timeout and as such, expected to be hit after the initial WPS
7787 * provisioning step.
7788 */
7789 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007790
7791 if (!wpa_s->p2p_go_group_formation_completed &&
7792 !wpa_s->group_formation_reported) {
7793 /*
7794 * GO has not yet notified group formation success since
7795 * the WPS step was not completed cleanly. Do that
7796 * notification now since the P2P Client was able to
7797 * connect and as such, must have received the
7798 * credential from the WPS step.
7799 */
7800 if (wpa_s->global->p2p)
7801 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007802 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007803 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007804 }
7805 if (!wpa_s->p2p_go_group_formation_completed) {
7806 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7807 wpa_s->p2p_go_group_formation_completed = 1;
7808 wpa_s->global->p2p_group_formation = NULL;
7809 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007810 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007811 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007812 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007813 if (addr == NULL)
7814 return;
7815 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7816}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007817
Dmitry Shmidt04949592012-07-19 12:16:46 -07007818
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007819static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7820 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007821{
7822 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007823 int ret = 0;
7824
Dmitry Shmidt04949592012-07-19 12:16:46 -07007825 if (wpa_s->global->p2p_group_formation)
7826 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007827 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007828 offchannel_send_action_done(wpa_s);
7829 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007830 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007831 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7832 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7833 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7834 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007835 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007836 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007837 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007838 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007839 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007840 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007841 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007842}
7843
7844
7845int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7846{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007847 int res;
7848
Dmitry Shmidt04949592012-07-19 12:16:46 -07007849 if (!wpa_s->p2p_fallback_to_go_neg ||
7850 wpa_s->p2p_in_provisioning <= 5)
7851 return 0;
7852
7853 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7854 return 0; /* peer operating as a GO */
7855
7856 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7857 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007858 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007859 "reason=GO-not-found");
7860 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007861
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007862 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007863}
7864
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007865
7866unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7867{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007868 struct wpa_supplicant *ifs;
7869
7870 if (wpa_s->wpa_state > WPA_SCANNING) {
7871 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7872 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007873 wpa_s->conf->p2p_search_delay);
7874 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007875 }
7876
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007877 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7878 radio_list) {
7879 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007880 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7881 "delay due to concurrent operation on "
7882 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007883 wpa_s->conf->p2p_search_delay,
7884 ifs->ifname);
7885 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007886 }
7887 }
7888
7889 return 0;
7890}
7891
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007892
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007893static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7894 struct wpa_ssid *s, const u8 *addr,
7895 int iface_addr)
7896{
7897 struct psk_list_entry *psk, *tmp;
7898 int changed = 0;
7899
7900 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7901 list) {
7902 if ((iface_addr && !psk->p2p &&
7903 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7904 (!iface_addr && psk->p2p &&
7905 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7906 wpa_dbg(wpa_s, MSG_DEBUG,
7907 "P2P: Remove persistent group PSK list entry for "
7908 MACSTR " p2p=%u",
7909 MAC2STR(psk->addr), psk->p2p);
7910 dl_list_del(&psk->list);
7911 os_free(psk);
7912 changed++;
7913 }
7914 }
7915
7916 return changed;
7917}
7918
7919
7920void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7921 const u8 *p2p_dev_addr,
7922 const u8 *psk, size_t psk_len)
7923{
7924 struct wpa_ssid *ssid = wpa_s->current_ssid;
7925 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007926 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007927
7928 if (psk_len != sizeof(p->psk))
7929 return;
7930
7931 if (p2p_dev_addr) {
7932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7933 " p2p_dev_addr=" MACSTR,
7934 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7935 if (is_zero_ether_addr(p2p_dev_addr))
7936 p2p_dev_addr = NULL;
7937 } else {
7938 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7939 MAC2STR(mac_addr));
7940 }
7941
7942 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7943 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7944 /* To be added to persistent group once created */
7945 if (wpa_s->global->add_psk == NULL) {
7946 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7947 if (wpa_s->global->add_psk == NULL)
7948 return;
7949 }
7950 p = wpa_s->global->add_psk;
7951 if (p2p_dev_addr) {
7952 p->p2p = 1;
7953 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7954 } else {
7955 p->p2p = 0;
7956 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7957 }
7958 os_memcpy(p->psk, psk, psk_len);
7959 return;
7960 }
7961
7962 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7963 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7964 return;
7965 }
7966
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007967 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007968 ssid->ssid_len);
7969 if (!persistent) {
7970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7971 return;
7972 }
7973
7974 p = os_zalloc(sizeof(*p));
7975 if (p == NULL)
7976 return;
7977 if (p2p_dev_addr) {
7978 p->p2p = 1;
7979 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7980 } else {
7981 p->p2p = 0;
7982 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7983 }
7984 os_memcpy(p->psk, psk, psk_len);
7985
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007986 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7987 (last = dl_list_last(&persistent->psk_list,
7988 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007989 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7990 MACSTR " (p2p=%u) to make room for a new one",
7991 MAC2STR(last->addr), last->p2p);
7992 dl_list_del(&last->list);
7993 os_free(last);
7994 }
7995
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007996 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007997 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7998 p2p_dev_addr == NULL);
7999 if (p2p_dev_addr) {
8000 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8001 MACSTR, MAC2STR(p2p_dev_addr));
8002 } else {
8003 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8004 MAC2STR(mac_addr));
8005 }
8006 dl_list_add(&persistent->psk_list, &p->list);
8007
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008008 if (wpa_s->p2pdev->conf->update_config &&
8009 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008010 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008011}
8012
8013
8014static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8015 struct wpa_ssid *s, const u8 *addr,
8016 int iface_addr)
8017{
8018 int res;
8019
8020 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008021 if (res > 0 && wpa_s->conf->update_config &&
8022 wpa_config_write(wpa_s->confname, wpa_s->conf))
8023 wpa_dbg(wpa_s, MSG_DEBUG,
8024 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008025}
8026
8027
8028static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8029 const u8 *peer, int iface_addr)
8030{
8031 struct hostapd_data *hapd;
8032 struct hostapd_wpa_psk *psk, *prev, *rem;
8033 struct sta_info *sta;
8034
8035 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8036 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8037 return;
8038
8039 /* Remove per-station PSK entry */
8040 hapd = wpa_s->ap_iface->bss[0];
8041 prev = NULL;
8042 psk = hapd->conf->ssid.wpa_psk;
8043 while (psk) {
8044 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8045 (!iface_addr &&
8046 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8047 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8048 MACSTR " iface_addr=%d",
8049 MAC2STR(peer), iface_addr);
8050 if (prev)
8051 prev->next = psk->next;
8052 else
8053 hapd->conf->ssid.wpa_psk = psk->next;
8054 rem = psk;
8055 psk = psk->next;
8056 os_free(rem);
8057 } else {
8058 prev = psk;
8059 psk = psk->next;
8060 }
8061 }
8062
8063 /* Disconnect from group */
8064 if (iface_addr)
8065 sta = ap_get_sta(hapd, peer);
8066 else
8067 sta = ap_get_sta_p2p(hapd, peer);
8068 if (sta) {
8069 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8070 " (iface_addr=%d) from group",
8071 MAC2STR(peer), iface_addr);
8072 hostapd_drv_sta_deauth(hapd, sta->addr,
8073 WLAN_REASON_DEAUTH_LEAVING);
8074 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8075 }
8076}
8077
8078
8079void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8080 int iface_addr)
8081{
8082 struct wpa_ssid *s;
8083 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008084 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008085
8086 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8087
8088 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008089 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008090 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8091 continue;
8092 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008093 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8094 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008095 }
8096
8097 /* Remove from any operating group */
8098 for (w = wpa_s->global->ifaces; w; w = w->next)
8099 wpas_p2p_remove_client_go(w, peer, iface_addr);
8100}
8101
8102
8103static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8104{
8105 struct wpa_supplicant *wpa_s = eloop_ctx;
8106 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8107}
8108
8109
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008110static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8111{
8112 struct wpa_supplicant *wpa_s = eloop_ctx;
8113
8114 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8115 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8116}
8117
8118
8119int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8120 struct wpa_ssid *ssid)
8121{
8122 struct wpa_supplicant *iface;
8123
8124 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8125 if (!iface->current_ssid ||
8126 iface->current_ssid->frequency == freq ||
8127 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8128 !iface->current_ssid->p2p_group))
8129 continue;
8130
8131 /* Remove the connection with least priority */
8132 if (!wpas_is_p2p_prioritized(iface)) {
8133 /* STA connection has priority over existing
8134 * P2P connection, so remove the interface. */
8135 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8136 eloop_register_timeout(0, 0,
8137 wpas_p2p_group_freq_conflict,
8138 iface, NULL);
8139 /* If connection in progress is P2P connection, do not
8140 * proceed for the connection. */
8141 if (wpa_s == iface)
8142 return -1;
8143 else
8144 return 0;
8145 } else {
8146 /* P2P connection has priority, disable the STA network
8147 */
8148 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8149 ssid);
8150 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8151 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8152 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8153 ETH_ALEN);
8154 /* If P2P connection is in progress, continue
8155 * connecting...*/
8156 if (wpa_s == iface)
8157 return 0;
8158 else
8159 return -1;
8160 }
8161 }
8162
8163 return 0;
8164}
8165
8166
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008167int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8168{
8169 struct wpa_ssid *ssid = wpa_s->current_ssid;
8170
8171 if (ssid == NULL || !ssid->p2p_group)
8172 return 0;
8173
8174 if (wpa_s->p2p_last_4way_hs_fail &&
8175 wpa_s->p2p_last_4way_hs_fail == ssid) {
8176 u8 go_dev_addr[ETH_ALEN];
8177 struct wpa_ssid *persistent;
8178
8179 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8180 ssid->ssid,
8181 ssid->ssid_len) <= 0) {
8182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8183 goto disconnect;
8184 }
8185
8186 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8187 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008188 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008189 ssid->ssid,
8190 ssid->ssid_len);
8191 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8192 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8193 goto disconnect;
8194 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008195 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008196 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8197 persistent->id);
8198 disconnect:
8199 wpa_s->p2p_last_4way_hs_fail = NULL;
8200 /*
8201 * Remove the group from a timeout to avoid issues with caller
8202 * continuing to use the interface if this is on a P2P group
8203 * interface.
8204 */
8205 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8206 wpa_s, NULL);
8207 return 1;
8208 }
8209
8210 wpa_s->p2p_last_4way_hs_fail = ssid;
8211 return 0;
8212}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008213
8214
8215#ifdef CONFIG_WPS_NFC
8216
8217static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8218 struct wpabuf *p2p)
8219{
8220 struct wpabuf *ret;
8221 size_t wsc_len;
8222
8223 if (p2p == NULL) {
8224 wpabuf_free(wsc);
8225 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8226 return NULL;
8227 }
8228
8229 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8230 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8231 if (ret == NULL) {
8232 wpabuf_free(wsc);
8233 wpabuf_free(p2p);
8234 return NULL;
8235 }
8236
8237 wpabuf_put_be16(ret, wsc_len);
8238 if (wsc)
8239 wpabuf_put_buf(ret, wsc);
8240 wpabuf_put_be16(ret, wpabuf_len(p2p));
8241 wpabuf_put_buf(ret, p2p);
8242
8243 wpabuf_free(wsc);
8244 wpabuf_free(p2p);
8245 wpa_hexdump_buf(MSG_DEBUG,
8246 "P2P: Generated NFC connection handover message", ret);
8247
8248 if (ndef && ret) {
8249 struct wpabuf *tmp;
8250 tmp = ndef_build_p2p(ret);
8251 wpabuf_free(ret);
8252 if (tmp == NULL) {
8253 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8254 return NULL;
8255 }
8256 ret = tmp;
8257 }
8258
8259 return ret;
8260}
8261
8262
8263static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8264 struct wpa_ssid **ssid, u8 *go_dev_addr)
8265{
8266 struct wpa_supplicant *iface;
8267
8268 if (go_dev_addr)
8269 os_memset(go_dev_addr, 0, ETH_ALEN);
8270 if (ssid)
8271 *ssid = NULL;
8272 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8273 if (iface->wpa_state < WPA_ASSOCIATING ||
8274 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8275 !iface->current_ssid->p2p_group ||
8276 iface->current_ssid->mode != WPAS_MODE_INFRA)
8277 continue;
8278 if (ssid)
8279 *ssid = iface->current_ssid;
8280 if (go_dev_addr)
8281 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8282 return iface->assoc_freq;
8283 }
8284 return 0;
8285}
8286
8287
8288struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8289 int ndef)
8290{
8291 struct wpabuf *wsc, *p2p;
8292 struct wpa_ssid *ssid;
8293 u8 go_dev_addr[ETH_ALEN];
8294 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8295
8296 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8297 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8298 return NULL;
8299 }
8300
8301 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8302 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8303 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8304 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8305 return NULL;
8306 }
8307
8308 if (cli_freq == 0) {
8309 wsc = wps_build_nfc_handover_req_p2p(
8310 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8311 } else
8312 wsc = NULL;
8313 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8314 go_dev_addr, ssid ? ssid->ssid : NULL,
8315 ssid ? ssid->ssid_len : 0);
8316
8317 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8318}
8319
8320
8321struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8322 int ndef, int tag)
8323{
8324 struct wpabuf *wsc, *p2p;
8325 struct wpa_ssid *ssid;
8326 u8 go_dev_addr[ETH_ALEN];
8327 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8328
8329 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8330 return NULL;
8331
8332 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8333 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8334 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8335 return NULL;
8336
8337 if (cli_freq == 0) {
8338 wsc = wps_build_nfc_handover_sel_p2p(
8339 wpa_s->parent->wps,
8340 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8341 DEV_PW_NFC_CONNECTION_HANDOVER,
8342 wpa_s->conf->wps_nfc_dh_pubkey,
8343 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8344 } else
8345 wsc = NULL;
8346 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8347 go_dev_addr, ssid ? ssid->ssid : NULL,
8348 ssid ? ssid->ssid_len : 0);
8349
8350 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8351}
8352
8353
8354static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8355 struct p2p_nfc_params *params)
8356{
8357 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8358 "connection handover (freq=%d)",
8359 params->go_freq);
8360
8361 if (params->go_freq && params->go_ssid_len) {
8362 wpa_s->p2p_wps_method = WPS_NFC;
8363 wpa_s->pending_join_wps_method = WPS_NFC;
8364 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8365 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8366 ETH_ALEN);
8367 return wpas_p2p_join_start(wpa_s, params->go_freq,
8368 params->go_ssid,
8369 params->go_ssid_len);
8370 }
8371
8372 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8373 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008374 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008375 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8376 params->go_ssid_len ? params->go_ssid : NULL,
8377 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008378}
8379
8380
8381static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8382 struct p2p_nfc_params *params, int tag)
8383{
8384 int res, persistent;
8385 struct wpa_ssid *ssid;
8386
8387 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8388 "connection handover");
8389 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8390 ssid = wpa_s->current_ssid;
8391 if (ssid == NULL)
8392 continue;
8393 if (ssid->mode != WPAS_MODE_P2P_GO)
8394 continue;
8395 if (wpa_s->ap_iface == NULL)
8396 continue;
8397 break;
8398 }
8399 if (wpa_s == NULL) {
8400 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8401 return -1;
8402 }
8403
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008404 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008405 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008406 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008407 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8408 return -1;
8409 }
8410 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008411 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8412 wpa_s->p2pdev->p2p_oob_dev_pw,
8413 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8414 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008415 if (res)
8416 return res;
8417
8418 if (!tag) {
8419 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8420 return 0;
8421 }
8422
8423 if (!params->peer ||
8424 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8425 return 0;
8426
8427 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8428 " to join", MAC2STR(params->peer->p2p_device_addr));
8429
8430 wpa_s->global->p2p_invite_group = wpa_s;
8431 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008432 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008433 params->peer->p2p_device_addr,
8434 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008435 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008436
8437 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8438 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8439 ssid->ssid, ssid->ssid_len, ssid->frequency,
8440 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008441 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008442}
8443
8444
8445static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8446 struct p2p_nfc_params *params,
8447 int forced_freq)
8448{
8449 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8450 "connection handover");
8451 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8452 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008453 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008454 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8455 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008456}
8457
8458
8459static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8460 struct p2p_nfc_params *params,
8461 int forced_freq)
8462{
8463 int res;
8464
8465 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8466 "connection handover");
8467 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8468 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008469 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008470 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8471 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008472 if (res)
8473 return res;
8474
8475 res = wpas_p2p_listen(wpa_s, 60);
8476 if (res) {
8477 p2p_unauthorize(wpa_s->global->p2p,
8478 params->peer->p2p_device_addr);
8479 }
8480
8481 return res;
8482}
8483
8484
8485static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8486 const struct wpabuf *data,
8487 int sel, int tag, int forced_freq)
8488{
8489 const u8 *pos, *end;
8490 u16 len, id;
8491 struct p2p_nfc_params params;
8492 int res;
8493
8494 os_memset(&params, 0, sizeof(params));
8495 params.sel = sel;
8496
8497 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8498
8499 pos = wpabuf_head(data);
8500 end = pos + wpabuf_len(data);
8501
8502 if (end - pos < 2) {
8503 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8504 "attributes");
8505 return -1;
8506 }
8507 len = WPA_GET_BE16(pos);
8508 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008509 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008510 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8511 "attributes");
8512 return -1;
8513 }
8514 params.wsc_attr = pos;
8515 params.wsc_len = len;
8516 pos += len;
8517
8518 if (end - pos < 2) {
8519 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8520 "attributes");
8521 return -1;
8522 }
8523 len = WPA_GET_BE16(pos);
8524 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008525 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008526 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8527 "attributes");
8528 return -1;
8529 }
8530 params.p2p_attr = pos;
8531 params.p2p_len = len;
8532 pos += len;
8533
8534 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8535 params.wsc_attr, params.wsc_len);
8536 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8537 params.p2p_attr, params.p2p_len);
8538 if (pos < end) {
8539 wpa_hexdump(MSG_DEBUG,
8540 "P2P: Ignored extra data after P2P attributes",
8541 pos, end - pos);
8542 }
8543
8544 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8545 if (res)
8546 return res;
8547
8548 if (params.next_step == NO_ACTION)
8549 return 0;
8550
8551 if (params.next_step == BOTH_GO) {
8552 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8553 MAC2STR(params.peer->p2p_device_addr));
8554 return 0;
8555 }
8556
8557 if (params.next_step == PEER_CLIENT) {
8558 if (!is_zero_ether_addr(params.go_dev_addr)) {
8559 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8560 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8561 " ssid=\"%s\"",
8562 MAC2STR(params.peer->p2p_device_addr),
8563 params.go_freq,
8564 MAC2STR(params.go_dev_addr),
8565 wpa_ssid_txt(params.go_ssid,
8566 params.go_ssid_len));
8567 } else {
8568 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8569 "peer=" MACSTR " freq=%d",
8570 MAC2STR(params.peer->p2p_device_addr),
8571 params.go_freq);
8572 }
8573 return 0;
8574 }
8575
8576 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8577 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8578 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8579 return 0;
8580 }
8581
8582 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8583 wpa_s->p2p_oob_dev_pw = NULL;
8584
8585 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8586 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8587 "received");
8588 return -1;
8589 }
8590
8591 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8592 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8593 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8594 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8595 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8596 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8597 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8598
8599 if (tag) {
8600 if (id < 0x10) {
8601 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8602 "peer OOB Device Password Id %u", id);
8603 return -1;
8604 }
8605 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8606 "Device Password Id %u", id);
8607 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8608 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8609 params.oob_dev_pw_len -
8610 WPS_OOB_PUBKEY_HASH_LEN - 2);
8611 wpa_s->p2p_oob_dev_pw_id = id;
8612 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8613 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8614 params.oob_dev_pw_len -
8615 WPS_OOB_PUBKEY_HASH_LEN - 2);
8616 if (wpa_s->p2p_oob_dev_pw == NULL)
8617 return -1;
8618
8619 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8620 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8621 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8622 return -1;
8623 } else {
8624 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8625 "without Device Password");
8626 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8627 }
8628
8629 switch (params.next_step) {
8630 case NO_ACTION:
8631 case BOTH_GO:
8632 case PEER_CLIENT:
8633 /* already covered above */
8634 return 0;
8635 case JOIN_GROUP:
8636 return wpas_p2p_nfc_join_group(wpa_s, &params);
8637 case AUTH_JOIN:
8638 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8639 case INIT_GO_NEG:
8640 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8641 case RESP_GO_NEG:
8642 /* TODO: use own OOB Dev Pw */
8643 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8644 }
8645
8646 return -1;
8647}
8648
8649
8650int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8651 const struct wpabuf *data, int forced_freq)
8652{
8653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8654 return -1;
8655
8656 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8657}
8658
8659
8660int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8661 const struct wpabuf *req,
8662 const struct wpabuf *sel, int forced_freq)
8663{
8664 struct wpabuf *tmp;
8665 int ret;
8666
8667 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8668 return -1;
8669
8670 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8671
8672 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8673 wpabuf_head(req), wpabuf_len(req));
8674 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8675 wpabuf_head(sel), wpabuf_len(sel));
8676 if (forced_freq)
8677 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8678 tmp = ndef_parse_p2p(init ? sel : req);
8679 if (tmp == NULL) {
8680 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8681 return -1;
8682 }
8683
8684 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8685 forced_freq);
8686 wpabuf_free(tmp);
8687
8688 return ret;
8689}
8690
8691
8692int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8693{
8694 const u8 *if_addr;
8695 int go_intent = wpa_s->conf->p2p_go_intent;
8696 struct wpa_supplicant *iface;
8697
8698 if (wpa_s->global->p2p == NULL)
8699 return -1;
8700
8701 if (!enabled) {
8702 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8703 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8704 {
8705 if (!iface->ap_iface)
8706 continue;
8707 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8708 }
8709 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8710 0, NULL);
8711 if (wpa_s->p2p_nfc_tag_enabled)
8712 wpas_p2p_remove_pending_group_interface(wpa_s);
8713 wpa_s->p2p_nfc_tag_enabled = 0;
8714 return 0;
8715 }
8716
8717 if (wpa_s->global->p2p_disabled)
8718 return -1;
8719
8720 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8721 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8722 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8723 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8724 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8725 "to allow static handover cases");
8726 return -1;
8727 }
8728
8729 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8730
8731 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8732 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8733 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8734 if (wpa_s->p2p_oob_dev_pw == NULL)
8735 return -1;
8736 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8737
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008738 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8739 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8740 /*
8741 * P2P Group Interface present and the command came on group
8742 * interface, so enable the token for the current interface.
8743 */
8744 wpa_s->create_p2p_iface = 0;
8745 } else {
8746 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8747 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008748
8749 if (wpa_s->create_p2p_iface) {
8750 enum wpa_driver_if_type iftype;
8751 /* Prepare to add a new interface for the group */
8752 iftype = WPA_IF_P2P_GROUP;
8753 if (go_intent == 15)
8754 iftype = WPA_IF_P2P_GO;
8755 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8756 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8757 "interface for the group");
8758 return -1;
8759 }
8760
8761 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008762 } else if (wpa_s->p2p_mgmt)
8763 if_addr = wpa_s->parent->own_addr;
8764 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008765 if_addr = wpa_s->own_addr;
8766
8767 wpa_s->p2p_nfc_tag_enabled = enabled;
8768
8769 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8770 struct hostapd_data *hapd;
8771 if (iface->ap_iface == NULL)
8772 continue;
8773 hapd = iface->ap_iface->bss[0];
8774 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8775 hapd->conf->wps_nfc_dh_pubkey =
8776 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8777 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8778 hapd->conf->wps_nfc_dh_privkey =
8779 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8780 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8781 hapd->conf->wps_nfc_dev_pw =
8782 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8783 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8784
8785 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8786 wpa_dbg(iface, MSG_DEBUG,
8787 "P2P: Failed to enable NFC Tag for GO");
8788 }
8789 }
8790 p2p_set_authorized_oob_dev_pw_id(
8791 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8792 if_addr);
8793
8794 return 0;
8795}
8796
8797#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008798
8799
8800static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8801 struct wpa_used_freq_data *freqs,
8802 unsigned int num)
8803{
8804 u8 curr_chan, cand, chan;
8805 unsigned int i;
8806
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008807 /*
8808 * If possible, optimize the Listen channel to be a channel that is
8809 * already used by one of the other interfaces.
8810 */
8811 if (!wpa_s->conf->p2p_optimize_listen_chan)
8812 return;
8813
8814 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8815 return;
8816
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008817 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8818 for (i = 0, cand = 0; i < num; i++) {
8819 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8820 if (curr_chan == chan) {
8821 cand = 0;
8822 break;
8823 }
8824
8825 if (chan == 1 || chan == 6 || chan == 11)
8826 cand = chan;
8827 }
8828
8829 if (cand) {
8830 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008831 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008832 cand);
8833 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8834 }
8835}
8836
8837
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008838static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008839{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008840 struct hostapd_config *conf;
8841 struct p2p_go_neg_results params;
8842 struct csa_settings csa_settings;
8843 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8844 int old_freq = current_ssid->frequency;
8845 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008846
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008847 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8848 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8849 return -1;
8850 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008851
8852 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008853 * TODO: This function may not always work correctly. For example,
8854 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008855 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008856 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008857 wpa_dbg(wpa_s, MSG_DEBUG,
8858 "P2P CSA: Failed to select new frequency for GO");
8859 return -1;
8860 }
8861
8862 if (current_ssid->frequency == params.freq) {
8863 wpa_dbg(wpa_s, MSG_DEBUG,
8864 "P2P CSA: Selected same frequency - not moving GO");
8865 return 0;
8866 }
8867
8868 conf = hostapd_config_defaults();
8869 if (!conf) {
8870 wpa_dbg(wpa_s, MSG_DEBUG,
8871 "P2P CSA: Failed to allocate default config");
8872 return -1;
8873 }
8874
8875 current_ssid->frequency = params.freq;
8876 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8877 wpa_dbg(wpa_s, MSG_DEBUG,
8878 "P2P CSA: Failed to create new GO config");
8879 ret = -1;
8880 goto out;
8881 }
8882
8883 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8884 wpa_dbg(wpa_s, MSG_DEBUG,
8885 "P2P CSA: CSA to a different band is not supported");
8886 ret = -1;
8887 goto out;
8888 }
8889
8890 os_memset(&csa_settings, 0, sizeof(csa_settings));
8891 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8892 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8893 csa_settings.freq_params.freq = params.freq;
8894 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8895 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8896 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8897
8898 if (conf->ieee80211ac) {
8899 int freq1 = 0, freq2 = 0;
8900 u8 chan, opclass;
8901
8902 if (ieee80211_freq_to_channel_ext(params.freq,
8903 conf->secondary_channel,
8904 conf->vht_oper_chwidth,
8905 &opclass, &chan) ==
8906 NUM_HOSTAPD_MODES) {
8907 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8908 ret = -1;
8909 goto out;
8910 }
8911
8912 if (conf->vht_oper_centr_freq_seg0_idx)
8913 freq1 = ieee80211_chan_to_freq(
8914 NULL, opclass,
8915 conf->vht_oper_centr_freq_seg0_idx);
8916
8917 if (conf->vht_oper_centr_freq_seg1_idx)
8918 freq2 = ieee80211_chan_to_freq(
8919 NULL, opclass,
8920 conf->vht_oper_centr_freq_seg1_idx);
8921
8922 if (freq1 < 0 || freq2 < 0) {
8923 wpa_dbg(wpa_s, MSG_DEBUG,
8924 "P2P CSA: Selected invalid VHT center freqs");
8925 ret = -1;
8926 goto out;
8927 }
8928
8929 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8930 csa_settings.freq_params.center_freq1 = freq1;
8931 csa_settings.freq_params.center_freq2 = freq2;
8932
8933 switch (conf->vht_oper_chwidth) {
8934 case VHT_CHANWIDTH_80MHZ:
8935 case VHT_CHANWIDTH_80P80MHZ:
8936 csa_settings.freq_params.bandwidth = 80;
8937 break;
8938 case VHT_CHANWIDTH_160MHZ:
8939 csa_settings.freq_params.bandwidth = 160;
8940 break;
8941 }
8942 }
8943
8944 ret = ap_switch_channel(wpa_s, &csa_settings);
8945out:
8946 current_ssid->frequency = old_freq;
8947 hostapd_config_free(conf);
8948 return ret;
8949}
8950
8951
8952static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8953{
8954 struct p2p_go_neg_results params;
8955 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8956
8957 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8958
8959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8960 current_ssid->frequency);
8961
8962 /* Stop the AP functionality */
8963 /* TODO: Should do this in a way that does not indicated to possible
8964 * P2P Clients in the group that the group is terminated. */
8965 wpa_supplicant_ap_deinit(wpa_s);
8966
8967 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008968 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008969 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8970 wpas_p2p_group_delete(wpa_s,
8971 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8972 return;
8973 }
8974 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8975 params.freq);
8976
8977 if (params.freq &&
8978 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8979 wpa_printf(MSG_DEBUG,
8980 "P2P: Selected freq (%u MHz) is not valid for P2P",
8981 params.freq);
8982 wpas_p2p_group_delete(wpa_s,
8983 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8984 return;
8985 }
8986
8987 /* Update the frequency */
8988 current_ssid->frequency = params.freq;
8989 wpa_s->connect_without_scan = current_ssid;
8990 wpa_s->reassociate = 1;
8991 wpa_s->disconnected = 0;
8992 wpa_supplicant_req_scan(wpa_s, 0, 0);
8993}
8994
8995
8996static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8997{
8998 struct wpa_supplicant *wpa_s = eloop_ctx;
8999
9000 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009001 return;
9002
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009003 wpas_p2p_go_update_common_freqs(wpa_s);
9004
9005 /* Do not move GO in the middle of a CSA */
9006 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9007 wpa_printf(MSG_DEBUG,
9008 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009009 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009010 }
9011
9012 /*
9013 * First, try a channel switch flow. If it is not supported or fails,
9014 * take down the GO and bring it up again.
9015 */
9016 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9017 wpas_p2p_move_go_no_csa(wpa_s);
9018}
9019
9020
9021static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9022{
9023 struct wpa_supplicant *wpa_s = eloop_ctx;
9024 struct wpa_used_freq_data *freqs = NULL;
9025 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009026
9027 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9028 if (!freqs)
9029 return;
9030
9031 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9032
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009033 /* Previous attempt to move a GO was not possible -- try again. */
9034 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9035 WPAS_P2P_CHANNEL_UPDATE_ANY);
9036
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009037 os_free(freqs);
9038}
9039
9040
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009041/*
9042 * Consider moving a GO from its currently used frequency:
9043 * 1. It is possible that due to regulatory consideration the frequency
9044 * can no longer be used and there is a need to evacuate the GO.
9045 * 2. It is possible that due to MCC considerations, it would be preferable
9046 * to move the GO to a channel that is currently used by some other
9047 * station interface.
9048 *
9049 * In case a frequency that became invalid is once again valid, cancel a
9050 * previously initiated GO frequency change.
9051 */
9052static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9053 struct wpa_used_freq_data *freqs,
9054 unsigned int num)
9055{
9056 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9057 unsigned int timeout;
9058 int freq;
9059
9060 wpas_p2p_go_update_common_freqs(wpa_s);
9061
9062 freq = wpa_s->current_ssid->frequency;
9063 for (i = 0, invalid_freq = 0; i < num; i++) {
9064 if (freqs[i].freq == freq) {
9065 flags = freqs[i].flags;
9066
9067 /* The channel is invalid, must change it */
9068 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
9069 wpa_dbg(wpa_s, MSG_DEBUG,
9070 "P2P: Freq=%d MHz no longer valid for GO",
9071 freq);
9072 invalid_freq = 1;
9073 }
9074 } else if (freqs[i].flags == 0) {
9075 /* Freq is not used by any other station interface */
9076 continue;
9077 } else if (!p2p_supported_freq(wpa_s->global->p2p,
9078 freqs[i].freq)) {
9079 /* Freq is not valid for P2P use cases */
9080 continue;
9081 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9082 P2P_GO_FREQ_MOVE_SCM) {
9083 policy_move = 1;
9084 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9085 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9086 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9087 policy_move = 1;
9088 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9089 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9090 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9091 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9092 policy_move = 1;
9093 } else if ((wpa_s->drv_flags &
9094 WPA_DRIVER_FLAGS_AP_CSA) &&
9095 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9096 u8 chan;
9097
9098 /*
9099 * We do not support CSA between bands, so move
9100 * GO only within the same band.
9101 */
9102 if (wpa_s->ap_iface->current_mode->mode ==
9103 ieee80211_freq_to_chan(freqs[i].freq,
9104 &chan))
9105 policy_move = 1;
9106 }
9107 }
9108 }
9109
9110 wpa_dbg(wpa_s, MSG_DEBUG,
9111 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9112 invalid_freq, policy_move, flags);
9113
9114 /*
9115 * The channel is valid, or we are going to have a policy move, so
9116 * cancel timeout.
9117 */
9118 if (!invalid_freq || policy_move) {
9119 wpa_dbg(wpa_s, MSG_DEBUG,
9120 "P2P: Cancel a GO move from freq=%d MHz", freq);
9121 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9122
9123 if (wpas_p2p_in_progress(wpa_s)) {
9124 wpa_dbg(wpa_s, MSG_DEBUG,
9125 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9126 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9127 wpa_s, NULL);
9128 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9129 wpas_p2p_reconsider_moving_go,
9130 wpa_s, NULL);
9131 return;
9132 }
9133 }
9134
9135 if (!invalid_freq && (!policy_move || flags != 0)) {
9136 wpa_dbg(wpa_s, MSG_DEBUG,
9137 "P2P: Not initiating a GO frequency change");
9138 return;
9139 }
9140
9141 /*
9142 * Do not consider moving GO if it is in the middle of a CSA. When the
9143 * CSA is finished this flow should be retriggered.
9144 */
9145 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9146 wpa_dbg(wpa_s, MSG_DEBUG,
9147 "P2P: Not initiating a GO frequency change - CSA is in progress");
9148 return;
9149 }
9150
9151 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9152 timeout = P2P_GO_FREQ_CHANGE_TIME;
9153 else
9154 timeout = 0;
9155
9156 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9157 freq, timeout);
9158 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9159 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9160}
9161
9162
9163static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9164 struct wpa_used_freq_data *freqs,
9165 unsigned int num,
9166 enum wpas_p2p_channel_update_trig trig)
9167{
9168 struct wpa_supplicant *ifs;
9169
9170 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9171 NULL);
9172
9173 /*
9174 * Travers all the radio interfaces, and for each GO interface, check
9175 * if there is a need to move the GO from the frequency it is using,
9176 * or in case the frequency is valid again, cancel the evacuation flow.
9177 */
9178 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9179 radio_list) {
9180 if (ifs->current_ssid == NULL ||
9181 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9182 continue;
9183
9184 /*
9185 * The GO was just started or completed channel switch, no need
9186 * to move it.
9187 */
9188 if (wpa_s == ifs &&
9189 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9190 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9191 wpa_dbg(wpa_s, MSG_DEBUG,
9192 "P2P: GO move - schedule re-consideration");
9193 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9194 wpas_p2p_reconsider_moving_go,
9195 wpa_s, NULL);
9196 continue;
9197 }
9198
9199 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9200 }
9201}
9202
9203
9204void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9205{
9206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9207 return;
9208
9209 wpas_p2p_update_channel_list(wpa_s,
9210 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9211}
9212
9213
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009214void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9215{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009216 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9217 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9218 "the management interface is being removed");
9219 wpas_p2p_deinit_global(wpa_s->global);
9220 }
9221}
9222
9223
9224void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9225{
9226 if (wpa_s->ap_iface->bss)
9227 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9228 wpas_p2p_group_deinit(wpa_s);
9229}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009230
9231
9232int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9233 unsigned int period, unsigned int interval,
9234 unsigned int count)
9235{
9236 struct p2p_data *p2p = wpa_s->global->p2p;
9237 u8 *device_types;
9238 size_t dev_types_len;
9239 struct wpabuf *buf;
9240 int ret;
9241
9242 if (wpa_s->p2p_lo_started) {
9243 wpa_dbg(wpa_s, MSG_DEBUG,
9244 "P2P Listen offload is already started");
9245 return 0;
9246 }
9247
9248 if (wpa_s->global->p2p == NULL ||
9249 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9250 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9251 return -1;
9252 }
9253
9254 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9255 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9256 freq);
9257 return -1;
9258 }
9259
9260 /* Get device type */
9261 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9262 WPS_DEV_TYPE_LEN;
9263 device_types = os_malloc(dev_types_len);
9264 if (!device_types)
9265 return -1;
9266 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9267 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9268 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9269
9270 /* Get Probe Response IE(s) */
9271 buf = p2p_build_probe_resp_template(p2p, freq);
9272 if (!buf) {
9273 os_free(device_types);
9274 return -1;
9275 }
9276
9277 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9278 device_types, dev_types_len,
9279 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9280 if (ret < 0)
9281 wpa_dbg(wpa_s, MSG_DEBUG,
9282 "P2P: Failed to start P2P listen offload");
9283
9284 os_free(device_types);
9285 wpabuf_free(buf);
9286
9287 if (ret == 0) {
9288 wpa_s->p2p_lo_started = 1;
9289
9290 /* Stop current P2P listen if any */
9291 wpas_stop_listen(wpa_s);
9292 }
9293
9294 return ret;
9295}
9296
9297
9298int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9299{
9300 int ret;
9301
9302 if (!wpa_s->p2p_lo_started)
9303 return 0;
9304
9305 ret = wpa_drv_p2p_lo_stop(wpa_s);
9306 if (ret < 0)
9307 wpa_dbg(wpa_s, MSG_DEBUG,
9308 "P2P: Failed to stop P2P listen offload");
9309
9310 wpa_s->p2p_lo_started = 0;
9311 return ret;
9312}