blob: c7e8ef4d9620182eef674e582667adc5bd2823bd [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Dmitry Shmidt203eadb2015-03-05 14:16:04 -080025#include "ap/dfs.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070026#include "eapol_supp/eapol_supp_sm.h"
27#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wpa_supplicant_i.h"
29#include "driver_i.h"
30#include "ap.h"
31#include "config_ssid.h"
32#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "notify.h"
34#include "scan.h"
35#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080036#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037#include "wps_supplicant.h"
38#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080039#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040
41
42/*
43 * How many times to try to scan to find the GO before giving up on join
44 * request.
45 */
46#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47
Dmitry Shmidt04949592012-07-19 12:16:46 -070048#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050/**
51 * Defines time interval in seconds when a GO needs to evacuate a frequency that
52 * it is currently using, but is no longer valid for P2P use cases.
53 */
54#define P2P_GO_FREQ_CHANGE_TIME 5
55
56/**
57 * Defines CSA parameters which are used when GO evacuates the no longer valid
58 * channel (and if the driver supports channel switch).
59 */
60#define P2P_GO_CSA_COUNT 7
61#define P2P_GO_CSA_BLOCK_TX 0
62
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080063#ifndef P2P_MAX_CLIENT_IDLE
64/*
65 * How many seconds to try to reconnect to the GO when connection in P2P client
66 * role has been lost.
67 */
68#define P2P_MAX_CLIENT_IDLE 10
69#endif /* P2P_MAX_CLIENT_IDLE */
70
Dmitry Shmidt04949592012-07-19 12:16:46 -070071#ifndef P2P_MAX_INITIAL_CONN_WAIT
72/*
73 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070074 * WPS provisioning step or after the re-invocation of a persistent group on a
75 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070076 */
77#define P2P_MAX_INITIAL_CONN_WAIT 10
78#endif /* P2P_MAX_INITIAL_CONN_WAIT */
79
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070080#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
81/*
82 * How many seconds to wait for initial 4-way handshake to get completed after
83 * WPS provisioning step on the GO. This controls the extra time the P2P
84 * operation is considered to be in progress (e.g., to delay other scans) after
85 * WPS provisioning has been completed on the GO during group formation.
86 */
87#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
88#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
89
Dmitry Shmidt56052862013-10-04 10:23:25 -070090#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
91/*
92 * How many seconds to wait for initial 4-way handshake to get completed after
93 * re-invocation of a persistent group on the GO when the client is expected
94 * to connect automatically (no user interaction).
95 */
96#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
97#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
98
Dmitry Shmidt34af3062013-07-11 10:46:32 -070099#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
100
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800101/*
102 * How many seconds to wait to re-attempt to move GOs, in case previous attempt
103 * was not possible.
104 */
105#define P2P_RECONSIDER_GO_MOVE_DELAY 30
106
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700107enum p2p_group_removal_reason {
108 P2P_GROUP_REMOVAL_UNKNOWN,
109 P2P_GROUP_REMOVAL_SILENT,
110 P2P_GROUP_REMOVAL_FORMATION_FAILED,
111 P2P_GROUP_REMOVAL_REQUESTED,
112 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
113 P2P_GROUP_REMOVAL_UNAVAILABLE,
114 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800115 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800116 P2P_GROUP_REMOVAL_FREQ_CONFLICT,
117 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700118};
119
Jouni Malinendc7b7132012-09-14 12:53:47 -0700120
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
122static struct wpa_supplicant *
123wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
124 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800125static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
126 const u8 *ssid, size_t ssid_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800127static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
128 int *force_freq, int *pref_freq, int go,
129 unsigned int *pref_freq_list,
130 unsigned int *num_pref_freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800131static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
132 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
134static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700135 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800136 int auto_join, int freq,
137 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700138static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
139static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
140static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
141static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800142static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
143 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800144static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800145static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
146 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800147static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800148static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700149static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700150static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800151static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
152 enum wpa_driver_if_type type);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -0700153static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
154 int already_deleted);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800155static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *freqs,
157 unsigned int num);
158static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx);
159static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq);
160static void
161wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *freqs, unsigned int num,
163 enum wpas_p2p_channel_update_trig trig);
164static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165
166
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167/*
168 * Get the number of concurrent channels that the HW can operate, but that are
169 * currently not in use by any of the wpa_supplicant interfaces.
170 */
171static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
172{
173 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
177 if (!freqs)
178 return -1;
179
180 num = get_shared_radio_freqs(wpa_s, freqs,
181 wpa_s->num_multichan_concurrent);
182 os_free(freqs);
183
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184 unused = wpa_s->num_multichan_concurrent - num;
185 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
186 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187}
188
189
190/*
191 * Get the frequencies that are currently in use by one or more of the virtual
192 * interfaces, and that are also valid for P2P operation.
193 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700194static unsigned int
195wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
196 struct wpa_used_freq_data *p2p_freqs,
197 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700198{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700199 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700200 unsigned int num, i, j;
201
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700202 freqs = os_calloc(wpa_s->num_multichan_concurrent,
203 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700205 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700207 num = get_shared_radio_freqs_data(wpa_s, freqs,
208 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700209
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700210 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700211
212 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700213 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700214 p2p_freqs[j++] = freqs[i];
215 }
216
217 os_free(freqs);
218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700219 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800220
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700221 return j;
222}
223
224
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700225static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
226 int freq)
227{
228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
229 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700230
231 /* Use the wpa_s used to control the P2P Device operation */
232 wpa_s = wpa_s->global->p2p_init_wpa_s;
233
234 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800235 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
236 wpas_p2p_num_unused_channels(wpa_s) > 0) {
237 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
238 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700239 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800240 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700241 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
242}
243
244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
246 struct wpa_scan_results *scan_res)
247{
248 size_t i;
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250 if (wpa_s->p2p_scan_work) {
251 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
252 wpa_s->p2p_scan_work = NULL;
253 radio_work_done(work);
254 }
255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
257 return;
258
259 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
260 (int) scan_res->num);
261
262 for (i = 0; i < scan_res->num; i++) {
263 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700265 const u8 *ies;
266 size_t ies_len;
267
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800268 time_tmp_age.sec = bss->age / 1000;
269 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700271
272 ies = (const u8 *) (bss + 1);
273 ies_len = bss->ie_len;
274 if (bss->beacon_ie_len > 0 &&
275 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
276 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
277 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
278 MACSTR, MAC2STR(bss->bssid));
279 ies = ies + ies_len;
280 ies_len = bss->beacon_ie_len;
281 }
282
283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800285 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700286 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 break;
288 }
289
290 p2p_scan_res_handled(wpa_s->global->p2p);
291}
292
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
295{
296 struct wpa_supplicant *wpa_s = work->wpa_s;
297 struct wpa_driver_scan_params *params = work->ctx;
298 int ret;
299
300 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800301 if (!work->started) {
302 wpa_scan_free_params(params);
303 return;
304 }
305
306 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 return;
308 }
309
310 ret = wpa_drv_scan(wpa_s, params);
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800311 if (ret == 0)
312 wpa_s->curr_scan_cookie = params->scan_cookie;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800313 wpa_scan_free_params(params);
314 work->ctx = NULL;
315 if (ret) {
316 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800317 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 return;
319 }
320
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800321 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 os_get_reltime(&wpa_s->scan_trigger_time);
323 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
324 wpa_s->own_scan_requested = 1;
325 wpa_s->p2p_scan_work = work;
326}
327
328
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800329static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
330 int freq)
331{
332 if (wpa_s->global->p2p_24ghz_social_channels &&
333 (freq == 2412 || freq == 2437 || freq == 2462)) {
334 /*
335 * Search all social channels regardless of whether these have
336 * been disabled for P2P operating channel use to avoid missing
337 * peers.
338 */
339 return 1;
340 }
341 return p2p_supported_freq(wpa_s->global->p2p, freq);
342}
343
344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
346 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700347 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348{
349 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800350 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700352 unsigned int num_channels = 0;
353 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700355 u8 *n, i;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800356 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
358 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
359 return -1;
360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361 if (wpa_s->p2p_scan_work) {
362 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
363 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700364 }
365
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800366 params = os_zalloc(sizeof(*params));
367 if (params == NULL)
368 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
370 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800371 params->num_ssids = 1;
372 n = os_malloc(P2P_WILDCARD_SSID_LEN);
373 if (n == NULL)
374 goto fail;
375 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
376 params->ssids[0].ssid = n;
377 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378
379 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
381 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 num_req_dev_types, req_dev_types);
383 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800384 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 switch (type) {
387 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700388 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
389 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800390 if (params->freqs == NULL)
391 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700392 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800393 if (wpas_p2p_search_social_channel(
394 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700395 params->freqs[num_channels++] =
396 social_channels_freq[i];
397 }
398 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 break;
400 case P2P_SCAN_FULL:
401 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800402 case P2P_SCAN_SPECIFIC:
403 params->freqs = os_calloc(2, sizeof(int));
404 if (params->freqs == NULL)
405 goto fail;
406 params->freqs[0] = freq;
407 params->freqs[1] = 0;
408 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700410 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
411 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800412 if (params->freqs == NULL)
413 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700414 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800415 if (wpas_p2p_search_social_channel(
416 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700417 params->freqs[num_channels++] =
418 social_channels_freq[i];
419 }
420 if (p2p_supported_freq(wpa_s->global->p2p, freq))
421 params->freqs[num_channels++] = freq;
422 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 break;
424 }
425
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800426 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
427 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
428 if (ies == NULL) {
429 wpabuf_free(wps_ie);
430 goto fail;
431 }
432 wpabuf_put_buf(ies, wps_ie);
433 wpabuf_free(wps_ie);
434
435 bands = wpas_get_bands(wpa_s, params->freqs);
436 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id, bands);
437
438 params->p2p_probe = 1;
439 n = os_malloc(wpabuf_len(ies));
440 if (n == NULL) {
441 wpabuf_free(ies);
442 goto fail;
443 }
444 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
445 params->extra_ies = n;
446 params->extra_ies_len = wpabuf_len(ies);
447 wpabuf_free(ies);
448
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800449 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800450 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
451 params) < 0)
452 goto fail;
453 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800455fail:
456 wpa_scan_free_params(params);
457 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458}
459
460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
462{
463 switch (p2p_group_interface) {
464 case P2P_GROUP_INTERFACE_PENDING:
465 return WPA_IF_P2P_GROUP;
466 case P2P_GROUP_INTERFACE_GO:
467 return WPA_IF_P2P_GO;
468 case P2P_GROUP_INTERFACE_CLIENT:
469 return WPA_IF_P2P_CLIENT;
470 }
471
472 return WPA_IF_P2P_GROUP;
473}
474
475
476static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
477 const u8 *ssid,
478 size_t ssid_len, int *go)
479{
480 struct wpa_ssid *s;
481
482 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
483 for (s = wpa_s->conf->ssid; s; s = s->next) {
484 if (s->disabled != 0 || !s->p2p_group ||
485 s->ssid_len != ssid_len ||
486 os_memcmp(ssid, s->ssid, ssid_len) != 0)
487 continue;
488 if (s->mode == WPAS_MODE_P2P_GO &&
489 s != wpa_s->current_ssid)
490 continue;
491 if (go)
492 *go = s->mode == WPAS_MODE_P2P_GO;
493 return wpa_s;
494 }
495 }
496
497 return NULL;
498}
499
500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800501static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
502{
503 struct wpa_supplicant *wpa_s = eloop_ctx;
504 wpa_printf(MSG_DEBUG,
505 "P2P: Complete previously requested removal of %s",
506 wpa_s->ifname);
507 wpas_p2p_disconnect(wpa_s);
508}
509
510
511static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
512 struct wpa_supplicant *calling_wpa_s)
513{
514 if (calling_wpa_s == wpa_s && wpa_s &&
515 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
516 /*
517 * The calling wpa_s instance is going to be removed. Do that
518 * from an eloop callback to keep the instance available until
519 * the caller has returned. This my be needed, e.g., to provide
520 * control interface responses on the per-interface socket.
521 */
522 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
523 wpa_s, NULL) < 0)
524 return -1;
525 return 0;
526 }
527
528 return wpas_p2p_disconnect(wpa_s);
529}
530
531
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800532/* Determine total number of clients in active groups where we are the GO */
533static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
534{
535 unsigned int count = 0;
536 struct wpa_ssid *s;
537
538 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
539 for (s = wpa_s->conf->ssid; s; s = s->next) {
540 wpa_printf(MSG_DEBUG,
541 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
542 wpa_s, s, s->disabled, s->p2p_group,
543 s->mode);
544 if (!s->disabled && s->p2p_group &&
545 s->mode == WPAS_MODE_P2P_GO) {
546 count += p2p_get_group_num_members(
547 wpa_s->p2p_group);
548 }
549 }
550 }
551
552 return count;
553}
554
555
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800556static unsigned int p2p_is_active_persistent_group(struct wpa_supplicant *wpa_s)
557{
558 return !wpa_s->p2p_mgmt && wpa_s->current_ssid &&
559 !wpa_s->current_ssid->disabled &&
560 wpa_s->current_ssid->p2p_group &&
561 wpa_s->current_ssid->p2p_persistent_group;
562}
563
564
565static unsigned int p2p_is_active_persistent_go(struct wpa_supplicant *wpa_s)
566{
567 return p2p_is_active_persistent_group(wpa_s) &&
568 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO;
569}
570
571
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800572/* Find an interface for a P2P group where we are the GO */
573static struct wpa_supplicant *
574wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
575{
576 struct wpa_supplicant *save = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800577
578 if (!wpa_s)
579 return NULL;
580
581 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800582 if (!p2p_is_active_persistent_go(wpa_s))
583 continue;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800584
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800585 /* Prefer a group with connected clients */
586 if (p2p_get_group_num_members(wpa_s->p2p_group))
587 return wpa_s;
588 save = wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800589 }
590
591 /* No group with connected clients, so pick the one without (if any) */
592 return save;
593}
594
595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800596static unsigned int p2p_is_active_persistent_cli(struct wpa_supplicant *wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800597{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800598 return p2p_is_active_persistent_group(wpa_s) &&
599 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
600}
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800601
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800602
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800603/* Find an interface for a P2P group where we are the P2P Client */
604static struct wpa_supplicant *
605wpas_p2p_get_cli_group(struct wpa_supplicant *wpa_s)
606{
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800607 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800608 if (p2p_is_active_persistent_cli(wpa_s))
609 return wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800610 }
611
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800612 return NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800613}
614
615
616/* Find a persistent group where we are the GO */
617static struct wpa_ssid *
618wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
619{
620 struct wpa_ssid *s;
621
622 for (s = wpa_s->conf->ssid; s; s = s->next) {
623 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
624 return s;
625 }
626
627 return NULL;
628}
629
630
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800631static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role,
632 unsigned int *force_freq,
633 unsigned int *pref_freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800634{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800636 struct wpa_ssid *s;
637 u8 conncap = P2PS_SETUP_NONE;
638 unsigned int owned_members = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800639 struct wpa_supplicant *go_wpa_s, *cli_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800640 struct wpa_ssid *persistent_go;
641 int p2p_no_group_iface;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800642 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800643
644 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
645
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800646 if (force_freq)
647 *force_freq = 0;
648 if (pref_freq)
649 *pref_freq = 0;
650
651 size = P2P_MAX_PREF_CHANNELS;
652 if (force_freq && pref_freq &&
653 !wpas_p2p_setup_freqs(wpa_s, 0, (int *) force_freq,
654 (int *) pref_freq, 0, pref_freq_list, &size))
655 wpas_p2p_set_own_freq_preference(wpa_s,
656 *force_freq ? *force_freq :
657 *pref_freq);
658
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800659 /*
660 * For non-concurrent capable devices:
661 * If persistent_go, then no new.
662 * If GO, then no client.
663 * If client, then no GO.
664 */
665 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800666 if (go_wpa_s)
667 owned_members = p2p_get_group_num_members(go_wpa_s->p2p_group);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800668 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800669 p2p_no_group_iface = !wpas_p2p_create_iface(wpa_s);
670 cli_wpa_s = wpas_p2p_get_cli_group(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800671
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800672 wpa_printf(MSG_DEBUG,
673 "P2P: GO(iface)=%p members=%u CLI(iface)=%p persistent(ssid)=%p",
674 go_wpa_s, owned_members, cli_wpa_s, persistent_go);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800675
676 /* If not concurrent, restrict our choices */
677 if (p2p_no_group_iface) {
678 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
679
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800680 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800681 return P2PS_SETUP_NONE;
682
683 if (go_wpa_s) {
684 if (role == P2PS_SETUP_CLIENT ||
685 incoming == P2PS_SETUP_GROUP_OWNER ||
686 p2p_client_limit_reached(go_wpa_s->p2p_group))
687 return P2PS_SETUP_NONE;
688
689 return P2PS_SETUP_GROUP_OWNER;
690 }
691
692 if (persistent_go) {
693 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
694 if (!incoming)
695 return P2PS_SETUP_GROUP_OWNER |
696 P2PS_SETUP_CLIENT;
697 if (incoming == P2PS_SETUP_NEW) {
698 u8 r;
699
700 if (os_get_random(&r, sizeof(r)) < 0 ||
701 (r & 1))
702 return P2PS_SETUP_CLIENT;
703 return P2PS_SETUP_GROUP_OWNER;
704 }
705 }
706 }
707 }
708
709 /* If a required role has been specified, handle it here */
710 if (role && role != P2PS_SETUP_NEW) {
711 switch (incoming) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800712 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
713 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
714 /*
715 * Peer has an active GO, so if the role allows it and
716 * we do not have any active roles, become client.
717 */
718 if ((role & P2PS_SETUP_CLIENT) && !go_wpa_s &&
719 !cli_wpa_s)
720 return P2PS_SETUP_CLIENT;
721
722 /* fall through */
723
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800724 case P2PS_SETUP_NONE:
725 case P2PS_SETUP_NEW:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800726 conncap = role;
727 goto grp_owner;
728
729 case P2PS_SETUP_GROUP_OWNER:
730 /*
731 * Must be a complimentary role - cannot be a client to
732 * more than one peer.
733 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800734 if (incoming == role || cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800735 return P2PS_SETUP_NONE;
736
737 return P2PS_SETUP_CLIENT;
738
739 case P2PS_SETUP_CLIENT:
740 /* Must be a complimentary role */
741 if (incoming != role) {
742 conncap = P2PS_SETUP_GROUP_OWNER;
743 goto grp_owner;
744 }
745
746 default:
747 return P2PS_SETUP_NONE;
748 }
749 }
750
751 /*
752 * For now, we only will support ownership of one group, and being a
753 * client of one group. Therefore, if we have either an existing GO
754 * group, or an existing client group, we will not do a new GO
755 * negotiation, but rather try to re-use the existing groups.
756 */
757 switch (incoming) {
758 case P2PS_SETUP_NONE:
759 case P2PS_SETUP_NEW:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800760 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800761 conncap = P2PS_SETUP_GROUP_OWNER;
762 else if (!owned_members)
763 conncap = P2PS_SETUP_NEW;
764 else if (incoming == P2PS_SETUP_NONE)
765 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
766 else
767 conncap = P2PS_SETUP_CLIENT;
768 break;
769
770 case P2PS_SETUP_CLIENT:
771 conncap = P2PS_SETUP_GROUP_OWNER;
772 break;
773
774 case P2PS_SETUP_GROUP_OWNER:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800775 if (!cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800776 conncap = P2PS_SETUP_CLIENT;
777 break;
778
779 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
780 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800781 if (cli_wpa_s)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800782 conncap = P2PS_SETUP_GROUP_OWNER;
783 else {
784 u8 r;
785
786 if (os_get_random(&r, sizeof(r)) < 0 ||
787 (r & 1))
788 conncap = P2PS_SETUP_CLIENT;
789 else
790 conncap = P2PS_SETUP_GROUP_OWNER;
791 }
792 break;
793
794 default:
795 return P2PS_SETUP_NONE;
796 }
797
798grp_owner:
799 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
800 (!incoming && (conncap & P2PS_SETUP_NEW))) {
801 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
802 conncap &= ~P2PS_SETUP_GROUP_OWNER;
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800803
804 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800805 if (!s && !go_wpa_s && p2p_no_group_iface) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800806 p2p_set_intended_addr(wpa_s->global->p2p,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800807 wpa_s->p2p_mgmt ?
808 wpa_s->parent->own_addr :
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800809 wpa_s->own_addr);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800810 } else if (!s && !go_wpa_s) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800811 if (wpas_p2p_add_group_interface(wpa_s,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800812 WPA_IF_P2P_GROUP) < 0) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800813 wpa_printf(MSG_ERROR,
814 "P2P: Failed to allocate a new interface for the group");
815 return P2PS_SETUP_NONE;
816 }
817 wpa_s->global->pending_group_iface_for_p2ps = 1;
818 p2p_set_intended_addr(wpa_s->global->p2p,
819 wpa_s->pending_interface_addr);
820 }
821 }
822
823 return conncap;
824}
825
826
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700827static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
828 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829{
830 struct wpa_ssid *ssid;
831 char *gtype;
832 const char *reason;
833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 ssid = wpa_s->current_ssid;
835 if (ssid == NULL) {
836 /*
837 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700838 * pending P2P group interface waiting for provisioning or a
839 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 */
841 ssid = wpa_s->conf->ssid;
842 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700843 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 break;
845 ssid = ssid->next;
846 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300847 if (ssid == NULL &&
848 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
849 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700850 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
851 "not found");
852 return -1;
853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 }
855 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
856 gtype = "GO";
857 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
858 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
859 wpa_s->reassociate = 0;
860 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 gtype = "client";
862 } else
863 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700864
865 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
866 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
867
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800868 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700869 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
871 wpa_s, NULL)) {
872 wpa_printf(MSG_DEBUG,
873 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
874 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
875 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
876 wpa_s, NULL);
877 }
878 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 if (wpa_s->cross_connect_in_use) {
881 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800882 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700883 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
884 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700886 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 case P2P_GROUP_REMOVAL_REQUESTED:
888 reason = " reason=REQUESTED";
889 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700890 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
891 reason = " reason=FORMATION_FAILED";
892 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
894 reason = " reason=IDLE";
895 break;
896 case P2P_GROUP_REMOVAL_UNAVAILABLE:
897 reason = " reason=UNAVAILABLE";
898 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700899 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
900 reason = " reason=GO_ENDING_SESSION";
901 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700902 case P2P_GROUP_REMOVAL_PSK_FAILURE:
903 reason = " reason=PSK_FAILURE";
904 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800905 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
906 reason = " reason=FREQ_CONFLICT";
907 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908 default:
909 reason = "";
910 break;
911 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700912 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800913 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700914 P2P_EVENT_GROUP_REMOVED "%s %s%s",
915 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700916 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800918 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
919 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700920 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
921 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800922 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800923 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800924 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
925 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700926 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800927 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700928 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700929
Dmitry Shmidt15907092014-03-25 10:42:57 -0700930 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800931 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
932 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, wpa_s, NULL);
Dmitry Shmidt15907092014-03-25 10:42:57 -0700933
Dmitry Shmidt96571392013-10-14 12:54:46 -0700934 /*
935 * Make sure wait for the first client does not remain active after the
936 * group has been removed.
937 */
938 wpa_s->global->p2p_go_wait_client.sec = 0;
939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
941 struct wpa_global *global;
942 char *ifname;
943 enum wpa_driver_if_type type;
944 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
945 wpa_s->ifname);
946 global = wpa_s->global;
947 ifname = os_strdup(wpa_s->ifname);
948 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800949 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700950 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 wpa_s = global->ifaces;
952 if (wpa_s && ifname)
953 wpa_drv_if_remove(wpa_s, type, ifname);
954 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300955 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 }
957
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800958 /*
959 * The primary interface was used for P2P group operations, so
960 * need to reset its p2pdev.
961 */
962 wpa_s->p2pdev = wpa_s->parent;
963
Dmitry Shmidt96571392013-10-14 12:54:46 -0700964 if (!wpa_s->p2p_go_group_formation_completed) {
965 wpa_s->global->p2p_group_formation = NULL;
966 wpa_s->p2p_in_provisioning = 0;
967 }
968
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800969 wpa_s->show_group_started = 0;
970 os_free(wpa_s->go_params);
971 wpa_s->go_params = NULL;
972
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800973 os_free(wpa_s->p2p_group_common_freqs);
974 wpa_s->p2p_group_common_freqs = NULL;
975 wpa_s->p2p_group_common_freqs_num = 0;
976
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800977 wpa_s->waiting_presence_resp = 0;
978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
980 if (ssid && (ssid->p2p_group ||
981 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
982 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
983 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700984 if (ssid == wpa_s->current_ssid) {
985 wpa_sm_set_config(wpa_s->wpa, NULL);
986 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700988 }
989 /*
990 * Networks objects created during any P2P activities are not
991 * exposed out as they might/will confuse certain non-P2P aware
992 * applications since these network objects won't behave like
993 * regular ones.
994 *
995 * Likewise, we don't send out network removed signals for such
996 * network objects.
997 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 wpa_config_remove_network(wpa_s->conf, id);
999 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001000 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 } else {
1002 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
1003 "found");
1004 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 if (wpa_s->ap_iface)
1006 wpa_supplicant_ap_deinit(wpa_s);
1007 else
1008 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001009
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001010 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
1011
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001012 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013}
1014
1015
1016static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
1017 u8 *go_dev_addr,
1018 const u8 *ssid, size_t ssid_len)
1019{
1020 struct wpa_bss *bss;
1021 const u8 *bssid;
1022 struct wpabuf *p2p;
1023 u8 group_capab;
1024 const u8 *addr;
1025
1026 if (wpa_s->go_params)
1027 bssid = wpa_s->go_params->peer_interface_addr;
1028 else
1029 bssid = wpa_s->bssid;
1030
1031 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001032 if (bss == NULL && wpa_s->go_params &&
1033 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
1034 bss = wpa_bss_get_p2p_dev_addr(
1035 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036 if (bss == NULL) {
1037 u8 iface_addr[ETH_ALEN];
1038 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
1039 iface_addr) == 0)
1040 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
1041 }
1042 if (bss == NULL) {
1043 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1044 "group is persistent - BSS " MACSTR " not found",
1045 MAC2STR(bssid));
1046 return 0;
1047 }
1048
1049 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -07001050 if (p2p == NULL)
1051 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
1052 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 if (p2p == NULL) {
1054 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
1055 "group is persistent - BSS " MACSTR
1056 " did not include P2P IE", MAC2STR(bssid));
1057 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
1058 (u8 *) (bss + 1), bss->ie_len);
1059 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
1060 ((u8 *) bss + 1) + bss->ie_len,
1061 bss->beacon_ie_len);
1062 return 0;
1063 }
1064
1065 group_capab = p2p_get_group_capab(p2p);
1066 addr = p2p_get_go_dev_addr(p2p);
1067 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1068 "group_capab=0x%x", group_capab);
1069 if (addr) {
1070 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1071 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1072 MAC2STR(addr));
1073 } else
1074 os_memset(go_dev_addr, 0, ETH_ALEN);
1075 wpabuf_free(p2p);
1076
1077 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1078 "go_dev_addr=" MACSTR,
1079 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1080
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -07001081 return !!(group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082}
1083
1084
Jouni Malinen75ecf522011-06-27 15:19:46 -07001085static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1086 struct wpa_ssid *ssid,
1087 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088{
1089 struct wpa_ssid *s;
1090 int changed = 0;
1091
1092 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1093 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1094 for (s = wpa_s->conf->ssid; s; s = s->next) {
1095 if (s->disabled == 2 &&
1096 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1097 s->ssid_len == ssid->ssid_len &&
1098 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1099 break;
1100 }
1101
1102 if (s) {
1103 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1104 "entry");
1105 if (ssid->passphrase && !s->passphrase)
1106 changed = 1;
1107 else if (ssid->passphrase && s->passphrase &&
1108 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1109 changed = 1;
1110 } else {
1111 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1112 "entry");
1113 changed = 1;
1114 s = wpa_config_add_network(wpa_s->conf);
1115 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001116 return -1;
1117
1118 /*
1119 * Instead of network_added we emit persistent_group_added
1120 * notification. Also to keep the defense checks in
1121 * persistent_group obj registration method, we set the
1122 * relevant flags in s to designate it as a persistent group.
1123 */
1124 s->p2p_group = 1;
1125 s->p2p_persistent_group = 1;
1126 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_config_set_network_defaults(s);
1128 }
1129
1130 s->p2p_group = 1;
1131 s->p2p_persistent_group = 1;
1132 s->disabled = 2;
1133 s->bssid_set = 1;
1134 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1135 s->mode = ssid->mode;
1136 s->auth_alg = WPA_AUTH_ALG_OPEN;
1137 s->key_mgmt = WPA_KEY_MGMT_PSK;
1138 s->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07001139 s->pbss = ssid->pbss;
1140 s->pairwise_cipher = ssid->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 s->export_keys = 1;
1142 if (ssid->passphrase) {
1143 os_free(s->passphrase);
1144 s->passphrase = os_strdup(ssid->passphrase);
1145 }
1146 if (ssid->psk_set) {
1147 s->psk_set = 1;
1148 os_memcpy(s->psk, ssid->psk, 32);
1149 }
1150 if (s->passphrase && !s->psk_set)
1151 wpa_config_update_psk(s);
1152 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1153 os_free(s->ssid);
1154 s->ssid = os_malloc(ssid->ssid_len);
1155 }
1156 if (s->ssid) {
1157 s->ssid_len = ssid->ssid_len;
1158 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1159 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001160 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1161 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1162 wpa_s->global->add_psk = NULL;
1163 changed = 1;
1164 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166 if (changed && wpa_s->conf->update_config &&
1167 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1168 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1169 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001170
1171 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172}
1173
1174
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001175static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1176 const u8 *addr)
1177{
1178 struct wpa_ssid *ssid, *s;
1179 u8 *n;
1180 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001181 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001182 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183
1184 ssid = wpa_s->current_ssid;
1185 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1186 !ssid->p2p_persistent_group)
1187 return;
1188
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001189 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1191 continue;
1192
1193 if (s->ssid_len == ssid->ssid_len &&
1194 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1195 break;
1196 }
1197
1198 if (s == NULL)
1199 return;
1200
1201 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001202 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001203 ETH_ALEN) != 0)
1204 continue;
1205
1206 if (i == s->num_p2p_clients - 1)
1207 return; /* already the most recent entry */
1208
1209 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001210 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1211 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1212 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001213 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001214 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1215 ETH_ALEN);
1216 os_memset(s->p2p_client_list +
1217 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1218 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001219 found = 1;
1220 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 }
1222
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001223 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1224 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001225 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001226 if (n == NULL)
1227 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001228 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1229 ETH_ALEN);
1230 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1231 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001232 s->p2p_client_list = n;
1233 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001234 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001235 /* Not enough room for an additional entry - drop the oldest
1236 * entry */
1237 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001238 s->p2p_client_list + 2 * ETH_ALEN,
1239 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001240 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001241 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001242 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001243 os_memset(s->p2p_client_list +
1244 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1245 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001248 if (p2p_wpa_s->conf->update_config &&
1249 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001250 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001251}
1252
1253
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001254static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1255 int go, struct wpa_ssid *ssid, int freq,
1256 const u8 *psk, const char *passphrase,
1257 const u8 *go_dev_addr, int persistent,
1258 const char *extra)
1259{
1260 const char *ssid_txt;
1261 char psk_txt[65];
1262
1263 if (psk)
1264 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1265 else
1266 psk_txt[0] = '\0';
1267
1268 if (ssid)
1269 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1270 else
1271 ssid_txt = "";
1272
1273 if (passphrase && passphrase[0] == '\0')
1274 passphrase = NULL;
1275
1276 /*
1277 * Include PSK/passphrase only in the control interface message and
1278 * leave it out from the debug log entry.
1279 */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001280 wpa_msg_global_ctrl(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001281 P2P_EVENT_GROUP_STARTED
1282 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1283 MACSTR "%s%s",
1284 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1285 psk ? " psk=" : "", psk_txt,
1286 passphrase ? " passphrase=\"" : "",
1287 passphrase ? passphrase : "",
1288 passphrase ? "\"" : "",
1289 MAC2STR(go_dev_addr),
1290 persistent ? " [PERSISTENT]" : "", extra);
1291 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1292 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1293 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1294 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1295 extra);
1296}
1297
1298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001300 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301{
1302 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 int client;
1304 int persistent;
1305 u8 go_dev_addr[ETH_ALEN];
1306
1307 /*
1308 * This callback is likely called for the main interface. Update wpa_s
1309 * to use the group interface if a new interface was created for the
1310 * group.
1311 */
1312 if (wpa_s->global->p2p_group_formation)
1313 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001314 if (wpa_s->p2p_go_group_formation_completed) {
1315 wpa_s->global->p2p_group_formation = NULL;
1316 wpa_s->p2p_in_provisioning = 0;
1317 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001318 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001319 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320
1321 if (!success) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001322 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001323 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001324 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001325 if (already_deleted)
1326 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001327 wpas_p2p_group_delete(wpa_s,
1328 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return;
1330 }
1331
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001332 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001333 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334
1335 ssid = wpa_s->current_ssid;
1336 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1337 ssid->mode = WPAS_MODE_P2P_GO;
1338 p2p_group_notif_formation_done(wpa_s->p2p_group);
1339 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1340 }
1341
1342 persistent = 0;
1343 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 client = ssid->mode == WPAS_MODE_INFRA;
1345 if (ssid->mode == WPAS_MODE_P2P_GO) {
1346 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001347 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1348 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 } else
1350 persistent = wpas_p2p_persistent_group(wpa_s,
1351 go_dev_addr,
1352 ssid->ssid,
1353 ssid->ssid_len);
1354 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 client = wpa_s->p2p_group_interface ==
1356 P2P_GROUP_INTERFACE_CLIENT;
1357 os_memset(go_dev_addr, 0, ETH_ALEN);
1358 }
1359
1360 wpa_s->show_group_started = 0;
1361 if (client) {
1362 /*
1363 * Indicate event only after successfully completed 4-way
1364 * handshake, i.e., when the interface is ready for data
1365 * packets.
1366 */
1367 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001369 wpas_p2p_group_started(wpa_s, 1, ssid,
1370 ssid ? ssid->frequency : 0,
1371 ssid && ssid->passphrase == NULL &&
1372 ssid->psk_set ? ssid->psk : NULL,
1373 ssid ? ssid->passphrase : NULL,
1374 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375 wpas_p2p_cross_connect_setup(wpa_s);
1376 wpas_p2p_set_group_idle_timeout(wpa_s);
1377 }
1378
1379 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001380 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
1381 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001382 else {
1383 os_free(wpa_s->global->add_psk);
1384 wpa_s->global->add_psk = NULL;
1385 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001386
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001387 if (!client) {
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001388 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 0, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001389 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001390 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391}
1392
1393
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001394struct send_action_work {
1395 unsigned int freq;
1396 u8 dst[ETH_ALEN];
1397 u8 src[ETH_ALEN];
1398 u8 bssid[ETH_ALEN];
1399 size_t len;
1400 unsigned int wait_time;
1401 u8 buf[0];
1402};
1403
1404
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001405static void wpas_p2p_free_send_action_work(struct wpa_supplicant *wpa_s)
1406{
1407 struct send_action_work *awork = wpa_s->p2p_send_action_work->ctx;
1408
1409 wpa_printf(MSG_DEBUG,
1410 "P2P: Free Action frame radio work @%p (freq=%u dst="
1411 MACSTR " src=" MACSTR " bssid=" MACSTR " wait_time=%u)",
1412 wpa_s->p2p_send_action_work, awork->freq,
1413 MAC2STR(awork->dst), MAC2STR(awork->src),
1414 MAC2STR(awork->bssid), awork->wait_time);
1415 wpa_hexdump(MSG_DEBUG, "P2P: Freeing pending Action frame",
1416 awork->buf, awork->len);
1417 os_free(awork);
1418 wpa_s->p2p_send_action_work->ctx = NULL;
1419 radio_work_done(wpa_s->p2p_send_action_work);
1420 wpa_s->p2p_send_action_work = NULL;
1421}
1422
1423
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001424static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1425 void *timeout_ctx)
1426{
1427 struct wpa_supplicant *wpa_s = eloop_ctx;
1428
1429 if (!wpa_s->p2p_send_action_work)
1430 return;
1431
1432 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001433 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001434}
1435
1436
Dmitry Shmidt03658832014-08-13 11:03:49 -07001437static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001439 if (wpa_s->p2p_send_action_work) {
1440 struct send_action_work *awork;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001441
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001442 awork = wpa_s->p2p_send_action_work->ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001443 wpa_printf(MSG_DEBUG,
1444 "P2P: Clear Action TX work @%p (wait_time=%u)",
1445 wpa_s->p2p_send_action_work, awork->wait_time);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001446 if (awork->wait_time == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001447 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001448 } else {
1449 /*
1450 * In theory, this should not be needed, but number of
1451 * places in the P2P code is still using non-zero wait
1452 * time for the last Action frame in the sequence and
1453 * some of these do not call send_action_done().
1454 */
1455 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1456 wpa_s, NULL);
1457 eloop_register_timeout(
1458 0, awork->wait_time * 1000,
1459 wpas_p2p_send_action_work_timeout,
1460 wpa_s, NULL);
1461 }
1462 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001463}
1464
1465
1466static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1467 unsigned int freq,
1468 const u8 *dst, const u8 *src,
1469 const u8 *bssid,
1470 const u8 *data, size_t data_len,
1471 enum offchannel_send_action_result
1472 result)
1473{
1474 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1475
1476 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1479 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001481 switch (result) {
1482 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1483 res = P2P_SEND_ACTION_SUCCESS;
1484 break;
1485 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1486 res = P2P_SEND_ACTION_NO_ACK;
1487 break;
1488 case OFFCHANNEL_SEND_ACTION_FAILED:
1489 res = P2P_SEND_ACTION_FAILED;
1490 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001491 }
1492
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001493 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001495 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1496 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001497 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001498 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1499 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001501 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1502 "during p2p_connect-auto");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001503 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001504 P2P_EVENT_FALLBACK_TO_GO_NEG
1505 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001506 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1507 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001508 }
1509}
1510
1511
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001512static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1513{
1514 struct wpa_supplicant *wpa_s = work->wpa_s;
1515 struct send_action_work *awork = work->ctx;
1516
1517 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001518 if (work->started) {
1519 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1520 wpa_s, NULL);
1521 wpa_s->p2p_send_action_work = NULL;
1522 offchannel_send_action_done(wpa_s);
1523 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524 os_free(awork);
1525 return;
1526 }
1527
1528 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1529 awork->bssid, awork->buf, awork->len,
1530 awork->wait_time,
1531 wpas_p2p_send_action_tx_status, 1) < 0) {
1532 os_free(awork);
1533 radio_work_done(work);
1534 return;
1535 }
1536 wpa_s->p2p_send_action_work = work;
1537}
1538
1539
1540static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1541 unsigned int freq, const u8 *dst,
1542 const u8 *src, const u8 *bssid, const u8 *buf,
1543 size_t len, unsigned int wait_time)
1544{
1545 struct send_action_work *awork;
1546
1547 if (wpa_s->p2p_send_action_work) {
1548 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1549 return -1;
1550 }
1551
1552 awork = os_zalloc(sizeof(*awork) + len);
1553 if (awork == NULL)
1554 return -1;
1555
1556 awork->freq = freq;
1557 os_memcpy(awork->dst, dst, ETH_ALEN);
1558 os_memcpy(awork->src, src, ETH_ALEN);
1559 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1560 awork->len = len;
1561 awork->wait_time = wait_time;
1562 os_memcpy(awork->buf, buf, len);
1563
1564 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1565 wpas_send_action_cb, awork) < 0) {
1566 os_free(awork);
1567 return -1;
1568 }
1569
1570 return 0;
1571}
1572
1573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1575 const u8 *src, const u8 *bssid, const u8 *buf,
1576 size_t len, unsigned int wait_time)
1577{
1578 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001579 int listen_freq = -1, send_freq = -1;
1580
1581 if (wpa_s->p2p_listen_work)
1582 listen_freq = wpa_s->p2p_listen_work->freq;
1583 if (wpa_s->p2p_send_action_work)
1584 send_freq = wpa_s->p2p_send_action_work->freq;
1585 if (listen_freq != (int) freq && send_freq != (int) freq) {
1586 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1587 listen_freq, send_freq);
1588 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1589 len, wait_time);
1590 }
1591
1592 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1594 wait_time,
1595 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596}
1597
1598
1599static void wpas_send_action_done(void *ctx)
1600{
1601 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001602
1603 if (wpa_s->p2p_send_action_work) {
1604 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1605 wpa_s, NULL);
1606 os_free(wpa_s->p2p_send_action_work->ctx);
1607 radio_work_done(wpa_s->p2p_send_action_work);
1608 wpa_s->p2p_send_action_work = NULL;
1609 }
1610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001611 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612}
1613
1614
1615static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1616 struct p2p_go_neg_results *params)
1617{
1618 if (wpa_s->go_params == NULL) {
1619 wpa_s->go_params = os_malloc(sizeof(*params));
1620 if (wpa_s->go_params == NULL)
1621 return -1;
1622 }
1623 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1624 return 0;
1625}
1626
1627
1628static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1629 struct p2p_go_neg_results *res)
1630{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001631 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001632 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1633 " dev_addr " MACSTR " wps_method %d",
1634 MAC2STR(res->peer_interface_addr),
1635 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1637 res->ssid, res->ssid_len);
1638 wpa_supplicant_ap_deinit(wpa_s);
1639 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001640 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001642#ifdef CONFIG_WPS_NFC
1643 } else if (res->wps_method == WPS_NFC) {
1644 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1645 res->peer_interface_addr,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001646 wpa_s->p2pdev->p2p_oob_dev_pw,
1647 wpa_s->p2pdev->p2p_oob_dev_pw_id, 1,
1648 wpa_s->p2pdev->p2p_oob_dev_pw_id ==
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001649 DEV_PW_NFC_CONNECTION_HANDOVER ?
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001650 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash :
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001651 NULL,
1652 NULL, 0, 0);
1653#endif /* CONFIG_WPS_NFC */
1654 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001656 if (wpa_s->p2p_wps_method == WPS_P2PS)
1657 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1659 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1660 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1661 wpa_s->p2p_pin, 1, dev_pw_id);
1662 }
1663}
1664
1665
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001666static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1667 struct wpa_ssid *ssid)
1668{
1669 struct wpa_ssid *persistent;
1670 struct psk_list_entry *psk;
1671 struct hostapd_data *hapd;
1672
1673 if (!wpa_s->ap_iface)
1674 return;
1675
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001676 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001677 ssid->ssid_len);
1678 if (persistent == NULL)
1679 return;
1680
1681 hapd = wpa_s->ap_iface->bss[0];
1682
1683 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1684 list) {
1685 struct hostapd_wpa_psk *hpsk;
1686
1687 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1688 MACSTR " psk=%d",
1689 MAC2STR(psk->addr), psk->p2p);
1690 hpsk = os_zalloc(sizeof(*hpsk));
1691 if (hpsk == NULL)
1692 break;
1693 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1694 if (psk->p2p)
1695 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1696 else
1697 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1698 hpsk->next = hapd->conf->ssid.wpa_psk;
1699 hapd->conf->ssid.wpa_psk = hpsk;
1700 }
1701}
1702
1703
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001704static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1705{
1706 unsigned int i;
1707
1708 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1709 wpa_s->p2p_group_common_freqs_num);
1710
1711 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1712 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1713 i, wpa_s->p2p_group_common_freqs[i]);
1714}
1715
1716
1717static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1718 struct p2p_go_neg_results *params)
1719{
1720 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1721
1722 wpa_s->p2p_group_common_freqs_num = 0;
1723 os_free(wpa_s->p2p_group_common_freqs);
1724 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1725 if (!wpa_s->p2p_group_common_freqs)
1726 return;
1727
1728 for (i = 0; i < len; i++) {
1729 if (!wpa_s->go_params->freq_list[i])
1730 break;
1731 wpa_s->p2p_group_common_freqs[i] =
1732 wpa_s->go_params->freq_list[i];
1733 }
1734 wpa_s->p2p_group_common_freqs_num = i;
1735}
1736
1737
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001738static void p2p_config_write(struct wpa_supplicant *wpa_s)
1739{
1740#ifndef CONFIG_NO_CONFIG_WRITE
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001741 if (wpa_s->p2pdev->conf->update_config &&
1742 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001743 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1744#endif /* CONFIG_NO_CONFIG_WRITE */
1745}
1746
1747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748static void p2p_go_configured(void *ctx, void *data)
1749{
1750 struct wpa_supplicant *wpa_s = ctx;
1751 struct p2p_go_neg_results *params = data;
1752 struct wpa_ssid *ssid;
1753
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001754 wpa_s->ap_configured_cb = NULL;
1755 wpa_s->ap_configured_cb_ctx = NULL;
1756 wpa_s->ap_configured_cb_data = NULL;
1757 if (!wpa_s->go_params) {
1758 wpa_printf(MSG_ERROR,
1759 "P2P: p2p_go_configured() called with wpa_s->go_params == NULL");
1760 return;
1761 }
1762
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001763 p2p_go_save_group_common_freqs(wpa_s, params);
1764 p2p_go_dump_common_freqs(wpa_s);
1765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 ssid = wpa_s->current_ssid;
1767 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1768 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1769 if (wpa_s->global->p2p_group_formation == wpa_s)
1770 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001771 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1772 params->passphrase[0] == '\0' ?
1773 params->psk : NULL,
1774 params->passphrase,
1775 wpa_s->global->p2p_dev_addr,
1776 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001777 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001778
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001779 if (wpa_s->p2pdev->p2ps_method_config_any) {
1780 if (is_zero_ether_addr(wpa_s->p2pdev->p2ps_join_addr)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001781 wpa_dbg(wpa_s, MSG_DEBUG,
1782 "P2PS: Setting default PIN for ANY");
1783 wpa_supplicant_ap_wps_pin(wpa_s, NULL,
1784 "12345670", NULL, 0,
1785 0);
1786 } else {
1787 wpa_dbg(wpa_s, MSG_DEBUG,
1788 "P2PS: Setting default PIN for " MACSTR,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001789 MAC2STR(wpa_s->p2pdev->p2ps_join_addr));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001790 wpa_supplicant_ap_wps_pin(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001791 wpa_s, wpa_s->p2pdev->p2ps_join_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001792 "12345670", NULL, 0, 0);
1793 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001794 wpa_s->p2pdev->p2ps_method_config_any = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001795 }
1796
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001797 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001798 if (params->persistent_group) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001799 wpas_p2p_store_persistent_group(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001800 wpa_s->p2pdev, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001801 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001802 wpas_p2p_add_psk_list(wpa_s, ssid);
1803 }
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07001804
1805 wpas_notify_p2p_group_started(wpa_s, ssid,
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08001806 params->persistent_group, 0,
1807 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 wpas_p2p_cross_connect_setup(wpa_s);
1809 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001810
1811 if (wpa_s->p2p_first_connection_timeout) {
1812 wpa_dbg(wpa_s, MSG_DEBUG,
1813 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1814 wpa_s->p2p_first_connection_timeout);
1815 wpa_s->p2p_go_group_formation_completed = 0;
1816 wpa_s->global->p2p_group_formation = wpa_s;
1817 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001818 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001819 eloop_register_timeout(
1820 wpa_s->p2p_first_connection_timeout, 0,
1821 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001822 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001823 }
1824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return;
1826 }
1827
1828 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1829 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1830 params->peer_interface_addr)) {
1831 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1832 "filtering");
1833 return;
1834 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001835 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001837 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001838#ifdef CONFIG_WPS_NFC
1839 } else if (params->wps_method == WPS_NFC) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001840 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001841 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001842 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001843 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1844 return;
1845 }
1846 wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001847 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
1848 wpa_s->p2pdev->p2p_oob_dev_pw,
1849 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
1850 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001851#endif /* CONFIG_WPS_NFC */
1852 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001854 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855 os_free(wpa_s->go_params);
1856 wpa_s->go_params = NULL;
1857}
1858
1859
1860static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1861 struct p2p_go_neg_results *params,
1862 int group_formation)
1863{
1864 struct wpa_ssid *ssid;
1865
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001866 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1867 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1869 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001871 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872
1873 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001874 if (ssid == NULL) {
1875 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001879 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 wpa_s->p2p_go_group_formation_completed = 0;
1881 wpa_s->group_formation_reported = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 wpa_config_set_network_defaults(ssid);
1885 ssid->temporary = 1;
1886 ssid->p2p_group = 1;
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07001887 ssid->p2p_persistent_group = !!params->persistent_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1889 WPAS_MODE_P2P_GO;
1890 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001891 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001892 ssid->vht = params->vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08001893 ssid->max_oper_chwidth = params->max_oper_chwidth;
1894 ssid->vht_center_freq2 = params->vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001895 ssid->ssid = os_zalloc(params->ssid_len + 1);
1896 if (ssid->ssid) {
1897 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1898 ssid->ssid_len = params->ssid_len;
1899 }
1900 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1901 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1902 ssid->proto = WPA_PROTO_RSN;
1903 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001904 ssid->group_cipher = WPA_CIPHER_CCMP;
1905 if (params->freq > 56160) {
1906 /*
1907 * Enable GCMP instead of CCMP as pairwise_cipher and
1908 * group_cipher in 60 GHz.
1909 */
1910 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1911 ssid->group_cipher = WPA_CIPHER_GCMP;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001912 /* P2P GO in 60 GHz is always a PCP (PBSS) */
1913 ssid->pbss = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001914 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001915 if (os_strlen(params->passphrase) > 0) {
1916 ssid->passphrase = os_strdup(params->passphrase);
1917 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001918 wpa_msg_global(wpa_s, MSG_ERROR,
1919 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001920 wpa_config_remove_network(wpa_s->conf, ssid->id);
1921 return;
1922 }
1923 } else
1924 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001925 ssid->psk_set = params->psk_set;
1926 if (ssid->psk_set)
1927 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001928 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001929 wpa_config_update_psk(ssid);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08001930 ssid->ap_max_inactivity = wpa_s->p2pdev->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931
1932 wpa_s->ap_configured_cb = p2p_go_configured;
1933 wpa_s->ap_configured_cb_ctx = wpa_s;
1934 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001935 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001936 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 wpa_s->reassociate = 1;
1938 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001939 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1940 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 wpa_supplicant_req_scan(wpa_s, 0, 0);
1942}
1943
1944
1945static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1946 const struct wpa_supplicant *src)
1947{
1948 struct wpa_config *d;
1949 const struct wpa_config *s;
1950
1951 d = dst->conf;
1952 s = src->conf;
1953
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001954#define C(n) \
1955do { \
1956 if (s->n && !d->n) \
1957 d->n = os_strdup(s->n); \
1958} while (0)
1959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 C(device_name);
1961 C(manufacturer);
1962 C(model_name);
1963 C(model_number);
1964 C(serial_number);
1965 C(config_methods);
1966#undef C
1967
1968 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1969 os_memcpy(d->sec_device_type, s->sec_device_type,
1970 sizeof(d->sec_device_type));
1971 d->num_sec_device_types = s->num_sec_device_types;
1972
1973 d->p2p_group_idle = s->p2p_group_idle;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001974 d->p2p_go_freq_change_policy = s->p2p_go_freq_change_policy;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001975 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001976 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001977 d->max_num_sta = s->max_num_sta;
1978 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001979 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001980 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001981 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001982 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001983 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001984 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001985 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001986
Dmitry Shmidt849734c2016-05-27 09:59:01 -07001987 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey &&
1988 !d->wps_nfc_pw_from_config) {
1989 wpabuf_free(d->wps_nfc_dh_privkey);
1990 wpabuf_free(d->wps_nfc_dh_pubkey);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001991 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1992 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1993 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001994 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995}
1996
1997
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001998static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1999 char *ifname, size_t len)
2000{
2001 char *ifname_ptr = wpa_s->ifname;
2002
2003 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
2004 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
2005 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
2006 }
2007
2008 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
2009 if (os_strlen(ifname) >= IFNAMSIZ &&
2010 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002011 int res;
2012
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002013 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002014 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
2015 if (os_snprintf_error(len, res) && len)
2016 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002017 }
2018}
2019
2020
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
2022 enum wpa_driver_if_type type)
2023{
2024 char ifname[120], force_ifname[120];
2025
2026 if (wpa_s->pending_interface_name[0]) {
2027 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
2028 "- skip creation of a new one");
2029 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
2030 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
2031 "unknown?! ifname='%s'",
2032 wpa_s->pending_interface_name);
2033 return -1;
2034 }
2035 return 0;
2036 }
2037
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002038 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 force_ifname[0] = '\0';
2040
2041 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
2042 ifname);
2043 wpa_s->p2p_group_idx++;
2044
2045 wpa_s->pending_interface_type = type;
2046 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
2047 wpa_s->pending_interface_addr, NULL) < 0) {
2048 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
2049 "interface");
2050 return -1;
2051 }
2052
2053 if (force_ifname[0]) {
2054 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
2055 force_ifname);
2056 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
2057 sizeof(wpa_s->pending_interface_name));
2058 } else
2059 os_strlcpy(wpa_s->pending_interface_name, ifname,
2060 sizeof(wpa_s->pending_interface_name));
2061 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
2062 MACSTR, wpa_s->pending_interface_name,
2063 MAC2STR(wpa_s->pending_interface_addr));
2064
2065 return 0;
2066}
2067
2068
2069static void wpas_p2p_remove_pending_group_interface(
2070 struct wpa_supplicant *wpa_s)
2071{
2072 if (!wpa_s->pending_interface_name[0] ||
2073 is_zero_ether_addr(wpa_s->pending_interface_addr))
2074 return; /* No pending virtual interface */
2075
2076 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
2077 wpa_s->pending_interface_name);
2078 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
2079 wpa_s->pending_interface_name);
2080 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2081 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002082 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083}
2084
2085
2086static struct wpa_supplicant *
2087wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
2088{
2089 struct wpa_interface iface;
2090 struct wpa_supplicant *group_wpa_s;
2091
2092 if (!wpa_s->pending_interface_name[0]) {
2093 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
2094 if (!wpas_p2p_create_iface(wpa_s))
2095 return NULL;
2096 /*
2097 * Something has forced us to remove the pending interface; try
2098 * to create a new one and hope for the best that we will get
2099 * the same local address.
2100 */
2101 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
2102 WPA_IF_P2P_CLIENT) < 0)
2103 return NULL;
2104 }
2105
2106 os_memset(&iface, 0, sizeof(iface));
2107 iface.ifname = wpa_s->pending_interface_name;
2108 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002109 if (wpa_s->conf->ctrl_interface == NULL &&
2110 wpa_s->parent != wpa_s &&
2111 wpa_s->p2p_mgmt &&
2112 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2113 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2114 else
2115 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002116 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002117 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118 if (group_wpa_s == NULL) {
2119 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2120 "wpa_supplicant interface");
2121 return NULL;
2122 }
2123 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2125 P2P_GROUP_INTERFACE_CLIENT;
2126 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002127 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128
2129 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2130
2131 return group_wpa_s;
2132}
2133
2134
2135static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2136 void *timeout_ctx)
2137{
2138 struct wpa_supplicant *wpa_s = eloop_ctx;
2139 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002140 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002141}
2142
2143
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002144static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2145 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002146{
2147 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002148 wpa_s->p2pdev, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149 if (wpa_s->global->p2p)
2150 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002151 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152}
2153
2154
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002155static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2156{
2157 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2158 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002159 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002160 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002161 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002162 wpa_s->global->p2p_fail_on_wps_complete = 0;
2163}
2164
2165
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002166void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2167{
2168 if (wpa_s->global->p2p_group_formation != wpa_s)
2169 return;
2170 /* Speed up group formation timeout since this cannot succeed */
2171 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002172 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002173 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002174 wpa_s->p2pdev, NULL);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002175}
2176
2177
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002178static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179{
2180 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002181 struct wpa_supplicant *group_wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182
2183 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2184 wpa_drv_cancel_remain_on_channel(wpa_s);
2185 wpa_s->off_channel_freq = 0;
2186 wpa_s->roc_waiting_drv_freq = 0;
2187 }
2188
2189 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002190 wpa_msg_global(wpa_s, MSG_INFO,
2191 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2192 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002193 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpas_p2p_remove_pending_group_interface(wpa_s);
2195 return;
2196 }
2197
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002198 if (!res->role_go) {
2199 /* Inform driver of the operating channel of GO. */
2200 wpa_drv_set_prob_oper_freq(wpa_s, res->freq);
2201 }
2202
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002203 if (wpa_s->p2p_go_ht40)
2204 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002205 if (wpa_s->p2p_go_vht)
2206 res->vht = 1;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08002207 res->max_oper_chwidth = wpa_s->p2p_go_max_oper_chwidth;
2208 res->vht_center_freq2 = wpa_s->p2p_go_vht_center_freq2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002209
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002210 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2211 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2212 " wps_method=%s",
2213 res->role_go ? "GO" : "client", res->freq, res->ht40,
2214 MAC2STR(res->peer_device_addr),
2215 MAC2STR(res->peer_interface_addr),
2216 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002217 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218
Dmitry Shmidt04949592012-07-19 12:16:46 -07002219 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2220 struct wpa_ssid *ssid;
2221 ssid = wpa_config_get_network(wpa_s->conf,
2222 wpa_s->p2p_persistent_id);
2223 if (ssid && ssid->disabled == 2 &&
2224 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2225 size_t len = os_strlen(ssid->passphrase);
2226 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2227 "on requested persistent group");
2228 os_memcpy(res->passphrase, ssid->passphrase, len);
2229 res->passphrase[len] = '\0';
2230 }
2231 }
2232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 if (wpa_s->create_p2p_iface) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002234 group_wpa_s =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2236 if (group_wpa_s == NULL) {
2237 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002238 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2239 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002240 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241 return;
2242 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2244 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002246 group_wpa_s = wpa_s->parent;
2247 wpa_s->global->p2p_group_formation = group_wpa_s;
2248 if (group_wpa_s != wpa_s)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07002249 wpas_p2p_clone_config(group_wpa_s, wpa_s);
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002252 group_wpa_s->p2p_in_provisioning = 1;
2253 group_wpa_s->p2pdev = wpa_s;
2254 if (group_wpa_s != wpa_s) {
2255 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2256 sizeof(group_wpa_s->p2p_pin));
2257 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2258 }
2259 if (res->role_go) {
2260 wpas_start_wps_go(group_wpa_s, res, 1);
2261 } else {
2262 os_get_reltime(&group_wpa_s->scan_min_time);
2263 wpas_start_wps_enrollee(group_wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 }
2265
2266 wpa_s->p2p_long_listen = 0;
2267 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2268
2269 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2270 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2271 (res->peer_config_timeout % 100) * 10000,
2272 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2273}
2274
2275
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002276static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2277 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278{
2279 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002280 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002281 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2282 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002284 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285}
2286
2287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002288static void wpas_dev_found(void *ctx, const u8 *addr,
2289 const struct p2p_peer_info *info,
2290 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002292#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 struct wpa_supplicant *wpa_s = ctx;
2294 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002295 char *wfd_dev_info_hex = NULL;
2296
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002297#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002298 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2299 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002300#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002301
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002302 if (info->p2ps_instance) {
2303 char str[256];
2304 const u8 *buf = wpabuf_head(info->p2ps_instance);
2305 size_t len = wpabuf_len(info->p2ps_instance);
2306
2307 while (len) {
2308 u32 id;
2309 u16 methods;
2310 u8 str_len;
2311
2312 if (len < 4 + 2 + 1)
2313 break;
2314 id = WPA_GET_LE32(buf);
2315 buf += sizeof(u32);
2316 methods = WPA_GET_BE16(buf);
2317 buf += sizeof(u16);
2318 str_len = *buf++;
2319 if (str_len > len - 4 - 2 - 1)
2320 break;
2321 os_memcpy(str, buf, str_len);
2322 str[str_len] = '\0';
2323 buf += str_len;
2324 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2325
2326 wpa_msg_global(wpa_s, MSG_INFO,
2327 P2P_EVENT_DEVICE_FOUND MACSTR
2328 " p2p_dev_addr=" MACSTR
2329 " pri_dev_type=%s name='%s'"
2330 " config_methods=0x%x"
2331 " dev_capab=0x%x"
2332 " group_capab=0x%x"
2333 " adv_id=%x asp_svc=%s%s",
2334 MAC2STR(addr),
2335 MAC2STR(info->p2p_device_addr),
2336 wps_dev_type_bin2str(
2337 info->pri_dev_type,
2338 devtype, sizeof(devtype)),
2339 info->device_name, methods,
2340 info->dev_capab, info->group_capab,
2341 id, str,
2342 info->vendor_elems ?
2343 " vendor_elems=1" : "");
2344 }
2345 goto done;
2346 }
2347
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002348 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2349 " p2p_dev_addr=" MACSTR
2350 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002351 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002352 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2353 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2354 sizeof(devtype)),
2355 info->device_name, info->config_methods,
2356 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002357 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002358 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002359 info->vendor_elems ? " vendor_elems=1" : "",
2360 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002362done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002363 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002364#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2367}
2368
2369
2370static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2371{
2372 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002374 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2375 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2378}
2379
2380
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002381static void wpas_find_stopped(void *ctx)
2382{
2383 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002384
2385 if (wpa_s->p2p_scan_work && wpas_abort_ongoing_scan(wpa_s) < 0)
2386 wpa_printf(MSG_DEBUG, "P2P: Abort ongoing scan failed");
2387
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002388 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002389 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002390}
2391
2392
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002393struct wpas_p2p_listen_work {
2394 unsigned int freq;
2395 unsigned int duration;
2396 struct wpabuf *probe_resp_ie;
2397};
2398
2399
2400static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2401{
2402 if (lwork == NULL)
2403 return;
2404 wpabuf_free(lwork->probe_resp_ie);
2405 os_free(lwork);
2406}
2407
2408
2409static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2410{
2411 struct wpas_p2p_listen_work *lwork;
2412
2413 if (!wpa_s->p2p_listen_work)
2414 return;
2415
2416 lwork = wpa_s->p2p_listen_work->ctx;
2417 wpas_p2p_listen_work_free(lwork);
2418 radio_work_done(wpa_s->p2p_listen_work);
2419 wpa_s->p2p_listen_work = NULL;
2420}
2421
2422
2423static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2424{
2425 struct wpa_supplicant *wpa_s = work->wpa_s;
2426 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002427 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002428
2429 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002430 if (work->started) {
2431 wpa_s->p2p_listen_work = NULL;
2432 wpas_stop_listen(wpa_s);
2433 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002434 wpas_p2p_listen_work_free(lwork);
2435 return;
2436 }
2437
2438 wpa_s->p2p_listen_work = work;
2439
2440 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2441
2442 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2443 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2444 "report received Probe Request frames");
2445 wpas_p2p_listen_work_done(wpa_s);
2446 return;
2447 }
2448
2449 wpa_s->pending_listen_freq = lwork->freq;
2450 wpa_s->pending_listen_duration = lwork->duration;
2451
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002452 duration = lwork->duration;
2453#ifdef CONFIG_TESTING_OPTIONS
2454 if (wpa_s->extra_roc_dur) {
2455 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2456 duration, duration + wpa_s->extra_roc_dur);
2457 duration += wpa_s->extra_roc_dur;
2458 }
2459#endif /* CONFIG_TESTING_OPTIONS */
2460
2461 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002462 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2463 "to remain on channel (%u MHz) for Listen "
2464 "state", lwork->freq);
2465 wpas_p2p_listen_work_done(wpa_s);
2466 wpa_s->pending_listen_freq = 0;
2467 return;
2468 }
2469 wpa_s->off_channel_freq = 0;
2470 wpa_s->roc_waiting_drv_freq = lwork->freq;
2471}
2472
2473
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474static int wpas_start_listen(void *ctx, unsigned int freq,
2475 unsigned int duration,
2476 const struct wpabuf *probe_resp_ie)
2477{
2478 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002479 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002481 if (wpa_s->p2p_listen_work) {
2482 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483 return -1;
2484 }
2485
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002486 lwork = os_zalloc(sizeof(*lwork));
2487 if (lwork == NULL)
2488 return -1;
2489 lwork->freq = freq;
2490 lwork->duration = duration;
2491 if (probe_resp_ie) {
2492 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2493 if (lwork->probe_resp_ie == NULL) {
2494 wpas_p2p_listen_work_free(lwork);
2495 return -1;
2496 }
2497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002499 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2500 lwork) < 0) {
2501 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502 return -1;
2503 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504
2505 return 0;
2506}
2507
2508
2509static void wpas_stop_listen(void *ctx)
2510{
2511 struct wpa_supplicant *wpa_s = ctx;
2512 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2513 wpa_drv_cancel_remain_on_channel(wpa_s);
2514 wpa_s->off_channel_freq = 0;
2515 wpa_s->roc_waiting_drv_freq = 0;
2516 }
2517 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002518
2519 /*
2520 * Don't cancel Probe Request RX reporting for a connected P2P Client
2521 * handling Probe Request frames.
2522 */
2523 if (!wpa_s->p2p_cli_probe)
2524 wpa_drv_probe_req_report(wpa_s, 0);
2525
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002526 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527}
2528
2529
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002530static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2531 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532{
2533 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002534 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2535 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536}
2537
2538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2540 const u8 *peer, const char *params,
2541 unsigned int generated_pin)
2542{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002543 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2544 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545}
2546
2547
2548static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2549 const u8 *peer, const char *params)
2550{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002551 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2552 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553}
2554
2555
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002556static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2557 const u8 *dev_addr, const u8 *pri_dev_type,
2558 const char *dev_name, u16 supp_config_methods,
2559 u8 dev_capab, u8 group_capab, const u8 *group_id,
2560 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561{
2562 struct wpa_supplicant *wpa_s = ctx;
2563 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002564 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 u8 empty_dev_type[8];
2566 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002567 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002568 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002569
2570 if (group_id) {
2571 for (group = wpa_s->global->ifaces; group; group = group->next)
2572 {
2573 struct wpa_ssid *s = group->current_ssid;
2574 if (s != NULL &&
2575 s->mode == WPAS_MODE_P2P_GO &&
2576 group_id_len - ETH_ALEN == s->ssid_len &&
2577 os_memcmp(group_id + ETH_ALEN, s->ssid,
2578 s->ssid_len) == 0)
2579 break;
2580 }
2581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582
2583 if (pri_dev_type == NULL) {
2584 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2585 pri_dev_type = empty_dev_type;
2586 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002587 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2588 " pri_dev_type=%s name='%s' config_methods=0x%x "
2589 "dev_capab=0x%x group_capab=0x%x%s%s",
2590 MAC2STR(dev_addr),
2591 wps_dev_type_bin2str(pri_dev_type, devtype,
2592 sizeof(devtype)),
2593 dev_name, supp_config_methods, dev_capab, group_capab,
2594 group ? " group=" : "",
2595 group ? group->ifname : "");
2596 if (os_snprintf_error(sizeof(params), res))
2597 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002599
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 if (config_methods & WPS_CONFIG_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002601 if (wps_generate_pin(&generated_pin) < 0) {
2602 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2603 wpas_notify_p2p_provision_discovery(
2604 wpa_s, peer, 0 /* response */,
2605 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2606 return;
2607 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 wpas_prov_disc_local_display(wpa_s, peer, params,
2609 generated_pin);
2610 } else if (config_methods & WPS_CONFIG_KEYPAD)
2611 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2612 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002613 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2614 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002615
2616 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2617 P2P_PROV_DISC_SUCCESS,
2618 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619}
2620
2621
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002622static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623{
2624 struct wpa_supplicant *wpa_s = ctx;
2625 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002626 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002627
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002628 if (wpa_s->pending_pd_before_join &&
2629 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2630 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2631 wpa_s->pending_pd_before_join = 0;
2632 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2633 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002634 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002635 return;
2636 }
2637
Dmitry Shmidt04949592012-07-19 12:16:46 -07002638 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002639 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2640 int res;
2641
2642 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2643 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2644 if (os_snprintf_error(sizeof(params), res))
2645 params[sizeof(params) - 1] = '\0';
2646 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002647 params[0] = '\0';
2648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002649 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002650 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 else if (config_methods & WPS_CONFIG_KEYPAD) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002652 if (wps_generate_pin(&generated_pin) < 0) {
2653 wpa_printf(MSG_DEBUG, "P2P: Could not generate PIN");
2654 wpas_notify_p2p_provision_discovery(
2655 wpa_s, peer, 0 /* response */,
2656 P2P_PROV_DISC_INFO_UNAVAILABLE, 0, 0);
2657 return;
2658 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002659 wpas_prov_disc_local_display(wpa_s, peer, params,
2660 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002662 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2663 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664
Jouni Malinen75ecf522011-06-27 15:19:46 -07002665 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2666 P2P_PROV_DISC_SUCCESS,
2667 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668}
2669
2670
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002671static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002672 enum p2p_prov_disc_status status,
2673 u32 adv_id, const u8 *adv_mac,
2674 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002675{
2676 struct wpa_supplicant *wpa_s = ctx;
2677
Dmitry Shmidt04949592012-07-19 12:16:46 -07002678 if (wpa_s->p2p_fallback_to_go_neg) {
2679 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2680 "failed - fall back to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002681 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002682 P2P_EVENT_FALLBACK_TO_GO_NEG
2683 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002684 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2685 return;
2686 }
2687
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002688 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002689 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002690 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2691 "join-existing-group operation (no ACK for PD "
2692 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002693 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002694 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002695 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002696
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002697 if (adv_id && adv_mac && deferred_session_resp) {
2698 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2699 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2700 " deferred_session_resp='%s'",
2701 MAC2STR(peer), status, adv_id,
2702 deferred_session_resp);
2703 } else if (adv_id && adv_mac) {
2704 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2705 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2706 MAC2STR(peer), status, adv_id);
2707 } else {
2708 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2709 " p2p_dev_addr=" MACSTR " status=%d",
2710 MAC2STR(peer), status);
2711 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002712
Jouni Malinen75ecf522011-06-27 15:19:46 -07002713 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2714 status, 0, 0);
2715}
2716
2717
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002718static int freq_included(struct wpa_supplicant *wpa_s,
2719 const struct p2p_channels *channels,
2720 unsigned int freq)
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002721{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002722 if ((channels == NULL || p2p_channels_includes_freq(channels, freq)) &&
2723 wpas_p2p_go_is_peer_freq(wpa_s, freq))
2724 return 1;
2725 return 0;
2726}
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002727
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002728
2729static void wpas_p2p_go_update_common_freqs(struct wpa_supplicant *wpa_s)
2730{
2731 unsigned int num = P2P_MAX_CHANNELS;
2732 int *common_freqs;
2733 int ret;
2734
2735 p2p_go_dump_common_freqs(wpa_s);
2736 common_freqs = os_calloc(num, sizeof(int));
2737 if (!common_freqs)
2738 return;
2739
2740 ret = p2p_group_get_common_freqs(wpa_s->p2p_group, common_freqs, &num);
2741 if (ret < 0) {
2742 wpa_dbg(wpa_s, MSG_DEBUG,
2743 "P2P: Failed to get group common freqs");
2744 os_free(common_freqs);
2745 return;
2746 }
2747
2748 os_free(wpa_s->p2p_group_common_freqs);
2749 wpa_s->p2p_group_common_freqs = common_freqs;
2750 wpa_s->p2p_group_common_freqs_num = num;
2751 p2p_go_dump_common_freqs(wpa_s);
2752}
2753
2754
2755/*
2756 * Check if the given frequency is one of the possible operating frequencies
2757 * set after the completion of the GO Negotiation.
2758 */
2759static int wpas_p2p_go_is_peer_freq(struct wpa_supplicant *wpa_s, int freq)
2760{
2761 unsigned int i;
2762
2763 p2p_go_dump_common_freqs(wpa_s);
2764
2765 /* assume no restrictions */
2766 if (!wpa_s->p2p_group_common_freqs_num)
2767 return 1;
2768
2769 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
2770 if (wpa_s->p2p_group_common_freqs[i] == freq)
2771 return 1;
2772 }
2773 return 0;
2774}
2775
2776
2777static int wpas_sta_check_ecsa(struct hostapd_data *hapd,
2778 struct sta_info *sta, void *ctx)
2779{
2780 int *ecsa_support = ctx;
2781
2782 *ecsa_support &= sta->ecsa_supported;
2783
2784 return 0;
2785}
2786
2787
2788/* Check if all the peers support eCSA */
2789static int wpas_p2p_go_clients_support_ecsa(struct wpa_supplicant *wpa_s)
2790{
2791 int ecsa_support = 1;
2792
2793 ap_for_each_sta(wpa_s->ap_iface->bss[0], wpas_sta_check_ecsa,
2794 &ecsa_support);
2795
2796 return ecsa_support;
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002797}
2798
2799
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002800/**
2801 * Pick the best frequency to use from all the currently used frequencies.
2802 */
2803static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2804 struct wpa_used_freq_data *freqs,
2805 unsigned int num)
2806{
2807 unsigned int i, c;
2808
2809 /* find a candidate freq that is supported by P2P */
2810 for (c = 0; c < num; c++)
2811 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2812 break;
2813
2814 if (c == num)
2815 return 0;
2816
2817 /* once we have a candidate, try to find a 'better' one */
2818 for (i = c + 1; i < num; i++) {
2819 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2820 continue;
2821
2822 /*
2823 * 1. Infrastructure station interfaces have higher preference.
2824 * 2. P2P Clients have higher preference.
2825 * 3. All others.
2826 */
2827 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2828 c = i;
2829 break;
2830 }
2831
2832 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2833 c = i;
2834 }
2835 return freqs[c].freq;
2836}
2837
2838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2840 const u8 *go_dev_addr, const u8 *ssid,
2841 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002842 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002843 const struct p2p_channels *channels,
2844 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002845{
2846 struct wpa_supplicant *wpa_s = ctx;
2847 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002848 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002850 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851
2852 if (!persistent_group) {
2853 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002854 " to join an active group (SSID: %s)",
2855 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2857 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2858 == 0 ||
2859 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2860 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2861 "authorized invitation");
2862 goto accept_inv;
2863 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002864
2865#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002866 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2867 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002868 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002869 wpa_s->p2p_wps_method = WPS_NFC;
2870 wpa_s->pending_join_wps_method = WPS_NFC;
2871 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002872 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002873 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002874 bssid, ETH_ALEN);
2875 goto accept_inv;
2876 }
2877#endif /* CONFIG_WPS_NFC */
2878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 /*
2880 * Do not accept the invitation automatically; notify user and
2881 * request approval.
2882 */
2883 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2884 }
2885
2886 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2887 if (grp) {
2888 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2889 "running persistent group");
2890 if (*go)
2891 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2892 goto accept_inv;
2893 }
2894
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002895 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2896 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2897 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2898 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002899 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002900 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2902
2903 for (s = wpa_s->conf->ssid; s; s = s->next) {
2904 if (s->disabled == 2 &&
2905 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2906 s->ssid_len == ssid_len &&
2907 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2908 break;
2909 }
2910
2911 if (!s) {
2912 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2913 " requested reinvocation of an unknown group",
2914 MAC2STR(sa));
2915 return P2P_SC_FAIL_UNKNOWN_GROUP;
2916 }
2917
2918 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2919 *go = 1;
2920 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2921 wpa_printf(MSG_DEBUG, "P2P: The only available "
2922 "interface is already in use - reject "
2923 "invitation");
2924 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2925 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08002926 if (wpa_s->p2p_mgmt)
2927 os_memcpy(group_bssid, wpa_s->parent->own_addr,
2928 ETH_ALEN);
2929 else
2930 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 } else if (s->mode == WPAS_MODE_P2P_GO) {
2932 *go = 1;
2933 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2934 {
2935 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2936 "interface address for the group");
2937 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2938 }
2939 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2940 ETH_ALEN);
2941 }
2942
2943accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002944 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2945
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002946 best_freq = 0;
2947 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2948 sizeof(struct wpa_used_freq_data));
2949 if (freqs) {
2950 int num_channels = wpa_s->num_multichan_concurrent;
2951 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2952 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2953 os_free(freqs);
2954 }
2955
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002956 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002957 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002958 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002959 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002960
2961 if (wpa_s->num_multichan_concurrent < 2 ||
2962 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2963 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002964 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966 }
2967
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002968 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2969 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002970 if (*go == 0) {
2971 /* We are the client */
2972 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2973 "running a GO but we are capable of MCC, "
2974 "figure out the best channel to use");
2975 *force_freq = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002976 } else if (!freq_included(wpa_s, channels, *force_freq)) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002977 /* We are the GO, and *force_freq is not in the
2978 * intersection */
2979 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2980 "in intersection but we are capable of MCC, "
2981 "figure out the best channel to use",
2982 *force_freq);
2983 *force_freq = 0;
2984 }
2985 }
2986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002987 return P2P_SC_SUCCESS;
2988}
2989
2990
2991static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2992 const u8 *ssid, size_t ssid_len,
2993 const u8 *go_dev_addr, u8 status,
2994 int op_freq)
2995{
2996 struct wpa_supplicant *wpa_s = ctx;
2997 struct wpa_ssid *s;
2998
2999 for (s = wpa_s->conf->ssid; s; s = s->next) {
3000 if (s->disabled == 2 &&
3001 s->ssid_len == ssid_len &&
3002 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3003 break;
3004 }
3005
3006 if (status == P2P_SC_SUCCESS) {
3007 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003008 " was accepted; op_freq=%d MHz, SSID=%s",
3009 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003011 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003012 if (go) {
3013 wpa_msg_global(wpa_s, MSG_INFO,
3014 P2P_EVENT_INVITATION_ACCEPTED
3015 "sa=" MACSTR
3016 " persistent=%d freq=%d",
3017 MAC2STR(sa), s->id, op_freq);
3018 } else {
3019 wpa_msg_global(wpa_s, MSG_INFO,
3020 P2P_EVENT_INVITATION_ACCEPTED
3021 "sa=" MACSTR
3022 " persistent=%d",
3023 MAC2STR(sa), s->id);
3024 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003025 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003026 wpa_s, s, go, 0, op_freq, 0, 0, 0, 0, NULL,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003027 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0,
3028 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003030 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt849734c2016-05-27 09:59:01 -07003031 wpa_msg_global(wpa_s, MSG_INFO,
3032 P2P_EVENT_INVITATION_ACCEPTED
3033 "sa=" MACSTR " go_dev_addr=" MACSTR
3034 " bssid=" MACSTR " unknown-network",
3035 MAC2STR(sa), MAC2STR(go_dev_addr),
3036 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003037 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003038 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003039 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 }
3041 return;
3042 }
3043
3044 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3045 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3046 " was rejected (status %u)", MAC2STR(sa), status);
3047 return;
3048 }
3049
3050 if (!s) {
3051 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003052 wpa_msg_global(wpa_s, MSG_INFO,
3053 P2P_EVENT_INVITATION_RECEIVED
3054 "sa=" MACSTR " go_dev_addr=" MACSTR
3055 " bssid=" MACSTR " unknown-network",
3056 MAC2STR(sa), MAC2STR(go_dev_addr),
3057 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003058 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003059 wpa_msg_global(wpa_s, MSG_INFO,
3060 P2P_EVENT_INVITATION_RECEIVED
3061 "sa=" MACSTR " go_dev_addr=" MACSTR
3062 " unknown-network",
3063 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003065 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr,
3066 bssid, 0, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 return;
3068 }
3069
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003070 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003071 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3072 "sa=" MACSTR " persistent=%d freq=%d",
3073 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003074 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003075 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3076 "sa=" MACSTR " persistent=%d",
3077 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003078 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003079 wpas_notify_p2p_invitation_received(wpa_s, sa, go_dev_addr, bssid,
3080 s->id, op_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081}
3082
3083
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003084static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3085 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003086 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003087{
3088 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003089 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003090
3091 if (ssid == NULL)
3092 return;
3093
3094 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003095 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003096 ETH_ALEN) == 0)
3097 break;
3098 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003099 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003100 if (ssid->mode != WPAS_MODE_P2P_GO &&
3101 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3102 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3103 "due to invitation result", ssid->id);
3104 wpas_notify_network_removed(wpa_s, ssid);
3105 wpa_config_remove_network(wpa_s->conf, ssid->id);
3106 return;
3107 }
3108 return; /* Peer not found in client list */
3109 }
3110
3111 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003112 "group %d client list%s",
3113 MAC2STR(peer), ssid->id,
3114 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08003115 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3116 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3117 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003118 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07003119 if (p2p_wpa_s->conf->update_config &&
3120 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003121 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003122}
3123
3124
3125static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3126 const u8 *peer)
3127{
3128 struct wpa_ssid *ssid;
3129
3130 wpa_s = wpa_s->global->p2p_invite_group;
3131 if (wpa_s == NULL)
3132 return; /* No known invitation group */
3133 ssid = wpa_s->current_ssid;
3134 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3135 !ssid->p2p_persistent_group)
3136 return; /* Not operating as a GO in persistent group */
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003137 ssid = wpas_p2p_get_persistent(wpa_s->p2pdev, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003138 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003139 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003140}
3141
3142
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003143static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003144 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003145 const u8 *peer, int neg_freq,
3146 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147{
3148 struct wpa_supplicant *wpa_s = ctx;
3149 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003150 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151
3152 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003153 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3154 "status=%d " MACSTR,
3155 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003157 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3158 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003159 }
3160 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3161
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003162 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3163 status, MAC2STR(peer));
3164 if (wpa_s->pending_invite_ssid_id == -1) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003165 struct wpa_supplicant *group_if =
3166 wpa_s->global->p2p_invite_group;
3167
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003168 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3169 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003170
3171 /*
3172 * Invitation to an active group. If this is successful and we
3173 * are the GO, set the client wait to postpone some concurrent
3174 * operations and to allow provisioning and connection to happen
3175 * more quickly.
3176 */
3177 if (status == P2P_SC_SUCCESS &&
3178 group_if && group_if->current_ssid &&
3179 group_if->current_ssid->mode == WPAS_MODE_P2P_GO) {
3180 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
3181#ifdef CONFIG_TESTING_OPTIONS
3182 if (group_if->p2p_go_csa_on_inv) {
3183 wpa_printf(MSG_DEBUG,
3184 "Testing: force P2P GO CSA after invitation");
3185 eloop_cancel_timeout(
3186 wpas_p2p_reconsider_moving_go,
3187 wpa_s, NULL);
3188 eloop_register_timeout(
3189 0, 50000,
3190 wpas_p2p_reconsider_moving_go,
3191 wpa_s, NULL);
3192 }
3193#endif /* CONFIG_TESTING_OPTIONS */
3194 }
3195 return;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003198 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3199 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3200 "invitation exchange to indicate readiness for "
3201 "re-invocation");
3202 }
3203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003205 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3206 ssid = wpa_config_get_network(
3207 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003208 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003209 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003210 wpas_p2p_remove_pending_group_interface(wpa_s);
3211 return;
3212 }
3213
3214 ssid = wpa_config_get_network(wpa_s->conf,
3215 wpa_s->pending_invite_ssid_id);
3216 if (ssid == NULL) {
3217 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3218 "data matching with invitation");
3219 return;
3220 }
3221
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003222 /*
3223 * The peer could have missed our ctrl::ack frame for Invitation
3224 * Response and continue retransmitting the frame. To reduce the
3225 * likelihood of the peer not getting successful TX status for the
3226 * Invitation Response frame, wait a short time here before starting
3227 * the persistent group so that we will remain on the current channel to
3228 * acknowledge any possible retransmission from the peer.
3229 */
3230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3231 "starting persistent group");
3232 os_sleep(0, 50000);
3233
Dmitry Shmidt15907092014-03-25 10:42:57 -07003234 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003235 freq_included(wpa_s, channels, neg_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003236 freq = neg_freq;
3237 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003238 freq_included(wpa_s, channels, peer_oper_freq))
Dmitry Shmidt15907092014-03-25 10:42:57 -07003239 freq = peer_oper_freq;
3240 else
3241 freq = 0;
3242
3243 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3244 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003246 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003247 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003248 freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003249 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003250 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003251 wpa_s->p2p_go_max_oper_chwidth,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003252 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003253 ssid->mode == WPAS_MODE_P2P_GO ?
3254 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003255 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256}
3257
3258
Dmitry Shmidt04949592012-07-19 12:16:46 -07003259static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3260 unsigned int freq)
3261{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003262 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3263 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003264 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003265}
3266
3267
3268static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3269{
3270 reg->channel[reg->channels] = chan;
3271 reg->channels++;
3272}
3273
3274
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003276 struct p2p_channels *chan,
3277 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278{
3279 int i, cla = 0;
3280
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003281 wpa_s->global->p2p_24ghz_social_channels = 1;
3282
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003283 os_memset(cli_chan, 0, sizeof(*cli_chan));
3284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3286 "band");
3287
3288 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3289 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003290 chan->reg_class[cla].channels = 0;
3291 for (i = 0; i < 11; i++) {
3292 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3293 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3294 }
3295 if (chan->reg_class[cla].channels)
3296 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297
3298 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3299 "band");
3300
3301 /* Operating class 115 - 5 GHz, channels 36-48 */
3302 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303 chan->reg_class[cla].channels = 0;
3304 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3305 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3306 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3307 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3308 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3309 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3310 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3311 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3312 if (chan->reg_class[cla].channels)
3313 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314
3315 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3316 "band");
3317
3318 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3319 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003320 chan->reg_class[cla].channels = 0;
3321 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3322 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3323 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3324 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3325 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3326 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3327 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3328 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3329 if (chan->reg_class[cla].channels)
3330 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003331
3332 chan->reg_classes = cla;
3333 return 0;
3334}
3335
3336
Dmitry Shmidt04949592012-07-19 12:16:46 -07003337static int has_channel(struct wpa_global *global,
3338 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003339{
3340 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003341 unsigned int freq;
3342
3343 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3344 chan * 5;
3345 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003346 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347
3348 for (i = 0; i < mode->num_channels; i++) {
3349 if (mode->channels[i].chan == chan) {
3350 if (flags)
3351 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003352 if (mode->channels[i].flag &
3353 (HOSTAPD_CHAN_DISABLED |
3354 HOSTAPD_CHAN_RADAR))
3355 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003356 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3357 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003358 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003359 }
3360 }
3361
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003362 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363}
3364
3365
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003366static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3367 struct hostapd_hw_modes *mode,
3368 u8 channel)
3369{
3370 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
Dmitry Shmidtaca489e2016-09-28 15:44:14 -07003371 size_t i;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003372
3373 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3374 return 0;
3375
3376 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3377 /*
3378 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3379 * so the center channel is 6 channels away from the start/end.
3380 */
3381 if (channel >= center_channels[i] - 6 &&
3382 channel <= center_channels[i] + 6)
3383 return center_channels[i];
3384
3385 return 0;
3386}
3387
3388
3389static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3390 struct hostapd_hw_modes *mode,
3391 u8 channel, u8 bw)
3392{
3393 u8 center_chan;
3394 int i, flags;
3395 enum chan_allowed res, ret = ALLOWED;
3396
3397 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3398 if (!center_chan)
3399 return NOT_ALLOWED;
3400 if (center_chan >= 58 && center_chan <= 138)
3401 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3402
3403 /* check all the channels are available */
3404 for (i = 0; i < 4; i++) {
3405 int adj_chan = center_chan - 6 + i * 4;
3406
3407 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3408 if (res == NOT_ALLOWED)
3409 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003410 if (res == NO_IR)
3411 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003412
3413 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3414 return NOT_ALLOWED;
3415 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3416 return NOT_ALLOWED;
3417 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3418 return NOT_ALLOWED;
3419 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3420 return NOT_ALLOWED;
3421 }
3422
3423 return ret;
3424}
3425
3426
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003427static int wpas_p2p_get_center_160mhz(struct wpa_supplicant *wpa_s,
3428 struct hostapd_hw_modes *mode,
3429 u8 channel)
3430{
3431 u8 center_channels[] = { 50, 114 };
3432 unsigned int i;
3433
3434 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3435 return 0;
3436
3437 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3438 /*
3439 * In 160 MHz, the bandwidth "spans" 28 channels (e.g., 36-64),
3440 * so the center channel is 14 channels away from the start/end.
3441 */
3442 if (channel >= center_channels[i] - 14 &&
3443 channel <= center_channels[i] + 14)
3444 return center_channels[i];
3445
3446 return 0;
3447}
3448
3449
3450static enum chan_allowed wpas_p2p_verify_160mhz(struct wpa_supplicant *wpa_s,
3451 struct hostapd_hw_modes *mode,
3452 u8 channel, u8 bw)
3453{
3454 u8 center_chan;
3455 int i, flags;
3456 enum chan_allowed res, ret = ALLOWED;
3457
3458 center_chan = wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3459 if (!center_chan)
3460 return NOT_ALLOWED;
3461 /* VHT 160 MHz uses DFS channels in most countries. */
3462
3463 /* Check all the channels are available */
3464 for (i = 0; i < 8; i++) {
3465 int adj_chan = center_chan - 14 + i * 4;
3466
3467 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3468 if (res == NOT_ALLOWED)
3469 return NOT_ALLOWED;
3470
3471 if (res == NO_IR)
3472 ret = NO_IR;
3473
3474 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_150))
3475 return NOT_ALLOWED;
3476 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_130))
3477 return NOT_ALLOWED;
3478 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_110))
3479 return NOT_ALLOWED;
3480 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_90))
3481 return NOT_ALLOWED;
3482 if (i == 4 && !(flags & HOSTAPD_CHAN_VHT_90_70))
3483 return NOT_ALLOWED;
3484 if (i == 5 && !(flags & HOSTAPD_CHAN_VHT_110_50))
3485 return NOT_ALLOWED;
3486 if (i == 6 && !(flags & HOSTAPD_CHAN_VHT_130_30))
3487 return NOT_ALLOWED;
3488 if (i == 7 && !(flags & HOSTAPD_CHAN_VHT_150_10))
3489 return NOT_ALLOWED;
3490 }
3491
3492 return ret;
3493}
3494
3495
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003496static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3497 struct hostapd_hw_modes *mode,
3498 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003499{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003500 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003501 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003502
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003503 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3504 if (bw == BW40MINUS) {
3505 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3506 return NOT_ALLOWED;
3507 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3508 } else if (bw == BW40PLUS) {
3509 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3510 return NOT_ALLOWED;
3511 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3512 } else if (bw == BW80) {
3513 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003514 } else if (bw == BW160) {
3515 res2 = wpas_p2p_verify_160mhz(wpa_s, mode, channel, bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003516 }
3517
3518 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3519 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003520 if (res == NO_IR || res2 == NO_IR)
3521 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003522 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003523}
3524
3525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003527 struct p2p_channels *chan,
3528 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003530 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003531 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003532
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003533 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003534 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3535 "of all supported channels; assume dualband "
3536 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003537 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 }
3539
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003540 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003541
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003542 for (op = 0; global_op_class[op].op_class; op++) {
3543 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003545 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003547 if (o->p2p == NO_P2P_SUPP)
3548 continue;
3549
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003550 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 if (mode == NULL)
3552 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003553 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3554 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003555 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003556 enum chan_allowed res;
3557 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3558 if (res == ALLOWED) {
3559 if (reg == NULL) {
3560 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3561 o->op_class);
3562 reg = &chan->reg_class[cla];
3563 cla++;
3564 reg->reg_class = o->op_class;
3565 }
3566 reg->channel[reg->channels] = ch;
3567 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003568 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003569 wpa_s->conf->p2p_add_cli_chan) {
3570 if (cli_reg == NULL) {
3571 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3572 o->op_class);
3573 cli_reg = &cli_chan->reg_class[cli_cla];
3574 cli_cla++;
3575 cli_reg->reg_class = o->op_class;
3576 }
3577 cli_reg->channel[cli_reg->channels] = ch;
3578 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 }
3581 if (reg) {
3582 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3583 reg->channel, reg->channels);
3584 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003585 if (cli_reg) {
3586 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3587 cli_reg->channel, cli_reg->channels);
3588 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003589 }
3590
3591 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003592 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003594 return 0;
3595}
3596
3597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003598int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3599 struct hostapd_hw_modes *mode, u8 channel)
3600{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003601 int op;
3602 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003603
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003604 for (op = 0; global_op_class[op].op_class; op++) {
3605 const struct oper_class_map *o = &global_op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003606 u8 ch;
3607
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003608 if (o->p2p == NO_P2P_SUPP)
3609 continue;
3610
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003611 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3612 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003613 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3614 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003615 continue;
3616 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003617 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003618 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003619 }
3620 }
3621 return 0;
3622}
3623
3624
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003625int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3626 struct hostapd_hw_modes *mode, u8 channel)
3627{
3628 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3629 return 0;
3630
3631 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3632}
3633
3634
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08003635int wpas_p2p_get_vht160_center(struct wpa_supplicant *wpa_s,
3636 struct hostapd_hw_modes *mode, u8 channel)
3637{
3638 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW160))
3639 return 0;
3640 return wpas_p2p_get_center_160mhz(wpa_s, mode, channel);
3641}
3642
3643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3645 size_t buf_len)
3646{
3647 struct wpa_supplicant *wpa_s = ctx;
3648
3649 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3650 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3651 break;
3652 }
3653 if (wpa_s == NULL)
3654 return -1;
3655
3656 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3657}
3658
3659
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003660struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3661 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003662{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003663 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3664 struct wpa_ssid *s = wpa_s->current_ssid;
3665 if (s == NULL)
3666 continue;
3667 if (s->mode != WPAS_MODE_P2P_GO &&
3668 s->mode != WPAS_MODE_AP &&
3669 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3670 continue;
3671 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003672 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003673 continue;
3674 return wpa_s;
3675 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003676
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003677 return NULL;
3678
3679}
3680
3681
3682struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3683 const u8 *peer_dev_addr)
3684{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003685 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3686 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003687 if (ssid && (ssid->mode != WPAS_MODE_INFRA || !ssid->p2p_group))
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003688 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003689 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3690 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003691 }
3692
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003693 return NULL;
3694}
3695
3696
3697static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3698{
3699 struct wpa_supplicant *wpa_s = ctx;
3700
3701 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003702}
3703
3704
Dmitry Shmidt18463232014-01-24 12:29:41 -08003705static int wpas_is_concurrent_session_active(void *ctx)
3706{
3707 struct wpa_supplicant *wpa_s = ctx;
3708 struct wpa_supplicant *ifs;
3709
3710 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3711 if (ifs == wpa_s)
3712 continue;
3713 if (ifs->wpa_state > WPA_ASSOCIATED)
3714 return 1;
3715 }
3716 return 0;
3717}
3718
3719
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003720static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3721{
3722 struct wpa_supplicant *wpa_s = ctx;
3723 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3724}
3725
3726
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003727int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3728 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003729{
3730 struct wpa_interface iface;
3731 struct wpa_supplicant *p2pdev_wpa_s;
3732 char ifname[100];
3733 char force_name[100];
3734 int ret;
3735
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003736 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3737 wpa_s->ifname);
3738 if (os_snprintf_error(sizeof(ifname), ret))
3739 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003740 force_name[0] = '\0';
3741 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3742 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3743 force_name, wpa_s->pending_interface_addr, NULL);
3744 if (ret < 0) {
3745 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3746 return ret;
3747 }
3748 os_strlcpy(wpa_s->pending_interface_name, ifname,
3749 sizeof(wpa_s->pending_interface_name));
3750
3751 os_memset(&iface, 0, sizeof(iface));
3752 iface.p2p_mgmt = 1;
3753 iface.ifname = wpa_s->pending_interface_name;
3754 iface.driver = wpa_s->driver->name;
3755 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003756
3757 /*
3758 * If a P2P Device configuration file was given, use it as the interface
3759 * configuration file (instead of using parent's configuration file.
3760 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003761 if (conf_p2p_dev) {
3762 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003763 iface.ctrl_interface = NULL;
3764 } else {
3765 iface.confname = wpa_s->confname;
3766 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3767 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003768
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003769 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003770 if (!p2pdev_wpa_s) {
3771 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3772 return -1;
3773 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003774
Dmitry Shmidt9c175262016-03-03 10:20:07 -08003775 p2pdev_wpa_s->p2pdev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003776 wpa_s->pending_interface_name[0] = '\0';
3777 return 0;
3778}
3779
3780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003781static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3782 const u8 *noa, size_t noa_len)
3783{
3784 struct wpa_supplicant *wpa_s, *intf = ctx;
3785 char hex[100];
3786
3787 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3788 if (wpa_s->waiting_presence_resp)
3789 break;
3790 }
3791 if (!wpa_s) {
3792 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3793 return;
3794 }
3795 wpa_s->waiting_presence_resp = 0;
3796
3797 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3798 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3799 " status=%u noa=%s", MAC2STR(src), status, hex);
3800}
3801
3802
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003803static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3804 size_t ssid_len, u8 *go_dev_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003805 u8 *ret_ssid, size_t *ret_ssid_len,
3806 u8 *intended_iface_addr)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003807{
3808 struct wpa_supplicant *wpa_s = ctx;
3809 struct wpa_ssid *s;
3810
3811 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3812 if (s) {
3813 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3814 *ret_ssid_len = s->ssid_len;
3815 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003816
3817 if (s->mode != WPAS_MODE_P2P_GO) {
3818 os_memset(intended_iface_addr, 0, ETH_ALEN);
3819 } else if (wpas_p2p_create_iface(wpa_s)) {
3820 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO))
3821 return 0;
3822
3823 os_memcpy(intended_iface_addr,
3824 wpa_s->pending_interface_addr, ETH_ALEN);
3825 } else {
3826 os_memcpy(intended_iface_addr, wpa_s->own_addr,
3827 ETH_ALEN);
3828 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003829 return 1;
3830 }
3831
3832 return 0;
3833}
3834
3835
3836static int wpas_get_go_info(void *ctx, u8 *intended_addr,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003837 u8 *ssid, size_t *ssid_len, int *group_iface,
3838 unsigned int *freq)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003839{
3840 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003841 struct wpa_supplicant *go;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003842 struct wpa_ssid *s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003843
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003844 /*
3845 * group_iface will be set to 1 only if a dedicated interface for P2P
3846 * role is required. First, we try to reuse an active GO. However,
3847 * if it is not present, we will try to reactivate an existing
3848 * persistent group and set group_iface to 1, so the caller will know
3849 * that the pending interface should be used.
3850 */
3851 *group_iface = 0;
3852
3853 if (freq)
3854 *freq = 0;
3855
3856 go = wpas_p2p_get_go_group(wpa_s);
3857 if (!go) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003858 s = wpas_p2p_get_persistent_go(wpa_s);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003859 *group_iface = wpas_p2p_create_iface(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003860 if (s)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003861 os_memcpy(intended_addr, s->bssid, ETH_ALEN);
3862 else
3863 return 0;
3864 } else {
3865 s = go->current_ssid;
3866 os_memcpy(intended_addr, go->own_addr, ETH_ALEN);
3867 if (freq)
3868 *freq = go->assoc_freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003869 }
3870
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003871 os_memcpy(ssid, s->ssid, s->ssid_len);
3872 *ssid_len = s->ssid_len;
3873
3874 return 1;
3875}
3876
3877
3878static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3879 const u8 *ssid, size_t ssid_len)
3880{
3881 struct wpa_supplicant *wpa_s = ctx;
3882 struct wpa_ssid *s;
3883 int save_config = 0;
3884 size_t i;
3885
3886 /* Start with our first choice of Persistent Groups */
3887 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3888 if (go && ssid && ssid_len &&
3889 s->ssid_len == ssid_len &&
3890 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3891 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3892 break;
3893
3894 /* Remove stale persistent group */
3895 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3896 wpa_config_remove_network(wpa_s->conf, s->id);
3897 save_config = 1;
3898 continue;
3899 }
3900
3901 for (i = 0; i < s->num_p2p_clients; i++) {
3902 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3903 peer, ETH_ALEN) != 0)
3904 continue;
3905
3906 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3907 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3908 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3909 break;
3910 }
3911 s->num_p2p_clients--;
3912 save_config = 1;
3913 }
3914
3915 if (save_config)
3916 p2p_config_write(wpa_s);
3917
3918 /* Return TRUE if valid SSID remains */
3919 return s != NULL;
3920}
3921
3922
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003923static void wpas_p2ps_get_feat_cap_str(char *buf, size_t buf_len,
3924 const u8 *feat_cap, size_t feat_cap_len)
3925{
3926 static const char pref[] = " feature_cap=";
3927 int ret;
3928
3929 buf[0] = '\0';
3930
3931 /*
3932 * We expect a feature capability to contain at least one byte to be
3933 * reported. The string buffer provided by the caller function is
3934 * expected to be big enough to contain all bytes of the attribute for
3935 * known specifications. This function truncates the reported bytes if
3936 * the feature capability data exceeds the string buffer size.
3937 */
3938 if (!feat_cap || !feat_cap_len || buf_len < sizeof(pref) + 2)
3939 return;
3940
3941 os_memcpy(buf, pref, sizeof(pref));
3942 ret = wpa_snprintf_hex(&buf[sizeof(pref) - 1],
3943 buf_len - sizeof(pref) + 1,
3944 feat_cap, feat_cap_len);
3945
3946 if (ret != (2 * (int) feat_cap_len))
3947 wpa_printf(MSG_WARNING, "P2PS feature_cap bytes truncated");
3948}
3949
3950
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003951static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3952 const u8 *adv_mac, const u8 *ses_mac,
3953 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3954 u8 conncap, int passwd_id,
3955 const u8 *persist_ssid,
3956 size_t persist_ssid_size, int response_done,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003957 int prov_start, const char *session_info,
3958 const u8 *feat_cap, size_t feat_cap_len,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08003959 unsigned int freq,
3960 const u8 *group_ssid, size_t group_ssid_len)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003961{
3962 struct wpa_supplicant *wpa_s = ctx;
3963 u8 mac[ETH_ALEN];
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003964 struct wpa_ssid *persistent_go, *stale, *s = NULL;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003965 int save_config = 0;
3966 struct wpa_supplicant *go_wpa_s;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003967 char feat_cap_str[256];
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003968
3969 if (!dev)
3970 return;
3971
3972 os_memset(mac, 0, ETH_ALEN);
3973 if (!adv_mac)
3974 adv_mac = mac;
3975 if (!ses_mac)
3976 ses_mac = mac;
3977 if (!grp_mac)
3978 grp_mac = mac;
3979
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003980 wpas_p2ps_get_feat_cap_str(feat_cap_str, sizeof(feat_cap_str),
3981 feat_cap, feat_cap_len);
3982
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003983 if (prov_start) {
3984 if (session_info == NULL) {
3985 wpa_msg_global(wpa_s, MSG_INFO,
3986 P2P_EVENT_P2PS_PROVISION_START MACSTR
3987 " adv_id=%x conncap=%x"
3988 " adv_mac=" MACSTR
3989 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003990 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003991 MAC2STR(dev), adv_id, conncap,
3992 MAC2STR(adv_mac),
3993 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003994 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003995 } else {
3996 wpa_msg_global(wpa_s, MSG_INFO,
3997 P2P_EVENT_P2PS_PROVISION_START MACSTR
3998 " adv_id=%x conncap=%x"
3999 " adv_mac=" MACSTR
4000 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004001 " dev_passwd_id=%d info='%s'%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004002 MAC2STR(dev), adv_id, conncap,
4003 MAC2STR(adv_mac),
4004 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004005 passwd_id, session_info, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004006 }
4007 return;
4008 }
4009
4010 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4011 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4012
4013 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4014 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4015 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4016
4017 if (persistent_go && !persistent_go->num_p2p_clients) {
4018 /* remove empty persistent GO */
4019 wpa_config_remove_network(wpa_s->conf,
4020 persistent_go->id);
4021 }
4022
4023 wpa_msg_global(wpa_s, MSG_INFO,
4024 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4025 " status=%d"
4026 " adv_id=%x adv_mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004027 " session=%x mac=" MACSTR "%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004028 MAC2STR(dev), status,
4029 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004030 ses_id, MAC2STR(ses_mac), feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004031 return;
4032 }
4033
4034 /* Clean up stale persistent groups with this device */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004035 if (persist_ssid && persist_ssid_size)
4036 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4037 persist_ssid_size);
4038
4039 if (persist_ssid && s && s->mode != WPAS_MODE_P2P_GO &&
4040 is_zero_ether_addr(grp_mac)) {
4041 wpa_dbg(wpa_s, MSG_ERROR,
4042 "P2P: Peer device is a GO in a persistent group, but it did not provide the intended MAC address");
4043 return;
4044 }
4045
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004046 for (;;) {
4047 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4048 if (!stale)
4049 break;
4050
4051 if (s && s->ssid_len == stale->ssid_len &&
4052 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4053 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4054 break;
4055
4056 /* Remove stale persistent group */
4057 if (stale->mode != WPAS_MODE_P2P_GO ||
4058 stale->num_p2p_clients <= 1) {
4059 wpa_config_remove_network(wpa_s->conf, stale->id);
4060 } else {
4061 size_t i;
4062
4063 for (i = 0; i < stale->num_p2p_clients; i++) {
4064 if (os_memcmp(stale->p2p_client_list +
4065 i * ETH_ALEN,
4066 dev, ETH_ALEN) == 0) {
4067 os_memmove(stale->p2p_client_list +
4068 i * ETH_ALEN,
4069 stale->p2p_client_list +
4070 (i + 1) * ETH_ALEN,
4071 (stale->num_p2p_clients -
4072 i - 1) * ETH_ALEN);
4073 break;
4074 }
4075 }
4076 stale->num_p2p_clients--;
4077 }
4078 save_config = 1;
4079 }
4080
4081 if (save_config)
4082 p2p_config_write(wpa_s);
4083
4084 if (s) {
4085 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4086 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4087
4088 if (persistent_go && s != persistent_go &&
4089 !persistent_go->num_p2p_clients) {
4090 /* remove empty persistent GO */
4091 wpa_config_remove_network(wpa_s->conf,
4092 persistent_go->id);
4093 /* Save config */
4094 }
4095
4096 wpa_msg_global(wpa_s, MSG_INFO,
4097 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4098 " status=%d"
4099 " adv_id=%x adv_mac=" MACSTR
4100 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004101 " persist=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004102 MAC2STR(dev), status,
4103 adv_id, MAC2STR(adv_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004104 ses_id, MAC2STR(ses_mac), s->id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004105 return;
4106 }
4107
4108 if (conncap == P2PS_SETUP_GROUP_OWNER) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004109 /*
4110 * We need to copy the interface name. Simply saving a
4111 * pointer isn't enough, since if we use pending_interface_name
4112 * it will be overwritten when the group is added.
4113 */
4114 char go_ifname[100];
4115
4116 go_ifname[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004117 if (!go_wpa_s) {
4118 wpa_s->global->pending_p2ps_group = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004119 wpa_s->global->pending_p2ps_group_freq = freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004120
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004121 if (!wpas_p2p_create_iface(wpa_s))
4122 os_memcpy(go_ifname, wpa_s->ifname,
4123 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004124 else if (wpa_s->pending_interface_name[0])
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004125 os_memcpy(go_ifname,
4126 wpa_s->pending_interface_name,
4127 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004128
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004129 if (!go_ifname[0]) {
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004130 wpas_p2ps_prov_complete(
4131 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4132 dev, adv_mac, ses_mac,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004133 grp_mac, adv_id, ses_id, 0, 0,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004134 NULL, 0, 0, 0, NULL, NULL, 0, 0,
4135 NULL, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004136 return;
4137 }
4138
4139 /* If PD Resp complete, start up the GO */
4140 if (response_done && persistent_go) {
4141 wpas_p2p_group_add_persistent(
4142 wpa_s, persistent_go,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004143 0, 0, freq, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004144 persistent_go->mode ==
4145 WPAS_MODE_P2P_GO ?
4146 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004147 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004148 } else if (response_done) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004149 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004150 }
4151
4152 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004153 os_memcpy(wpa_s->p2ps_join_addr, grp_mac,
4154 ETH_ALEN);
4155 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004156 }
4157 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004158 os_memcpy(go_ifname, go_wpa_s->ifname,
4159 sizeof(go_ifname));
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004160
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004161 if (is_zero_ether_addr(grp_mac)) {
4162 wpa_dbg(go_wpa_s, MSG_DEBUG,
4163 "P2P: Setting PIN-1 for ANY");
4164 wpa_supplicant_ap_wps_pin(go_wpa_s, NULL,
4165 "12345670", NULL, 0,
4166 0);
4167 } else {
4168 wpa_dbg(go_wpa_s, MSG_DEBUG,
4169 "P2P: Setting PIN-1 for " MACSTR,
4170 MAC2STR(grp_mac));
4171 wpa_supplicant_ap_wps_pin(go_wpa_s, grp_mac,
4172 "12345670", NULL, 0,
4173 0);
4174 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004175
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004176 os_memcpy(wpa_s->p2ps_join_addr, grp_mac, ETH_ALEN);
4177 wpa_s->p2ps_method_config_any = 1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004178 }
4179
4180 wpa_msg_global(wpa_s, MSG_INFO,
4181 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4182 " status=%d conncap=%x"
4183 " adv_id=%x adv_mac=" MACSTR
4184 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004185 " dev_passwd_id=%d go=%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004186 MAC2STR(dev), status, conncap,
4187 adv_id, MAC2STR(adv_mac),
4188 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004189 passwd_id, go_ifname, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004190 return;
4191 }
4192
4193 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4194 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4195
4196 if (persistent_go && !persistent_go->num_p2p_clients) {
4197 /* remove empty persistent GO */
4198 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
4199 }
4200
4201 if (conncap == P2PS_SETUP_CLIENT) {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004202 char ssid_hex[32 * 2 + 1];
4203
4204 if (group_ssid)
4205 wpa_snprintf_hex(ssid_hex, sizeof(ssid_hex),
4206 group_ssid, group_ssid_len);
4207 else
4208 ssid_hex[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004209 wpa_msg_global(wpa_s, MSG_INFO,
4210 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4211 " status=%d conncap=%x"
4212 " adv_id=%x adv_mac=" MACSTR
4213 " session=%x mac=" MACSTR
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004214 " dev_passwd_id=%d join=" MACSTR "%s%s%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004215 MAC2STR(dev), status, conncap,
4216 adv_id, MAC2STR(adv_mac),
4217 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004218 passwd_id, MAC2STR(grp_mac), feat_cap_str,
4219 group_ssid ? " group_ssid=" : "", ssid_hex);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004220 } else {
4221 wpa_msg_global(wpa_s, MSG_INFO,
4222 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4223 " status=%d conncap=%x"
4224 " adv_id=%x adv_mac=" MACSTR
4225 " session=%x mac=" MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004226 " dev_passwd_id=%d%s",
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004227 MAC2STR(dev), status, conncap,
4228 adv_id, MAC2STR(adv_mac),
4229 ses_id, MAC2STR(ses_mac),
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004230 passwd_id, feat_cap_str);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004231 }
4232}
4233
4234
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004235static int _wpas_p2p_in_progress(void *ctx)
4236{
4237 struct wpa_supplicant *wpa_s = ctx;
4238 return wpas_p2p_in_progress(wpa_s);
4239}
4240
4241
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004242static int wpas_prov_disc_resp_cb(void *ctx)
4243{
4244 struct wpa_supplicant *wpa_s = ctx;
4245 struct wpa_ssid *persistent_go;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004246 unsigned int freq;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004247
4248 if (!wpa_s->global->pending_p2ps_group)
4249 return 0;
4250
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004251 freq = wpa_s->global->pending_p2ps_group_freq;
4252 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004253 wpa_s->global->pending_p2ps_group = 0;
4254
4255 if (wpas_p2p_get_go_group(wpa_s))
4256 return 0;
4257 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4258
4259 if (persistent_go) {
4260 wpas_p2p_group_add_persistent(
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004261 wpa_s, persistent_go, 0, 0, 0, 0, 0, 0, 0, NULL,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004262 persistent_go->mode == WPAS_MODE_P2P_GO ?
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004263 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004264 } else {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004265 wpas_p2p_group_add(wpa_s, 1, freq, 0, 0, 0, 0);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004266 }
4267
4268 return 1;
4269}
4270
4271
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004272static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
4273 unsigned int *len,
4274 unsigned int *freq_list)
4275{
4276 struct wpa_supplicant *wpa_s = ctx;
4277
4278 return wpa_drv_get_pref_freq_list(wpa_s, go ? WPA_IF_P2P_GO :
4279 WPA_IF_P2P_CLIENT, len, freq_list);
4280}
4281
4282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004283/**
4284 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4285 * @global: Pointer to global data from wpa_supplicant_init()
4286 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4287 * Returns: 0 on success, -1 on failure
4288 */
4289int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4290{
4291 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 int i;
4293
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004294 if (wpa_s->conf->p2p_disabled)
4295 return 0;
4296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4298 return 0;
4299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 if (global->p2p)
4301 return 0;
4302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004305 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306 p2p.p2p_scan = wpas_p2p_scan;
4307 p2p.send_action = wpas_send_action;
4308 p2p.send_action_done = wpas_send_action_done;
4309 p2p.go_neg_completed = wpas_go_neg_completed;
4310 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4311 p2p.dev_found = wpas_dev_found;
4312 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004313 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314 p2p.start_listen = wpas_start_listen;
4315 p2p.stop_listen = wpas_stop_listen;
4316 p2p.send_probe_resp = wpas_send_probe_resp;
4317 p2p.sd_request = wpas_sd_request;
4318 p2p.sd_response = wpas_sd_response;
4319 p2p.prov_disc_req = wpas_prov_disc_req;
4320 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07004321 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004322 p2p.invitation_process = wpas_invitation_process;
4323 p2p.invitation_received = wpas_invitation_received;
4324 p2p.invitation_result = wpas_invitation_result;
4325 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004326 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004327 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08004328 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004329 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004330 p2p.get_persistent_group = wpas_get_persistent_group;
4331 p2p.get_go_info = wpas_get_go_info;
4332 p2p.remove_stale_groups = wpas_remove_stale_groups;
4333 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
4334 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
4335 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004336 p2p.get_pref_freq_list = wpas_p2p_get_pref_freq_list;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004337
4338 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004339 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004340 p2p.dev_name = wpa_s->conf->device_name;
4341 p2p.manufacturer = wpa_s->conf->manufacturer;
4342 p2p.model_name = wpa_s->conf->model_name;
4343 p2p.model_number = wpa_s->conf->model_number;
4344 p2p.serial_number = wpa_s->conf->serial_number;
4345 if (wpa_s->wps) {
4346 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4347 p2p.config_methods = wpa_s->wps->config_methods;
4348 }
4349
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004350 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4351 wpa_printf(MSG_ERROR,
4352 "P2P: Failed to configure supported channel list");
4353 return -1;
4354 }
4355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356 if (wpa_s->conf->p2p_listen_reg_class &&
4357 wpa_s->conf->p2p_listen_channel) {
4358 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4359 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004360 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 /*
4363 * Pick one of the social channels randomly as the listen
4364 * channel.
4365 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004366 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4367 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004368 wpa_printf(MSG_INFO,
4369 "P2P: No social channels supported by the driver - do not enable P2P");
4370 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004371 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004372 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004374 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4375 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004376
4377 if (wpa_s->conf->p2p_oper_reg_class &&
4378 wpa_s->conf->p2p_oper_channel) {
4379 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4380 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4381 p2p.cfg_op_channel = 1;
4382 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4383 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4384
4385 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004387 * Use random operation channel from 2.4 GHz band social
4388 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4389 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004390 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004391 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4392 &p2p.op_channel) != 0) {
4393 wpa_printf(MSG_ERROR,
4394 "P2P: Failed to select random social channel as operation channel");
4395 return -1;
4396 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 p2p.cfg_op_channel = 0;
4398 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4399 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4400 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004401
4402 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4403 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4404 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4405 }
4406
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004407 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4408 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4409 p2p.country[2] = 0x04;
4410 } else
4411 os_memcpy(p2p.country, "XX\x04", 3);
4412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004413 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4414 WPS_DEV_TYPE_LEN);
4415
4416 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4417 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4418 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4419
4420 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4421 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4422
4423 p2p.max_peers = 100;
4424
4425 if (wpa_s->conf->p2p_ssid_postfix) {
4426 p2p.ssid_postfix_len =
4427 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4428 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4429 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4430 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4431 p2p.ssid_postfix_len);
4432 }
4433
4434 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4435
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004436 p2p.max_listen = wpa_s->max_remain_on_chan;
4437
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004438 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4439 wpa_s->conf->p2p_passphrase_len <= 63)
4440 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4441 else
4442 p2p.passphrase_len = 8;
4443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 global->p2p = p2p_init(&p2p);
4445 if (global->p2p == NULL)
4446 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004447 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448
4449 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4450 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4451 continue;
4452 p2p_add_wps_vendor_extension(
4453 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4454 }
4455
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004456 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 return 0;
4459}
4460
4461
4462/**
4463 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4464 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4465 *
4466 * This function deinitialize per-interface P2P data.
4467 */
4468void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4469{
4470 if (wpa_s->driver && wpa_s->drv_priv)
4471 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004472
4473 if (wpa_s->go_params) {
4474 /* Clear any stored provisioning info */
4475 p2p_clear_provisioning_info(
4476 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004477 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004478 }
4479
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004480 os_free(wpa_s->go_params);
4481 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004482 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4484 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4485 wpa_s->p2p_long_listen = 0;
4486 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4487 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4488 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004489 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004490 wpas_p2p_listen_work_done(wpa_s);
4491 if (wpa_s->p2p_send_action_work) {
4492 os_free(wpa_s->p2p_send_action_work->ctx);
4493 radio_work_done(wpa_s->p2p_send_action_work);
4494 wpa_s->p2p_send_action_work = NULL;
4495 }
4496 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004498 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4499 wpa_s->p2p_oob_dev_pw = NULL;
4500
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004501 os_free(wpa_s->p2p_group_common_freqs);
4502 wpa_s->p2p_group_common_freqs = NULL;
4503 wpa_s->p2p_group_common_freqs_num = 0;
4504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 /* TODO: remove group interface from the driver if this wpa_s instance
4506 * is on top of a P2P group interface */
4507}
4508
4509
4510/**
4511 * wpas_p2p_deinit_global - Deinitialize global P2P module
4512 * @global: Pointer to global data from wpa_supplicant_init()
4513 *
4514 * This function deinitializes the global (per device) P2P module.
4515 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004516static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517{
4518 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004519
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004520 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004521
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004522 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523
4524 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004525 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4526 wpa_s = wpa_s->next;
4527 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 tmp = global->ifaces;
4529 while (tmp &&
4530 (tmp == wpa_s ||
4531 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4532 tmp = tmp->next;
4533 }
4534 if (tmp == NULL)
4535 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004536 /* Disconnect from the P2P group and deinit the interface */
4537 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004538 }
4539
4540 /*
4541 * Deinit GO data on any possibly remaining interface (if main
4542 * interface is used as GO).
4543 */
4544 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4545 if (wpa_s->ap_iface)
4546 wpas_p2p_group_deinit(wpa_s);
4547 }
4548
4549 p2p_deinit(global->p2p);
4550 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004551 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004552}
4553
4554
4555static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4556{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004557 if (wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004558 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 if (wpa_s->drv_flags &
4560 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4561 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4562 return 1; /* P2P group requires a new interface in every case
4563 */
4564 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4565 return 0; /* driver does not support concurrent operations */
4566 if (wpa_s->global->ifaces->next)
4567 return 1; /* more that one interface already in use */
4568 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4569 return 1; /* this interface is already in use */
4570 return 0;
4571}
4572
4573
4574static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4575 const u8 *peer_addr,
4576 enum p2p_wps_method wps_method,
4577 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004578 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004579 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004581 if (persistent_group && wpa_s->conf->persistent_reconnect)
4582 persistent_group = 2;
4583
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004584 /*
4585 * Increase GO config timeout if HT40 is used since it takes some time
4586 * to scan channels for coex purposes before the BSS can be started.
4587 */
4588 p2p_set_config_timeout(wpa_s->global->p2p,
4589 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4592 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004593 persistent_group, ssid ? ssid->ssid : NULL,
4594 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004595 wpa_s->p2p_pd_before_go_neg, pref_freq,
4596 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4597 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598}
4599
4600
4601static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4602 const u8 *peer_addr,
4603 enum p2p_wps_method wps_method,
4604 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004605 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004606 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004608 if (persistent_group && wpa_s->conf->persistent_reconnect)
4609 persistent_group = 2;
4610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4612 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004613 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004614 ssid ? ssid->ssid_len : 0, pref_freq,
4615 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4616 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617}
4618
4619
4620static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4621{
4622 wpa_s->p2p_join_scan_count++;
4623 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4624 wpa_s->p2p_join_scan_count);
4625 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4626 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4627 " for join operationg - stop join attempt",
4628 MAC2STR(wpa_s->pending_join_iface_addr));
4629 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004630 if (wpa_s->p2p_auto_pd) {
4631 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004632 wpa_msg_global(wpa_s, MSG_INFO,
4633 P2P_EVENT_PROV_DISC_FAILURE
4634 " p2p_dev_addr=" MACSTR " status=N/A",
4635 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004636 return;
4637 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004638 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004639 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004640 wpas_notify_p2p_group_formation_failure(wpa_s, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641 }
4642}
4643
4644
Dmitry Shmidt04949592012-07-19 12:16:46 -07004645static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4646{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004647 int res;
4648 unsigned int num, i;
4649 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004650
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004651 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4652 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004653 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004654 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004655
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004656 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4657 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004658 if (!freqs)
4659 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004660
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004661 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4662 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004663
4664 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004665 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004666 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4667 freq);
4668 res = 0;
4669 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004670 }
4671 }
4672
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004673 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004674 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004675
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004676exit_free:
4677 os_free(freqs);
4678 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004679}
4680
4681
4682static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4683 const u8 *peer_dev_addr)
4684{
4685 struct wpa_bss *bss;
4686 int updated;
4687
4688 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4689 if (bss == NULL)
4690 return -1;
4691 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4692 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4693 "last scan");
4694 return 0;
4695 }
4696
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004697 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4698 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004699 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4700 "%ld.%06ld (%supdated in last scan)",
4701 bss->last_update.sec, bss->last_update.usec,
4702 updated ? "": "not ");
4703
4704 return updated;
4705}
4706
4707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4709 struct wpa_scan_results *scan_res)
4710{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004711 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 int freq;
4713 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004715 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4716
4717 if (wpa_s->global->p2p_disabled)
4718 return;
4719
Dmitry Shmidt04949592012-07-19 12:16:46 -07004720 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4721 scan_res ? (int) scan_res->num : -1,
4722 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004723
4724 if (scan_res)
4725 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4726
Dmitry Shmidt04949592012-07-19 12:16:46 -07004727 if (wpa_s->p2p_auto_pd) {
4728 int join = wpas_p2p_peer_go(wpa_s,
4729 wpa_s->pending_join_dev_addr);
4730 if (join == 0 &&
4731 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4732 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004733 bss = wpa_bss_get_bssid_latest(
4734 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004735 if (bss) {
4736 freq = bss->freq;
4737 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4738 "the peer " MACSTR " at %d MHz",
4739 wpa_s->auto_pd_scan_retry,
4740 MAC2STR(wpa_s->
4741 pending_join_dev_addr),
4742 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004743 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004744 return;
4745 }
4746 }
4747
4748 if (join < 0)
4749 join = 0;
4750
4751 wpa_s->p2p_auto_pd = 0;
4752 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4753 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4754 MAC2STR(wpa_s->pending_join_dev_addr), join);
4755 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004756 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004757 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004758 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004759 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004760 wpa_msg_global(wpa_s, MSG_INFO,
4761 P2P_EVENT_PROV_DISC_FAILURE
4762 " p2p_dev_addr=" MACSTR " status=N/A",
4763 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004764 }
4765 return;
4766 }
4767
4768 if (wpa_s->p2p_auto_join) {
4769 int join = wpas_p2p_peer_go(wpa_s,
4770 wpa_s->pending_join_dev_addr);
4771 if (join < 0) {
4772 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4773 "running a GO -> use GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004774 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004775 P2P_EVENT_FALLBACK_TO_GO_NEG
4776 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004777 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4778 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4779 wpa_s->p2p_persistent_group, 0, 0, 0,
4780 wpa_s->p2p_go_intent,
4781 wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004782 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004783 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004784 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004785 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08004786 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004787 wpa_s->p2p_go_max_oper_chwidth,
4788 NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004789 return;
4790 }
4791
4792 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4793 "try to join the group", join ? "" :
4794 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004795 if (!join) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004796 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004797 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004798 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004799 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004800 }
4801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4803 wpa_s->pending_join_iface_addr);
4804 if (freq < 0 &&
4805 p2p_get_interface_addr(wpa_s->global->p2p,
4806 wpa_s->pending_join_dev_addr,
4807 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004808 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4809 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004810 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4811 "address for join from " MACSTR " to " MACSTR
4812 " based on newly discovered P2P peer entry",
4813 MAC2STR(wpa_s->pending_join_iface_addr),
4814 MAC2STR(iface_addr));
4815 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4816 ETH_ALEN);
4817
4818 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4819 wpa_s->pending_join_iface_addr);
4820 }
4821 if (freq >= 0) {
4822 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4823 "from P2P peer table: %d MHz", freq);
4824 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004825 if (wpa_s->p2p_join_ssid_len) {
4826 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4827 MACSTR " and SSID %s",
4828 MAC2STR(wpa_s->pending_join_iface_addr),
4829 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4830 wpa_s->p2p_join_ssid_len));
4831 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4832 wpa_s->p2p_join_ssid,
4833 wpa_s->p2p_join_ssid_len);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004834 } else if (!bss) {
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004835 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4836 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4837 bss = wpa_bss_get_bssid_latest(wpa_s,
4838 wpa_s->pending_join_iface_addr);
4839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004841 u8 dev_addr[ETH_ALEN];
4842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 freq = bss->freq;
4844 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004845 "from BSS table: %d MHz (SSID %s)", freq,
4846 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004847 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4848 dev_addr) == 0 &&
4849 os_memcmp(wpa_s->pending_join_dev_addr,
4850 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4851 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4852 ETH_ALEN) != 0) {
4853 wpa_printf(MSG_DEBUG,
4854 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4855 MAC2STR(dev_addr),
4856 MAC2STR(wpa_s->pending_join_dev_addr));
4857 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4858 ETH_ALEN);
4859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 }
4861 if (freq > 0) {
4862 u16 method;
4863
Dmitry Shmidt04949592012-07-19 12:16:46 -07004864 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004865 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004866 P2P_EVENT_GROUP_FORMATION_FAILURE
4867 "reason=FREQ_CONFLICT");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004868 wpas_notify_p2p_group_formation_failure(
4869 wpa_s, "FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004870 return;
4871 }
4872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4874 "prior to joining an existing group (GO " MACSTR
4875 " freq=%u MHz)",
4876 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4877 wpa_s->pending_pd_before_join = 1;
4878
4879 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004880 case WPS_PIN_DISPLAY:
4881 method = WPS_CONFIG_KEYPAD;
4882 break;
4883 case WPS_PIN_KEYPAD:
4884 method = WPS_CONFIG_DISPLAY;
4885 break;
4886 case WPS_PBC:
4887 method = WPS_CONFIG_PUSHBUTTON;
4888 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004889 case WPS_P2PS:
4890 method = WPS_CONFIG_P2PS;
4891 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 default:
4893 method = 0;
4894 break;
4895 }
4896
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004897 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4898 wpa_s->pending_join_dev_addr) ==
4899 method)) {
4900 /*
4901 * We have already performed provision discovery for
4902 * joining the group. Proceed directly to join
4903 * operation without duplicated provision discovery. */
4904 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4905 "with " MACSTR " already done - proceed to "
4906 "join",
4907 MAC2STR(wpa_s->pending_join_dev_addr));
4908 wpa_s->pending_pd_before_join = 0;
4909 goto start;
4910 }
4911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004913 wpa_s->pending_join_dev_addr,
4914 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004915 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4917 "Discovery Request before joining an "
4918 "existing group");
4919 wpa_s->pending_pd_before_join = 0;
4920 goto start;
4921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 return;
4923 }
4924
4925 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4926 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4927 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4928 wpas_p2p_check_join_scan_limit(wpa_s);
4929 return;
4930
4931start:
4932 /* Start join operation immediately */
Dmitry Shmidtde47be72016-01-07 12:52:55 -08004933 wpas_p2p_join_start(wpa_s, 0, wpa_s->p2p_join_ssid,
4934 wpa_s->p2p_join_ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935}
4936
4937
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004938static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4939 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004940{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004941 int ret;
4942 struct wpa_driver_scan_params params;
4943 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004944 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004945 int freqs[2] = { 0, 0 };
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004946 unsigned int bands;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004947
4948 os_memset(&params, 0, sizeof(params));
4949
4950 /* P2P Wildcard SSID */
4951 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004952 if (ssid && ssid_len) {
4953 params.ssids[0].ssid = ssid;
4954 params.ssids[0].ssid_len = ssid_len;
4955 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4956 wpa_s->p2p_join_ssid_len = ssid_len;
4957 } else {
4958 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4959 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4960 wpa_s->p2p_join_ssid_len = 0;
4961 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004962
4963 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004964 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4965 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4966 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 if (wps_ie == NULL) {
4968 wpas_p2p_scan_res_join(wpa_s, NULL);
4969 return;
4970 }
4971
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004972 if (!freq) {
4973 int oper_freq;
4974 /*
4975 * If freq is not provided, check the operating freq of the GO
4976 * and use a single channel scan on if possible.
4977 */
4978 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4979 wpa_s->pending_join_iface_addr);
4980 if (oper_freq > 0)
4981 freq = oper_freq;
4982 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004983 if (freq > 0) {
4984 freqs[0] = freq;
4985 params.freqs = freqs;
4986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987
Dmitry Shmidt9c175262016-03-03 10:20:07 -08004988 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4989 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4990 if (ies == NULL) {
4991 wpabuf_free(wps_ie);
4992 wpas_p2p_scan_res_join(wpa_s, NULL);
4993 return;
4994 }
4995 wpabuf_put_buf(ies, wps_ie);
4996 wpabuf_free(wps_ie);
4997
4998 bands = wpas_get_bands(wpa_s, freqs);
4999 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
5000
5001 params.p2p_probe = 1;
5002 params.extra_ies = wpabuf_head(ies);
5003 params.extra_ies_len = wpabuf_len(ies);
5004
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005005 /*
5006 * Run a scan to update BSS table and start Provision Discovery once
5007 * the new scan results become available.
5008 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005009 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005010 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005011 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005012 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005013 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005014 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005015
5016 wpabuf_free(ies);
5017
5018 if (ret) {
5019 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5020 "try again later");
5021 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5022 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5023 wpas_p2p_check_join_scan_limit(wpa_s);
5024 }
5025}
5026
5027
Dmitry Shmidt04949592012-07-19 12:16:46 -07005028static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5029{
5030 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005031 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005032}
5033
5034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005035static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005036 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005037 int auto_join, int op_freq,
5038 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005039{
5040 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005041 MACSTR " dev " MACSTR " op_freq=%d)%s",
5042 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005043 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005044 if (ssid && ssid_len) {
5045 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5046 wpa_ssid_txt(ssid, ssid_len));
5047 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048
Dmitry Shmidt04949592012-07-19 12:16:46 -07005049 wpa_s->p2p_auto_pd = 0;
5050 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5052 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5053 wpa_s->pending_join_wps_method = wps_method;
5054
5055 /* Make sure we are not running find during connection establishment */
5056 wpas_p2p_stop_find(wpa_s);
5057
5058 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005059 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005060 return 0;
5061}
5062
5063
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005064static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5065 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066{
5067 struct wpa_supplicant *group;
5068 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005069 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070
5071 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5072 if (group == NULL)
5073 return -1;
5074 if (group != wpa_s) {
5075 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5076 sizeof(group->p2p_pin));
5077 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005078 } else {
5079 /*
5080 * Need to mark the current interface for p2p_group_formation
5081 * when a separate group interface is not used. This is needed
5082 * to allow p2p_cancel stop a pending p2p_connect-join.
5083 * wpas_p2p_init_group_interface() addresses this for the case
5084 * where a separate group interface is used.
5085 */
5086 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005087 }
5088
5089 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005090 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091
5092 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005093 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5095 ETH_ALEN);
5096 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005097 if (freq && ssid && ssid_len) {
5098 res.freq = freq;
5099 res.ssid_len = ssid_len;
5100 os_memcpy(res.ssid, ssid, ssid_len);
5101 } else {
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005102 if (ssid && ssid_len) {
5103 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5104 ssid, ssid_len);
5105 } else {
5106 bss = wpa_bss_get_bssid_latest(
5107 wpa_s, wpa_s->pending_join_iface_addr);
5108 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005109 if (bss) {
5110 res.freq = bss->freq;
5111 res.ssid_len = bss->ssid_len;
5112 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5113 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5114 bss->freq,
5115 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005116 } else if (ssid && ssid_len) {
5117 res.ssid_len = ssid_len;
5118 os_memcpy(res.ssid, ssid, ssid_len);
5119 wpa_printf(MSG_DEBUG, "P2P: Join target GO (SSID %s)",
5120 wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005121 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005122 }
5123
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005124 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5125 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5126 "starting client");
5127 wpa_drv_cancel_remain_on_channel(wpa_s);
5128 wpa_s->off_channel_freq = 0;
5129 wpa_s->roc_waiting_drv_freq = 0;
5130 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131 wpas_start_wps_enrollee(group, &res);
5132
5133 /*
5134 * Allow a longer timeout for join-a-running-group than normal 15
5135 * second group formation timeout since the GO may not have authorized
5136 * our connection yet.
5137 */
5138 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5139 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5140 wpa_s, NULL);
5141
5142 return 0;
5143}
5144
5145
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005146static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005147 int *force_freq, int *pref_freq, int go,
5148 unsigned int *pref_freq_list,
5149 unsigned int *num_pref_freq)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005150{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005151 struct wpa_used_freq_data *freqs;
5152 int res, best_freq, num_unused;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005153 unsigned int freq_in_use = 0, num, i, max_pref_freq;
5154
5155 max_pref_freq = *num_pref_freq;
5156 *num_pref_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005157
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005158 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5159 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005160 if (!freqs)
5161 return -1;
5162
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005163 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5164 wpa_s->num_multichan_concurrent);
5165
5166 /*
5167 * It is possible that the total number of used frequencies is bigger
5168 * than the number of frequencies used for P2P, so get the system wide
5169 * number of unused frequencies.
5170 */
5171 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5172
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005173 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005174 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5175 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005176
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005177 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005178 int ret;
5179 if (go)
5180 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5181 else
5182 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5183 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005184 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5185 ieee80211_is_dfs(freq)) {
5186 /*
5187 * If freq is a DFS channel and DFS is offloaded
5188 * to the driver, allow P2P GO to use it.
5189 */
5190 wpa_printf(MSG_DEBUG,
5191 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
5192 freq);
5193 } else {
5194 wpa_printf(MSG_DEBUG,
5195 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
5196 freq);
5197 res = -3;
5198 goto exit_free;
5199 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005200 }
5201
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005202 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005203 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005204 freq_in_use = 1;
5205 }
5206
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005207 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005208 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5209 freq);
5210 res = -2;
5211 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005212 }
5213 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5214 "requested channel (%u MHz)", freq);
5215 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005216 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005217 }
5218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005219 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005220
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005221 if (!wpa_s->conf->num_p2p_pref_chan && *pref_freq == 0) {
5222 enum wpa_driver_if_type iface_type;
5223
5224 if (go)
5225 iface_type = WPA_IF_P2P_GO;
5226 else
5227 iface_type = WPA_IF_P2P_CLIENT;
5228
5229 wpa_printf(MSG_DEBUG, "P2P: best_freq=%d, go=%d",
5230 best_freq, go);
5231
5232 res = wpa_drv_get_pref_freq_list(wpa_s, iface_type,
5233 &max_pref_freq,
5234 pref_freq_list);
5235 if (!res && max_pref_freq > 0) {
5236 *num_pref_freq = max_pref_freq;
5237 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005238 while (i < *num_pref_freq &&
5239 (!p2p_supported_freq(wpa_s->global->p2p,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005240 pref_freq_list[i]) ||
5241 wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt29333592017-01-09 12:27:11 -08005242 pref_freq_list[i]))) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005243 wpa_printf(MSG_DEBUG,
5244 "P2P: preferred_freq_list[%d]=%d is disallowed",
5245 i, pref_freq_list[i]);
5246 i++;
5247 }
5248 if (i != *num_pref_freq) {
5249 best_freq = pref_freq_list[i];
5250 wpa_printf(MSG_DEBUG,
5251 "P2P: Using preferred_freq_list[%d]=%d",
5252 i, best_freq);
5253 } else {
5254 wpa_printf(MSG_DEBUG,
5255 "P2P: All driver preferred frequencies are disallowed for P2P use");
5256 *num_pref_freq = 0;
5257 }
5258 } else {
5259 wpa_printf(MSG_DEBUG,
5260 "P2P: No preferred frequency list available");
5261 }
5262 }
5263
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005264 /* We have a candidate frequency to use */
5265 if (best_freq > 0) {
5266 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005267 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005268 best_freq);
5269 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005270 } else {
5271 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 -07005272 best_freq);
5273 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005274 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005275 } else if (num_unused > 0) {
5276 wpa_printf(MSG_DEBUG,
5277 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5278 *force_freq = 0;
5279 } else {
5280 wpa_printf(MSG_DEBUG,
5281 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5282 res = -2;
5283 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005284 }
5285
5286exit_ok:
5287 res = 0;
5288exit_free:
5289 os_free(freqs);
5290 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005291}
5292
5293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005294/**
5295 * wpas_p2p_connect - Request P2P Group Formation to be started
5296 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5297 * @peer_addr: Address of the peer P2P Device
5298 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5299 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005300 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005301 * @join: Whether to join an existing group (as a client) instead of starting
5302 * Group Owner negotiation; @peer_addr is BSSID in that case
5303 * @auth: Whether to only authorize the connection instead of doing that and
5304 * initiating Group Owner negotiation
5305 * @go_intent: GO Intent or -1 to use default
5306 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005307 * @freq2: Center frequency of segment 1 for the GO operating in VHT 80P80 mode
Dmitry Shmidt04949592012-07-19 12:16:46 -07005308 * @persistent_id: Persistent group credentials to use for forcing GO
5309 * parameters or -1 to generate new values (SSID/passphrase)
5310 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5311 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005312 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005313 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005314 * @vht_chwidth: Channel width supported by GO operating with VHT support
5315 * (VHT_CHANWIDTH_*).
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005316 * @group_ssid: Specific Group SSID for join or %NULL if not set
5317 * @group_ssid_len: Length of @group_ssid in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5319 * failure, -2 on failure due to channel not currently available,
5320 * -3 if forced channel is not supported
5321 */
5322int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5323 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005324 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005325 int go_intent, int freq, unsigned int vht_center_freq2,
5326 int persistent_id, int pd, int ht40, int vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005327 unsigned int vht_chwidth, const u8 *group_ssid,
5328 size_t group_ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005330 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005331 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005332 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005333 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005334 struct wpa_ssid *ssid = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005335 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336
5337 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5338 return -1;
5339
Dmitry Shmidt04949592012-07-19 12:16:46 -07005340 if (persistent_id >= 0) {
5341 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5342 if (ssid == NULL || ssid->disabled != 2 ||
5343 ssid->mode != WPAS_MODE_P2P_GO)
5344 return -1;
5345 }
5346
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005347 os_free(wpa_s->global->add_psk);
5348 wpa_s->global->add_psk = NULL;
5349
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005350 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005351 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005352 wpa_s->global->pending_p2ps_group_freq = 0;
5353 wpa_s->p2ps_method_config_any = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 if (go_intent < 0)
5356 go_intent = wpa_s->conf->p2p_go_intent;
5357
5358 if (!auth)
5359 wpa_s->p2p_long_listen = 0;
5360
5361 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005362 wpa_s->p2p_persistent_group = !!persistent_group;
5363 wpa_s->p2p_persistent_id = persistent_id;
5364 wpa_s->p2p_go_intent = go_intent;
5365 wpa_s->p2p_connect_freq = freq;
5366 wpa_s->p2p_fallback_to_go_neg = 0;
5367 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005368 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005369 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005370 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
5371 wpa_s->p2p_go_max_oper_chwidth = vht_chwidth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372
5373 if (pin)
5374 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5375 else if (wps_method == WPS_PIN_DISPLAY) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08005376 if (wps_generate_pin((unsigned int *) &ret) < 0)
5377 return -1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005378 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5379 "%08d", ret);
5380 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5381 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5383 wpa_s->p2p_pin);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07005384 } else if (wps_method == WPS_P2PS) {
5385 /* Force the P2Ps default PIN to be used */
5386 os_strlcpy(wpa_s->p2p_pin, "12345670", sizeof(wpa_s->p2p_pin));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 } else
5388 wpa_s->p2p_pin[0] = '\0';
5389
Dmitry Shmidt04949592012-07-19 12:16:46 -07005390 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5392 if (auth) {
5393 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5394 "connect a running group from " MACSTR,
5395 MAC2STR(peer_addr));
5396 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5397 return ret;
5398 }
5399 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5400 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5401 iface_addr) < 0) {
5402 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5403 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5404 dev_addr);
5405 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005406 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005407 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005408 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5409 "%ld.%06ld",
5410 wpa_s->p2p_auto_started.sec,
5411 wpa_s->p2p_auto_started.usec);
5412 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005413 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005414 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08005415 auto_join, freq,
5416 group_ssid, group_ssid_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005417 return -1;
5418 return ret;
5419 }
5420
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005421 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005422 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005423 go_intent == 15, pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005424 if (res)
5425 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005426 wpas_p2p_set_own_freq_preference(wpa_s,
5427 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005428
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005429 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
5430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5432
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005433 if (wpa_s->create_p2p_iface) {
5434 /* Prepare to add a new interface for the group */
5435 iftype = WPA_IF_P2P_GROUP;
5436 if (go_intent == 15)
5437 iftype = WPA_IF_P2P_GO;
5438 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5439 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5440 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005441 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005444 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005445 } else {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005446 if (wpa_s->p2p_mgmt)
5447 if_addr = wpa_s->parent->own_addr;
5448 else
5449 if_addr = wpa_s->own_addr;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005450 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
5451 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005452
5453 if (auth) {
5454 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005455 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005456 force_freq, persistent_group, ssid,
5457 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 return -1;
5459 return ret;
5460 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005461
5462 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5463 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005464 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465 if (wpa_s->create_p2p_iface)
5466 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 return -1;
5468 }
5469 return ret;
5470}
5471
5472
5473/**
5474 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5475 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5476 * @freq: Frequency of the channel in MHz
5477 * @duration: Duration of the stay on the channel in milliseconds
5478 *
5479 * This callback is called when the driver indicates that it has started the
5480 * requested remain-on-channel duration.
5481 */
5482void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5483 unsigned int freq, unsigned int duration)
5484{
5485 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5486 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005487 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)",
5488 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5489 wpa_s->roc_waiting_drv_freq, freq, duration);
5490 if (wpa_s->off_channel_freq &&
5491 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005492 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5493 wpa_s->pending_listen_duration);
5494 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08005495 } else {
5496 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5497 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5498 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 }
5500}
5501
5502
Jithu Jance57cea1a2014-08-20 10:22:04 -07005503int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504{
5505 /* Limit maximum Listen state time based on driver limitation. */
5506 if (timeout > wpa_s->max_remain_on_chan)
5507 timeout = wpa_s->max_remain_on_chan;
5508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005509 return p2p_listen(wpa_s->global->p2p, timeout);
5510}
5511
5512
5513/**
5514 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5515 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5516 * @freq: Frequency of the channel in MHz
5517 *
5518 * This callback is called when the driver indicates that a remain-on-channel
5519 * operation has been completed, i.e., the duration on the requested channel
5520 * has timed out.
5521 */
5522void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5523 unsigned int freq)
5524{
5525 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5526 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005527 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005528 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005529 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5530 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005531 if (wpa_s->p2p_long_listen > 0)
5532 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5534 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005535 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 if (wpa_s->p2p_long_listen > 0) {
5538 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5539 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005540 } else {
5541 /*
5542 * When listen duration is over, stop listen & update p2p_state
5543 * to IDLE.
5544 */
5545 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005546 }
5547}
5548
5549
5550/**
5551 * wpas_p2p_group_remove - Remove a P2P group
5552 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5553 * @ifname: Network interface name of the group interface or "*" to remove all
5554 * groups
5555 * Returns: 0 on success, -1 on failure
5556 *
5557 * This function is used to remove a P2P group. This can be used to disconnect
5558 * from a group in which the local end is a P2P Client or to end a P2P Group in
5559 * case the local end is the Group Owner. If a virtual network interface was
5560 * created for this group, that interface will be removed. Otherwise, only the
5561 * configured P2P group network will be removed from the interface.
5562 */
5563int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5564{
5565 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005566 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567
5568 if (os_strcmp(ifname, "*") == 0) {
5569 struct wpa_supplicant *prev;
5570 wpa_s = global->ifaces;
5571 while (wpa_s) {
5572 prev = wpa_s;
5573 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005574 if (prev->p2p_group_interface !=
5575 NOT_P2P_GROUP_INTERFACE ||
5576 (prev->current_ssid &&
5577 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005578 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005579 }
5580 return 0;
5581 }
5582
5583 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5584 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5585 break;
5586 }
5587
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005588 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589}
5590
5591
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005592static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5593{
5594 unsigned int r;
5595
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005596 if (!wpa_s->conf->num_p2p_pref_chan && !freq) {
5597 unsigned int i, size = P2P_MAX_PREF_CHANNELS;
5598 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS];
5599 int res;
5600
5601 res = wpa_drv_get_pref_freq_list(wpa_s, WPA_IF_P2P_GO,
5602 &size, pref_freq_list);
5603 if (!res && size > 0) {
5604 i = 0;
Dmitry Shmidt29333592017-01-09 12:27:11 -08005605 while (i < size &&
5606 wpas_p2p_disallowed_freq(wpa_s->global,
5607 pref_freq_list[i])) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005608 wpa_printf(MSG_DEBUG,
5609 "P2P: preferred_freq_list[%d]=%d is disallowed",
5610 i, pref_freq_list[i]);
5611 i++;
5612 }
5613 if (i != size) {
5614 freq = pref_freq_list[i];
5615 wpa_printf(MSG_DEBUG,
5616 "P2P: Using preferred_freq_list[%d]=%d",
5617 i, freq);
5618 } else {
5619 wpa_printf(MSG_DEBUG,
5620 "P2P: All driver preferred frequencies are disallowed for P2P use");
5621 }
5622 } else {
5623 wpa_printf(MSG_DEBUG,
5624 "P2P: No preferred frequency list available");
5625 }
5626 }
5627
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005628 if (freq == 2) {
5629 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5630 "band");
5631 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005632 p2p_supported_freq_go(wpa_s->global->p2p,
5633 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005634 freq = wpa_s->best_24_freq;
5635 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5636 "channel: %d MHz", freq);
5637 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005638 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5639 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005640 freq = 2412 + (r % 3) * 25;
5641 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5642 "channel: %d MHz", freq);
5643 }
5644 }
5645
5646 if (freq == 5) {
5647 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5648 "band");
5649 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005650 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005651 wpa_s->best_5_freq)) {
5652 freq = wpa_s->best_5_freq;
5653 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5654 "channel: %d MHz", freq);
5655 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005656 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5657 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005658 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005659 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005660 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5661 "5 GHz channel for P2P group");
5662 return -1;
5663 }
5664 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5665 "channel: %d MHz", freq);
5666 }
5667 }
5668
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005669 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005670 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5671 ieee80211_is_dfs(freq)) {
5672 /*
5673 * If freq is a DFS channel and DFS is offloaded to the
5674 * driver, allow P2P GO to use it.
5675 */
5676 wpa_printf(MSG_DEBUG, "P2P: "
5677 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5678 __func__, freq);
5679 return freq;
5680 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005681 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5682 "(%u MHz) is not supported for P2P uses",
5683 freq);
5684 return -1;
5685 }
5686
5687 return freq;
5688}
5689
5690
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005691static int wpas_p2p_supported_freq_go(struct wpa_supplicant *wpa_s,
5692 const struct p2p_channels *channels,
5693 int freq)
5694{
5695 if (!wpas_p2p_disallowed_freq(wpa_s->global, freq) &&
5696 p2p_supported_freq_go(wpa_s->global->p2p, freq) &&
5697 freq_included(wpa_s, channels, freq))
5698 return 1;
5699 return 0;
5700}
5701
5702
5703static void wpas_p2p_select_go_freq_no_pref(struct wpa_supplicant *wpa_s,
5704 struct p2p_go_neg_results *params,
5705 const struct p2p_channels *channels)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005706{
5707 unsigned int i, r;
5708
5709 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005710 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005711 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005712
5713 for (i = 0; i < 3; i++) {
5714 params->freq = 2412 + ((r + i) % 3) * 25;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005715 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005716 goto out;
5717 }
5718
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005719 /* try all other channels in operating class 81 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005720 for (i = 0; i < 11; i++) {
5721 params->freq = 2412 + i * 5;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005722
5723 /* skip social channels; covered in the previous loop */
5724 if (params->freq == 2412 ||
5725 params->freq == 2437 ||
5726 params->freq == 2462)
5727 continue;
5728
5729 if (wpas_p2p_supported_freq_go(wpa_s, channels, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005730 goto out;
5731 }
5732
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005733 /* try all channels in operating class 115 */
5734 for (i = 0; i < 4; i++) {
5735 params->freq = 5180 + i * 20;
5736 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005737 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005738 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5739 goto out;
5740 }
5741
5742 /* try all channels in operating class 124 */
5743 for (i = 0; i < 4; i++) {
5744 params->freq = 5745 + i * 20;
5745 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005746 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005747 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5748 goto out;
5749 }
5750
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005751 /* try social channel class 180 channel 2 */
5752 params->freq = 58320 + 1 * 2160;
5753 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005754 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005755 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5756 goto out;
5757
5758 /* try all channels in reg. class 180 */
5759 for (i = 0; i < 4; i++) {
5760 params->freq = 58320 + i * 2160;
5761 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005762 freq_included(wpa_s, channels, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005763 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5764 goto out;
5765 }
5766
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005767 params->freq = 0;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005768 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005769 return;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005770out:
5771 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5772 params->freq);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005773}
5774
5775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005776static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5777 struct p2p_go_neg_results *params,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005778 int freq, int vht_center_freq2, int ht40,
5779 int vht, int max_oper_chwidth,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005780 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005781{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005782 struct wpa_used_freq_data *freqs;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005783 unsigned int cand;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005784 unsigned int num, i;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005785 int ignore_no_freqs = 0;
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005786 int unused_channels = wpas_p2p_num_unused_channels(wpa_s) > 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787
5788 os_memset(params, 0, sizeof(*params));
5789 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005790 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005791 params->vht = vht;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08005792 params->max_oper_chwidth = max_oper_chwidth;
5793 params->vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005794
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005795 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5796 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005797 if (!freqs)
5798 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005799
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005800 num = get_shared_radio_freqs_data(wpa_s, freqs,
5801 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005802
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005803 if (wpa_s->current_ssid &&
5804 wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO &&
5805 wpa_s->wpa_state == WPA_COMPLETED) {
5806 wpa_printf(MSG_DEBUG, "P2P: %s called for an active GO",
5807 __func__);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005808
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005809 /*
5810 * If the frequency selection is done for an active P2P GO that
5811 * is not sharing a frequency, allow to select a new frequency
5812 * even if there are no unused frequencies as we are about to
5813 * move the P2P GO so its frequency can be re-used.
5814 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005815 for (i = 0; i < num; i++) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005816 if (freqs[i].freq == wpa_s->current_ssid->frequency &&
5817 freqs[i].flags == 0) {
5818 ignore_no_freqs = 1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005819 break;
5820 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005821 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005822 }
5823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005824 /* try using the forced freq */
5825 if (freq) {
5826 if (!wpas_p2p_supported_freq_go(wpa_s, channels, freq)) {
5827 wpa_printf(MSG_DEBUG,
5828 "P2P: Forced GO freq %d MHz not accepted",
5829 freq);
5830 goto fail;
5831 }
5832
5833 for (i = 0; i < num; i++) {
5834 if (freqs[i].freq == freq) {
5835 wpa_printf(MSG_DEBUG,
5836 "P2P: forced freq (%d MHz) is also shared",
5837 freq);
5838 params->freq = freq;
5839 goto success;
5840 }
5841 }
5842
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005843 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005844 wpa_printf(MSG_DEBUG,
5845 "P2P: Cannot force GO on freq (%d MHz) as all the channels are in use",
5846 freq);
5847 goto fail;
5848 }
5849
5850 wpa_printf(MSG_DEBUG,
5851 "P2P: force GO freq (%d MHz) on a free channel",
5852 freq);
5853 params->freq = freq;
5854 goto success;
5855 }
5856
5857 /* consider using one of the shared frequencies */
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005858 if (num &&
5859 (!wpa_s->conf->p2p_ignore_shared_freq || !unused_channels)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005860 cand = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5861 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5862 wpa_printf(MSG_DEBUG,
5863 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005864 cand);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005865 params->freq = cand;
5866 goto success;
5867 }
5868
5869 /* try using one of the shared freqs */
5870 for (i = 0; i < num; i++) {
5871 if (wpas_p2p_supported_freq_go(wpa_s, channels,
5872 freqs[i].freq)) {
5873 wpa_printf(MSG_DEBUG,
5874 "P2P: Use shared freq (%d MHz) for GO",
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005875 freqs[i].freq);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005876 params->freq = freqs[i].freq;
5877 goto success;
5878 }
5879 }
5880 }
5881
Dmitry Shmidt31a29cc2016-03-09 15:58:17 -08005882 if (!ignore_no_freqs && !unused_channels) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005883 wpa_printf(MSG_DEBUG,
5884 "P2P: Cannot force GO on any of the channels we are already using");
5885 goto fail;
5886 }
5887
5888 /* try using the setting from the configuration file */
5889 if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5890 wpa_s->conf->p2p_oper_channel >= 1 &&
5891 wpa_s->conf->p2p_oper_channel <= 11 &&
5892 wpas_p2p_supported_freq_go(
5893 wpa_s, channels,
5894 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5895 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5896 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5897 "frequency %d MHz", params->freq);
5898 goto success;
5899 }
5900
5901 if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5902 wpa_s->conf->p2p_oper_reg_class == 116 ||
5903 wpa_s->conf->p2p_oper_reg_class == 117 ||
5904 wpa_s->conf->p2p_oper_reg_class == 124 ||
5905 wpa_s->conf->p2p_oper_reg_class == 125 ||
5906 wpa_s->conf->p2p_oper_reg_class == 126 ||
5907 wpa_s->conf->p2p_oper_reg_class == 127) &&
5908 wpas_p2p_supported_freq_go(wpa_s, channels,
5909 5000 +
5910 5 * wpa_s->conf->p2p_oper_channel)) {
5911 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5912 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5913 "frequency %d MHz", params->freq);
5914 goto success;
5915 }
5916
5917 /* Try using best channels */
5918 if (wpa_s->conf->p2p_oper_channel == 0 &&
5919 wpa_s->best_overall_freq > 0 &&
5920 wpas_p2p_supported_freq_go(wpa_s, channels,
5921 wpa_s->best_overall_freq)) {
5922 params->freq = wpa_s->best_overall_freq;
5923 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5924 "channel %d MHz", params->freq);
5925 goto success;
5926 }
5927
5928 if (wpa_s->conf->p2p_oper_channel == 0 &&
5929 wpa_s->best_24_freq > 0 &&
5930 wpas_p2p_supported_freq_go(wpa_s, channels,
5931 wpa_s->best_24_freq)) {
5932 params->freq = wpa_s->best_24_freq;
5933 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5934 "channel %d MHz", params->freq);
5935 goto success;
5936 }
5937
5938 if (wpa_s->conf->p2p_oper_channel == 0 &&
5939 wpa_s->best_5_freq > 0 &&
5940 wpas_p2p_supported_freq_go(wpa_s, channels,
5941 wpa_s->best_5_freq)) {
5942 params->freq = wpa_s->best_5_freq;
5943 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5944 "channel %d MHz", params->freq);
5945 goto success;
5946 }
5947
5948 /* try using preferred channels */
5949 cand = p2p_get_pref_freq(wpa_s->global->p2p, channels);
5950 if (cand && wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5951 params->freq = cand;
5952 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5953 "channels", params->freq);
5954 goto success;
5955 }
5956
5957 /* Try using one of the group common freqs */
5958 if (wpa_s->p2p_group_common_freqs) {
5959 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++) {
5960 cand = wpa_s->p2p_group_common_freqs[i];
5961 if (wpas_p2p_supported_freq_go(wpa_s, channels, cand)) {
5962 params->freq = cand;
5963 wpa_printf(MSG_DEBUG,
5964 "P2P: Use freq %d MHz common with the peer",
5965 params->freq);
5966 goto success;
5967 }
5968 }
5969 }
5970
5971 /* no preference, select some channel */
5972 wpas_p2p_select_go_freq_no_pref(wpa_s, params, channels);
5973
5974 if (params->freq == 0) {
5975 wpa_printf(MSG_DEBUG, "P2P: did not find a freq for GO use");
5976 goto fail;
5977 }
5978
5979success:
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005980 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005981 return 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08005982fail:
5983 os_free(freqs);
5984 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005985}
5986
5987
5988static struct wpa_supplicant *
5989wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5990 int go)
5991{
5992 struct wpa_supplicant *group_wpa_s;
5993
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005994 if (!wpas_p2p_create_iface(wpa_s)) {
Dmitry Shmidt9c175262016-03-03 10:20:07 -08005995 if (wpa_s->p2p_mgmt) {
5996 /*
5997 * We may be called on the p2p_dev interface which
5998 * cannot be used for group operations, so always use
5999 * the primary interface.
6000 */
6001 wpa_s->parent->p2pdev = wpa_s;
6002 wpa_s = wpa_s->parent;
6003 }
6004 wpa_dbg(wpa_s, MSG_DEBUG,
6005 "P2P: Use primary interface for group operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006006 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006007 if (wpa_s != wpa_s->p2pdev)
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006008 wpas_p2p_clone_config(wpa_s, wpa_s->p2pdev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006010 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011
6012 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006013 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006014 wpa_msg_global(wpa_s, MSG_ERROR,
6015 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006016 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006017 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6019 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006020 wpa_msg_global(wpa_s, MSG_ERROR,
6021 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 wpas_p2p_remove_pending_group_interface(wpa_s);
6023 return NULL;
6024 }
6025
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006026 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6027 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006028 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006029 return group_wpa_s;
6030}
6031
6032
6033/**
6034 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6035 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6036 * @persistent_group: Whether to create a persistent group
6037 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006038 * @vht_center_freq2: segment_1 center frequency for GO operating in VHT 80P80
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006039 * @ht40: Start GO with 40 MHz channel width
6040 * @vht: Start GO with VHT support
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006041 * @vht_chwidth: channel bandwidth for GO operating with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 * Returns: 0 on success, -1 on failure
6043 *
6044 * This function creates a new P2P group with the local end as the Group Owner,
6045 * i.e., without using Group Owner Negotiation.
6046 */
6047int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006048 int freq, int vht_center_freq2, int ht40, int vht,
6049 int max_oper_chwidth)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050{
6051 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006052
6053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6054 return -1;
6055
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006056 os_free(wpa_s->global->add_psk);
6057 wpa_s->global->add_psk = NULL;
6058
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006059 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006060 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006061 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006062
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006063 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6064 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006067 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6068 ht40, vht, max_oper_chwidth, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006069 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006070 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006071 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006072 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
6073 ieee80211_is_dfs(params.freq)) {
6074 /*
6075 * If freq is a DFS channel and DFS is offloaded to the
6076 * driver, allow P2P GO to use it.
6077 */
6078 wpa_printf(MSG_DEBUG,
6079 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
6080 __func__, params.freq);
6081 } else {
6082 wpa_printf(MSG_DEBUG,
6083 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
6084 params.freq);
6085 return -1;
6086 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006087 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088 p2p_go_params(wpa_s->global->p2p, &params);
6089 params.persistent_group = persistent_group;
6090
6091 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6092 if (wpa_s == NULL)
6093 return -1;
6094 wpas_start_wps_go(wpa_s, &params, 0);
6095
6096 return 0;
6097}
6098
6099
6100static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006101 struct wpa_ssid *params, int addr_allocated,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006102 int freq, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006103{
6104 struct wpa_ssid *ssid;
6105
6106 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6107 if (wpa_s == NULL)
6108 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006109 if (force_scan)
6110 os_get_reltime(&wpa_s->scan_min_time);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006111 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006112
6113 wpa_supplicant_ap_deinit(wpa_s);
6114
6115 ssid = wpa_config_add_network(wpa_s->conf);
6116 if (ssid == NULL)
6117 return -1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006118 os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006119 wpa_config_set_network_defaults(ssid);
6120 ssid->temporary = 1;
6121 ssid->proto = WPA_PROTO_RSN;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006122 ssid->pbss = params->pbss;
6123 ssid->pairwise_cipher = params->pbss ? WPA_CIPHER_GCMP :
6124 WPA_CIPHER_CCMP;
6125 ssid->group_cipher = params->pbss ? WPA_CIPHER_GCMP : WPA_CIPHER_CCMP;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006126 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6127 ssid->ssid = os_malloc(params->ssid_len);
6128 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129 wpa_config_remove_network(wpa_s->conf, ssid->id);
6130 return -1;
6131 }
6132 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6133 ssid->ssid_len = params->ssid_len;
6134 ssid->p2p_group = 1;
6135 ssid->export_keys = 1;
6136 if (params->psk_set) {
6137 os_memcpy(ssid->psk, params->psk, 32);
6138 ssid->psk_set = 1;
6139 }
6140 if (params->passphrase)
6141 ssid->passphrase = os_strdup(params->passphrase);
6142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006143 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006144 wpa_s->p2p_in_invitation = 1;
6145 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006146 wpa_s->p2p_go_group_formation_completed = 0;
6147 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006148
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006149 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006150 NULL);
6151 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6152 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006153 wpa_s->p2pdev, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006154 wpa_supplicant_select_network(wpa_s, ssid);
6155
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006156 return 0;
6157}
6158
6159
6160int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6161 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006162 int force_freq, int neg_freq,
6163 int vht_center_freq2, int ht40,
6164 int vht, int max_oper_chwidth,
6165 const struct p2p_channels *channels,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006166 int connection_timeout, int force_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006167{
6168 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006169 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170
6171 if (ssid->disabled != 2 || ssid->ssid == NULL)
6172 return -1;
6173
6174 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6175 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6176 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6177 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006178 if (go == 0 &&
6179 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006180 wpa_s->p2pdev, NULL)) {
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006181 /*
6182 * This can happen if Invitation Response frame was lost
6183 * and the peer (GO of a persistent group) tries to
6184 * invite us again. Reschedule the timeout to avoid
6185 * terminating the wait for the connection too early
6186 * since we now know that the peer is still trying to
6187 * invite us instead of having already started the GO.
6188 */
6189 wpa_printf(MSG_DEBUG,
6190 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
6191 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6192 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006193 wpa_s->p2pdev, NULL);
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07006194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006195 return 0;
6196 }
6197
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006198 os_free(wpa_s->global->add_psk);
6199 wpa_s->global->add_psk = NULL;
6200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006202 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203
Dmitry Shmidt04949592012-07-19 12:16:46 -07006204 wpa_s->p2p_fallback_to_go_neg = 0;
6205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006206 if (ssid->mode == WPAS_MODE_P2P_GO) {
6207 if (force_freq > 0) {
6208 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6209 if (freq < 0)
6210 return -1;
6211 } else {
6212 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6213 if (freq < 0 ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006214 (freq > 0 && !freq_included(wpa_s, channels, freq)))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006215 freq = 0;
6216 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006217 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006218 freq = neg_freq;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006219 if (freq <= 0 || !freq_included(wpa_s, channels, freq)) {
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006220 struct os_reltime now;
6221 struct wpa_bss *bss =
6222 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006223
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006224 os_get_reltime(&now);
6225 if (bss &&
6226 !os_reltime_expired(&now, &bss->last_update, 5) &&
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006227 freq_included(wpa_s, channels, bss->freq))
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006228 freq = bss->freq;
6229 else
6230 freq = 0;
6231 }
6232
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006233 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq,
6234 force_scan);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006235 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07006236 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07006237 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07006238
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006239 if (wpas_p2p_init_go_params(wpa_s, &params, freq, vht_center_freq2,
6240 ht40, vht, max_oper_chwidth, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006241 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242
6243 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006244 params.psk_set = ssid->psk_set;
6245 if (params.psk_set)
6246 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006247 if (ssid->passphrase) {
6248 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6249 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6250 "persistent group");
6251 return -1;
6252 }
6253 os_strlcpy(params.passphrase, ssid->passphrase,
6254 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006255 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6257 params.ssid_len = ssid->ssid_len;
6258 params.persistent_group = 1;
6259
6260 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6261 if (wpa_s == NULL)
6262 return -1;
6263
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006264 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6265
Dmitry Shmidt56052862013-10-04 10:23:25 -07006266 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006267 wpas_start_wps_go(wpa_s, &params, 0);
6268
6269 return 0;
6270}
6271
6272
6273static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6274 struct wpabuf *proberesp_ies)
6275{
6276 struct wpa_supplicant *wpa_s = ctx;
6277 if (wpa_s->ap_iface) {
6278 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006279 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6280 wpabuf_free(beacon_ies);
6281 wpabuf_free(proberesp_ies);
6282 return;
6283 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006284 if (beacon_ies) {
6285 wpabuf_free(hapd->p2p_beacon_ie);
6286 hapd->p2p_beacon_ie = beacon_ies;
6287 }
6288 wpabuf_free(hapd->p2p_probe_resp_ie);
6289 hapd->p2p_probe_resp_ie = proberesp_ies;
6290 } else {
6291 wpabuf_free(beacon_ies);
6292 wpabuf_free(proberesp_ies);
6293 }
6294 wpa_supplicant_ap_update_beacon(wpa_s);
6295}
6296
6297
6298static void wpas_p2p_idle_update(void *ctx, int idle)
6299{
6300 struct wpa_supplicant *wpa_s = ctx;
6301 if (!wpa_s->ap_iface)
6302 return;
6303 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006304 if (idle) {
6305 if (wpa_s->global->p2p_fail_on_wps_complete &&
6306 wpa_s->p2p_in_provisioning) {
6307 wpas_p2p_grpform_fail_after_wps(wpa_s);
6308 return;
6309 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006310 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006311 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6313}
6314
6315
6316struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006317 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318{
6319 struct p2p_group *group;
6320 struct p2p_group_config *cfg;
6321
Dmitry Shmidt849734c2016-05-27 09:59:01 -07006322 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6323 !ssid->p2p_group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006324 return NULL;
6325
6326 cfg = os_zalloc(sizeof(*cfg));
6327 if (cfg == NULL)
6328 return NULL;
6329
Dmitry Shmidt04949592012-07-19 12:16:46 -07006330 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006331 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006332 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006333 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006334 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6335 if (wpa_s->max_stations &&
6336 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6337 cfg->max_clients = wpa_s->max_stations;
6338 else
6339 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006340 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6341 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006342 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006343 cfg->cb_ctx = wpa_s;
6344 cfg->ie_update = wpas_p2p_ie_update;
6345 cfg->idle_update = wpas_p2p_idle_update;
Dmitry Shmidte4663042016-04-04 10:07:49 -07006346 cfg->ip_addr_alloc = WPA_GET_BE32(wpa_s->p2pdev->conf->ip_addr_start)
6347 != 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006348
6349 group = p2p_group_init(wpa_s->global->p2p, cfg);
6350 if (group == NULL)
6351 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006352 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006353 p2p_group_notif_formation_done(group);
6354 wpa_s->p2p_group = group;
6355 return group;
6356}
6357
6358
6359void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6360 int registrar)
6361{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006362 struct wpa_ssid *ssid = wpa_s->current_ssid;
6363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364 if (!wpa_s->p2p_in_provisioning) {
6365 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6366 "provisioning not in progress");
6367 return;
6368 }
6369
Dmitry Shmidt04949592012-07-19 12:16:46 -07006370 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6371 u8 go_dev_addr[ETH_ALEN];
6372 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6373 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6374 ssid->ssid_len);
6375 /* Clear any stored provisioning info */
6376 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6377 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006378
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006379 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->p2pdev,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006381 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006382 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6383 /*
6384 * Use a separate timeout for initial data connection to
6385 * complete to allow the group to be removed automatically if
6386 * something goes wrong in this step before the P2P group idle
6387 * timeout mechanism is taken into use.
6388 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006389 wpa_dbg(wpa_s, MSG_DEBUG,
6390 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6391 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006392 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6393 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006394 wpa_s->p2pdev, NULL);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006395 /* Complete group formation on successful data connection. */
6396 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006397 } else if (ssid) {
6398 /*
6399 * Use a separate timeout for initial data connection to
6400 * complete to allow the group to be removed automatically if
6401 * the client does not complete data connection successfully.
6402 */
6403 wpa_dbg(wpa_s, MSG_DEBUG,
6404 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6405 P2P_MAX_INITIAL_CONN_WAIT_GO);
6406 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6407 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006408 wpa_s->p2pdev, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006409 /*
6410 * Complete group formation on first successful data connection
6411 */
6412 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006414 if (wpa_s->global->p2p)
6415 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006416 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006417}
6418
6419
Jouni Malinen75ecf522011-06-27 15:19:46 -07006420void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6421 struct wps_event_fail *fail)
6422{
6423 if (!wpa_s->p2p_in_provisioning) {
6424 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6425 "provisioning not in progress");
6426 return;
6427 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006428
6429 if (wpa_s->go_params) {
6430 p2p_clear_provisioning_info(
6431 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006432 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006433 }
6434
Jouni Malinen75ecf522011-06-27 15:19:46 -07006435 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006436
6437 if (wpa_s == wpa_s->global->p2p_group_formation) {
6438 /*
6439 * Allow some time for the failed WPS negotiation exchange to
6440 * complete, but remove the group since group formation cannot
6441 * succeed after provisioning failure.
6442 */
6443 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6444 wpa_s->global->p2p_fail_on_wps_complete = 1;
6445 eloop_deplete_timeout(0, 50000,
6446 wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006447 wpa_s->p2pdev, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006448 }
6449}
6450
6451
6452int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6453{
6454 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6455 !wpa_s->p2p_in_provisioning)
6456 return 0;
6457
6458 wpas_p2p_grpform_fail_after_wps(wpa_s);
6459
6460 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006461}
6462
6463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006465 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006466 enum wpas_p2p_prov_disc_use use,
6467 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006468{
6469 u16 config_methods;
6470
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006471 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006472 wpa_s->global->pending_p2ps_group_freq = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006473 wpa_s->p2p_fallback_to_go_neg = 0;
6474 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006475 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6476 p2ps_prov->conncap = p2ps_group_capability(
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006477 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role,
6478 &p2ps_prov->force_freq, &p2ps_prov->pref_freq);
6479
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006480 wpa_printf(MSG_DEBUG,
6481 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6482 __func__, p2ps_prov->conncap,
6483 p2ps_prov->adv_id, p2ps_prov->conncap,
6484 p2ps_prov->status, p2ps_prov->info);
6485
6486 config_methods = 0;
6487 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006488 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006489 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006490 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006491 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6492 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006493 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006494 else {
6495 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006496 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006497 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499
Dmitry Shmidt04949592012-07-19 12:16:46 -07006500 if (use == WPAS_P2P_PD_AUTO) {
6501 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6502 wpa_s->pending_pd_config_methods = config_methods;
6503 wpa_s->p2p_auto_pd = 1;
6504 wpa_s->p2p_auto_join = 0;
6505 wpa_s->pending_pd_before_join = 0;
6506 wpa_s->auto_pd_scan_retry = 0;
6507 wpas_p2p_stop_find(wpa_s);
6508 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006509 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006510 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6511 wpa_s->p2p_auto_started.sec,
6512 wpa_s->p2p_auto_started.usec);
6513 wpas_p2p_join_scan(wpa_s, NULL);
6514 return 0;
6515 }
6516
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006517 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6518 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006519 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006522 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006523 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006524 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006525}
6526
6527
6528int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6529 char *end)
6530{
6531 return p2p_scan_result_text(ies, ies_len, buf, end);
6532}
6533
6534
6535static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6536{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006537 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006538 return;
6539
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006540 if (wpa_s->p2p_send_action_work) {
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006541 wpas_p2p_free_send_action_work(wpa_s);
Dmitry Shmidtde47be72016-01-07 12:52:55 -08006542 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
6543 wpa_s, NULL);
6544 offchannel_send_action_done(wpa_s);
6545 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07006546
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006547 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6548 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006549 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006550}
6551
6552
6553int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6554 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006555 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006556 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006557 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006558{
6559 wpas_p2p_clear_pending_action_tx(wpa_s);
6560 wpa_s->p2p_long_listen = 0;
6561
Dmitry Shmidt04949592012-07-19 12:16:46 -07006562 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6563 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006564 return -1;
6565
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006566 wpa_supplicant_cancel_sched_scan(wpa_s);
6567
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006569 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08006570 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006571}
6572
6573
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006574static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6575 struct wpa_scan_results *scan_res)
6576{
6577 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6578
6579 if (wpa_s->p2p_scan_work) {
6580 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6581 wpa_s->p2p_scan_work = NULL;
6582 radio_work_done(work);
6583 }
6584
6585 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6586 return;
6587
6588 /*
6589 * Indicate that results have been processed so that the P2P module can
6590 * continue pending tasks.
6591 */
6592 p2p_scan_res_handled(wpa_s->global->p2p);
6593}
6594
6595
6596static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006597{
6598 wpas_p2p_clear_pending_action_tx(wpa_s);
6599 wpa_s->p2p_long_listen = 0;
6600 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6601 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006602
6603 if (wpa_s->global->p2p)
6604 p2p_stop_find(wpa_s->global->p2p);
6605
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006606 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6607 wpa_printf(MSG_DEBUG,
6608 "P2P: Do not consider the scan results after stop_find");
6609 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6610 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006611}
6612
6613
6614void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6615{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006616 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006617 if (!wpa_s->global->pending_group_iface_for_p2ps)
6618 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619}
6620
6621
6622static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6623{
6624 struct wpa_supplicant *wpa_s = eloop_ctx;
6625 wpa_s->p2p_long_listen = 0;
6626}
6627
6628
6629int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6630{
6631 int res;
6632
6633 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6634 return -1;
6635
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006636 if (wpa_s->p2p_lo_started) {
6637 wpa_printf(MSG_DEBUG,
6638 "P2P: Cannot start P2P listen, it is offloaded");
6639 return -1;
6640 }
6641
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006642 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006643 wpas_p2p_clear_pending_action_tx(wpa_s);
6644
6645 if (timeout == 0) {
6646 /*
6647 * This is a request for unlimited Listen state. However, at
6648 * least for now, this is mapped to a Listen state for one
6649 * hour.
6650 */
6651 timeout = 3600;
6652 }
6653 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6654 wpa_s->p2p_long_listen = 0;
6655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006656 /*
6657 * Stop previous find/listen operation to avoid trying to request a new
6658 * remain-on-channel operation while the driver is still running the
6659 * previous one.
6660 */
6661 if (wpa_s->global->p2p)
6662 p2p_stop_find(wpa_s->global->p2p);
6663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006664 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6665 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6666 wpa_s->p2p_long_listen = timeout * 1000;
6667 eloop_register_timeout(timeout, 0,
6668 wpas_p2p_long_listen_timeout,
6669 wpa_s, NULL);
6670 }
6671
6672 return res;
6673}
6674
6675
6676int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6677 u8 *buf, size_t len, int p2p_group)
6678{
6679 struct wpabuf *p2p_ie;
6680 int ret;
6681
6682 if (wpa_s->global->p2p_disabled)
6683 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006684 /*
6685 * Advertize mandatory cross connection capability even on
6686 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6687 */
6688 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006689 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006690 if (wpa_s->global->p2p == NULL)
6691 return -1;
6692 if (bss == NULL)
6693 return -1;
6694
6695 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6696 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6697 p2p_group, p2p_ie);
6698 wpabuf_free(p2p_ie);
6699
6700 return ret;
6701}
6702
6703
6704int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006705 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006706 const u8 *ie, size_t ie_len,
6707 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006708{
6709 if (wpa_s->global->p2p_disabled)
6710 return 0;
6711 if (wpa_s->global->p2p == NULL)
6712 return 0;
6713
Dmitry Shmidt04949592012-07-19 12:16:46 -07006714 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07006715 ie, ie_len, rx_freq, wpa_s->p2p_lo_started)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006716 case P2P_PREQ_NOT_P2P:
6717 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6718 ssi_signal);
6719 /* fall through */
6720 case P2P_PREQ_MALFORMED:
6721 case P2P_PREQ_NOT_LISTEN:
6722 case P2P_PREQ_NOT_PROCESSED:
6723 default: /* make gcc happy */
6724 return 0;
6725 case P2P_PREQ_PROCESSED:
6726 return 1;
6727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006728}
6729
6730
6731void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6732 const u8 *sa, const u8 *bssid,
6733 u8 category, const u8 *data, size_t len, int freq)
6734{
6735 if (wpa_s->global->p2p_disabled)
6736 return;
6737 if (wpa_s->global->p2p == NULL)
6738 return;
6739
6740 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6741 freq);
6742}
6743
6744
6745void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6746{
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006747 unsigned int bands;
6748
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006749 if (wpa_s->global->p2p_disabled)
6750 return;
6751 if (wpa_s->global->p2p == NULL)
6752 return;
6753
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006754 bands = wpas_get_bands(wpa_s, NULL);
6755 p2p_scan_ie(wpa_s->global->p2p, ies, NULL, bands);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756}
6757
6758
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006759static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006760{
6761 p2p_group_deinit(wpa_s->p2p_group);
6762 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006763
6764 wpa_s->ap_configured_cb = NULL;
6765 wpa_s->ap_configured_cb_ctx = NULL;
6766 wpa_s->ap_configured_cb_data = NULL;
6767 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006768}
6769
6770
6771int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6772{
6773 wpa_s->p2p_long_listen = 0;
6774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006775 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6776 return -1;
6777
6778 return p2p_reject(wpa_s->global->p2p, addr);
6779}
6780
6781
6782/* Invite to reinvoke a persistent group */
6783int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006784 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006785 int vht_center_freq2, int ht40, int vht, int max_chwidth,
6786 int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006787{
6788 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006789 u8 *bssid = NULL;
6790 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006791 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006792 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006793 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006794
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006795 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006796 if (peer_addr)
6797 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6798 else
6799 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006800
Jouni Malinen31be0a42012-08-31 21:20:51 +03006801 wpa_s->p2p_persistent_go_freq = freq;
6802 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006803 wpa_s->p2p_go_vht = !!vht;
6804 wpa_s->p2p_go_max_oper_chwidth = max_chwidth;
6805 wpa_s->p2p_go_vht_center_freq2 = vht_center_freq2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006806 if (ssid->mode == WPAS_MODE_P2P_GO) {
6807 role = P2P_INVITE_ROLE_GO;
6808 if (peer_addr == NULL) {
6809 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6810 "address in invitation command");
6811 return -1;
6812 }
6813 if (wpas_p2p_create_iface(wpa_s)) {
6814 if (wpas_p2p_add_group_interface(wpa_s,
6815 WPA_IF_P2P_GO) < 0) {
6816 wpa_printf(MSG_ERROR, "P2P: Failed to "
6817 "allocate a new interface for the "
6818 "group");
6819 return -1;
6820 }
6821 bssid = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006822 } else if (wpa_s->p2p_mgmt)
6823 bssid = wpa_s->parent->own_addr;
6824 else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006825 bssid = wpa_s->own_addr;
6826 } else {
6827 role = P2P_INVITE_ROLE_CLIENT;
6828 peer_addr = ssid->bssid;
6829 }
6830 wpa_s->pending_invite_ssid_id = ssid->id;
6831
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006832 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006833 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006834 role == P2P_INVITE_ROLE_GO,
6835 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006836 if (res)
6837 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006838
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006839 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6840 return -1;
6841
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08006842 p2p_set_own_pref_freq_list(wpa_s->global->p2p, pref_freq_list, size);
6843
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006844 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6845 no_pref_freq_given && pref_freq > 0 &&
6846 wpa_s->num_multichan_concurrent > 1 &&
6847 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6848 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6849 pref_freq);
6850 pref_freq = 0;
6851 }
6852
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006853 /*
6854 * Stop any find/listen operations before invitation and possibly
6855 * connection establishment.
6856 */
6857 wpas_p2p_stop_find_oper(wpa_s);
6858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006860 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006861 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006862}
6863
6864
6865/* Invite to join an active group */
6866int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6867 const u8 *peer_addr, const u8 *go_dev_addr)
6868{
6869 struct wpa_global *global = wpa_s->global;
6870 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006871 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006872 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006873 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006874 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006875 int res;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006876 unsigned int pref_freq_list[P2P_MAX_PREF_CHANNELS], size;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006877
Jouni Malinen31be0a42012-08-31 21:20:51 +03006878 wpa_s->p2p_persistent_go_freq = 0;
6879 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006880 wpa_s->p2p_go_vht = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006881 wpa_s->p2p_go_vht_center_freq2 = 0;
6882 wpa_s->p2p_go_max_oper_chwidth = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006884 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6885 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6886 break;
6887 }
6888 if (wpa_s == NULL) {
6889 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6890 return -1;
6891 }
6892
6893 ssid = wpa_s->current_ssid;
6894 if (ssid == NULL) {
6895 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6896 "invitation");
6897 return -1;
6898 }
6899
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006900 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006901 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006902 wpas_p2p_get_persistent(wpa_s->p2pdev, peer_addr,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006903 ssid->ssid, ssid->ssid_len);
6904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006905 if (ssid->mode == WPAS_MODE_P2P_GO) {
6906 role = P2P_INVITE_ROLE_ACTIVE_GO;
6907 bssid = wpa_s->own_addr;
6908 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006909 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006910 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006911 } else {
6912 role = P2P_INVITE_ROLE_CLIENT;
6913 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6914 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6915 "invite to current group");
6916 return -1;
6917 }
6918 bssid = wpa_s->bssid;
6919 if (go_dev_addr == NULL &&
6920 !is_zero_ether_addr(wpa_s->go_dev_addr))
6921 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006922 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6923 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006925 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006927 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6928 return -1;
6929
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006930 size = P2P_MAX_PREF_CHANNELS;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006931 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08006932 role == P2P_INVITE_ROLE_ACTIVE_GO,
6933 pref_freq_list, &size);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006934 if (res)
6935 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006936 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006938 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006939 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006940 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006941}
6942
6943
6944void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6945{
6946 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006947 u8 go_dev_addr[ETH_ALEN];
6948 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006949 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006950 u8 ip[3 * 4];
6951 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006952
Dmitry Shmidt04949592012-07-19 12:16:46 -07006953 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6954 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08006955 wpa_s->p2pdev, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006956 }
6957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006958 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006959 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006960
6961 wpa_s->show_group_started = 0;
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08006962 if (!wpa_s->p2p_go_group_formation_completed &&
6963 wpa_s->global->p2p_group_formation == wpa_s) {
6964 wpa_dbg(wpa_s, MSG_DEBUG,
6965 "P2P: Marking group formation completed on client on data connection");
6966 wpa_s->p2p_go_group_formation_completed = 1;
6967 wpa_s->global->p2p_group_formation = NULL;
6968 wpa_s->p2p_in_provisioning = 0;
6969 wpa_s->p2p_in_invitation = 0;
6970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006971
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006972 os_memset(go_dev_addr, 0, ETH_ALEN);
6973 if (ssid->bssid_set)
6974 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6975 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6976 ssid->ssid_len);
6977 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6978
6979 if (wpa_s->global->p2p_group_formation == wpa_s)
6980 wpa_s->global->p2p_group_formation = NULL;
6981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006982 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6983 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006984
6985 ip_addr[0] = '\0';
6986 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006987 int res;
6988
6989 res = os_snprintf(ip_addr, sizeof(ip_addr),
6990 " ip_addr=%u.%u.%u.%u "
6991 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6992 ip[0], ip[1], ip[2], ip[3],
6993 ip[4], ip[5], ip[6], ip[7],
6994 ip[8], ip[9], ip[10], ip[11]);
6995 if (os_snprintf_error(sizeof(ip_addr), res))
6996 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006997 }
6998
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006999 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7000 ssid->passphrase == NULL && ssid->psk_set ?
7001 ssid->psk : NULL,
7002 ssid->passphrase, go_dev_addr, persistent,
7003 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007004
7005 if (persistent)
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -07007006 wpas_p2p_store_persistent_group(wpa_s->p2pdev,
7007 ssid, go_dev_addr);
7008
Dmitry Shmidtabb90a32016-12-05 15:34:39 -08007009 wpas_notify_p2p_group_started(wpa_s, ssid, persistent, 1, ip);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007010}
7011
7012
7013int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7014 u32 interval1, u32 duration2, u32 interval2)
7015{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007016 int ret;
7017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007018 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7019 return -1;
7020
7021 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7022 wpa_s->current_ssid == NULL ||
7023 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7024 return -1;
7025
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007026 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7027 wpa_s->own_addr, wpa_s->assoc_freq,
7028 duration1, interval1, duration2, interval2);
7029 if (ret == 0)
7030 wpa_s->waiting_presence_resp = 1;
7031
7032 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033}
7034
7035
7036int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7037 unsigned int interval)
7038{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007039 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7040 return -1;
7041
7042 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7043}
7044
7045
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7047{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007048 if (wpa_s->current_ssid == NULL) {
7049 /*
7050 * current_ssid can be cleared when P2P client interface gets
7051 * disconnected, so assume this interface was used as P2P
7052 * client.
7053 */
7054 return 1;
7055 }
7056 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007057 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7058}
7059
7060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007061static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7062{
7063 struct wpa_supplicant *wpa_s = eloop_ctx;
7064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007065 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007066 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7067 "disabled");
7068 return;
7069 }
7070
Dmitry Shmidt04949592012-07-19 12:16:46 -07007071 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7072 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007073 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007074}
7075
7076
7077static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7078{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007079 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007080
Dmitry Shmidt04949592012-07-19 12:16:46 -07007081 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7082 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7083
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007084 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7085 return;
7086
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007087 timeout = wpa_s->conf->p2p_group_idle;
7088 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7089 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7090 timeout = P2P_MAX_CLIENT_IDLE;
7091
7092 if (timeout == 0)
7093 return;
7094
Dmitry Shmidt04949592012-07-19 12:16:46 -07007095 if (timeout < 0) {
7096 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7097 timeout = 0; /* special client mode no-timeout */
7098 else
7099 return;
7100 }
7101
7102 if (wpa_s->p2p_in_provisioning) {
7103 /*
7104 * Use the normal group formation timeout during the
7105 * provisioning phase to avoid terminating this process too
7106 * early due to group idle timeout.
7107 */
7108 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7109 "during provisioning");
7110 return;
7111 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307112
Dmitry Shmidt04949592012-07-19 12:16:46 -07007113 if (wpa_s->show_group_started) {
7114 /*
7115 * Use the normal group formation timeout between the end of
7116 * the provisioning phase and completion of 4-way handshake to
7117 * avoid terminating this process too early due to group idle
7118 * timeout.
7119 */
7120 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7121 "while waiting for initial 4-way handshake to "
7122 "complete");
7123 return;
7124 }
7125
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007126 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007127 timeout);
7128 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7129 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007130}
7131
7132
Jouni Malinen2b89da82012-08-31 22:04:41 +03007133/* Returns 1 if the interface was removed */
7134int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7135 u16 reason_code, const u8 *ie, size_t ie_len,
7136 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007137{
7138 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007139 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007140
Dmitry Shmidt04949592012-07-19 12:16:46 -07007141 if (!locally_generated)
7142 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7143 ie_len);
7144
7145 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7146 wpa_s->current_ssid &&
7147 wpa_s->current_ssid->p2p_group &&
7148 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7149 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7150 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007151 if (wpas_p2p_group_delete(wpa_s,
7152 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7153 > 0)
7154 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007155 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007156
7157 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007158}
7159
7160
7161void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007162 u16 reason_code, const u8 *ie, size_t ie_len,
7163 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007164{
7165 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7166 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007167
Dmitry Shmidt04949592012-07-19 12:16:46 -07007168 if (!locally_generated)
7169 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7170 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007171}
7172
7173
7174void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7175{
7176 struct p2p_data *p2p = wpa_s->global->p2p;
7177
7178 if (p2p == NULL)
7179 return;
7180
7181 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7182 return;
7183
7184 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7185 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7186
7187 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7188 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7189
7190 if (wpa_s->wps &&
7191 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7192 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7193
7194 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7195 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7196
7197 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7198 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7199 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7200 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7201 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7202 }
7203
7204 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7205 p2p_set_sec_dev_types(p2p,
7206 (void *) wpa_s->conf->sec_device_type,
7207 wpa_s->conf->num_sec_device_types);
7208
7209 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7210 int i;
7211 p2p_remove_wps_vendor_extensions(p2p);
7212 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7213 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7214 continue;
7215 p2p_add_wps_vendor_extension(
7216 p2p, wpa_s->conf->wps_vendor_ext[i]);
7217 }
7218 }
7219
7220 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7221 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7222 char country[3];
7223 country[0] = wpa_s->conf->country[0];
7224 country[1] = wpa_s->conf->country[1];
7225 country[2] = 0x04;
7226 p2p_set_country(p2p, country);
7227 }
7228
7229 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7230 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7231 wpa_s->conf->p2p_ssid_postfix ?
7232 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7233 0);
7234 }
7235
7236 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7237 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007238
7239 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7240 u8 reg_class, channel;
7241 int ret;
7242 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007243 u8 channel_forced;
7244
Jouni Malinen75ecf522011-06-27 15:19:46 -07007245 if (wpa_s->conf->p2p_listen_reg_class &&
7246 wpa_s->conf->p2p_listen_channel) {
7247 reg_class = wpa_s->conf->p2p_listen_reg_class;
7248 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007249 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007250 } else {
7251 reg_class = 81;
7252 /*
7253 * Pick one of the social channels randomly as the
7254 * listen channel.
7255 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007256 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7257 channel = 1;
7258 else
7259 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007260 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007261 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007262 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7263 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007264 if (ret)
7265 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7266 "failed: %d", ret);
7267 }
7268 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7269 u8 op_reg_class, op_channel, cfg_op_channel;
7270 int ret = 0;
7271 unsigned int r;
7272 if (wpa_s->conf->p2p_oper_reg_class &&
7273 wpa_s->conf->p2p_oper_channel) {
7274 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7275 op_channel = wpa_s->conf->p2p_oper_channel;
7276 cfg_op_channel = 1;
7277 } else {
7278 op_reg_class = 81;
7279 /*
7280 * Use random operation channel from (1, 6, 11)
7281 *if no other preference is indicated.
7282 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007283 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7284 op_channel = 1;
7285 else
7286 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007287 cfg_op_channel = 0;
7288 }
7289 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7290 cfg_op_channel);
7291 if (ret)
7292 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7293 "failed: %d", ret);
7294 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007295
7296 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7297 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7298 wpa_s->conf->p2p_pref_chan) < 0) {
7299 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7300 "update failed");
7301 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007302
7303 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7304 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7305 "update failed");
7306 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007307 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007308
7309 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7310 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007311}
7312
7313
7314int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7315 int duration)
7316{
7317 if (!wpa_s->ap_iface)
7318 return -1;
7319 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7320 duration);
7321}
7322
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007323
7324int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7325{
7326 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7327 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007328
7329 wpa_s->global->cross_connection = enabled;
7330 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7331
7332 if (!enabled) {
7333 struct wpa_supplicant *iface;
7334
7335 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7336 {
7337 if (iface->cross_connect_enabled == 0)
7338 continue;
7339
7340 iface->cross_connect_enabled = 0;
7341 iface->cross_connect_in_use = 0;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007342 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007343 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7344 iface->ifname,
7345 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007346 }
7347 }
7348
7349 return 0;
7350}
7351
7352
7353static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7354{
7355 struct wpa_supplicant *iface;
7356
7357 if (!uplink->global->cross_connection)
7358 return;
7359
7360 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7361 if (!iface->cross_connect_enabled)
7362 continue;
7363 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7364 0)
7365 continue;
7366 if (iface->ap_iface == NULL)
7367 continue;
7368 if (iface->cross_connect_in_use)
7369 continue;
7370
7371 iface->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007372 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007373 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7374 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007375 }
7376}
7377
7378
7379static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7380{
7381 struct wpa_supplicant *iface;
7382
7383 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7384 if (!iface->cross_connect_enabled)
7385 continue;
7386 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7387 0)
7388 continue;
7389 if (!iface->cross_connect_in_use)
7390 continue;
7391
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007392 wpa_msg_global(iface->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007393 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7394 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007395 iface->cross_connect_in_use = 0;
7396 }
7397}
7398
7399
7400void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7401{
7402 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7403 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7404 wpa_s->cross_connect_disallowed)
7405 wpas_p2p_disable_cross_connect(wpa_s);
7406 else
7407 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007408 if (!wpa_s->ap_iface &&
7409 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7410 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007411}
7412
7413
7414void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7415{
7416 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007417 if (!wpa_s->ap_iface &&
7418 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7419 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007420 wpas_p2p_set_group_idle_timeout(wpa_s);
7421}
7422
7423
7424static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7425{
7426 struct wpa_supplicant *iface;
7427
7428 if (!wpa_s->global->cross_connection)
7429 return;
7430
7431 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7432 if (iface == wpa_s)
7433 continue;
7434 if (iface->drv_flags &
7435 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7436 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007437 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7438 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007439 continue;
7440
7441 wpa_s->cross_connect_enabled = 1;
7442 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7443 sizeof(wpa_s->cross_connect_uplink));
7444 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7445 "%s to %s whenever uplink is available",
7446 wpa_s->ifname, wpa_s->cross_connect_uplink);
7447
7448 if (iface->ap_iface || iface->current_ssid == NULL ||
7449 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7450 iface->cross_connect_disallowed ||
7451 iface->wpa_state != WPA_COMPLETED)
7452 break;
7453
7454 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007455 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007456 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7457 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007458 break;
7459 }
7460}
7461
7462
7463int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7464{
7465 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7466 !wpa_s->p2p_in_provisioning)
7467 return 0; /* not P2P client operation */
7468
7469 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7470 "session overlap");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007471 if (wpa_s != wpa_s->p2pdev)
7472 wpa_msg_ctrl(wpa_s->p2pdev, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007473 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474 return 1;
7475}
7476
7477
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007478void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7479{
7480 struct wpa_supplicant *wpa_s = eloop_ctx;
7481 wpas_p2p_notif_pbc_overlap(wpa_s);
7482}
7483
7484
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007485void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s,
7486 enum wpas_p2p_channel_update_trig trig)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007487{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007488 struct p2p_channels chan, cli_chan;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007489 struct wpa_used_freq_data *freqs = NULL;
7490 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007491
7492 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7493 return;
7494
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007495 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7496 if (!freqs)
7497 return;
7498
7499 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007501 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007502 os_memset(&cli_chan, 0, sizeof(cli_chan));
7503 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007504 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7505 "channel list");
7506 return;
7507 }
7508
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007509 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007510
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007511 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007512
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08007513 /*
7514 * The used frequencies map changed, so it is possible that a GO is
7515 * using a channel that is no longer valid for P2P use. It is also
7516 * possible that due to policy consideration, it would be preferable to
7517 * move it to a frequency already used by other station interfaces.
7518 */
7519 wpas_p2p_consider_moving_gos(wpa_s, freqs, num, trig);
7520
7521 os_free(freqs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007522}
7523
7524
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007525static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7526 struct wpa_scan_results *scan_res)
7527{
7528 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7529}
7530
7531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007532int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7533{
7534 struct wpa_global *global = wpa_s->global;
7535 int found = 0;
7536 const u8 *peer;
7537
7538 if (global->p2p == NULL)
7539 return -1;
7540
7541 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7542
7543 if (wpa_s->pending_interface_name[0] &&
7544 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7545 found = 1;
7546
7547 peer = p2p_get_go_neg_peer(global->p2p);
7548 if (peer) {
7549 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7550 MACSTR, MAC2STR(peer));
7551 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007552 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007553 }
7554
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007555 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7556 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7557 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7558 found = 1;
7559 }
7560
7561 if (wpa_s->pending_pd_before_join) {
7562 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7563 wpa_s->pending_pd_before_join = 0;
7564 found = 1;
7565 }
7566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007567 wpas_p2p_stop_find(wpa_s);
7568
7569 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7570 if (wpa_s == global->p2p_group_formation &&
7571 (wpa_s->p2p_in_provisioning ||
7572 wpa_s->parent->pending_interface_type ==
7573 WPA_IF_P2P_CLIENT)) {
7574 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7575 "formation found - cancelling",
7576 wpa_s->ifname);
7577 found = 1;
7578 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007579 wpa_s->p2pdev, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007580 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007581 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007582 break;
7583 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007584 wpas_p2p_group_delete(wpa_s,
7585 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007586 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007587 } else if (wpa_s->p2p_in_invitation) {
7588 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7589 wpa_s->ifname);
7590 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007591 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007592 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007593 }
7594 }
7595
7596 if (!found) {
7597 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7598 return -1;
7599 }
7600
7601 return 0;
7602}
7603
7604
7605void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7606{
7607 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7608 return;
7609
7610 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7611 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007612 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007613}
7614
7615
7616void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7617 int freq_24, int freq_5, int freq_overall)
7618{
7619 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007620 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007621 return;
7622 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7623}
7624
7625
7626int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7627{
7628 u8 peer[ETH_ALEN];
7629 struct p2p_data *p2p = wpa_s->global->p2p;
7630
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007631 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007632 return -1;
7633
7634 if (hwaddr_aton(addr, peer))
7635 return -1;
7636
7637 return p2p_unauthorize(p2p, peer);
7638}
7639
7640
7641/**
7642 * wpas_p2p_disconnect - Disconnect from a P2P Group
7643 * @wpa_s: Pointer to wpa_supplicant data
7644 * Returns: 0 on success, -1 on failure
7645 *
7646 * This can be used to disconnect from a group in which the local end is a P2P
7647 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7648 * virtual network interface was created for this group, that interface will be
7649 * removed. Otherwise, only the configured P2P group network will be removed
7650 * from the interface.
7651 */
7652int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7653{
7654
7655 if (wpa_s == NULL)
7656 return -1;
7657
Jouni Malinen2b89da82012-08-31 22:04:41 +03007658 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7659 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007660}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007661
7662
7663int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7664{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007665 int ret;
7666
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007667 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7668 return 0;
7669
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007670 ret = p2p_in_progress(wpa_s->global->p2p);
7671 if (ret == 0) {
7672 /*
7673 * Check whether there is an ongoing WPS provisioning step (or
7674 * other parts of group formation) on another interface since
7675 * p2p_in_progress() does not report this to avoid issues for
7676 * scans during such provisioning step.
7677 */
7678 if (wpa_s->global->p2p_group_formation &&
7679 wpa_s->global->p2p_group_formation != wpa_s) {
7680 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7681 "in group formation",
7682 wpa_s->global->p2p_group_formation->ifname);
7683 ret = 1;
7684 }
7685 }
7686
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007687 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007688 struct os_reltime now;
7689 os_get_reltime(&now);
7690 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7691 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007692 /* Wait for the first client has expired */
7693 wpa_s->global->p2p_go_wait_client.sec = 0;
7694 } else {
7695 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7696 ret = 1;
7697 }
7698 }
7699
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007700 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007701}
7702
7703
7704void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7705 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007706{
7707 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7708 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007709 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007710 /**
7711 * Remove the network by scheduling the group formation
7712 * timeout to happen immediately. The teardown code
7713 * needs to be scheduled to run asynch later so that we
7714 * don't delete data from under ourselves unexpectedly.
7715 * Calling wpas_p2p_group_formation_timeout directly
7716 * causes a series of crashes in WPS failure scenarios.
7717 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007718 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7719 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007720 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007721 wpa_s->p2pdev, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007722 }
7723}
7724
7725
7726struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007727 const u8 *addr, const u8 *ssid,
7728 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007729{
7730 struct wpa_ssid *s;
7731 size_t i;
7732
7733 for (s = wpa_s->conf->ssid; s; s = s->next) {
7734 if (s->disabled != 2)
7735 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007736 if (ssid &&
7737 (ssid_len != s->ssid_len ||
7738 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7739 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007740 if (addr == NULL) {
7741 if (s->mode == WPAS_MODE_P2P_GO)
7742 return s;
7743 continue;
7744 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007745 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7746 return s; /* peer is GO in the persistent group */
7747 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7748 continue;
7749 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007750 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007751 addr, ETH_ALEN) == 0)
7752 return s; /* peer is P2P client in persistent
7753 * group */
7754 }
7755 }
7756
7757 return NULL;
7758}
7759
7760
7761void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7762 const u8 *addr)
7763{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007764 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007765 wpa_s->p2pdev, NULL) > 0) {
Dmitry Shmidt56052862013-10-04 10:23:25 -07007766 /*
7767 * This can happen if WPS provisioning step is not terminated
7768 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7769 * peer was able to connect, there is no need to time out group
7770 * formation after this, though. In addition, this is used with
7771 * the initial connection wait on the GO as a separate formation
7772 * timeout and as such, expected to be hit after the initial WPS
7773 * provisioning step.
7774 */
7775 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007776
7777 if (!wpa_s->p2p_go_group_formation_completed &&
7778 !wpa_s->group_formation_reported) {
7779 /*
7780 * GO has not yet notified group formation success since
7781 * the WPS step was not completed cleanly. Do that
7782 * notification now since the P2P Client was able to
7783 * connect and as such, must have received the
7784 * credential from the WPS step.
7785 */
7786 if (wpa_s->global->p2p)
7787 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007788 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007789 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007790 }
7791 if (!wpa_s->p2p_go_group_formation_completed) {
7792 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7793 wpa_s->p2p_go_group_formation_completed = 1;
7794 wpa_s->global->p2p_group_formation = NULL;
7795 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007796 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007797 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007798 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007799 if (addr == NULL)
7800 return;
7801 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7802}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007803
Dmitry Shmidt04949592012-07-19 12:16:46 -07007804
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007805static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7806 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007807{
7808 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007809 int ret = 0;
7810
Dmitry Shmidt04949592012-07-19 12:16:46 -07007811 if (wpa_s->global->p2p_group_formation)
7812 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007813 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007814 offchannel_send_action_done(wpa_s);
7815 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007816 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007817 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7818 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7819 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7820 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007821 wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007822 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007823 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007824 wpa_s->p2p_go_ht40,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08007825 wpa_s->p2p_go_vht,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08007826 wpa_s->p2p_go_max_oper_chwidth, NULL, 0);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007827 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007828}
7829
7830
7831int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7832{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007833 int res;
7834
Dmitry Shmidt04949592012-07-19 12:16:46 -07007835 if (!wpa_s->p2p_fallback_to_go_neg ||
7836 wpa_s->p2p_in_provisioning <= 5)
7837 return 0;
7838
7839 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7840 return 0; /* peer operating as a GO */
7841
7842 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7843 "fallback to GO Negotiation");
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007844 wpa_msg_global(wpa_s->p2pdev, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007845 "reason=GO-not-found");
7846 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007847
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007848 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007849}
7850
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007851
7852unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7853{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007854 struct wpa_supplicant *ifs;
7855
7856 if (wpa_s->wpa_state > WPA_SCANNING) {
7857 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7858 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007859 wpa_s->conf->p2p_search_delay);
7860 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007861 }
7862
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007863 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7864 radio_list) {
7865 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007866 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7867 "delay due to concurrent operation on "
7868 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007869 wpa_s->conf->p2p_search_delay,
7870 ifs->ifname);
7871 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007872 }
7873 }
7874
7875 return 0;
7876}
7877
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007878
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007879static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7880 struct wpa_ssid *s, const u8 *addr,
7881 int iface_addr)
7882{
7883 struct psk_list_entry *psk, *tmp;
7884 int changed = 0;
7885
7886 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7887 list) {
7888 if ((iface_addr && !psk->p2p &&
7889 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7890 (!iface_addr && psk->p2p &&
7891 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7892 wpa_dbg(wpa_s, MSG_DEBUG,
7893 "P2P: Remove persistent group PSK list entry for "
7894 MACSTR " p2p=%u",
7895 MAC2STR(psk->addr), psk->p2p);
7896 dl_list_del(&psk->list);
7897 os_free(psk);
7898 changed++;
7899 }
7900 }
7901
7902 return changed;
7903}
7904
7905
7906void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7907 const u8 *p2p_dev_addr,
7908 const u8 *psk, size_t psk_len)
7909{
7910 struct wpa_ssid *ssid = wpa_s->current_ssid;
7911 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007912 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007913
7914 if (psk_len != sizeof(p->psk))
7915 return;
7916
7917 if (p2p_dev_addr) {
7918 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7919 " p2p_dev_addr=" MACSTR,
7920 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7921 if (is_zero_ether_addr(p2p_dev_addr))
7922 p2p_dev_addr = NULL;
7923 } else {
7924 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7925 MAC2STR(mac_addr));
7926 }
7927
7928 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7929 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7930 /* To be added to persistent group once created */
7931 if (wpa_s->global->add_psk == NULL) {
7932 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7933 if (wpa_s->global->add_psk == NULL)
7934 return;
7935 }
7936 p = wpa_s->global->add_psk;
7937 if (p2p_dev_addr) {
7938 p->p2p = 1;
7939 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7940 } else {
7941 p->p2p = 0;
7942 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7943 }
7944 os_memcpy(p->psk, psk, psk_len);
7945 return;
7946 }
7947
7948 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7950 return;
7951 }
7952
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007953 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, NULL, ssid->ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007954 ssid->ssid_len);
7955 if (!persistent) {
7956 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7957 return;
7958 }
7959
7960 p = os_zalloc(sizeof(*p));
7961 if (p == NULL)
7962 return;
7963 if (p2p_dev_addr) {
7964 p->p2p = 1;
7965 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7966 } else {
7967 p->p2p = 0;
7968 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7969 }
7970 os_memcpy(p->psk, psk, psk_len);
7971
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007972 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7973 (last = dl_list_last(&persistent->psk_list,
7974 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007975 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7976 MACSTR " (p2p=%u) to make room for a new one",
7977 MAC2STR(last->addr), last->p2p);
7978 dl_list_del(&last->list);
7979 os_free(last);
7980 }
7981
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007982 wpas_p2p_remove_psk_entry(wpa_s->p2pdev, persistent,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007983 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7984 p2p_dev_addr == NULL);
7985 if (p2p_dev_addr) {
7986 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7987 MACSTR, MAC2STR(p2p_dev_addr));
7988 } else {
7989 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7990 MAC2STR(mac_addr));
7991 }
7992 dl_list_add(&persistent->psk_list, &p->list);
7993
Dmitry Shmidt9c175262016-03-03 10:20:07 -08007994 if (wpa_s->p2pdev->conf->update_config &&
7995 wpa_config_write(wpa_s->p2pdev->confname, wpa_s->p2pdev->conf))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007996 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007997}
7998
7999
8000static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8001 struct wpa_ssid *s, const u8 *addr,
8002 int iface_addr)
8003{
8004 int res;
8005
8006 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008007 if (res > 0 && wpa_s->conf->update_config &&
8008 wpa_config_write(wpa_s->confname, wpa_s->conf))
8009 wpa_dbg(wpa_s, MSG_DEBUG,
8010 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008011}
8012
8013
8014static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8015 const u8 *peer, int iface_addr)
8016{
8017 struct hostapd_data *hapd;
8018 struct hostapd_wpa_psk *psk, *prev, *rem;
8019 struct sta_info *sta;
8020
8021 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8022 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8023 return;
8024
8025 /* Remove per-station PSK entry */
8026 hapd = wpa_s->ap_iface->bss[0];
8027 prev = NULL;
8028 psk = hapd->conf->ssid.wpa_psk;
8029 while (psk) {
8030 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8031 (!iface_addr &&
8032 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8033 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8034 MACSTR " iface_addr=%d",
8035 MAC2STR(peer), iface_addr);
8036 if (prev)
8037 prev->next = psk->next;
8038 else
8039 hapd->conf->ssid.wpa_psk = psk->next;
8040 rem = psk;
8041 psk = psk->next;
8042 os_free(rem);
8043 } else {
8044 prev = psk;
8045 psk = psk->next;
8046 }
8047 }
8048
8049 /* Disconnect from group */
8050 if (iface_addr)
8051 sta = ap_get_sta(hapd, peer);
8052 else
8053 sta = ap_get_sta_p2p(hapd, peer);
8054 if (sta) {
8055 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8056 " (iface_addr=%d) from group",
8057 MAC2STR(peer), iface_addr);
8058 hostapd_drv_sta_deauth(hapd, sta->addr,
8059 WLAN_REASON_DEAUTH_LEAVING);
8060 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8061 }
8062}
8063
8064
8065void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8066 int iface_addr)
8067{
8068 struct wpa_ssid *s;
8069 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008070 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008071
8072 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8073
8074 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008075 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008076 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8077 continue;
8078 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07008079 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
8080 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008081 }
8082
8083 /* Remove from any operating group */
8084 for (w = wpa_s->global->ifaces; w; w = w->next)
8085 wpas_p2p_remove_client_go(w, peer, iface_addr);
8086}
8087
8088
8089static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8090{
8091 struct wpa_supplicant *wpa_s = eloop_ctx;
8092 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8093}
8094
8095
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008096static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8097{
8098 struct wpa_supplicant *wpa_s = eloop_ctx;
8099
8100 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8101 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8102}
8103
8104
8105int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8106 struct wpa_ssid *ssid)
8107{
8108 struct wpa_supplicant *iface;
8109
8110 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8111 if (!iface->current_ssid ||
8112 iface->current_ssid->frequency == freq ||
8113 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8114 !iface->current_ssid->p2p_group))
8115 continue;
8116
8117 /* Remove the connection with least priority */
8118 if (!wpas_is_p2p_prioritized(iface)) {
8119 /* STA connection has priority over existing
8120 * P2P connection, so remove the interface. */
8121 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8122 eloop_register_timeout(0, 0,
8123 wpas_p2p_group_freq_conflict,
8124 iface, NULL);
8125 /* If connection in progress is P2P connection, do not
8126 * proceed for the connection. */
8127 if (wpa_s == iface)
8128 return -1;
8129 else
8130 return 0;
8131 } else {
8132 /* P2P connection has priority, disable the STA network
8133 */
8134 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8135 ssid);
8136 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8137 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8138 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8139 ETH_ALEN);
8140 /* If P2P connection is in progress, continue
8141 * connecting...*/
8142 if (wpa_s == iface)
8143 return 0;
8144 else
8145 return -1;
8146 }
8147 }
8148
8149 return 0;
8150}
8151
8152
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008153int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8154{
8155 struct wpa_ssid *ssid = wpa_s->current_ssid;
8156
8157 if (ssid == NULL || !ssid->p2p_group)
8158 return 0;
8159
8160 if (wpa_s->p2p_last_4way_hs_fail &&
8161 wpa_s->p2p_last_4way_hs_fail == ssid) {
8162 u8 go_dev_addr[ETH_ALEN];
8163 struct wpa_ssid *persistent;
8164
8165 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8166 ssid->ssid,
8167 ssid->ssid_len) <= 0) {
8168 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8169 goto disconnect;
8170 }
8171
8172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8173 MACSTR, MAC2STR(go_dev_addr));
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008174 persistent = wpas_p2p_get_persistent(wpa_s->p2pdev, go_dev_addr,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008175 ssid->ssid,
8176 ssid->ssid_len);
8177 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8178 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8179 goto disconnect;
8180 }
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008181 wpa_msg_global(wpa_s->p2pdev, MSG_INFO,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008182 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8183 persistent->id);
8184 disconnect:
8185 wpa_s->p2p_last_4way_hs_fail = NULL;
8186 /*
8187 * Remove the group from a timeout to avoid issues with caller
8188 * continuing to use the interface if this is on a P2P group
8189 * interface.
8190 */
8191 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8192 wpa_s, NULL);
8193 return 1;
8194 }
8195
8196 wpa_s->p2p_last_4way_hs_fail = ssid;
8197 return 0;
8198}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008199
8200
8201#ifdef CONFIG_WPS_NFC
8202
8203static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8204 struct wpabuf *p2p)
8205{
8206 struct wpabuf *ret;
8207 size_t wsc_len;
8208
8209 if (p2p == NULL) {
8210 wpabuf_free(wsc);
8211 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8212 return NULL;
8213 }
8214
8215 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8216 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8217 if (ret == NULL) {
8218 wpabuf_free(wsc);
8219 wpabuf_free(p2p);
8220 return NULL;
8221 }
8222
8223 wpabuf_put_be16(ret, wsc_len);
8224 if (wsc)
8225 wpabuf_put_buf(ret, wsc);
8226 wpabuf_put_be16(ret, wpabuf_len(p2p));
8227 wpabuf_put_buf(ret, p2p);
8228
8229 wpabuf_free(wsc);
8230 wpabuf_free(p2p);
8231 wpa_hexdump_buf(MSG_DEBUG,
8232 "P2P: Generated NFC connection handover message", ret);
8233
8234 if (ndef && ret) {
8235 struct wpabuf *tmp;
8236 tmp = ndef_build_p2p(ret);
8237 wpabuf_free(ret);
8238 if (tmp == NULL) {
8239 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8240 return NULL;
8241 }
8242 ret = tmp;
8243 }
8244
8245 return ret;
8246}
8247
8248
8249static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8250 struct wpa_ssid **ssid, u8 *go_dev_addr)
8251{
8252 struct wpa_supplicant *iface;
8253
8254 if (go_dev_addr)
8255 os_memset(go_dev_addr, 0, ETH_ALEN);
8256 if (ssid)
8257 *ssid = NULL;
8258 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8259 if (iface->wpa_state < WPA_ASSOCIATING ||
8260 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8261 !iface->current_ssid->p2p_group ||
8262 iface->current_ssid->mode != WPAS_MODE_INFRA)
8263 continue;
8264 if (ssid)
8265 *ssid = iface->current_ssid;
8266 if (go_dev_addr)
8267 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8268 return iface->assoc_freq;
8269 }
8270 return 0;
8271}
8272
8273
8274struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8275 int ndef)
8276{
8277 struct wpabuf *wsc, *p2p;
8278 struct wpa_ssid *ssid;
8279 u8 go_dev_addr[ETH_ALEN];
8280 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8281
8282 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8283 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8284 return NULL;
8285 }
8286
8287 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8288 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8289 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8290 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8291 return NULL;
8292 }
8293
8294 if (cli_freq == 0) {
8295 wsc = wps_build_nfc_handover_req_p2p(
8296 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8297 } else
8298 wsc = NULL;
8299 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8300 go_dev_addr, ssid ? ssid->ssid : NULL,
8301 ssid ? ssid->ssid_len : 0);
8302
8303 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8304}
8305
8306
8307struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8308 int ndef, int tag)
8309{
8310 struct wpabuf *wsc, *p2p;
8311 struct wpa_ssid *ssid;
8312 u8 go_dev_addr[ETH_ALEN];
8313 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8314
8315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8316 return NULL;
8317
8318 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8319 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8320 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8321 return NULL;
8322
8323 if (cli_freq == 0) {
8324 wsc = wps_build_nfc_handover_sel_p2p(
8325 wpa_s->parent->wps,
8326 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8327 DEV_PW_NFC_CONNECTION_HANDOVER,
8328 wpa_s->conf->wps_nfc_dh_pubkey,
8329 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8330 } else
8331 wsc = NULL;
8332 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8333 go_dev_addr, ssid ? ssid->ssid : NULL,
8334 ssid ? ssid->ssid_len : 0);
8335
8336 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8337}
8338
8339
8340static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8341 struct p2p_nfc_params *params)
8342{
8343 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8344 "connection handover (freq=%d)",
8345 params->go_freq);
8346
8347 if (params->go_freq && params->go_ssid_len) {
8348 wpa_s->p2p_wps_method = WPS_NFC;
8349 wpa_s->pending_join_wps_method = WPS_NFC;
8350 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8351 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8352 ETH_ALEN);
8353 return wpas_p2p_join_start(wpa_s, params->go_freq,
8354 params->go_ssid,
8355 params->go_ssid_len);
8356 }
8357
8358 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8359 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008360 params->go_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008361 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8362 params->go_ssid_len ? params->go_ssid : NULL,
8363 params->go_ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008364}
8365
8366
8367static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8368 struct p2p_nfc_params *params, int tag)
8369{
8370 int res, persistent;
8371 struct wpa_ssid *ssid;
8372
8373 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8374 "connection handover");
8375 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8376 ssid = wpa_s->current_ssid;
8377 if (ssid == NULL)
8378 continue;
8379 if (ssid->mode != WPAS_MODE_P2P_GO)
8380 continue;
8381 if (wpa_s->ap_iface == NULL)
8382 continue;
8383 break;
8384 }
8385 if (wpa_s == NULL) {
8386 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8387 return -1;
8388 }
8389
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008390 if (wpa_s->p2pdev->p2p_oob_dev_pw_id !=
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008391 DEV_PW_NFC_CONNECTION_HANDOVER &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008392 !wpa_s->p2pdev->p2p_oob_dev_pw) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008393 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8394 return -1;
8395 }
8396 res = wpas_ap_wps_add_nfc_pw(
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008397 wpa_s, wpa_s->p2pdev->p2p_oob_dev_pw_id,
8398 wpa_s->p2pdev->p2p_oob_dev_pw,
8399 wpa_s->p2pdev->p2p_peer_oob_pk_hash_known ?
8400 wpa_s->p2pdev->p2p_peer_oob_pubkey_hash : NULL);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008401 if (res)
8402 return res;
8403
8404 if (!tag) {
8405 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8406 return 0;
8407 }
8408
8409 if (!params->peer ||
8410 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8411 return 0;
8412
8413 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8414 " to join", MAC2STR(params->peer->p2p_device_addr));
8415
8416 wpa_s->global->p2p_invite_group = wpa_s;
8417 persistent = ssid->p2p_persistent_group &&
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008418 wpas_p2p_get_persistent(wpa_s->p2pdev,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008419 params->peer->p2p_device_addr,
8420 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008421 wpa_s->p2pdev->pending_invite_ssid_id = -1;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008422
8423 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8424 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8425 ssid->ssid, ssid->ssid_len, ssid->frequency,
8426 wpa_s->global->p2p_dev_addr, persistent, 0,
Dmitry Shmidt849734c2016-05-27 09:59:01 -07008427 wpa_s->p2pdev->p2p_oob_dev_pw_id);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008428}
8429
8430
8431static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8432 struct p2p_nfc_params *params,
8433 int forced_freq)
8434{
8435 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8436 "connection handover");
8437 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8438 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008439 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008440 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8441 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008442}
8443
8444
8445static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8446 struct p2p_nfc_params *params,
8447 int forced_freq)
8448{
8449 int res;
8450
8451 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8452 "connection handover");
8453 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8454 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008455 forced_freq, wpa_s->p2p_go_vht_center_freq2,
Dmitry Shmidtde47be72016-01-07 12:52:55 -08008456 -1, 0, 1, 1, wpa_s->p2p_go_max_oper_chwidth,
8457 NULL, 0);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008458 if (res)
8459 return res;
8460
8461 res = wpas_p2p_listen(wpa_s, 60);
8462 if (res) {
8463 p2p_unauthorize(wpa_s->global->p2p,
8464 params->peer->p2p_device_addr);
8465 }
8466
8467 return res;
8468}
8469
8470
8471static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8472 const struct wpabuf *data,
8473 int sel, int tag, int forced_freq)
8474{
8475 const u8 *pos, *end;
8476 u16 len, id;
8477 struct p2p_nfc_params params;
8478 int res;
8479
8480 os_memset(&params, 0, sizeof(params));
8481 params.sel = sel;
8482
8483 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8484
8485 pos = wpabuf_head(data);
8486 end = pos + wpabuf_len(data);
8487
8488 if (end - pos < 2) {
8489 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8490 "attributes");
8491 return -1;
8492 }
8493 len = WPA_GET_BE16(pos);
8494 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008495 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008496 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8497 "attributes");
8498 return -1;
8499 }
8500 params.wsc_attr = pos;
8501 params.wsc_len = len;
8502 pos += len;
8503
8504 if (end - pos < 2) {
8505 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8506 "attributes");
8507 return -1;
8508 }
8509 len = WPA_GET_BE16(pos);
8510 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008511 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008512 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8513 "attributes");
8514 return -1;
8515 }
8516 params.p2p_attr = pos;
8517 params.p2p_len = len;
8518 pos += len;
8519
8520 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8521 params.wsc_attr, params.wsc_len);
8522 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8523 params.p2p_attr, params.p2p_len);
8524 if (pos < end) {
8525 wpa_hexdump(MSG_DEBUG,
8526 "P2P: Ignored extra data after P2P attributes",
8527 pos, end - pos);
8528 }
8529
8530 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8531 if (res)
8532 return res;
8533
8534 if (params.next_step == NO_ACTION)
8535 return 0;
8536
8537 if (params.next_step == BOTH_GO) {
8538 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8539 MAC2STR(params.peer->p2p_device_addr));
8540 return 0;
8541 }
8542
8543 if (params.next_step == PEER_CLIENT) {
8544 if (!is_zero_ether_addr(params.go_dev_addr)) {
8545 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8546 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8547 " ssid=\"%s\"",
8548 MAC2STR(params.peer->p2p_device_addr),
8549 params.go_freq,
8550 MAC2STR(params.go_dev_addr),
8551 wpa_ssid_txt(params.go_ssid,
8552 params.go_ssid_len));
8553 } else {
8554 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8555 "peer=" MACSTR " freq=%d",
8556 MAC2STR(params.peer->p2p_device_addr),
8557 params.go_freq);
8558 }
8559 return 0;
8560 }
8561
8562 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8563 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8564 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8565 return 0;
8566 }
8567
8568 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8569 wpa_s->p2p_oob_dev_pw = NULL;
8570
8571 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8572 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8573 "received");
8574 return -1;
8575 }
8576
8577 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8578 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8579 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8580 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8581 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8582 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8583 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8584
8585 if (tag) {
8586 if (id < 0x10) {
8587 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8588 "peer OOB Device Password Id %u", id);
8589 return -1;
8590 }
8591 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8592 "Device Password Id %u", id);
8593 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8594 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8595 params.oob_dev_pw_len -
8596 WPS_OOB_PUBKEY_HASH_LEN - 2);
8597 wpa_s->p2p_oob_dev_pw_id = id;
8598 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8599 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8600 params.oob_dev_pw_len -
8601 WPS_OOB_PUBKEY_HASH_LEN - 2);
8602 if (wpa_s->p2p_oob_dev_pw == NULL)
8603 return -1;
8604
8605 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8606 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8607 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8608 return -1;
8609 } else {
8610 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8611 "without Device Password");
8612 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8613 }
8614
8615 switch (params.next_step) {
8616 case NO_ACTION:
8617 case BOTH_GO:
8618 case PEER_CLIENT:
8619 /* already covered above */
8620 return 0;
8621 case JOIN_GROUP:
8622 return wpas_p2p_nfc_join_group(wpa_s, &params);
8623 case AUTH_JOIN:
8624 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
8625 case INIT_GO_NEG:
8626 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
8627 case RESP_GO_NEG:
8628 /* TODO: use own OOB Dev Pw */
8629 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
8630 }
8631
8632 return -1;
8633}
8634
8635
8636int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8637 const struct wpabuf *data, int forced_freq)
8638{
8639 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8640 return -1;
8641
8642 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8643}
8644
8645
8646int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8647 const struct wpabuf *req,
8648 const struct wpabuf *sel, int forced_freq)
8649{
8650 struct wpabuf *tmp;
8651 int ret;
8652
8653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8654 return -1;
8655
8656 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8657
8658 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8659 wpabuf_head(req), wpabuf_len(req));
8660 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8661 wpabuf_head(sel), wpabuf_len(sel));
8662 if (forced_freq)
8663 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8664 tmp = ndef_parse_p2p(init ? sel : req);
8665 if (tmp == NULL) {
8666 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8667 return -1;
8668 }
8669
8670 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8671 forced_freq);
8672 wpabuf_free(tmp);
8673
8674 return ret;
8675}
8676
8677
8678int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8679{
8680 const u8 *if_addr;
8681 int go_intent = wpa_s->conf->p2p_go_intent;
8682 struct wpa_supplicant *iface;
8683
8684 if (wpa_s->global->p2p == NULL)
8685 return -1;
8686
8687 if (!enabled) {
8688 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8689 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8690 {
8691 if (!iface->ap_iface)
8692 continue;
8693 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8694 }
8695 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8696 0, NULL);
8697 if (wpa_s->p2p_nfc_tag_enabled)
8698 wpas_p2p_remove_pending_group_interface(wpa_s);
8699 wpa_s->p2p_nfc_tag_enabled = 0;
8700 return 0;
8701 }
8702
8703 if (wpa_s->global->p2p_disabled)
8704 return -1;
8705
8706 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8707 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8708 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8709 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8710 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8711 "to allow static handover cases");
8712 return -1;
8713 }
8714
8715 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8716
8717 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8718 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8719 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8720 if (wpa_s->p2p_oob_dev_pw == NULL)
8721 return -1;
8722 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8723
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008724 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8725 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8726 /*
8727 * P2P Group Interface present and the command came on group
8728 * interface, so enable the token for the current interface.
8729 */
8730 wpa_s->create_p2p_iface = 0;
8731 } else {
8732 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8733 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008734
8735 if (wpa_s->create_p2p_iface) {
8736 enum wpa_driver_if_type iftype;
8737 /* Prepare to add a new interface for the group */
8738 iftype = WPA_IF_P2P_GROUP;
8739 if (go_intent == 15)
8740 iftype = WPA_IF_P2P_GO;
8741 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8742 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8743 "interface for the group");
8744 return -1;
8745 }
8746
8747 if_addr = wpa_s->pending_interface_addr;
Dmitry Shmidt9c175262016-03-03 10:20:07 -08008748 } else if (wpa_s->p2p_mgmt)
8749 if_addr = wpa_s->parent->own_addr;
8750 else
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008751 if_addr = wpa_s->own_addr;
8752
8753 wpa_s->p2p_nfc_tag_enabled = enabled;
8754
8755 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8756 struct hostapd_data *hapd;
8757 if (iface->ap_iface == NULL)
8758 continue;
8759 hapd = iface->ap_iface->bss[0];
8760 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8761 hapd->conf->wps_nfc_dh_pubkey =
8762 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8763 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8764 hapd->conf->wps_nfc_dh_privkey =
8765 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8766 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8767 hapd->conf->wps_nfc_dev_pw =
8768 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8769 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8770
8771 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8772 wpa_dbg(iface, MSG_DEBUG,
8773 "P2P: Failed to enable NFC Tag for GO");
8774 }
8775 }
8776 p2p_set_authorized_oob_dev_pw_id(
8777 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8778 if_addr);
8779
8780 return 0;
8781}
8782
8783#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008784
8785
8786static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8787 struct wpa_used_freq_data *freqs,
8788 unsigned int num)
8789{
8790 u8 curr_chan, cand, chan;
8791 unsigned int i;
8792
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008793 /*
8794 * If possible, optimize the Listen channel to be a channel that is
8795 * already used by one of the other interfaces.
8796 */
8797 if (!wpa_s->conf->p2p_optimize_listen_chan)
8798 return;
8799
8800 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8801 return;
8802
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008803 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8804 for (i = 0, cand = 0; i < num; i++) {
8805 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8806 if (curr_chan == chan) {
8807 cand = 0;
8808 break;
8809 }
8810
8811 if (chan == 1 || chan == 6 || chan == 11)
8812 cand = chan;
8813 }
8814
8815 if (cand) {
8816 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008817 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008818 cand);
8819 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8820 }
8821}
8822
8823
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008824static int wpas_p2p_move_go_csa(struct wpa_supplicant *wpa_s)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008825{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008826 struct hostapd_config *conf;
8827 struct p2p_go_neg_results params;
8828 struct csa_settings csa_settings;
8829 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8830 int old_freq = current_ssid->frequency;
8831 int ret;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008832
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008833 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP_CSA)) {
8834 wpa_dbg(wpa_s, MSG_DEBUG, "CSA is not enabled");
8835 return -1;
8836 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008837
8838 /*
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008839 * TODO: This function may not always work correctly. For example,
8840 * when we have a running GO and a BSS on a DFS channel.
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008841 */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008842 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008843 wpa_dbg(wpa_s, MSG_DEBUG,
8844 "P2P CSA: Failed to select new frequency for GO");
8845 return -1;
8846 }
8847
8848 if (current_ssid->frequency == params.freq) {
8849 wpa_dbg(wpa_s, MSG_DEBUG,
8850 "P2P CSA: Selected same frequency - not moving GO");
8851 return 0;
8852 }
8853
8854 conf = hostapd_config_defaults();
8855 if (!conf) {
8856 wpa_dbg(wpa_s, MSG_DEBUG,
8857 "P2P CSA: Failed to allocate default config");
8858 return -1;
8859 }
8860
8861 current_ssid->frequency = params.freq;
8862 if (wpa_supplicant_conf_ap_ht(wpa_s, current_ssid, conf)) {
8863 wpa_dbg(wpa_s, MSG_DEBUG,
8864 "P2P CSA: Failed to create new GO config");
8865 ret = -1;
8866 goto out;
8867 }
8868
8869 if (conf->hw_mode != wpa_s->ap_iface->current_mode->mode) {
8870 wpa_dbg(wpa_s, MSG_DEBUG,
8871 "P2P CSA: CSA to a different band is not supported");
8872 ret = -1;
8873 goto out;
8874 }
8875
8876 os_memset(&csa_settings, 0, sizeof(csa_settings));
8877 csa_settings.cs_count = P2P_GO_CSA_COUNT;
8878 csa_settings.block_tx = P2P_GO_CSA_BLOCK_TX;
8879 csa_settings.freq_params.freq = params.freq;
8880 csa_settings.freq_params.sec_channel_offset = conf->secondary_channel;
8881 csa_settings.freq_params.ht_enabled = conf->ieee80211n;
8882 csa_settings.freq_params.bandwidth = conf->secondary_channel ? 40 : 20;
8883
8884 if (conf->ieee80211ac) {
8885 int freq1 = 0, freq2 = 0;
8886 u8 chan, opclass;
8887
8888 if (ieee80211_freq_to_channel_ext(params.freq,
8889 conf->secondary_channel,
8890 conf->vht_oper_chwidth,
8891 &opclass, &chan) ==
8892 NUM_HOSTAPD_MODES) {
8893 wpa_printf(MSG_ERROR, "P2P CSA: Bad freq");
8894 ret = -1;
8895 goto out;
8896 }
8897
8898 if (conf->vht_oper_centr_freq_seg0_idx)
8899 freq1 = ieee80211_chan_to_freq(
8900 NULL, opclass,
8901 conf->vht_oper_centr_freq_seg0_idx);
8902
8903 if (conf->vht_oper_centr_freq_seg1_idx)
8904 freq2 = ieee80211_chan_to_freq(
8905 NULL, opclass,
8906 conf->vht_oper_centr_freq_seg1_idx);
8907
8908 if (freq1 < 0 || freq2 < 0) {
8909 wpa_dbg(wpa_s, MSG_DEBUG,
8910 "P2P CSA: Selected invalid VHT center freqs");
8911 ret = -1;
8912 goto out;
8913 }
8914
8915 csa_settings.freq_params.vht_enabled = conf->ieee80211ac;
8916 csa_settings.freq_params.center_freq1 = freq1;
8917 csa_settings.freq_params.center_freq2 = freq2;
8918
8919 switch (conf->vht_oper_chwidth) {
8920 case VHT_CHANWIDTH_80MHZ:
8921 case VHT_CHANWIDTH_80P80MHZ:
8922 csa_settings.freq_params.bandwidth = 80;
8923 break;
8924 case VHT_CHANWIDTH_160MHZ:
8925 csa_settings.freq_params.bandwidth = 160;
8926 break;
8927 }
8928 }
8929
8930 ret = ap_switch_channel(wpa_s, &csa_settings);
8931out:
8932 current_ssid->frequency = old_freq;
8933 hostapd_config_free(conf);
8934 return ret;
8935}
8936
8937
8938static void wpas_p2p_move_go_no_csa(struct wpa_supplicant *wpa_s)
8939{
8940 struct p2p_go_neg_results params;
8941 struct wpa_ssid *current_ssid = wpa_s->current_ssid;
8942
8943 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
8944
8945 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz",
8946 current_ssid->frequency);
8947
8948 /* Stop the AP functionality */
8949 /* TODO: Should do this in a way that does not indicated to possible
8950 * P2P Clients in the group that the group is terminated. */
8951 wpa_supplicant_ap_deinit(wpa_s);
8952
8953 /* Reselect the GO frequency */
Dmitry Shmidtd7ff03d2015-12-04 14:49:35 -08008954 if (wpas_p2p_init_go_params(wpa_s, &params, 0, 0, 0, 0, 0, NULL)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008955 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Failed to reselect freq");
8956 wpas_p2p_group_delete(wpa_s,
8957 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8958 return;
8959 }
8960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New freq selected for the GO (%u MHz)",
8961 params.freq);
8962
8963 if (params.freq &&
8964 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
8965 wpa_printf(MSG_DEBUG,
8966 "P2P: Selected freq (%u MHz) is not valid for P2P",
8967 params.freq);
8968 wpas_p2p_group_delete(wpa_s,
8969 P2P_GROUP_REMOVAL_GO_LEAVE_CHANNEL);
8970 return;
8971 }
8972
8973 /* Update the frequency */
8974 current_ssid->frequency = params.freq;
8975 wpa_s->connect_without_scan = current_ssid;
8976 wpa_s->reassociate = 1;
8977 wpa_s->disconnected = 0;
8978 wpa_supplicant_req_scan(wpa_s, 0, 0);
8979}
8980
8981
8982static void wpas_p2p_move_go(void *eloop_ctx, void *timeout_ctx)
8983{
8984 struct wpa_supplicant *wpa_s = eloop_ctx;
8985
8986 if (!wpa_s->ap_iface || !wpa_s->current_ssid)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008987 return;
8988
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008989 wpas_p2p_go_update_common_freqs(wpa_s);
8990
8991 /* Do not move GO in the middle of a CSA */
8992 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
8993 wpa_printf(MSG_DEBUG,
8994 "P2P: CSA is in progress - not moving GO");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008995 return;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08008996 }
8997
8998 /*
8999 * First, try a channel switch flow. If it is not supported or fails,
9000 * take down the GO and bring it up again.
9001 */
9002 if (wpas_p2p_move_go_csa(wpa_s) < 0)
9003 wpas_p2p_move_go_no_csa(wpa_s);
9004}
9005
9006
9007static void wpas_p2p_reconsider_moving_go(void *eloop_ctx, void *timeout_ctx)
9008{
9009 struct wpa_supplicant *wpa_s = eloop_ctx;
9010 struct wpa_used_freq_data *freqs = NULL;
9011 unsigned int num = wpa_s->num_multichan_concurrent;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009012
9013 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9014 if (!freqs)
9015 return;
9016
9017 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9018
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009019 /* Previous attempt to move a GO was not possible -- try again. */
9020 wpas_p2p_consider_moving_gos(wpa_s, freqs, num,
9021 WPAS_P2P_CHANNEL_UPDATE_ANY);
9022
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009023 os_free(freqs);
9024}
9025
9026
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08009027/*
9028 * Consider moving a GO from its currently used frequency:
9029 * 1. It is possible that due to regulatory consideration the frequency
9030 * can no longer be used and there is a need to evacuate the GO.
9031 * 2. It is possible that due to MCC considerations, it would be preferable
9032 * to move the GO to a channel that is currently used by some other
9033 * station interface.
9034 *
9035 * In case a frequency that became invalid is once again valid, cancel a
9036 * previously initiated GO frequency change.
9037 */
9038static void wpas_p2p_consider_moving_one_go(struct wpa_supplicant *wpa_s,
9039 struct wpa_used_freq_data *freqs,
9040 unsigned int num)
9041{
9042 unsigned int i, invalid_freq = 0, policy_move = 0, flags = 0;
9043 unsigned int timeout;
9044 int freq;
9045
9046 wpas_p2p_go_update_common_freqs(wpa_s);
9047
9048 freq = wpa_s->current_ssid->frequency;
9049 for (i = 0, invalid_freq = 0; i < num; i++) {
9050 if (freqs[i].freq == freq) {
9051 flags = freqs[i].flags;
9052
9053 /* The channel is invalid, must change it */
9054 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
9055 wpa_dbg(wpa_s, MSG_DEBUG,
9056 "P2P: Freq=%d MHz no longer valid for GO",
9057 freq);
9058 invalid_freq = 1;
9059 }
9060 } else if (freqs[i].flags == 0) {
9061 /* Freq is not used by any other station interface */
9062 continue;
9063 } else if (!p2p_supported_freq(wpa_s->global->p2p,
9064 freqs[i].freq)) {
9065 /* Freq is not valid for P2P use cases */
9066 continue;
9067 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9068 P2P_GO_FREQ_MOVE_SCM) {
9069 policy_move = 1;
9070 } else if (wpa_s->conf->p2p_go_freq_change_policy ==
9071 P2P_GO_FREQ_MOVE_SCM_PEER_SUPPORTS &&
9072 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9073 policy_move = 1;
9074 } else if ((wpa_s->conf->p2p_go_freq_change_policy ==
9075 P2P_GO_FREQ_MOVE_SCM_ECSA) &&
9076 wpas_p2p_go_is_peer_freq(wpa_s, freqs[i].freq)) {
9077 if (!p2p_get_group_num_members(wpa_s->p2p_group)) {
9078 policy_move = 1;
9079 } else if ((wpa_s->drv_flags &
9080 WPA_DRIVER_FLAGS_AP_CSA) &&
9081 wpas_p2p_go_clients_support_ecsa(wpa_s)) {
9082 u8 chan;
9083
9084 /*
9085 * We do not support CSA between bands, so move
9086 * GO only within the same band.
9087 */
9088 if (wpa_s->ap_iface->current_mode->mode ==
9089 ieee80211_freq_to_chan(freqs[i].freq,
9090 &chan))
9091 policy_move = 1;
9092 }
9093 }
9094 }
9095
9096 wpa_dbg(wpa_s, MSG_DEBUG,
9097 "P2P: GO move: invalid_freq=%u, policy_move=%u, flags=0x%X",
9098 invalid_freq, policy_move, flags);
9099
9100 /*
9101 * The channel is valid, or we are going to have a policy move, so
9102 * cancel timeout.
9103 */
9104 if (!invalid_freq || policy_move) {
9105 wpa_dbg(wpa_s, MSG_DEBUG,
9106 "P2P: Cancel a GO move from freq=%d MHz", freq);
9107 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9108
9109 if (wpas_p2p_in_progress(wpa_s)) {
9110 wpa_dbg(wpa_s, MSG_DEBUG,
9111 "P2P: GO move: policy CS is not allowed - setting timeout to re-consider GO move");
9112 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go,
9113 wpa_s, NULL);
9114 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9115 wpas_p2p_reconsider_moving_go,
9116 wpa_s, NULL);
9117 return;
9118 }
9119 }
9120
9121 if (!invalid_freq && (!policy_move || flags != 0)) {
9122 wpa_dbg(wpa_s, MSG_DEBUG,
9123 "P2P: Not initiating a GO frequency change");
9124 return;
9125 }
9126
9127 /*
9128 * Do not consider moving GO if it is in the middle of a CSA. When the
9129 * CSA is finished this flow should be retriggered.
9130 */
9131 if (hostapd_csa_in_progress(wpa_s->ap_iface)) {
9132 wpa_dbg(wpa_s, MSG_DEBUG,
9133 "P2P: Not initiating a GO frequency change - CSA is in progress");
9134 return;
9135 }
9136
9137 if (invalid_freq && !wpas_p2p_disallowed_freq(wpa_s->global, freq))
9138 timeout = P2P_GO_FREQ_CHANGE_TIME;
9139 else
9140 timeout = 0;
9141
9142 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Move GO from freq=%d MHz in %d secs",
9143 freq, timeout);
9144 eloop_cancel_timeout(wpas_p2p_move_go, wpa_s, NULL);
9145 eloop_register_timeout(timeout, 0, wpas_p2p_move_go, wpa_s, NULL);
9146}
9147
9148
9149static void wpas_p2p_consider_moving_gos(struct wpa_supplicant *wpa_s,
9150 struct wpa_used_freq_data *freqs,
9151 unsigned int num,
9152 enum wpas_p2p_channel_update_trig trig)
9153{
9154 struct wpa_supplicant *ifs;
9155
9156 eloop_cancel_timeout(wpas_p2p_reconsider_moving_go, ELOOP_ALL_CTX,
9157 NULL);
9158
9159 /*
9160 * Travers all the radio interfaces, and for each GO interface, check
9161 * if there is a need to move the GO from the frequency it is using,
9162 * or in case the frequency is valid again, cancel the evacuation flow.
9163 */
9164 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
9165 radio_list) {
9166 if (ifs->current_ssid == NULL ||
9167 ifs->current_ssid->mode != WPAS_MODE_P2P_GO)
9168 continue;
9169
9170 /*
9171 * The GO was just started or completed channel switch, no need
9172 * to move it.
9173 */
9174 if (wpa_s == ifs &&
9175 (trig == WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE ||
9176 trig == WPAS_P2P_CHANNEL_UPDATE_CS)) {
9177 wpa_dbg(wpa_s, MSG_DEBUG,
9178 "P2P: GO move - schedule re-consideration");
9179 eloop_register_timeout(P2P_RECONSIDER_GO_MOVE_DELAY, 0,
9180 wpas_p2p_reconsider_moving_go,
9181 wpa_s, NULL);
9182 continue;
9183 }
9184
9185 wpas_p2p_consider_moving_one_go(ifs, freqs, num);
9186 }
9187}
9188
9189
9190void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9191{
9192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9193 return;
9194
9195 wpas_p2p_update_channel_list(wpa_s,
9196 WPAS_P2P_CHANNEL_UPDATE_STATE_CHANGE);
9197}
9198
9199
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009200void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9201{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009202 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9203 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9204 "the management interface is being removed");
9205 wpas_p2p_deinit_global(wpa_s->global);
9206 }
9207}
9208
9209
9210void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9211{
9212 if (wpa_s->ap_iface->bss)
9213 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9214 wpas_p2p_group_deinit(wpa_s);
9215}
Dmitry Shmidt58d12ad2016-07-28 10:07:03 -07009216
9217
9218int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
9219 unsigned int period, unsigned int interval,
9220 unsigned int count)
9221{
9222 struct p2p_data *p2p = wpa_s->global->p2p;
9223 u8 *device_types;
9224 size_t dev_types_len;
9225 struct wpabuf *buf;
9226 int ret;
9227
9228 if (wpa_s->p2p_lo_started) {
9229 wpa_dbg(wpa_s, MSG_DEBUG,
9230 "P2P Listen offload is already started");
9231 return 0;
9232 }
9233
9234 if (wpa_s->global->p2p == NULL ||
9235 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_LISTEN_OFFLOAD)) {
9236 wpa_printf(MSG_DEBUG, "P2P: Listen offload not supported");
9237 return -1;
9238 }
9239
9240 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
9241 wpa_printf(MSG_ERROR, "P2P: Input channel not supported: %u",
9242 freq);
9243 return -1;
9244 }
9245
9246 /* Get device type */
9247 dev_types_len = (wpa_s->conf->num_sec_device_types + 1) *
9248 WPS_DEV_TYPE_LEN;
9249 device_types = os_malloc(dev_types_len);
9250 if (!device_types)
9251 return -1;
9252 os_memcpy(device_types, wpa_s->conf->device_type, WPS_DEV_TYPE_LEN);
9253 os_memcpy(&device_types[WPS_DEV_TYPE_LEN], wpa_s->conf->sec_device_type,
9254 wpa_s->conf->num_sec_device_types * WPS_DEV_TYPE_LEN);
9255
9256 /* Get Probe Response IE(s) */
9257 buf = p2p_build_probe_resp_template(p2p, freq);
9258 if (!buf) {
9259 os_free(device_types);
9260 return -1;
9261 }
9262
9263 ret = wpa_drv_p2p_lo_start(wpa_s, freq, period, interval, count,
9264 device_types, dev_types_len,
9265 wpabuf_mhead_u8(buf), wpabuf_len(buf));
9266 if (ret < 0)
9267 wpa_dbg(wpa_s, MSG_DEBUG,
9268 "P2P: Failed to start P2P listen offload");
9269
9270 os_free(device_types);
9271 wpabuf_free(buf);
9272
9273 if (ret == 0) {
9274 wpa_s->p2p_lo_started = 1;
9275
9276 /* Stop current P2P listen if any */
9277 wpas_stop_listen(wpa_s);
9278 }
9279
9280 return ret;
9281}
9282
9283
9284int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s)
9285{
9286 int ret;
9287
9288 if (!wpa_s->p2p_lo_started)
9289 return 0;
9290
9291 ret = wpa_drv_p2p_lo_stop(wpa_s);
9292 if (ret < 0)
9293 wpa_dbg(wpa_s, MSG_DEBUG,
9294 "P2P: Failed to stop P2P listen offload");
9295
9296 wpa_s->p2p_lo_started = 0;
9297 return ret;
9298}