blob: e7a4db3a6a5530fcb0a5d4c3b0d2af70caf2c19f [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 Shmidt1f69aa52012-01-24 16:10:04 -080050#ifndef P2P_MAX_CLIENT_IDLE
51/*
52 * How many seconds to try to reconnect to the GO when connection in P2P client
53 * role has been lost.
54 */
55#define P2P_MAX_CLIENT_IDLE 10
56#endif /* P2P_MAX_CLIENT_IDLE */
57
Dmitry Shmidt04949592012-07-19 12:16:46 -070058#ifndef P2P_MAX_INITIAL_CONN_WAIT
59/*
60 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070061 * WPS provisioning step or after the re-invocation of a persistent group on a
62 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070063 */
64#define P2P_MAX_INITIAL_CONN_WAIT 10
65#endif /* P2P_MAX_INITIAL_CONN_WAIT */
66
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070067#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
68/*
69 * How many seconds to wait for initial 4-way handshake to get completed after
70 * WPS provisioning step on the GO. This controls the extra time the P2P
71 * operation is considered to be in progress (e.g., to delay other scans) after
72 * WPS provisioning has been completed on the GO during group formation.
73 */
74#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
75#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
76
Dmitry Shmidt56052862013-10-04 10:23:25 -070077#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
78/*
79 * How many seconds to wait for initial 4-way handshake to get completed after
80 * re-invocation of a persistent group on the GO when the client is expected
81 * to connect automatically (no user interaction).
82 */
83#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
84#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
85
Dmitry Shmidt34af3062013-07-11 10:46:32 -070086#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070088enum p2p_group_removal_reason {
89 P2P_GROUP_REMOVAL_UNKNOWN,
90 P2P_GROUP_REMOVAL_SILENT,
91 P2P_GROUP_REMOVAL_FORMATION_FAILED,
92 P2P_GROUP_REMOVAL_REQUESTED,
93 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
94 P2P_GROUP_REMOVAL_UNAVAILABLE,
95 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080096 P2P_GROUP_REMOVAL_PSK_FAILURE,
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070098};
99
Jouni Malinendc7b7132012-09-14 12:53:47 -0700100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
102static struct wpa_supplicant *
103wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
104 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800105static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
106 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800107static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
110static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700111 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800112 int auto_join, int freq,
113 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
115static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
116static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
117static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800118static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
119 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800120static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800121static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800123static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800124static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700125static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700126static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800127static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
128 enum wpa_driver_if_type type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129
130
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700131/*
132 * Get the number of concurrent channels that the HW can operate, but that are
133 * currently not in use by any of the wpa_supplicant interfaces.
134 */
135static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
136{
137 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800138 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700139
140 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
141 if (!freqs)
142 return -1;
143
144 num = get_shared_radio_freqs(wpa_s, freqs,
145 wpa_s->num_multichan_concurrent);
146 os_free(freqs);
147
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800148 unused = wpa_s->num_multichan_concurrent - num;
149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
150 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700151}
152
153
154/*
155 * Get the frequencies that are currently in use by one or more of the virtual
156 * interfaces, and that are also valid for P2P operation.
157 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700158static unsigned int
159wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
160 struct wpa_used_freq_data *p2p_freqs,
161 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700162{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700163 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700164 unsigned int num, i, j;
165
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700166 freqs = os_calloc(wpa_s->num_multichan_concurrent,
167 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700168 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700169 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700171 num = get_shared_radio_freqs_data(wpa_s, freqs,
172 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700173
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700174 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
176 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700177 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700178 p2p_freqs[j++] = freqs[i];
179 }
180
181 os_free(freqs);
182
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700183 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800184
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700185 return j;
186}
187
188
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700189static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
190 int freq)
191{
192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
193 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700194
195 /* Use the wpa_s used to control the P2P Device operation */
196 wpa_s = wpa_s->global->p2p_init_wpa_s;
197
198 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800199 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
200 wpas_p2p_num_unused_channels(wpa_s) > 0) {
201 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
202 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700203 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800204 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700205 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
206}
207
208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
210 struct wpa_scan_results *scan_res)
211{
212 size_t i;
213
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800214 if (wpa_s->p2p_scan_work) {
215 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
216 wpa_s->p2p_scan_work = NULL;
217 radio_work_done(work);
218 }
219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
221 return;
222
223 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
224 (int) scan_res->num);
225
226 for (i = 0; i < scan_res->num; i++) {
227 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800228 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700229 const u8 *ies;
230 size_t ies_len;
231
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800232 time_tmp_age.sec = bss->age / 1000;
233 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800234 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700235
236 ies = (const u8 *) (bss + 1);
237 ies_len = bss->ie_len;
238 if (bss->beacon_ie_len > 0 &&
239 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
240 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
241 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
242 MACSTR, MAC2STR(bss->bssid));
243 ies = ies + ies_len;
244 ies_len = bss->beacon_ie_len;
245 }
246
247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800249 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700250 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251 break;
252 }
253
254 p2p_scan_res_handled(wpa_s->global->p2p);
255}
256
257
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800258static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
259{
260 struct wpa_supplicant *wpa_s = work->wpa_s;
261 struct wpa_driver_scan_params *params = work->ctx;
262 int ret;
263
264 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800265 if (!work->started) {
266 wpa_scan_free_params(params);
267 return;
268 }
269
270 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800271 return;
272 }
273
274 ret = wpa_drv_scan(wpa_s, params);
275 wpa_scan_free_params(params);
276 work->ctx = NULL;
277 if (ret) {
278 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800279 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800280 return;
281 }
282
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800283 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800284 os_get_reltime(&wpa_s->scan_trigger_time);
285 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
286 wpa_s->own_scan_requested = 1;
287 wpa_s->p2p_scan_work = work;
288}
289
290
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800291static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
292 int freq)
293{
294 if (wpa_s->global->p2p_24ghz_social_channels &&
295 (freq == 2412 || freq == 2437 || freq == 2462)) {
296 /*
297 * Search all social channels regardless of whether these have
298 * been disabled for P2P operating channel use to avoid missing
299 * peers.
300 */
301 return 1;
302 }
303 return p2p_supported_freq(wpa_s->global->p2p, freq);
304}
305
306
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
308 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700309 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310{
311 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800312 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700314 unsigned int num_channels = 0;
315 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700317 u8 *n, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318
319 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
320 return -1;
321
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 if (wpa_s->p2p_scan_work) {
323 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
324 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700325 }
326
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 params = os_zalloc(sizeof(*params));
328 if (params == NULL)
329 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330
331 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800332 params->num_ssids = 1;
333 n = os_malloc(P2P_WILDCARD_SSID_LEN);
334 if (n == NULL)
335 goto fail;
336 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
337 params->ssids[0].ssid = n;
338 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
340 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700341 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
342 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 num_req_dev_types, req_dev_types);
344 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800345 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800347 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
348 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 if (ies == NULL) {
350 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800351 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 }
353 wpabuf_put_buf(ies, wps_ie);
354 wpabuf_free(wps_ie);
355
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800356 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800358 params->p2p_probe = 1;
359 n = os_malloc(wpabuf_len(ies));
360 if (n == NULL) {
361 wpabuf_free(ies);
362 goto fail;
363 }
364 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
365 params->extra_ies = n;
366 params->extra_ies_len = wpabuf_len(ies);
367 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368
369 switch (type) {
370 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700371 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
372 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800373 if (params->freqs == NULL)
374 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700375 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800376 if (wpas_p2p_search_social_channel(
377 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700378 params->freqs[num_channels++] =
379 social_channels_freq[i];
380 }
381 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 break;
383 case P2P_SCAN_FULL:
384 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800385 case P2P_SCAN_SPECIFIC:
386 params->freqs = os_calloc(2, sizeof(int));
387 if (params->freqs == NULL)
388 goto fail;
389 params->freqs[0] = freq;
390 params->freqs[1] = 0;
391 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700393 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
394 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800395 if (params->freqs == NULL)
396 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700397 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800398 if (wpas_p2p_search_social_channel(
399 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700400 params->freqs[num_channels++] =
401 social_channels_freq[i];
402 }
403 if (p2p_supported_freq(wpa_s->global->p2p, freq))
404 params->freqs[num_channels++] = freq;
405 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 break;
407 }
408
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800409 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800410 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
411 params) < 0)
412 goto fail;
413 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800415fail:
416 wpa_scan_free_params(params);
417 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418}
419
420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
422{
423 switch (p2p_group_interface) {
424 case P2P_GROUP_INTERFACE_PENDING:
425 return WPA_IF_P2P_GROUP;
426 case P2P_GROUP_INTERFACE_GO:
427 return WPA_IF_P2P_GO;
428 case P2P_GROUP_INTERFACE_CLIENT:
429 return WPA_IF_P2P_CLIENT;
430 }
431
432 return WPA_IF_P2P_GROUP;
433}
434
435
436static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
437 const u8 *ssid,
438 size_t ssid_len, int *go)
439{
440 struct wpa_ssid *s;
441
442 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
443 for (s = wpa_s->conf->ssid; s; s = s->next) {
444 if (s->disabled != 0 || !s->p2p_group ||
445 s->ssid_len != ssid_len ||
446 os_memcmp(ssid, s->ssid, ssid_len) != 0)
447 continue;
448 if (s->mode == WPAS_MODE_P2P_GO &&
449 s != wpa_s->current_ssid)
450 continue;
451 if (go)
452 *go = s->mode == WPAS_MODE_P2P_GO;
453 return wpa_s;
454 }
455 }
456
457 return NULL;
458}
459
460
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800461static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
462{
463 struct wpa_supplicant *wpa_s = eloop_ctx;
464 wpa_printf(MSG_DEBUG,
465 "P2P: Complete previously requested removal of %s",
466 wpa_s->ifname);
467 wpas_p2p_disconnect(wpa_s);
468}
469
470
471static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
472 struct wpa_supplicant *calling_wpa_s)
473{
474 if (calling_wpa_s == wpa_s && wpa_s &&
475 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
476 /*
477 * The calling wpa_s instance is going to be removed. Do that
478 * from an eloop callback to keep the instance available until
479 * the caller has returned. This my be needed, e.g., to provide
480 * control interface responses on the per-interface socket.
481 */
482 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
483 wpa_s, NULL) < 0)
484 return -1;
485 return 0;
486 }
487
488 return wpas_p2p_disconnect(wpa_s);
489}
490
491
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800492/* Determine total number of clients in active groups where we are the GO */
493static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
494{
495 unsigned int count = 0;
496 struct wpa_ssid *s;
497
498 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
499 for (s = wpa_s->conf->ssid; s; s = s->next) {
500 wpa_printf(MSG_DEBUG,
501 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
502 wpa_s, s, s->disabled, s->p2p_group,
503 s->mode);
504 if (!s->disabled && s->p2p_group &&
505 s->mode == WPAS_MODE_P2P_GO) {
506 count += p2p_get_group_num_members(
507 wpa_s->p2p_group);
508 }
509 }
510 }
511
512 return count;
513}
514
515
516/* Find an interface for a P2P group where we are the GO */
517static struct wpa_supplicant *
518wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
519{
520 struct wpa_supplicant *save = NULL;
521 struct wpa_ssid *s;
522
523 if (!wpa_s)
524 return NULL;
525
526 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
527 for (s = wpa_s->conf->ssid; s; s = s->next) {
528 if (s->disabled || !s->p2p_group ||
529 s->mode != WPAS_MODE_P2P_GO)
530 continue;
531
532 /* Prefer a group with connected clients */
533 if (p2p_get_group_num_members(wpa_s->p2p_group))
534 return wpa_s;
535 save = wpa_s;
536 }
537 }
538
539 /* No group with connected clients, so pick the one without (if any) */
540 return save;
541}
542
543
544/* Find an active P2P group where we are the GO */
545static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
546 u8 *bssid)
547{
548 struct wpa_ssid *s, *empty = NULL;
549
550 if (!wpa_s)
551 return 0;
552
553 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
554 for (s = wpa_s->conf->ssid; s; s = s->next) {
555 if (s->disabled || !s->p2p_group ||
556 s->mode != WPAS_MODE_P2P_GO)
557 continue;
558
559 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
560 if (p2p_get_group_num_members(wpa_s->p2p_group))
561 return s;
562 empty = s;
563 }
564 }
565
566 return empty;
567}
568
569
570/* Find a persistent group where we are the GO */
571static struct wpa_ssid *
572wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
573{
574 struct wpa_ssid *s;
575
576 for (s = wpa_s->conf->ssid; s; s = s->next) {
577 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
578 return s;
579 }
580
581 return NULL;
582}
583
584
585static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
586{
587 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
588 struct wpa_ssid *s;
589 u8 conncap = P2PS_SETUP_NONE;
590 unsigned int owned_members = 0;
591 unsigned int owner = 0;
592 unsigned int client = 0;
593 struct wpa_supplicant *go_wpa_s;
594 struct wpa_ssid *persistent_go;
595 int p2p_no_group_iface;
596
597 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
598
599 /*
600 * For non-concurrent capable devices:
601 * If persistent_go, then no new.
602 * If GO, then no client.
603 * If client, then no GO.
604 */
605 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
606 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
607 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
608
609 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
610 go_wpa_s, persistent_go);
611
612 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
613 tmp_wpa_s = tmp_wpa_s->next) {
614 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
615 wpa_printf(MSG_DEBUG,
616 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
617 tmp_wpa_s, s, s->disabled,
618 s->p2p_group, s->mode);
619 if (!s->disabled && s->p2p_group) {
620 if (s->mode == WPAS_MODE_P2P_GO) {
621 owned_members +=
622 p2p_get_group_num_members(
623 tmp_wpa_s->p2p_group);
624 owner++;
625 } else
626 client++;
627 }
628 }
629 }
630
631 /* If not concurrent, restrict our choices */
632 if (p2p_no_group_iface) {
633 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
634
635 if (client)
636 return P2PS_SETUP_NONE;
637
638 if (go_wpa_s) {
639 if (role == P2PS_SETUP_CLIENT ||
640 incoming == P2PS_SETUP_GROUP_OWNER ||
641 p2p_client_limit_reached(go_wpa_s->p2p_group))
642 return P2PS_SETUP_NONE;
643
644 return P2PS_SETUP_GROUP_OWNER;
645 }
646
647 if (persistent_go) {
648 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
649 if (!incoming)
650 return P2PS_SETUP_GROUP_OWNER |
651 P2PS_SETUP_CLIENT;
652 if (incoming == P2PS_SETUP_NEW) {
653 u8 r;
654
655 if (os_get_random(&r, sizeof(r)) < 0 ||
656 (r & 1))
657 return P2PS_SETUP_CLIENT;
658 return P2PS_SETUP_GROUP_OWNER;
659 }
660 }
661 }
662 }
663
664 /* If a required role has been specified, handle it here */
665 if (role && role != P2PS_SETUP_NEW) {
666 switch (incoming) {
667 case P2PS_SETUP_NONE:
668 case P2PS_SETUP_NEW:
669 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
670 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
671 conncap = role;
672 goto grp_owner;
673
674 case P2PS_SETUP_GROUP_OWNER:
675 /*
676 * Must be a complimentary role - cannot be a client to
677 * more than one peer.
678 */
679 if (incoming == role || client)
680 return P2PS_SETUP_NONE;
681
682 return P2PS_SETUP_CLIENT;
683
684 case P2PS_SETUP_CLIENT:
685 /* Must be a complimentary role */
686 if (incoming != role) {
687 conncap = P2PS_SETUP_GROUP_OWNER;
688 goto grp_owner;
689 }
690
691 default:
692 return P2PS_SETUP_NONE;
693 }
694 }
695
696 /*
697 * For now, we only will support ownership of one group, and being a
698 * client of one group. Therefore, if we have either an existing GO
699 * group, or an existing client group, we will not do a new GO
700 * negotiation, but rather try to re-use the existing groups.
701 */
702 switch (incoming) {
703 case P2PS_SETUP_NONE:
704 case P2PS_SETUP_NEW:
705 if (client)
706 conncap = P2PS_SETUP_GROUP_OWNER;
707 else if (!owned_members)
708 conncap = P2PS_SETUP_NEW;
709 else if (incoming == P2PS_SETUP_NONE)
710 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
711 else
712 conncap = P2PS_SETUP_CLIENT;
713 break;
714
715 case P2PS_SETUP_CLIENT:
716 conncap = P2PS_SETUP_GROUP_OWNER;
717 break;
718
719 case P2PS_SETUP_GROUP_OWNER:
720 if (!client)
721 conncap = P2PS_SETUP_CLIENT;
722 break;
723
724 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
725 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
726 if (client)
727 conncap = P2PS_SETUP_GROUP_OWNER;
728 else {
729 u8 r;
730
731 if (os_get_random(&r, sizeof(r)) < 0 ||
732 (r & 1))
733 conncap = P2PS_SETUP_CLIENT;
734 else
735 conncap = P2PS_SETUP_GROUP_OWNER;
736 }
737 break;
738
739 default:
740 return P2PS_SETUP_NONE;
741 }
742
743grp_owner:
744 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
745 (!incoming && (conncap & P2PS_SETUP_NEW))) {
746 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
747 conncap &= ~P2PS_SETUP_GROUP_OWNER;
748 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
749 owner, owned_members, conncap);
750
751 s = wpas_p2p_get_persistent_go(wpa_s);
752
753 if (!s && !owner && p2p_no_group_iface) {
754 p2p_set_intended_addr(wpa_s->global->p2p,
755 wpa_s->own_addr);
756 } else if (!s && !owner) {
757 if (wpas_p2p_add_group_interface(wpa_s,
758 WPA_IF_P2P_GO) < 0) {
759 wpa_printf(MSG_ERROR,
760 "P2P: Failed to allocate a new interface for the group");
761 return P2PS_SETUP_NONE;
762 }
763 wpa_s->global->pending_group_iface_for_p2ps = 1;
764 p2p_set_intended_addr(wpa_s->global->p2p,
765 wpa_s->pending_interface_addr);
766 }
767 }
768
769 return conncap;
770}
771
772
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700773static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
774 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775{
776 struct wpa_ssid *ssid;
777 char *gtype;
778 const char *reason;
779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 ssid = wpa_s->current_ssid;
781 if (ssid == NULL) {
782 /*
783 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700784 * pending P2P group interface waiting for provisioning or a
785 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 */
787 ssid = wpa_s->conf->ssid;
788 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700789 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 break;
791 ssid = ssid->next;
792 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300793 if (ssid == NULL &&
794 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
795 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700796 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
797 "not found");
798 return -1;
799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 }
801 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
802 gtype = "GO";
803 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
804 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
805 wpa_s->reassociate = 0;
806 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807 gtype = "client";
808 } else
809 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700810
811 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
812 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
813
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800814 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700815 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800816 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
817 wpa_s, NULL)) {
818 wpa_printf(MSG_DEBUG,
819 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
820 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
821 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
822 wpa_s, NULL);
823 }
824 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 if (wpa_s->cross_connect_in_use) {
827 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700828 wpa_msg_global(wpa_s->parent, MSG_INFO,
829 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
830 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700832 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 case P2P_GROUP_REMOVAL_REQUESTED:
834 reason = " reason=REQUESTED";
835 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700836 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
837 reason = " reason=FORMATION_FAILED";
838 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
840 reason = " reason=IDLE";
841 break;
842 case P2P_GROUP_REMOVAL_UNAVAILABLE:
843 reason = " reason=UNAVAILABLE";
844 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
846 reason = " reason=GO_ENDING_SESSION";
847 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700848 case P2P_GROUP_REMOVAL_PSK_FAILURE:
849 reason = " reason=PSK_FAILURE";
850 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800851 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
852 reason = " reason=FREQ_CONFLICT";
853 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 default:
855 reason = "";
856 break;
857 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700858 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700859 wpa_msg_global(wpa_s->parent, MSG_INFO,
860 P2P_EVENT_GROUP_REMOVED "%s %s%s",
861 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700862 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800864 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
865 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700866 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
867 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800868 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700869 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800870 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
871 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700872 wpa_s->p2p_in_provisioning = 0;
873 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700874
Dmitry Shmidt15907092014-03-25 10:42:57 -0700875 wpa_s->p2p_in_invitation = 0;
876
Dmitry Shmidt96571392013-10-14 12:54:46 -0700877 /*
878 * Make sure wait for the first client does not remain active after the
879 * group has been removed.
880 */
881 wpa_s->global->p2p_go_wait_client.sec = 0;
882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
884 struct wpa_global *global;
885 char *ifname;
886 enum wpa_driver_if_type type;
887 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
888 wpa_s->ifname);
889 global = wpa_s->global;
890 ifname = os_strdup(wpa_s->ifname);
891 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800892 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700893 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 wpa_s = global->ifaces;
895 if (wpa_s && ifname)
896 wpa_drv_if_remove(wpa_s, type, ifname);
897 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300898 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 }
900
Dmitry Shmidt96571392013-10-14 12:54:46 -0700901 if (!wpa_s->p2p_go_group_formation_completed) {
902 wpa_s->global->p2p_group_formation = NULL;
903 wpa_s->p2p_in_provisioning = 0;
904 }
905
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800906 wpa_s->show_group_started = 0;
907 os_free(wpa_s->go_params);
908 wpa_s->go_params = NULL;
909
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800910 os_free(wpa_s->p2p_group_common_freqs);
911 wpa_s->p2p_group_common_freqs = NULL;
912 wpa_s->p2p_group_common_freqs_num = 0;
913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800914 wpa_s->waiting_presence_resp = 0;
915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
917 if (ssid && (ssid->p2p_group ||
918 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
919 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
920 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700921 if (ssid == wpa_s->current_ssid) {
922 wpa_sm_set_config(wpa_s->wpa, NULL);
923 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700925 }
926 /*
927 * Networks objects created during any P2P activities are not
928 * exposed out as they might/will confuse certain non-P2P aware
929 * applications since these network objects won't behave like
930 * regular ones.
931 *
932 * Likewise, we don't send out network removed signals for such
933 * network objects.
934 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 wpa_config_remove_network(wpa_s->conf, id);
936 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800937 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 } else {
939 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
940 "found");
941 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700942 if (wpa_s->ap_iface)
943 wpa_supplicant_ap_deinit(wpa_s);
944 else
945 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700946
947 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948}
949
950
951static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
952 u8 *go_dev_addr,
953 const u8 *ssid, size_t ssid_len)
954{
955 struct wpa_bss *bss;
956 const u8 *bssid;
957 struct wpabuf *p2p;
958 u8 group_capab;
959 const u8 *addr;
960
961 if (wpa_s->go_params)
962 bssid = wpa_s->go_params->peer_interface_addr;
963 else
964 bssid = wpa_s->bssid;
965
966 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800967 if (bss == NULL && wpa_s->go_params &&
968 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
969 bss = wpa_bss_get_p2p_dev_addr(
970 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 if (bss == NULL) {
972 u8 iface_addr[ETH_ALEN];
973 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
974 iface_addr) == 0)
975 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
976 }
977 if (bss == NULL) {
978 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
979 "group is persistent - BSS " MACSTR " not found",
980 MAC2STR(bssid));
981 return 0;
982 }
983
984 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700985 if (p2p == NULL)
986 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
987 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 if (p2p == NULL) {
989 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
990 "group is persistent - BSS " MACSTR
991 " did not include P2P IE", MAC2STR(bssid));
992 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
993 (u8 *) (bss + 1), bss->ie_len);
994 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
995 ((u8 *) bss + 1) + bss->ie_len,
996 bss->beacon_ie_len);
997 return 0;
998 }
999
1000 group_capab = p2p_get_group_capab(p2p);
1001 addr = p2p_get_go_dev_addr(p2p);
1002 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1003 "group_capab=0x%x", group_capab);
1004 if (addr) {
1005 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1006 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1007 MAC2STR(addr));
1008 } else
1009 os_memset(go_dev_addr, 0, ETH_ALEN);
1010 wpabuf_free(p2p);
1011
1012 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1013 "go_dev_addr=" MACSTR,
1014 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1015
1016 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1017}
1018
1019
Jouni Malinen75ecf522011-06-27 15:19:46 -07001020static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1021 struct wpa_ssid *ssid,
1022 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023{
1024 struct wpa_ssid *s;
1025 int changed = 0;
1026
1027 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1028 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1029 for (s = wpa_s->conf->ssid; s; s = s->next) {
1030 if (s->disabled == 2 &&
1031 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1032 s->ssid_len == ssid->ssid_len &&
1033 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1034 break;
1035 }
1036
1037 if (s) {
1038 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1039 "entry");
1040 if (ssid->passphrase && !s->passphrase)
1041 changed = 1;
1042 else if (ssid->passphrase && s->passphrase &&
1043 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1044 changed = 1;
1045 } else {
1046 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1047 "entry");
1048 changed = 1;
1049 s = wpa_config_add_network(wpa_s->conf);
1050 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001051 return -1;
1052
1053 /*
1054 * Instead of network_added we emit persistent_group_added
1055 * notification. Also to keep the defense checks in
1056 * persistent_group obj registration method, we set the
1057 * relevant flags in s to designate it as a persistent group.
1058 */
1059 s->p2p_group = 1;
1060 s->p2p_persistent_group = 1;
1061 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001062 wpa_config_set_network_defaults(s);
1063 }
1064
1065 s->p2p_group = 1;
1066 s->p2p_persistent_group = 1;
1067 s->disabled = 2;
1068 s->bssid_set = 1;
1069 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1070 s->mode = ssid->mode;
1071 s->auth_alg = WPA_AUTH_ALG_OPEN;
1072 s->key_mgmt = WPA_KEY_MGMT_PSK;
1073 s->proto = WPA_PROTO_RSN;
1074 s->pairwise_cipher = WPA_CIPHER_CCMP;
1075 s->export_keys = 1;
1076 if (ssid->passphrase) {
1077 os_free(s->passphrase);
1078 s->passphrase = os_strdup(ssid->passphrase);
1079 }
1080 if (ssid->psk_set) {
1081 s->psk_set = 1;
1082 os_memcpy(s->psk, ssid->psk, 32);
1083 }
1084 if (s->passphrase && !s->psk_set)
1085 wpa_config_update_psk(s);
1086 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1087 os_free(s->ssid);
1088 s->ssid = os_malloc(ssid->ssid_len);
1089 }
1090 if (s->ssid) {
1091 s->ssid_len = ssid->ssid_len;
1092 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1093 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001094 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1095 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1096 wpa_s->global->add_psk = NULL;
1097 changed = 1;
1098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 if (changed && wpa_s->conf->update_config &&
1101 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1102 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1103 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001104
1105 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106}
1107
1108
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001109static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1110 const u8 *addr)
1111{
1112 struct wpa_ssid *ssid, *s;
1113 u8 *n;
1114 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001115 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001116 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001117
1118 ssid = wpa_s->current_ssid;
1119 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1120 !ssid->p2p_persistent_group)
1121 return;
1122
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001123 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001124 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1125 continue;
1126
1127 if (s->ssid_len == ssid->ssid_len &&
1128 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1129 break;
1130 }
1131
1132 if (s == NULL)
1133 return;
1134
1135 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001136 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001137 ETH_ALEN) != 0)
1138 continue;
1139
1140 if (i == s->num_p2p_clients - 1)
1141 return; /* already the most recent entry */
1142
1143 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001144 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1145 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1146 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001147 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001148 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1149 ETH_ALEN);
1150 os_memset(s->p2p_client_list +
1151 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1152 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001153 found = 1;
1154 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001155 }
1156
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001157 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1158 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001159 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001160 if (n == NULL)
1161 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001162 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1163 ETH_ALEN);
1164 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1165 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001166 s->p2p_client_list = n;
1167 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001168 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001169 /* Not enough room for an additional entry - drop the oldest
1170 * entry */
1171 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001172 s->p2p_client_list + 2 * ETH_ALEN,
1173 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001174 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001175 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001176 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001177 os_memset(s->p2p_client_list +
1178 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1179 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001180 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001181
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001182 if (p2p_wpa_s->conf->update_config &&
1183 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001185}
1186
1187
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001188static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1189 int go, struct wpa_ssid *ssid, int freq,
1190 const u8 *psk, const char *passphrase,
1191 const u8 *go_dev_addr, int persistent,
1192 const char *extra)
1193{
1194 const char *ssid_txt;
1195 char psk_txt[65];
1196
1197 if (psk)
1198 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1199 else
1200 psk_txt[0] = '\0';
1201
1202 if (ssid)
1203 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1204 else
1205 ssid_txt = "";
1206
1207 if (passphrase && passphrase[0] == '\0')
1208 passphrase = NULL;
1209
1210 /*
1211 * Include PSK/passphrase only in the control interface message and
1212 * leave it out from the debug log entry.
1213 */
1214 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1215 P2P_EVENT_GROUP_STARTED
1216 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1217 MACSTR "%s%s",
1218 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1219 psk ? " psk=" : "", psk_txt,
1220 passphrase ? " passphrase=\"" : "",
1221 passphrase ? passphrase : "",
1222 passphrase ? "\"" : "",
1223 MAC2STR(go_dev_addr),
1224 persistent ? " [PERSISTENT]" : "", extra);
1225 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1226 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1227 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1228 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1229 extra);
1230}
1231
1232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001233static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1234 int success)
1235{
1236 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 int client;
1238 int persistent;
1239 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07001240 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241
1242 /*
1243 * This callback is likely called for the main interface. Update wpa_s
1244 * to use the group interface if a new interface was created for the
1245 * group.
1246 */
1247 if (wpa_s->global->p2p_group_formation)
1248 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001249 if (wpa_s->p2p_go_group_formation_completed) {
1250 wpa_s->global->p2p_group_formation = NULL;
1251 wpa_s->p2p_in_provisioning = 0;
1252 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001253 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001254 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255
1256 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001257 wpa_msg_global(wpa_s->parent, MSG_INFO,
1258 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001259 wpas_p2p_group_delete(wpa_s,
1260 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261 return;
1262 }
1263
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001264 wpa_msg_global(wpa_s->parent, MSG_INFO,
1265 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266
1267 ssid = wpa_s->current_ssid;
1268 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1269 ssid->mode = WPAS_MODE_P2P_GO;
1270 p2p_group_notif_formation_done(wpa_s->p2p_group);
1271 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1272 }
1273
1274 persistent = 0;
1275 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 client = ssid->mode == WPAS_MODE_INFRA;
1277 if (ssid->mode == WPAS_MODE_P2P_GO) {
1278 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001279 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1280 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 } else
1282 persistent = wpas_p2p_persistent_group(wpa_s,
1283 go_dev_addr,
1284 ssid->ssid,
1285 ssid->ssid_len);
1286 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 client = wpa_s->p2p_group_interface ==
1288 P2P_GROUP_INTERFACE_CLIENT;
1289 os_memset(go_dev_addr, 0, ETH_ALEN);
1290 }
1291
1292 wpa_s->show_group_started = 0;
1293 if (client) {
1294 /*
1295 * Indicate event only after successfully completed 4-way
1296 * handshake, i.e., when the interface is ready for data
1297 * packets.
1298 */
1299 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001301 wpas_p2p_group_started(wpa_s, 1, ssid,
1302 ssid ? ssid->frequency : 0,
1303 ssid && ssid->passphrase == NULL &&
1304 ssid->psk_set ? ssid->psk : NULL,
1305 ssid ? ssid->passphrase : NULL,
1306 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 wpas_p2p_cross_connect_setup(wpa_s);
1308 wpas_p2p_set_group_idle_timeout(wpa_s);
1309 }
1310
1311 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001312 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1313 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001314 else {
1315 os_free(wpa_s->global->add_psk);
1316 wpa_s->global->add_psk = NULL;
1317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001318 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001319 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001320 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001321 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001322 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001323 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324}
1325
1326
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001327struct send_action_work {
1328 unsigned int freq;
1329 u8 dst[ETH_ALEN];
1330 u8 src[ETH_ALEN];
1331 u8 bssid[ETH_ALEN];
1332 size_t len;
1333 unsigned int wait_time;
1334 u8 buf[0];
1335};
1336
1337
1338static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1339 void *timeout_ctx)
1340{
1341 struct wpa_supplicant *wpa_s = eloop_ctx;
1342
1343 if (!wpa_s->p2p_send_action_work)
1344 return;
1345
1346 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1347 os_free(wpa_s->p2p_send_action_work->ctx);
1348 radio_work_done(wpa_s->p2p_send_action_work);
1349 wpa_s->p2p_send_action_work = NULL;
1350}
1351
1352
Dmitry Shmidt03658832014-08-13 11:03:49 -07001353static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001355 if (wpa_s->p2p_send_action_work) {
1356 struct send_action_work *awork;
1357 awork = wpa_s->p2p_send_action_work->ctx;
1358 if (awork->wait_time == 0) {
1359 os_free(awork);
1360 radio_work_done(wpa_s->p2p_send_action_work);
1361 wpa_s->p2p_send_action_work = NULL;
1362 } else {
1363 /*
1364 * In theory, this should not be needed, but number of
1365 * places in the P2P code is still using non-zero wait
1366 * time for the last Action frame in the sequence and
1367 * some of these do not call send_action_done().
1368 */
1369 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1370 wpa_s, NULL);
1371 eloop_register_timeout(
1372 0, awork->wait_time * 1000,
1373 wpas_p2p_send_action_work_timeout,
1374 wpa_s, NULL);
1375 }
1376 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001377}
1378
1379
1380static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1381 unsigned int freq,
1382 const u8 *dst, const u8 *src,
1383 const u8 *bssid,
1384 const u8 *data, size_t data_len,
1385 enum offchannel_send_action_result
1386 result)
1387{
1388 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1389
1390 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001391
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1393 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001395 switch (result) {
1396 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1397 res = P2P_SEND_ACTION_SUCCESS;
1398 break;
1399 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1400 res = P2P_SEND_ACTION_NO_ACK;
1401 break;
1402 case OFFCHANNEL_SEND_ACTION_FAILED:
1403 res = P2P_SEND_ACTION_FAILED;
1404 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 }
1406
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001407 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001409 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1410 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001411 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001412 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1413 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001415 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1416 "during p2p_connect-auto");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001417 wpa_msg_global(wpa_s->parent, MSG_INFO,
1418 P2P_EVENT_FALLBACK_TO_GO_NEG
1419 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001420 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1421 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 }
1423}
1424
1425
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001426static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1427{
1428 struct wpa_supplicant *wpa_s = work->wpa_s;
1429 struct send_action_work *awork = work->ctx;
1430
1431 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001432 if (work->started) {
1433 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1434 wpa_s, NULL);
1435 wpa_s->p2p_send_action_work = NULL;
1436 offchannel_send_action_done(wpa_s);
1437 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001438 os_free(awork);
1439 return;
1440 }
1441
1442 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1443 awork->bssid, awork->buf, awork->len,
1444 awork->wait_time,
1445 wpas_p2p_send_action_tx_status, 1) < 0) {
1446 os_free(awork);
1447 radio_work_done(work);
1448 return;
1449 }
1450 wpa_s->p2p_send_action_work = work;
1451}
1452
1453
1454static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1455 unsigned int freq, const u8 *dst,
1456 const u8 *src, const u8 *bssid, const u8 *buf,
1457 size_t len, unsigned int wait_time)
1458{
1459 struct send_action_work *awork;
1460
1461 if (wpa_s->p2p_send_action_work) {
1462 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1463 return -1;
1464 }
1465
1466 awork = os_zalloc(sizeof(*awork) + len);
1467 if (awork == NULL)
1468 return -1;
1469
1470 awork->freq = freq;
1471 os_memcpy(awork->dst, dst, ETH_ALEN);
1472 os_memcpy(awork->src, src, ETH_ALEN);
1473 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1474 awork->len = len;
1475 awork->wait_time = wait_time;
1476 os_memcpy(awork->buf, buf, len);
1477
1478 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1479 wpas_send_action_cb, awork) < 0) {
1480 os_free(awork);
1481 return -1;
1482 }
1483
1484 return 0;
1485}
1486
1487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1489 const u8 *src, const u8 *bssid, const u8 *buf,
1490 size_t len, unsigned int wait_time)
1491{
1492 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001493 int listen_freq = -1, send_freq = -1;
1494
1495 if (wpa_s->p2p_listen_work)
1496 listen_freq = wpa_s->p2p_listen_work->freq;
1497 if (wpa_s->p2p_send_action_work)
1498 send_freq = wpa_s->p2p_send_action_work->freq;
1499 if (listen_freq != (int) freq && send_freq != (int) freq) {
1500 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1501 listen_freq, send_freq);
1502 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1503 len, wait_time);
1504 }
1505
1506 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001507 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1508 wait_time,
1509 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510}
1511
1512
1513static void wpas_send_action_done(void *ctx)
1514{
1515 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001516
1517 if (wpa_s->p2p_send_action_work) {
1518 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1519 wpa_s, NULL);
1520 os_free(wpa_s->p2p_send_action_work->ctx);
1521 radio_work_done(wpa_s->p2p_send_action_work);
1522 wpa_s->p2p_send_action_work = NULL;
1523 }
1524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001525 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526}
1527
1528
1529static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1530 struct p2p_go_neg_results *params)
1531{
1532 if (wpa_s->go_params == NULL) {
1533 wpa_s->go_params = os_malloc(sizeof(*params));
1534 if (wpa_s->go_params == NULL)
1535 return -1;
1536 }
1537 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1538 return 0;
1539}
1540
1541
1542static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1543 struct p2p_go_neg_results *res)
1544{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001545 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001546 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1547 " dev_addr " MACSTR " wps_method %d",
1548 MAC2STR(res->peer_interface_addr),
1549 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1551 res->ssid, res->ssid_len);
1552 wpa_supplicant_ap_deinit(wpa_s);
1553 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001554 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001556#ifdef CONFIG_WPS_NFC
1557 } else if (res->wps_method == WPS_NFC) {
1558 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1559 res->peer_interface_addr,
1560 wpa_s->parent->p2p_oob_dev_pw,
1561 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1562 wpa_s->parent->p2p_oob_dev_pw_id ==
1563 DEV_PW_NFC_CONNECTION_HANDOVER ?
1564 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1565 NULL,
1566 NULL, 0, 0);
1567#endif /* CONFIG_WPS_NFC */
1568 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001570 if (wpa_s->p2p_wps_method == WPS_P2PS)
1571 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1573 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1574 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1575 wpa_s->p2p_pin, 1, dev_pw_id);
1576 }
1577}
1578
1579
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001580static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1581 struct wpa_ssid *ssid)
1582{
1583 struct wpa_ssid *persistent;
1584 struct psk_list_entry *psk;
1585 struct hostapd_data *hapd;
1586
1587 if (!wpa_s->ap_iface)
1588 return;
1589
1590 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1591 ssid->ssid_len);
1592 if (persistent == NULL)
1593 return;
1594
1595 hapd = wpa_s->ap_iface->bss[0];
1596
1597 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1598 list) {
1599 struct hostapd_wpa_psk *hpsk;
1600
1601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1602 MACSTR " psk=%d",
1603 MAC2STR(psk->addr), psk->p2p);
1604 hpsk = os_zalloc(sizeof(*hpsk));
1605 if (hpsk == NULL)
1606 break;
1607 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1608 if (psk->p2p)
1609 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1610 else
1611 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1612 hpsk->next = hapd->conf->ssid.wpa_psk;
1613 hapd->conf->ssid.wpa_psk = hpsk;
1614 }
1615}
1616
1617
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001618static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1619{
1620 unsigned int i;
1621
1622 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1623 wpa_s->p2p_group_common_freqs_num);
1624
1625 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1626 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1627 i, wpa_s->p2p_group_common_freqs[i]);
1628}
1629
1630
1631static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1632 struct p2p_go_neg_results *params)
1633{
1634 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1635
1636 wpa_s->p2p_group_common_freqs_num = 0;
1637 os_free(wpa_s->p2p_group_common_freqs);
1638 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1639 if (!wpa_s->p2p_group_common_freqs)
1640 return;
1641
1642 for (i = 0; i < len; i++) {
1643 if (!wpa_s->go_params->freq_list[i])
1644 break;
1645 wpa_s->p2p_group_common_freqs[i] =
1646 wpa_s->go_params->freq_list[i];
1647 }
1648 wpa_s->p2p_group_common_freqs_num = i;
1649}
1650
1651
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001652static void p2p_config_write(struct wpa_supplicant *wpa_s)
1653{
1654#ifndef CONFIG_NO_CONFIG_WRITE
1655 if (wpa_s->parent->conf->update_config &&
1656 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1657 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1658#endif /* CONFIG_NO_CONFIG_WRITE */
1659}
1660
1661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662static void p2p_go_configured(void *ctx, void *data)
1663{
1664 struct wpa_supplicant *wpa_s = ctx;
1665 struct p2p_go_neg_results *params = data;
1666 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001667 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001669 p2p_go_save_group_common_freqs(wpa_s, params);
1670 p2p_go_dump_common_freqs(wpa_s);
1671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 ssid = wpa_s->current_ssid;
1673 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1674 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1675 if (wpa_s->global->p2p_group_formation == wpa_s)
1676 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001677 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1678 params->passphrase[0] == '\0' ?
1679 params->psk : NULL,
1680 params->passphrase,
1681 wpa_s->global->p2p_dev_addr,
1682 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001683 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001684
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001685 if (wpa_s->parent->p2ps_join_addr_valid) {
1686 wpa_dbg(wpa_s, MSG_DEBUG,
1687 "P2PS: Setting default PIN for " MACSTR,
1688 MAC2STR(wpa_s->parent->p2ps_join_addr));
1689 wpa_supplicant_ap_wps_pin(wpa_s,
1690 wpa_s->parent->p2ps_join_addr,
1691 "12345670", NULL, 0, 0);
1692 wpa_s->parent->p2ps_join_addr_valid = 0;
1693 }
1694
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001695 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001696 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001697 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001699 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001700 wpas_p2p_add_psk_list(wpa_s, ssid);
1701 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001702 if (network_id < 0)
1703 network_id = ssid->id;
1704 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705 wpas_p2p_cross_connect_setup(wpa_s);
1706 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001707
1708 if (wpa_s->p2p_first_connection_timeout) {
1709 wpa_dbg(wpa_s, MSG_DEBUG,
1710 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1711 wpa_s->p2p_first_connection_timeout);
1712 wpa_s->p2p_go_group_formation_completed = 0;
1713 wpa_s->global->p2p_group_formation = wpa_s;
1714 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1715 wpa_s->parent, NULL);
1716 eloop_register_timeout(
1717 wpa_s->p2p_first_connection_timeout, 0,
1718 wpas_p2p_group_formation_timeout,
1719 wpa_s->parent, NULL);
1720 }
1721
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001722 return;
1723 }
1724
1725 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1726 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1727 params->peer_interface_addr)) {
1728 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1729 "filtering");
1730 return;
1731 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001732 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001734 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001735#ifdef CONFIG_WPS_NFC
1736 } else if (params->wps_method == WPS_NFC) {
1737 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1738 DEV_PW_NFC_CONNECTION_HANDOVER &&
1739 !wpa_s->parent->p2p_oob_dev_pw) {
1740 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1741 return;
1742 }
1743 wpas_ap_wps_add_nfc_pw(
1744 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1745 wpa_s->parent->p2p_oob_dev_pw,
1746 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1747 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1748#endif /* CONFIG_WPS_NFC */
1749 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001751 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001752 os_free(wpa_s->go_params);
1753 wpa_s->go_params = NULL;
1754}
1755
1756
1757static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1758 struct p2p_go_neg_results *params,
1759 int group_formation)
1760{
1761 struct wpa_ssid *ssid;
1762
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001763 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1764 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1765 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1766 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769
1770 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001771 if (ssid == NULL) {
1772 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001774 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001776 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001777 wpa_s->p2p_go_group_formation_completed = 0;
1778 wpa_s->group_formation_reported = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 wpa_config_set_network_defaults(ssid);
1781 ssid->temporary = 1;
1782 ssid->p2p_group = 1;
1783 ssid->p2p_persistent_group = params->persistent_group;
1784 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1785 WPAS_MODE_P2P_GO;
1786 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001787 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001788 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 ssid->ssid = os_zalloc(params->ssid_len + 1);
1790 if (ssid->ssid) {
1791 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1792 ssid->ssid_len = params->ssid_len;
1793 }
1794 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1795 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1796 ssid->proto = WPA_PROTO_RSN;
1797 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001798 ssid->group_cipher = WPA_CIPHER_CCMP;
1799 if (params->freq > 56160) {
1800 /*
1801 * Enable GCMP instead of CCMP as pairwise_cipher and
1802 * group_cipher in 60 GHz.
1803 */
1804 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1805 ssid->group_cipher = WPA_CIPHER_GCMP;
1806 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001807 if (os_strlen(params->passphrase) > 0) {
1808 ssid->passphrase = os_strdup(params->passphrase);
1809 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001810 wpa_msg_global(wpa_s, MSG_ERROR,
1811 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001812 wpa_config_remove_network(wpa_s->conf, ssid->id);
1813 return;
1814 }
1815 } else
1816 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001817 ssid->psk_set = params->psk_set;
1818 if (ssid->psk_set)
1819 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001820 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001821 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001822 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823
1824 wpa_s->ap_configured_cb = p2p_go_configured;
1825 wpa_s->ap_configured_cb_ctx = wpa_s;
1826 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001827 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001828 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 wpa_s->reassociate = 1;
1830 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001831 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1832 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 wpa_supplicant_req_scan(wpa_s, 0, 0);
1834}
1835
1836
1837static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1838 const struct wpa_supplicant *src)
1839{
1840 struct wpa_config *d;
1841 const struct wpa_config *s;
1842
1843 d = dst->conf;
1844 s = src->conf;
1845
1846#define C(n) if (s->n) d->n = os_strdup(s->n)
1847 C(device_name);
1848 C(manufacturer);
1849 C(model_name);
1850 C(model_number);
1851 C(serial_number);
1852 C(config_methods);
1853#undef C
1854
1855 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1856 os_memcpy(d->sec_device_type, s->sec_device_type,
1857 sizeof(d->sec_device_type));
1858 d->num_sec_device_types = s->num_sec_device_types;
1859
1860 d->p2p_group_idle = s->p2p_group_idle;
1861 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001862 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001863 d->max_num_sta = s->max_num_sta;
1864 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001865 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001866 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001867 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001868 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001869 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001870 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001871 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001872
1873 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1874 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1875 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877}
1878
1879
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001880static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1881 char *ifname, size_t len)
1882{
1883 char *ifname_ptr = wpa_s->ifname;
1884
1885 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1886 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1887 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1888 }
1889
1890 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1891 if (os_strlen(ifname) >= IFNAMSIZ &&
1892 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001893 int res;
1894
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001895 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001896 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1897 if (os_snprintf_error(len, res) && len)
1898 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001899 }
1900}
1901
1902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1904 enum wpa_driver_if_type type)
1905{
1906 char ifname[120], force_ifname[120];
1907
1908 if (wpa_s->pending_interface_name[0]) {
1909 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1910 "- skip creation of a new one");
1911 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1912 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1913 "unknown?! ifname='%s'",
1914 wpa_s->pending_interface_name);
1915 return -1;
1916 }
1917 return 0;
1918 }
1919
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001920 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 force_ifname[0] = '\0';
1922
1923 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1924 ifname);
1925 wpa_s->p2p_group_idx++;
1926
1927 wpa_s->pending_interface_type = type;
1928 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1929 wpa_s->pending_interface_addr, NULL) < 0) {
1930 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1931 "interface");
1932 return -1;
1933 }
1934
1935 if (force_ifname[0]) {
1936 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1937 force_ifname);
1938 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1939 sizeof(wpa_s->pending_interface_name));
1940 } else
1941 os_strlcpy(wpa_s->pending_interface_name, ifname,
1942 sizeof(wpa_s->pending_interface_name));
1943 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1944 MACSTR, wpa_s->pending_interface_name,
1945 MAC2STR(wpa_s->pending_interface_addr));
1946
1947 return 0;
1948}
1949
1950
1951static void wpas_p2p_remove_pending_group_interface(
1952 struct wpa_supplicant *wpa_s)
1953{
1954 if (!wpa_s->pending_interface_name[0] ||
1955 is_zero_ether_addr(wpa_s->pending_interface_addr))
1956 return; /* No pending virtual interface */
1957
1958 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1959 wpa_s->pending_interface_name);
1960 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1961 wpa_s->pending_interface_name);
1962 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1963 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001964 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965}
1966
1967
1968static struct wpa_supplicant *
1969wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1970{
1971 struct wpa_interface iface;
1972 struct wpa_supplicant *group_wpa_s;
1973
1974 if (!wpa_s->pending_interface_name[0]) {
1975 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1976 if (!wpas_p2p_create_iface(wpa_s))
1977 return NULL;
1978 /*
1979 * Something has forced us to remove the pending interface; try
1980 * to create a new one and hope for the best that we will get
1981 * the same local address.
1982 */
1983 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1984 WPA_IF_P2P_CLIENT) < 0)
1985 return NULL;
1986 }
1987
1988 os_memset(&iface, 0, sizeof(iface));
1989 iface.ifname = wpa_s->pending_interface_name;
1990 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001991 if (wpa_s->conf->ctrl_interface == NULL &&
1992 wpa_s->parent != wpa_s &&
1993 wpa_s->p2p_mgmt &&
1994 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1995 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1996 else
1997 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001999 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 if (group_wpa_s == NULL) {
2001 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2002 "wpa_supplicant interface");
2003 return NULL;
2004 }
2005 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2007 P2P_GROUP_INTERFACE_CLIENT;
2008 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002009 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010
2011 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2012
2013 return group_wpa_s;
2014}
2015
2016
2017static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2018 void *timeout_ctx)
2019{
2020 struct wpa_supplicant *wpa_s = eloop_ctx;
2021 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002022 wpas_p2p_group_formation_failed(wpa_s);
2023}
2024
2025
2026void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
2027{
2028 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2029 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 if (wpa_s->global->p2p)
2031 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 wpas_group_formation_completed(wpa_s, 0);
2033}
2034
2035
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002036static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2037{
2038 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2039 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2040 wpa_s->parent, NULL);
2041 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2042 wpa_s->parent, NULL);
2043 wpa_s->global->p2p_fail_on_wps_complete = 0;
2044}
2045
2046
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002047void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2048{
2049 if (wpa_s->global->p2p_group_formation != wpa_s)
2050 return;
2051 /* Speed up group formation timeout since this cannot succeed */
2052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2053 wpa_s->parent, NULL);
2054 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2055 wpa_s->parent, NULL);
2056}
2057
2058
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002059static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060{
2061 struct wpa_supplicant *wpa_s = ctx;
2062
2063 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2064 wpa_drv_cancel_remain_on_channel(wpa_s);
2065 wpa_s->off_channel_freq = 0;
2066 wpa_s->roc_waiting_drv_freq = 0;
2067 }
2068
2069 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002070 wpa_msg_global(wpa_s, MSG_INFO,
2071 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2072 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002073 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 wpas_p2p_remove_pending_group_interface(wpa_s);
2075 return;
2076 }
2077
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002078 if (wpa_s->p2p_go_ht40)
2079 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002080 if (wpa_s->p2p_go_vht)
2081 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002082
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002083 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2084 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2085 " wps_method=%s",
2086 res->role_go ? "GO" : "client", res->freq, res->ht40,
2087 MAC2STR(res->peer_device_addr),
2088 MAC2STR(res->peer_interface_addr),
2089 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002090 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091
Dmitry Shmidt04949592012-07-19 12:16:46 -07002092 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2093 struct wpa_ssid *ssid;
2094 ssid = wpa_config_get_network(wpa_s->conf,
2095 wpa_s->p2p_persistent_id);
2096 if (ssid && ssid->disabled == 2 &&
2097 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2098 size_t len = os_strlen(ssid->passphrase);
2099 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2100 "on requested persistent group");
2101 os_memcpy(res->passphrase, ssid->passphrase, len);
2102 res->passphrase[len] = '\0';
2103 }
2104 }
2105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 if (wpa_s->create_p2p_iface) {
2107 struct wpa_supplicant *group_wpa_s =
2108 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2109 if (group_wpa_s == NULL) {
2110 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002111 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2112 wpa_s, NULL);
2113 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 return;
2115 }
2116 if (group_wpa_s != wpa_s) {
2117 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2118 sizeof(group_wpa_s->p2p_pin));
2119 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2120 }
2121 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2122 wpa_s->pending_interface_name[0] = '\0';
2123 group_wpa_s->p2p_in_provisioning = 1;
2124
2125 if (res->role_go)
2126 wpas_start_wps_go(group_wpa_s, res, 1);
2127 else
2128 wpas_start_wps_enrollee(group_wpa_s, res);
2129 } else {
2130 wpa_s->p2p_in_provisioning = 1;
2131 wpa_s->global->p2p_group_formation = wpa_s;
2132
2133 if (res->role_go)
2134 wpas_start_wps_go(wpa_s, res, 1);
2135 else
2136 wpas_start_wps_enrollee(ctx, res);
2137 }
2138
2139 wpa_s->p2p_long_listen = 0;
2140 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2141
2142 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2143 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2144 (res->peer_config_timeout % 100) * 10000,
2145 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2146}
2147
2148
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002149static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150{
2151 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002152 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2153 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154
2155 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
2156}
2157
2158
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002159static void wpas_dev_found(void *ctx, const u8 *addr,
2160 const struct p2p_peer_info *info,
2161 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002163#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 struct wpa_supplicant *wpa_s = ctx;
2165 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002166 char *wfd_dev_info_hex = NULL;
2167
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002168#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002169 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2170 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002171#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002172
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002173 if (info->p2ps_instance) {
2174 char str[256];
2175 const u8 *buf = wpabuf_head(info->p2ps_instance);
2176 size_t len = wpabuf_len(info->p2ps_instance);
2177
2178 while (len) {
2179 u32 id;
2180 u16 methods;
2181 u8 str_len;
2182
2183 if (len < 4 + 2 + 1)
2184 break;
2185 id = WPA_GET_LE32(buf);
2186 buf += sizeof(u32);
2187 methods = WPA_GET_BE16(buf);
2188 buf += sizeof(u16);
2189 str_len = *buf++;
2190 if (str_len > len - 4 - 2 - 1)
2191 break;
2192 os_memcpy(str, buf, str_len);
2193 str[str_len] = '\0';
2194 buf += str_len;
2195 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2196
2197 wpa_msg_global(wpa_s, MSG_INFO,
2198 P2P_EVENT_DEVICE_FOUND MACSTR
2199 " p2p_dev_addr=" MACSTR
2200 " pri_dev_type=%s name='%s'"
2201 " config_methods=0x%x"
2202 " dev_capab=0x%x"
2203 " group_capab=0x%x"
2204 " adv_id=%x asp_svc=%s%s",
2205 MAC2STR(addr),
2206 MAC2STR(info->p2p_device_addr),
2207 wps_dev_type_bin2str(
2208 info->pri_dev_type,
2209 devtype, sizeof(devtype)),
2210 info->device_name, methods,
2211 info->dev_capab, info->group_capab,
2212 id, str,
2213 info->vendor_elems ?
2214 " vendor_elems=1" : "");
2215 }
2216 goto done;
2217 }
2218
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002219 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2220 " p2p_dev_addr=" MACSTR
2221 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002222 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002223 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2224 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2225 sizeof(devtype)),
2226 info->device_name, info->config_methods,
2227 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002228 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002229 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002230 info->vendor_elems ? " vendor_elems=1" : "",
2231 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002233done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002234 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002235#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002237 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2238}
2239
2240
2241static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2242{
2243 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002244
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2246 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002247
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2249}
2250
2251
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002252static void wpas_find_stopped(void *ctx)
2253{
2254 struct wpa_supplicant *wpa_s = ctx;
2255 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002256 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002257}
2258
2259
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002260struct wpas_p2p_listen_work {
2261 unsigned int freq;
2262 unsigned int duration;
2263 struct wpabuf *probe_resp_ie;
2264};
2265
2266
2267static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2268{
2269 if (lwork == NULL)
2270 return;
2271 wpabuf_free(lwork->probe_resp_ie);
2272 os_free(lwork);
2273}
2274
2275
2276static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2277{
2278 struct wpas_p2p_listen_work *lwork;
2279
2280 if (!wpa_s->p2p_listen_work)
2281 return;
2282
2283 lwork = wpa_s->p2p_listen_work->ctx;
2284 wpas_p2p_listen_work_free(lwork);
2285 radio_work_done(wpa_s->p2p_listen_work);
2286 wpa_s->p2p_listen_work = NULL;
2287}
2288
2289
2290static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2291{
2292 struct wpa_supplicant *wpa_s = work->wpa_s;
2293 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002294 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002295
2296 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002297 if (work->started) {
2298 wpa_s->p2p_listen_work = NULL;
2299 wpas_stop_listen(wpa_s);
2300 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002301 wpas_p2p_listen_work_free(lwork);
2302 return;
2303 }
2304
2305 wpa_s->p2p_listen_work = work;
2306
2307 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2308
2309 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2310 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2311 "report received Probe Request frames");
2312 wpas_p2p_listen_work_done(wpa_s);
2313 return;
2314 }
2315
2316 wpa_s->pending_listen_freq = lwork->freq;
2317 wpa_s->pending_listen_duration = lwork->duration;
2318
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002319 duration = lwork->duration;
2320#ifdef CONFIG_TESTING_OPTIONS
2321 if (wpa_s->extra_roc_dur) {
2322 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2323 duration, duration + wpa_s->extra_roc_dur);
2324 duration += wpa_s->extra_roc_dur;
2325 }
2326#endif /* CONFIG_TESTING_OPTIONS */
2327
2328 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002329 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2330 "to remain on channel (%u MHz) for Listen "
2331 "state", lwork->freq);
2332 wpas_p2p_listen_work_done(wpa_s);
2333 wpa_s->pending_listen_freq = 0;
2334 return;
2335 }
2336 wpa_s->off_channel_freq = 0;
2337 wpa_s->roc_waiting_drv_freq = lwork->freq;
2338}
2339
2340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341static int wpas_start_listen(void *ctx, unsigned int freq,
2342 unsigned int duration,
2343 const struct wpabuf *probe_resp_ie)
2344{
2345 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002346 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002348 if (wpa_s->p2p_listen_work) {
2349 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 return -1;
2351 }
2352
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002353 lwork = os_zalloc(sizeof(*lwork));
2354 if (lwork == NULL)
2355 return -1;
2356 lwork->freq = freq;
2357 lwork->duration = duration;
2358 if (probe_resp_ie) {
2359 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2360 if (lwork->probe_resp_ie == NULL) {
2361 wpas_p2p_listen_work_free(lwork);
2362 return -1;
2363 }
2364 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002366 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2367 lwork) < 0) {
2368 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002369 return -1;
2370 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371
2372 return 0;
2373}
2374
2375
2376static void wpas_stop_listen(void *ctx)
2377{
2378 struct wpa_supplicant *wpa_s = ctx;
2379 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2380 wpa_drv_cancel_remain_on_channel(wpa_s);
2381 wpa_s->off_channel_freq = 0;
2382 wpa_s->roc_waiting_drv_freq = 0;
2383 }
2384 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2385 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002386 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387}
2388
2389
2390static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2391{
2392 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002393 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002394}
2395
2396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002397static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2398 const u8 *peer, const char *params,
2399 unsigned int generated_pin)
2400{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002401 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2402 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403}
2404
2405
2406static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2407 const u8 *peer, const char *params)
2408{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002409 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2410 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411}
2412
2413
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002414static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2415 const u8 *dev_addr, const u8 *pri_dev_type,
2416 const char *dev_name, u16 supp_config_methods,
2417 u8 dev_capab, u8 group_capab, const u8 *group_id,
2418 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419{
2420 struct wpa_supplicant *wpa_s = ctx;
2421 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002422 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002423 u8 empty_dev_type[8];
2424 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002425 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002426 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427
2428 if (group_id) {
2429 for (group = wpa_s->global->ifaces; group; group = group->next)
2430 {
2431 struct wpa_ssid *s = group->current_ssid;
2432 if (s != NULL &&
2433 s->mode == WPAS_MODE_P2P_GO &&
2434 group_id_len - ETH_ALEN == s->ssid_len &&
2435 os_memcmp(group_id + ETH_ALEN, s->ssid,
2436 s->ssid_len) == 0)
2437 break;
2438 }
2439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440
2441 if (pri_dev_type == NULL) {
2442 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2443 pri_dev_type = empty_dev_type;
2444 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002445 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2446 " pri_dev_type=%s name='%s' config_methods=0x%x "
2447 "dev_capab=0x%x group_capab=0x%x%s%s",
2448 MAC2STR(dev_addr),
2449 wps_dev_type_bin2str(pri_dev_type, devtype,
2450 sizeof(devtype)),
2451 dev_name, supp_config_methods, dev_capab, group_capab,
2452 group ? " group=" : "",
2453 group ? group->ifname : "");
2454 if (os_snprintf_error(sizeof(params), res))
2455 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002457
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 if (config_methods & WPS_CONFIG_DISPLAY) {
2459 generated_pin = wps_generate_pin();
2460 wpas_prov_disc_local_display(wpa_s, peer, params,
2461 generated_pin);
2462 } else if (config_methods & WPS_CONFIG_KEYPAD)
2463 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2464 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002465 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2466 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002467
2468 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2469 P2P_PROV_DISC_SUCCESS,
2470 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471}
2472
2473
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002474static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475{
2476 struct wpa_supplicant *wpa_s = ctx;
2477 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002478 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002480 if (wpa_s->pending_pd_before_join &&
2481 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2482 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2483 wpa_s->pending_pd_before_join = 0;
2484 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2485 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002486 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002487 return;
2488 }
2489
Dmitry Shmidt04949592012-07-19 12:16:46 -07002490 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002491 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2492 int res;
2493
2494 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2495 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2496 if (os_snprintf_error(sizeof(params), res))
2497 params[sizeof(params) - 1] = '\0';
2498 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002499 params[0] = '\0';
2500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002501 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002502 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 else if (config_methods & WPS_CONFIG_KEYPAD) {
2504 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002505 wpas_prov_disc_local_display(wpa_s, peer, params,
2506 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002508 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2509 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510
Jouni Malinen75ecf522011-06-27 15:19:46 -07002511 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2512 P2P_PROV_DISC_SUCCESS,
2513 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514}
2515
2516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002517static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002518 enum p2p_prov_disc_status status,
2519 u32 adv_id, const u8 *adv_mac,
2520 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002521{
2522 struct wpa_supplicant *wpa_s = ctx;
2523
Dmitry Shmidt04949592012-07-19 12:16:46 -07002524 if (wpa_s->p2p_fallback_to_go_neg) {
2525 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2526 "failed - fall back to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002527 wpa_msg_global(wpa_s->parent, MSG_INFO,
2528 P2P_EVENT_FALLBACK_TO_GO_NEG
2529 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002530 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2531 return;
2532 }
2533
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002534 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002535 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002536 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2537 "join-existing-group operation (no ACK for PD "
2538 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002539 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002540 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002541 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002542
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002543 if (adv_id && adv_mac && deferred_session_resp) {
2544 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2545 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2546 " deferred_session_resp='%s'",
2547 MAC2STR(peer), status, adv_id,
2548 deferred_session_resp);
2549 } else if (adv_id && adv_mac) {
2550 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2551 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2552 MAC2STR(peer), status, adv_id);
2553 } else {
2554 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2555 " p2p_dev_addr=" MACSTR " status=%d",
2556 MAC2STR(peer), status);
2557 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002558
Jouni Malinen75ecf522011-06-27 15:19:46 -07002559 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2560 status, 0, 0);
2561}
2562
2563
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002564static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2565{
2566 if (channels == NULL)
2567 return 1; /* Assume no restrictions */
2568 return p2p_channels_includes_freq(channels, freq);
2569
2570}
2571
2572
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002573/**
2574 * Pick the best frequency to use from all the currently used frequencies.
2575 */
2576static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2577 struct wpa_used_freq_data *freqs,
2578 unsigned int num)
2579{
2580 unsigned int i, c;
2581
2582 /* find a candidate freq that is supported by P2P */
2583 for (c = 0; c < num; c++)
2584 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2585 break;
2586
2587 if (c == num)
2588 return 0;
2589
2590 /* once we have a candidate, try to find a 'better' one */
2591 for (i = c + 1; i < num; i++) {
2592 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2593 continue;
2594
2595 /*
2596 * 1. Infrastructure station interfaces have higher preference.
2597 * 2. P2P Clients have higher preference.
2598 * 3. All others.
2599 */
2600 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2601 c = i;
2602 break;
2603 }
2604
2605 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2606 c = i;
2607 }
2608 return freqs[c].freq;
2609}
2610
2611
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2613 const u8 *go_dev_addr, const u8 *ssid,
2614 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002615 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002616 const struct p2p_channels *channels,
2617 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618{
2619 struct wpa_supplicant *wpa_s = ctx;
2620 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002621 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002623 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624
2625 if (!persistent_group) {
2626 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002627 " to join an active group (SSID: %s)",
2628 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2630 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2631 == 0 ||
2632 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2633 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2634 "authorized invitation");
2635 goto accept_inv;
2636 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002637
2638#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002639 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2640 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002641 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002642 wpa_s->p2p_wps_method = WPS_NFC;
2643 wpa_s->pending_join_wps_method = WPS_NFC;
2644 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002645 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002646 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002647 bssid, ETH_ALEN);
2648 goto accept_inv;
2649 }
2650#endif /* CONFIG_WPS_NFC */
2651
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 /*
2653 * Do not accept the invitation automatically; notify user and
2654 * request approval.
2655 */
2656 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2657 }
2658
2659 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2660 if (grp) {
2661 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2662 "running persistent group");
2663 if (*go)
2664 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2665 goto accept_inv;
2666 }
2667
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002668 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2669 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2670 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2671 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002672 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002673 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2675
2676 for (s = wpa_s->conf->ssid; s; s = s->next) {
2677 if (s->disabled == 2 &&
2678 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2679 s->ssid_len == ssid_len &&
2680 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2681 break;
2682 }
2683
2684 if (!s) {
2685 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2686 " requested reinvocation of an unknown group",
2687 MAC2STR(sa));
2688 return P2P_SC_FAIL_UNKNOWN_GROUP;
2689 }
2690
2691 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2692 *go = 1;
2693 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2694 wpa_printf(MSG_DEBUG, "P2P: The only available "
2695 "interface is already in use - reject "
2696 "invitation");
2697 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2698 }
2699 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2700 } else if (s->mode == WPAS_MODE_P2P_GO) {
2701 *go = 1;
2702 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2703 {
2704 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2705 "interface address for the group");
2706 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2707 }
2708 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2709 ETH_ALEN);
2710 }
2711
2712accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002713 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2714
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002715 best_freq = 0;
2716 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2717 sizeof(struct wpa_used_freq_data));
2718 if (freqs) {
2719 int num_channels = wpa_s->num_multichan_concurrent;
2720 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2721 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2722 os_free(freqs);
2723 }
2724
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002725 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002726 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002727 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002728 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002729
2730 if (wpa_s->num_multichan_concurrent < 2 ||
2731 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2732 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 -07002733 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 }
2736
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002737 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2738 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002739 if (*go == 0) {
2740 /* We are the client */
2741 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2742 "running a GO but we are capable of MCC, "
2743 "figure out the best channel to use");
2744 *force_freq = 0;
2745 } else if (!freq_included(channels, *force_freq)) {
2746 /* We are the GO, and *force_freq is not in the
2747 * intersection */
2748 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2749 "in intersection but we are capable of MCC, "
2750 "figure out the best channel to use",
2751 *force_freq);
2752 *force_freq = 0;
2753 }
2754 }
2755
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 return P2P_SC_SUCCESS;
2757}
2758
2759
2760static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2761 const u8 *ssid, size_t ssid_len,
2762 const u8 *go_dev_addr, u8 status,
2763 int op_freq)
2764{
2765 struct wpa_supplicant *wpa_s = ctx;
2766 struct wpa_ssid *s;
2767
2768 for (s = wpa_s->conf->ssid; s; s = s->next) {
2769 if (s->disabled == 2 &&
2770 s->ssid_len == ssid_len &&
2771 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2772 break;
2773 }
2774
2775 if (status == P2P_SC_SUCCESS) {
2776 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002777 " was accepted; op_freq=%d MHz, SSID=%s",
2778 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002780 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07002782 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002783 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002785 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002787 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002788 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 }
2790 return;
2791 }
2792
2793 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2794 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2795 " was rejected (status %u)", MAC2STR(sa), status);
2796 return;
2797 }
2798
2799 if (!s) {
2800 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002801 wpa_msg_global(wpa_s, MSG_INFO,
2802 P2P_EVENT_INVITATION_RECEIVED
2803 "sa=" MACSTR " go_dev_addr=" MACSTR
2804 " bssid=" MACSTR " unknown-network",
2805 MAC2STR(sa), MAC2STR(go_dev_addr),
2806 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002808 wpa_msg_global(wpa_s, MSG_INFO,
2809 P2P_EVENT_INVITATION_RECEIVED
2810 "sa=" MACSTR " go_dev_addr=" MACSTR
2811 " unknown-network",
2812 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 }
2814 return;
2815 }
2816
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002817 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002818 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2819 "sa=" MACSTR " persistent=%d freq=%d",
2820 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002821 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002822 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2823 "sa=" MACSTR " persistent=%d",
2824 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002826}
2827
2828
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002829static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2830 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002831 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002832{
2833 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002834 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002835
2836 if (ssid == NULL)
2837 return;
2838
2839 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002840 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002841 ETH_ALEN) == 0)
2842 break;
2843 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002844 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002845 if (ssid->mode != WPAS_MODE_P2P_GO &&
2846 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2847 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2848 "due to invitation result", ssid->id);
2849 wpas_notify_network_removed(wpa_s, ssid);
2850 wpa_config_remove_network(wpa_s->conf, ssid->id);
2851 return;
2852 }
2853 return; /* Peer not found in client list */
2854 }
2855
2856 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002857 "group %d client list%s",
2858 MAC2STR(peer), ssid->id,
2859 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002860 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2861 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2862 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002863 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002864 if (p2p_wpa_s->conf->update_config &&
2865 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002866 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002867}
2868
2869
2870static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2871 const u8 *peer)
2872{
2873 struct wpa_ssid *ssid;
2874
2875 wpa_s = wpa_s->global->p2p_invite_group;
2876 if (wpa_s == NULL)
2877 return; /* No known invitation group */
2878 ssid = wpa_s->current_ssid;
2879 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2880 !ssid->p2p_persistent_group)
2881 return; /* Not operating as a GO in persistent group */
2882 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2883 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002884 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002885}
2886
2887
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002888static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002889 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002890 const u8 *peer, int neg_freq,
2891 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002892{
2893 struct wpa_supplicant *wpa_s = ctx;
2894 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07002895 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896
2897 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002898 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2899 "status=%d " MACSTR,
2900 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002902 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2903 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 }
2905 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2906
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002907 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2908 status, MAC2STR(peer));
2909 if (wpa_s->pending_invite_ssid_id == -1) {
2910 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2911 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002915 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2916 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2917 "invitation exchange to indicate readiness for "
2918 "re-invocation");
2919 }
2920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002922 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2923 ssid = wpa_config_get_network(
2924 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002925 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002926 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 wpas_p2p_remove_pending_group_interface(wpa_s);
2928 return;
2929 }
2930
2931 ssid = wpa_config_get_network(wpa_s->conf,
2932 wpa_s->pending_invite_ssid_id);
2933 if (ssid == NULL) {
2934 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2935 "data matching with invitation");
2936 return;
2937 }
2938
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002939 /*
2940 * The peer could have missed our ctrl::ack frame for Invitation
2941 * Response and continue retransmitting the frame. To reduce the
2942 * likelihood of the peer not getting successful TX status for the
2943 * Invitation Response frame, wait a short time here before starting
2944 * the persistent group so that we will remain on the current channel to
2945 * acknowledge any possible retransmission from the peer.
2946 */
2947 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2948 "starting persistent group");
2949 os_sleep(0, 50000);
2950
Dmitry Shmidt15907092014-03-25 10:42:57 -07002951 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2952 freq_included(channels, neg_freq))
2953 freq = neg_freq;
2954 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
2955 freq_included(channels, peer_oper_freq))
2956 freq = peer_oper_freq;
2957 else
2958 freq = 0;
2959
2960 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
2961 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002963 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002964 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002965 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002966 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2967 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002968 ssid->mode == WPAS_MODE_P2P_GO ?
2969 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2970 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971}
2972
2973
Dmitry Shmidt04949592012-07-19 12:16:46 -07002974static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2975 unsigned int freq)
2976{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002977 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
2978 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002979 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002980}
2981
2982
2983static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2984{
2985 reg->channel[reg->channels] = chan;
2986 reg->channels++;
2987}
2988
2989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002990static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002991 struct p2p_channels *chan,
2992 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993{
2994 int i, cla = 0;
2995
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002996 wpa_s->global->p2p_24ghz_social_channels = 1;
2997
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002998 os_memset(cli_chan, 0, sizeof(*cli_chan));
2999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3001 "band");
3002
3003 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3004 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003005 chan->reg_class[cla].channels = 0;
3006 for (i = 0; i < 11; i++) {
3007 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3008 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3009 }
3010 if (chan->reg_class[cla].channels)
3011 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003012
3013 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3014 "band");
3015
3016 /* Operating class 115 - 5 GHz, channels 36-48 */
3017 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003018 chan->reg_class[cla].channels = 0;
3019 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3020 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3021 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3022 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3023 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3024 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3025 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3026 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3027 if (chan->reg_class[cla].channels)
3028 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029
3030 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3031 "band");
3032
3033 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3034 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003035 chan->reg_class[cla].channels = 0;
3036 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3037 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3038 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3039 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3040 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3041 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3042 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3043 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3044 if (chan->reg_class[cla].channels)
3045 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046
3047 chan->reg_classes = cla;
3048 return 0;
3049}
3050
3051
3052static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3053 u16 num_modes,
3054 enum hostapd_hw_mode mode)
3055{
3056 u16 i;
3057
3058 for (i = 0; i < num_modes; i++) {
3059 if (modes[i].mode == mode)
3060 return &modes[i];
3061 }
3062
3063 return NULL;
3064}
3065
3066
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003067enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003068 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003069};
3070
Dmitry Shmidt04949592012-07-19 12:16:46 -07003071static int has_channel(struct wpa_global *global,
3072 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073{
3074 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003075 unsigned int freq;
3076
3077 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3078 chan * 5;
3079 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003080 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081
3082 for (i = 0; i < mode->num_channels; i++) {
3083 if (mode->channels[i].chan == chan) {
3084 if (flags)
3085 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003086 if (mode->channels[i].flag &
3087 (HOSTAPD_CHAN_DISABLED |
3088 HOSTAPD_CHAN_RADAR))
3089 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003090 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3091 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003092 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 }
3094 }
3095
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003096 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003097}
3098
3099
3100struct p2p_oper_class_map {
3101 enum hostapd_hw_mode mode;
3102 u8 op_class;
3103 u8 min_chan;
3104 u8 max_chan;
3105 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003106 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107};
3108
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003109static const struct p2p_oper_class_map op_class[] = {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003110 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003111#if 0 /* Do not enable HT40 on 2 GHz for now */
3112 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3113 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3114#endif
3115 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3116 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3117 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3118 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3119 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3120 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003121
3122 /*
3123 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3124 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3125 * 80 MHz, but currently use the following definition for simplicity
3126 * (these center frequencies are not actual channels, which makes
3127 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3128 * removing invalid channels.
3129 */
3130 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003131 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003132 { -1, 0, 0, 0, 0, BW20 }
3133};
3134
3135
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003136static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3137 struct hostapd_hw_modes *mode,
3138 u8 channel)
3139{
3140 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3141 unsigned int i;
3142
3143 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3144 return 0;
3145
3146 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3147 /*
3148 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3149 * so the center channel is 6 channels away from the start/end.
3150 */
3151 if (channel >= center_channels[i] - 6 &&
3152 channel <= center_channels[i] + 6)
3153 return center_channels[i];
3154
3155 return 0;
3156}
3157
3158
3159static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3160 struct hostapd_hw_modes *mode,
3161 u8 channel, u8 bw)
3162{
3163 u8 center_chan;
3164 int i, flags;
3165 enum chan_allowed res, ret = ALLOWED;
3166
3167 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3168 if (!center_chan)
3169 return NOT_ALLOWED;
3170 if (center_chan >= 58 && center_chan <= 138)
3171 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3172
3173 /* check all the channels are available */
3174 for (i = 0; i < 4; i++) {
3175 int adj_chan = center_chan - 6 + i * 4;
3176
3177 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3178 if (res == NOT_ALLOWED)
3179 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003180 if (res == NO_IR)
3181 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003182
3183 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3184 return NOT_ALLOWED;
3185 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3186 return NOT_ALLOWED;
3187 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3188 return NOT_ALLOWED;
3189 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3190 return NOT_ALLOWED;
3191 }
3192
3193 return ret;
3194}
3195
3196
3197static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3198 struct hostapd_hw_modes *mode,
3199 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003200{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003201 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003202 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003203
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003204 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3205 if (bw == BW40MINUS) {
3206 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3207 return NOT_ALLOWED;
3208 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3209 } else if (bw == BW40PLUS) {
3210 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3211 return NOT_ALLOWED;
3212 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3213 } else if (bw == BW80) {
3214 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3215 }
3216
3217 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3218 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003219 if (res == NO_IR || res2 == NO_IR)
3220 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003221 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003222}
3223
3224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003226 struct p2p_channels *chan,
3227 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003228{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003229 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003230 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003231
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003232 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3234 "of all supported channels; assume dualband "
3235 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003236 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 }
3238
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003239 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240
3241 for (op = 0; op_class[op].op_class; op++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003242 const struct p2p_oper_class_map *o = &op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003244 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003246 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247 if (mode == NULL)
3248 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003249 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3250 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003252 enum chan_allowed res;
3253 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3254 if (res == ALLOWED) {
3255 if (reg == NULL) {
3256 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3257 o->op_class);
3258 reg = &chan->reg_class[cla];
3259 cla++;
3260 reg->reg_class = o->op_class;
3261 }
3262 reg->channel[reg->channels] = ch;
3263 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003264 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003265 wpa_s->conf->p2p_add_cli_chan) {
3266 if (cli_reg == NULL) {
3267 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3268 o->op_class);
3269 cli_reg = &cli_chan->reg_class[cli_cla];
3270 cli_cla++;
3271 cli_reg->reg_class = o->op_class;
3272 }
3273 cli_reg->channel[cli_reg->channels] = ch;
3274 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003275 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 }
3277 if (reg) {
3278 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3279 reg->channel, reg->channels);
3280 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003281 if (cli_reg) {
3282 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3283 cli_reg->channel, cli_reg->channels);
3284 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 }
3286
3287 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003288 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 return 0;
3291}
3292
3293
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003294int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3295 struct hostapd_hw_modes *mode, u8 channel)
3296{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003297 int op;
3298 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003299
3300 for (op = 0; op_class[op].op_class; op++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003301 const struct p2p_oper_class_map *o = &op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003302 u8 ch;
3303
3304 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3305 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3306 o->bw == BW20 || ch != channel)
3307 continue;
3308 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003309 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003310 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003311 }
3312 }
3313 return 0;
3314}
3315
3316
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003317int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3318 struct hostapd_hw_modes *mode, u8 channel)
3319{
3320 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3321 return 0;
3322
3323 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3324}
3325
3326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003327static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3328 size_t buf_len)
3329{
3330 struct wpa_supplicant *wpa_s = ctx;
3331
3332 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3333 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3334 break;
3335 }
3336 if (wpa_s == NULL)
3337 return -1;
3338
3339 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3340}
3341
3342
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003343struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3344 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003345{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003346 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3347 struct wpa_ssid *s = wpa_s->current_ssid;
3348 if (s == NULL)
3349 continue;
3350 if (s->mode != WPAS_MODE_P2P_GO &&
3351 s->mode != WPAS_MODE_AP &&
3352 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3353 continue;
3354 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003355 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003356 continue;
3357 return wpa_s;
3358 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003359
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003360 return NULL;
3361
3362}
3363
3364
3365struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3366 const u8 *peer_dev_addr)
3367{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003368 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3369 struct wpa_ssid *ssid = wpa_s->current_ssid;
3370 if (ssid == NULL)
3371 continue;
3372 if (ssid->mode != WPAS_MODE_INFRA)
3373 continue;
3374 if (wpa_s->wpa_state != WPA_COMPLETED &&
3375 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3376 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003377 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3378 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003379 }
3380
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003381 return NULL;
3382}
3383
3384
3385static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3386{
3387 struct wpa_supplicant *wpa_s = ctx;
3388
3389 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003390}
3391
3392
Dmitry Shmidt18463232014-01-24 12:29:41 -08003393static int wpas_is_concurrent_session_active(void *ctx)
3394{
3395 struct wpa_supplicant *wpa_s = ctx;
3396 struct wpa_supplicant *ifs;
3397
3398 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3399 if (ifs == wpa_s)
3400 continue;
3401 if (ifs->wpa_state > WPA_ASSOCIATED)
3402 return 1;
3403 }
3404 return 0;
3405}
3406
3407
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003408static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3409{
3410 struct wpa_supplicant *wpa_s = ctx;
3411 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3412}
3413
3414
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003415int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3416 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003417{
3418 struct wpa_interface iface;
3419 struct wpa_supplicant *p2pdev_wpa_s;
3420 char ifname[100];
3421 char force_name[100];
3422 int ret;
3423
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003424 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3425 wpa_s->ifname);
3426 if (os_snprintf_error(sizeof(ifname), ret))
3427 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003428 force_name[0] = '\0';
3429 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3430 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3431 force_name, wpa_s->pending_interface_addr, NULL);
3432 if (ret < 0) {
3433 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3434 return ret;
3435 }
3436 os_strlcpy(wpa_s->pending_interface_name, ifname,
3437 sizeof(wpa_s->pending_interface_name));
3438
3439 os_memset(&iface, 0, sizeof(iface));
3440 iface.p2p_mgmt = 1;
3441 iface.ifname = wpa_s->pending_interface_name;
3442 iface.driver = wpa_s->driver->name;
3443 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003444
3445 /*
3446 * If a P2P Device configuration file was given, use it as the interface
3447 * configuration file (instead of using parent's configuration file.
3448 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003449 if (conf_p2p_dev) {
3450 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003451 iface.ctrl_interface = NULL;
3452 } else {
3453 iface.confname = wpa_s->confname;
3454 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3455 }
3456 iface.conf_p2p_dev = NULL;
3457
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003458 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003459 if (!p2pdev_wpa_s) {
3460 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3461 return -1;
3462 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003463
3464 wpa_s->pending_interface_name[0] = '\0';
3465 return 0;
3466}
3467
3468
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003469static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3470 const u8 *noa, size_t noa_len)
3471{
3472 struct wpa_supplicant *wpa_s, *intf = ctx;
3473 char hex[100];
3474
3475 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3476 if (wpa_s->waiting_presence_resp)
3477 break;
3478 }
3479 if (!wpa_s) {
3480 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3481 return;
3482 }
3483 wpa_s->waiting_presence_resp = 0;
3484
3485 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3486 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3487 " status=%u noa=%s", MAC2STR(src), status, hex);
3488}
3489
3490
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003491static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3492 size_t ssid_len, u8 *go_dev_addr,
3493 u8 *ret_ssid, size_t *ret_ssid_len)
3494{
3495 struct wpa_supplicant *wpa_s = ctx;
3496 struct wpa_ssid *s;
3497
3498 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3499 if (s) {
3500 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3501 *ret_ssid_len = s->ssid_len;
3502 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3503 return 1;
3504 }
3505
3506 return 0;
3507}
3508
3509
3510static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3511 u8 *ssid, size_t *ssid_len, int *group_iface)
3512{
3513 struct wpa_supplicant *wpa_s = ctx;
3514 struct wpa_ssid *s;
3515 u8 bssid[ETH_ALEN];
3516
3517 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3518 if (!s) {
3519 s = wpas_p2p_get_persistent_go(wpa_s);
3520 if (s)
3521 os_memcpy(bssid, s->bssid, ETH_ALEN);
3522 }
3523
3524 *group_iface = wpas_p2p_create_iface(wpa_s);
3525 if (!s)
3526 return 0;
3527
3528 os_memcpy(intended_addr, bssid, ETH_ALEN);
3529 os_memcpy(ssid, s->ssid, s->ssid_len);
3530 *ssid_len = s->ssid_len;
3531
3532 return 1;
3533}
3534
3535
3536static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3537 const u8 *ssid, size_t ssid_len)
3538{
3539 struct wpa_supplicant *wpa_s = ctx;
3540 struct wpa_ssid *s;
3541 int save_config = 0;
3542 size_t i;
3543
3544 /* Start with our first choice of Persistent Groups */
3545 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3546 if (go && ssid && ssid_len &&
3547 s->ssid_len == ssid_len &&
3548 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3549 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3550 break;
3551
3552 /* Remove stale persistent group */
3553 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3554 wpa_config_remove_network(wpa_s->conf, s->id);
3555 save_config = 1;
3556 continue;
3557 }
3558
3559 for (i = 0; i < s->num_p2p_clients; i++) {
3560 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3561 peer, ETH_ALEN) != 0)
3562 continue;
3563
3564 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3565 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3566 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3567 break;
3568 }
3569 s->num_p2p_clients--;
3570 save_config = 1;
3571 }
3572
3573 if (save_config)
3574 p2p_config_write(wpa_s);
3575
3576 /* Return TRUE if valid SSID remains */
3577 return s != NULL;
3578}
3579
3580
3581static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3582 const u8 *adv_mac, const u8 *ses_mac,
3583 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3584 u8 conncap, int passwd_id,
3585 const u8 *persist_ssid,
3586 size_t persist_ssid_size, int response_done,
3587 int prov_start, const char *session_info)
3588{
3589 struct wpa_supplicant *wpa_s = ctx;
3590 u8 mac[ETH_ALEN];
3591 struct wpa_ssid *persistent_go, *stale, *s;
3592 int save_config = 0;
3593 struct wpa_supplicant *go_wpa_s;
3594
3595 if (!dev)
3596 return;
3597
3598 os_memset(mac, 0, ETH_ALEN);
3599 if (!adv_mac)
3600 adv_mac = mac;
3601 if (!ses_mac)
3602 ses_mac = mac;
3603 if (!grp_mac)
3604 grp_mac = mac;
3605
3606 if (prov_start) {
3607 if (session_info == NULL) {
3608 wpa_msg_global(wpa_s, MSG_INFO,
3609 P2P_EVENT_P2PS_PROVISION_START MACSTR
3610 " adv_id=%x conncap=%x"
3611 " adv_mac=" MACSTR
3612 " session=%x mac=" MACSTR
3613 " dev_passwd_id=%d",
3614 MAC2STR(dev), adv_id, conncap,
3615 MAC2STR(adv_mac),
3616 ses_id, MAC2STR(ses_mac),
3617 passwd_id);
3618 } else {
3619 wpa_msg_global(wpa_s, MSG_INFO,
3620 P2P_EVENT_P2PS_PROVISION_START MACSTR
3621 " adv_id=%x conncap=%x"
3622 " adv_mac=" MACSTR
3623 " session=%x mac=" MACSTR
3624 " dev_passwd_id=%d info='%s'",
3625 MAC2STR(dev), adv_id, conncap,
3626 MAC2STR(adv_mac),
3627 ses_id, MAC2STR(ses_mac),
3628 passwd_id, session_info);
3629 }
3630 return;
3631 }
3632
3633 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3634 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3635
3636 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3637 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3638 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3639
3640 if (persistent_go && !persistent_go->num_p2p_clients) {
3641 /* remove empty persistent GO */
3642 wpa_config_remove_network(wpa_s->conf,
3643 persistent_go->id);
3644 }
3645
3646 wpa_msg_global(wpa_s, MSG_INFO,
3647 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3648 " status=%d"
3649 " adv_id=%x adv_mac=" MACSTR
3650 " session=%x mac=" MACSTR,
3651 MAC2STR(dev), status,
3652 adv_id, MAC2STR(adv_mac),
3653 ses_id, MAC2STR(ses_mac));
3654 return;
3655 }
3656
3657 /* Clean up stale persistent groups with this device */
3658 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3659 persist_ssid_size);
3660 for (;;) {
3661 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3662 if (!stale)
3663 break;
3664
3665 if (s && s->ssid_len == stale->ssid_len &&
3666 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3667 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3668 break;
3669
3670 /* Remove stale persistent group */
3671 if (stale->mode != WPAS_MODE_P2P_GO ||
3672 stale->num_p2p_clients <= 1) {
3673 wpa_config_remove_network(wpa_s->conf, stale->id);
3674 } else {
3675 size_t i;
3676
3677 for (i = 0; i < stale->num_p2p_clients; i++) {
3678 if (os_memcmp(stale->p2p_client_list +
3679 i * ETH_ALEN,
3680 dev, ETH_ALEN) == 0) {
3681 os_memmove(stale->p2p_client_list +
3682 i * ETH_ALEN,
3683 stale->p2p_client_list +
3684 (i + 1) * ETH_ALEN,
3685 (stale->num_p2p_clients -
3686 i - 1) * ETH_ALEN);
3687 break;
3688 }
3689 }
3690 stale->num_p2p_clients--;
3691 }
3692 save_config = 1;
3693 }
3694
3695 if (save_config)
3696 p2p_config_write(wpa_s);
3697
3698 if (s) {
3699 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3700 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3701
3702 if (persistent_go && s != persistent_go &&
3703 !persistent_go->num_p2p_clients) {
3704 /* remove empty persistent GO */
3705 wpa_config_remove_network(wpa_s->conf,
3706 persistent_go->id);
3707 /* Save config */
3708 }
3709
3710 wpa_msg_global(wpa_s, MSG_INFO,
3711 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3712 " status=%d"
3713 " adv_id=%x adv_mac=" MACSTR
3714 " session=%x mac=" MACSTR
3715 " persist=%d",
3716 MAC2STR(dev), status,
3717 adv_id, MAC2STR(adv_mac),
3718 ses_id, MAC2STR(ses_mac), s->id);
3719 return;
3720 }
3721
3722 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3723 const char *go_ifname = NULL;
3724 if (!go_wpa_s) {
3725 wpa_s->global->pending_p2ps_group = 1;
3726
3727 if (wpa_s->conf->p2p_no_group_iface)
3728 go_ifname = wpa_s->ifname;
3729 else if (wpa_s->pending_interface_name[0])
3730 go_ifname = wpa_s->pending_interface_name;
3731
3732 if (!go_ifname) {
3733 wpas_p2ps_prov_complete(
3734 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3735 dev, adv_mac, ses_mac,
3736 NULL, adv_id, ses_id, 0, 0,
3737 NULL, 0, 0, 0, NULL);
3738 return;
3739 }
3740
3741 /* If PD Resp complete, start up the GO */
3742 if (response_done && persistent_go) {
3743 wpas_p2p_group_add_persistent(
3744 wpa_s, persistent_go,
3745 0, 0, 0, 0, 0, NULL,
3746 persistent_go->mode ==
3747 WPAS_MODE_P2P_GO ?
3748 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3749 0);
3750 } else if (response_done) {
3751 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3752 }
3753
3754 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3755 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3756 wpa_s->p2ps_join_addr_valid = 1;
3757 wpa_dbg(wpa_s, MSG_DEBUG,
3758 "P2PS: Saving PIN for " MACSTR,
3759 MAC2STR(dev));
3760 }
3761 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3762 go_ifname = go_wpa_s->ifname;
3763
3764 wpa_dbg(go_wpa_s, MSG_DEBUG,
3765 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3766 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3767 NULL, 0, 0);
3768
3769 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3770 wpa_s->p2ps_join_addr_valid = 1;
3771 wpa_dbg(wpa_s, MSG_DEBUG,
3772 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3773 }
3774
3775 wpa_msg_global(wpa_s, MSG_INFO,
3776 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3777 " status=%d conncap=%x"
3778 " adv_id=%x adv_mac=" MACSTR
3779 " session=%x mac=" MACSTR
3780 " dev_passwd_id=%d go=%s",
3781 MAC2STR(dev), status, conncap,
3782 adv_id, MAC2STR(adv_mac),
3783 ses_id, MAC2STR(ses_mac),
3784 passwd_id, go_ifname);
3785 return;
3786 }
3787
3788 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3789 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3790
3791 if (persistent_go && !persistent_go->num_p2p_clients) {
3792 /* remove empty persistent GO */
3793 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3794 }
3795
3796 if (conncap == P2PS_SETUP_CLIENT) {
3797 wpa_msg_global(wpa_s, MSG_INFO,
3798 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3799 " status=%d conncap=%x"
3800 " adv_id=%x adv_mac=" MACSTR
3801 " session=%x mac=" MACSTR
3802 " dev_passwd_id=%d join=" MACSTR,
3803 MAC2STR(dev), status, conncap,
3804 adv_id, MAC2STR(adv_mac),
3805 ses_id, MAC2STR(ses_mac),
3806 passwd_id, MAC2STR(grp_mac));
3807 } else {
3808 wpa_msg_global(wpa_s, MSG_INFO,
3809 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3810 " status=%d conncap=%x"
3811 " adv_id=%x adv_mac=" MACSTR
3812 " session=%x mac=" MACSTR
3813 " dev_passwd_id=%d",
3814 MAC2STR(dev), status, conncap,
3815 adv_id, MAC2STR(adv_mac),
3816 ses_id, MAC2STR(ses_mac),
3817 passwd_id);
3818 }
3819}
3820
3821
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003822static int _wpas_p2p_in_progress(void *ctx)
3823{
3824 struct wpa_supplicant *wpa_s = ctx;
3825 return wpas_p2p_in_progress(wpa_s);
3826}
3827
3828
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003829static int wpas_prov_disc_resp_cb(void *ctx)
3830{
3831 struct wpa_supplicant *wpa_s = ctx;
3832 struct wpa_ssid *persistent_go;
3833
3834 if (!wpa_s->global->pending_p2ps_group)
3835 return 0;
3836
3837 wpa_s->global->pending_p2ps_group = 0;
3838
3839 if (wpas_p2p_get_go_group(wpa_s))
3840 return 0;
3841 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3842
3843 if (persistent_go) {
3844 wpas_p2p_group_add_persistent(
3845 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
3846 persistent_go->mode == WPAS_MODE_P2P_GO ?
3847 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3848 } else {
3849 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3850 }
3851
3852 return 1;
3853}
3854
3855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856/**
3857 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3858 * @global: Pointer to global data from wpa_supplicant_init()
3859 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3860 * Returns: 0 on success, -1 on failure
3861 */
3862int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3863{
3864 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003865 int i;
3866
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003867 if (wpa_s->conf->p2p_disabled)
3868 return 0;
3869
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003870 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3871 return 0;
3872
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003873 if (global->p2p)
3874 return 0;
3875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003878 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 p2p.p2p_scan = wpas_p2p_scan;
3880 p2p.send_action = wpas_send_action;
3881 p2p.send_action_done = wpas_send_action_done;
3882 p2p.go_neg_completed = wpas_go_neg_completed;
3883 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3884 p2p.dev_found = wpas_dev_found;
3885 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003886 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003887 p2p.start_listen = wpas_start_listen;
3888 p2p.stop_listen = wpas_stop_listen;
3889 p2p.send_probe_resp = wpas_send_probe_resp;
3890 p2p.sd_request = wpas_sd_request;
3891 p2p.sd_response = wpas_sd_response;
3892 p2p.prov_disc_req = wpas_prov_disc_req;
3893 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003894 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 p2p.invitation_process = wpas_invitation_process;
3896 p2p.invitation_received = wpas_invitation_received;
3897 p2p.invitation_result = wpas_invitation_result;
3898 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003899 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003900 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003901 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003902 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003903 p2p.get_persistent_group = wpas_get_persistent_group;
3904 p2p.get_go_info = wpas_get_go_info;
3905 p2p.remove_stale_groups = wpas_remove_stale_groups;
3906 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
3907 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
3908 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003909
3910 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003911 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003912 p2p.dev_name = wpa_s->conf->device_name;
3913 p2p.manufacturer = wpa_s->conf->manufacturer;
3914 p2p.model_name = wpa_s->conf->model_name;
3915 p2p.model_number = wpa_s->conf->model_number;
3916 p2p.serial_number = wpa_s->conf->serial_number;
3917 if (wpa_s->wps) {
3918 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3919 p2p.config_methods = wpa_s->wps->config_methods;
3920 }
3921
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003922 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3923 wpa_printf(MSG_ERROR,
3924 "P2P: Failed to configure supported channel list");
3925 return -1;
3926 }
3927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 if (wpa_s->conf->p2p_listen_reg_class &&
3929 wpa_s->conf->p2p_listen_channel) {
3930 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3931 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003932 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003934 /*
3935 * Pick one of the social channels randomly as the listen
3936 * channel.
3937 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003938 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
3939 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003940 wpa_printf(MSG_INFO,
3941 "P2P: No social channels supported by the driver - do not enable P2P");
3942 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003943 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003944 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003946 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
3947 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948
3949 if (wpa_s->conf->p2p_oper_reg_class &&
3950 wpa_s->conf->p2p_oper_channel) {
3951 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3952 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3953 p2p.cfg_op_channel = 1;
3954 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3955 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3956
3957 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003959 * Use random operation channel from 2.4 GHz band social
3960 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
3961 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003962 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003963 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
3964 &p2p.op_channel) != 0) {
3965 wpa_printf(MSG_ERROR,
3966 "P2P: Failed to select random social channel as operation channel");
3967 return -1;
3968 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003969 p2p.cfg_op_channel = 0;
3970 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3971 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3972 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003973
3974 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3975 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3976 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3977 }
3978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003979 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3980 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3981 p2p.country[2] = 0x04;
3982 } else
3983 os_memcpy(p2p.country, "XX\x04", 3);
3984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003985 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3986 WPS_DEV_TYPE_LEN);
3987
3988 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3989 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3990 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3991
3992 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3993 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3994
3995 p2p.max_peers = 100;
3996
3997 if (wpa_s->conf->p2p_ssid_postfix) {
3998 p2p.ssid_postfix_len =
3999 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4000 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4001 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4002 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4003 p2p.ssid_postfix_len);
4004 }
4005
4006 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4007
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004008 p2p.max_listen = wpa_s->max_remain_on_chan;
4009
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004010 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4011 wpa_s->conf->p2p_passphrase_len <= 63)
4012 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4013 else
4014 p2p.passphrase_len = 8;
4015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004016 global->p2p = p2p_init(&p2p);
4017 if (global->p2p == NULL)
4018 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004019 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004020
4021 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4022 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4023 continue;
4024 p2p_add_wps_vendor_extension(
4025 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4026 }
4027
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004028 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4029
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004030 return 0;
4031}
4032
4033
4034/**
4035 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4036 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4037 *
4038 * This function deinitialize per-interface P2P data.
4039 */
4040void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4041{
4042 if (wpa_s->driver && wpa_s->drv_priv)
4043 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004044
4045 if (wpa_s->go_params) {
4046 /* Clear any stored provisioning info */
4047 p2p_clear_provisioning_info(
4048 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004049 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004050 }
4051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052 os_free(wpa_s->go_params);
4053 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004054 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4056 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4057 wpa_s->p2p_long_listen = 0;
4058 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4059 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4060 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004061 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004062 wpas_p2p_listen_work_done(wpa_s);
4063 if (wpa_s->p2p_send_action_work) {
4064 os_free(wpa_s->p2p_send_action_work->ctx);
4065 radio_work_done(wpa_s->p2p_send_action_work);
4066 wpa_s->p2p_send_action_work = NULL;
4067 }
4068 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004069
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004070 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4071 wpa_s->p2p_oob_dev_pw = NULL;
4072
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004073 os_free(wpa_s->p2p_group_common_freqs);
4074 wpa_s->p2p_group_common_freqs = NULL;
4075 wpa_s->p2p_group_common_freqs_num = 0;
4076
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004077 /* TODO: remove group interface from the driver if this wpa_s instance
4078 * is on top of a P2P group interface */
4079}
4080
4081
4082/**
4083 * wpas_p2p_deinit_global - Deinitialize global P2P module
4084 * @global: Pointer to global data from wpa_supplicant_init()
4085 *
4086 * This function deinitializes the global (per device) P2P module.
4087 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004088static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004089{
4090 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004092 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004093
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004094 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095
4096 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4098 wpa_s = wpa_s->next;
4099 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100 tmp = global->ifaces;
4101 while (tmp &&
4102 (tmp == wpa_s ||
4103 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4104 tmp = tmp->next;
4105 }
4106 if (tmp == NULL)
4107 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004108 /* Disconnect from the P2P group and deinit the interface */
4109 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 }
4111
4112 /*
4113 * Deinit GO data on any possibly remaining interface (if main
4114 * interface is used as GO).
4115 */
4116 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4117 if (wpa_s->ap_iface)
4118 wpas_p2p_group_deinit(wpa_s);
4119 }
4120
4121 p2p_deinit(global->p2p);
4122 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004123 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124}
4125
4126
4127static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4128{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004129 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4130 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004131 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132 if (wpa_s->drv_flags &
4133 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4134 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4135 return 1; /* P2P group requires a new interface in every case
4136 */
4137 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4138 return 0; /* driver does not support concurrent operations */
4139 if (wpa_s->global->ifaces->next)
4140 return 1; /* more that one interface already in use */
4141 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4142 return 1; /* this interface is already in use */
4143 return 0;
4144}
4145
4146
4147static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4148 const u8 *peer_addr,
4149 enum p2p_wps_method wps_method,
4150 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004151 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004152 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004154 if (persistent_group && wpa_s->conf->persistent_reconnect)
4155 persistent_group = 2;
4156
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004157 /*
4158 * Increase GO config timeout if HT40 is used since it takes some time
4159 * to scan channels for coex purposes before the BSS can be started.
4160 */
4161 p2p_set_config_timeout(wpa_s->global->p2p,
4162 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4165 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004166 persistent_group, ssid ? ssid->ssid : NULL,
4167 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004168 wpa_s->p2p_pd_before_go_neg, pref_freq,
4169 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4170 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171}
4172
4173
4174static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4175 const u8 *peer_addr,
4176 enum p2p_wps_method wps_method,
4177 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004178 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004179 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004181 if (persistent_group && wpa_s->conf->persistent_reconnect)
4182 persistent_group = 2;
4183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004184 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4185 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004186 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004187 ssid ? ssid->ssid_len : 0, pref_freq,
4188 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4189 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190}
4191
4192
4193static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4194{
4195 wpa_s->p2p_join_scan_count++;
4196 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4197 wpa_s->p2p_join_scan_count);
4198 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4199 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4200 " for join operationg - stop join attempt",
4201 MAC2STR(wpa_s->pending_join_iface_addr));
4202 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004203 if (wpa_s->p2p_auto_pd) {
4204 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004205 wpa_msg_global(wpa_s, MSG_INFO,
4206 P2P_EVENT_PROV_DISC_FAILURE
4207 " p2p_dev_addr=" MACSTR " status=N/A",
4208 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004209 return;
4210 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004211 wpa_msg_global(wpa_s->parent, MSG_INFO,
4212 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004213 }
4214}
4215
4216
Dmitry Shmidt04949592012-07-19 12:16:46 -07004217static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4218{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004219 int res;
4220 unsigned int num, i;
4221 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004222
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004223 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4224 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004225 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004226 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004227
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004228 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4229 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004230 if (!freqs)
4231 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004232
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004233 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4234 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004235
4236 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004237 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004238 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4239 freq);
4240 res = 0;
4241 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004242 }
4243 }
4244
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004245 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004246 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004247
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004248exit_free:
4249 os_free(freqs);
4250 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004251}
4252
4253
4254static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4255 const u8 *peer_dev_addr)
4256{
4257 struct wpa_bss *bss;
4258 int updated;
4259
4260 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4261 if (bss == NULL)
4262 return -1;
4263 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4264 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4265 "last scan");
4266 return 0;
4267 }
4268
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004269 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4270 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004271 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4272 "%ld.%06ld (%supdated in last scan)",
4273 bss->last_update.sec, bss->last_update.usec,
4274 updated ? "": "not ");
4275
4276 return updated;
4277}
4278
4279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4281 struct wpa_scan_results *scan_res)
4282{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004283 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004284 int freq;
4285 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004287 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4288
4289 if (wpa_s->global->p2p_disabled)
4290 return;
4291
Dmitry Shmidt04949592012-07-19 12:16:46 -07004292 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4293 scan_res ? (int) scan_res->num : -1,
4294 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295
4296 if (scan_res)
4297 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4298
Dmitry Shmidt04949592012-07-19 12:16:46 -07004299 if (wpa_s->p2p_auto_pd) {
4300 int join = wpas_p2p_peer_go(wpa_s,
4301 wpa_s->pending_join_dev_addr);
4302 if (join == 0 &&
4303 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4304 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004305 bss = wpa_bss_get_bssid_latest(
4306 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004307 if (bss) {
4308 freq = bss->freq;
4309 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4310 "the peer " MACSTR " at %d MHz",
4311 wpa_s->auto_pd_scan_retry,
4312 MAC2STR(wpa_s->
4313 pending_join_dev_addr),
4314 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004315 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004316 return;
4317 }
4318 }
4319
4320 if (join < 0)
4321 join = 0;
4322
4323 wpa_s->p2p_auto_pd = 0;
4324 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4325 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4326 MAC2STR(wpa_s->pending_join_dev_addr), join);
4327 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004328 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004329 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004330 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004331 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004332 wpa_msg_global(wpa_s, MSG_INFO,
4333 P2P_EVENT_PROV_DISC_FAILURE
4334 " p2p_dev_addr=" MACSTR " status=N/A",
4335 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004336 }
4337 return;
4338 }
4339
4340 if (wpa_s->p2p_auto_join) {
4341 int join = wpas_p2p_peer_go(wpa_s,
4342 wpa_s->pending_join_dev_addr);
4343 if (join < 0) {
4344 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4345 "running a GO -> use GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004346 wpa_msg_global(wpa_s->parent, MSG_INFO,
4347 P2P_EVENT_FALLBACK_TO_GO_NEG
4348 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004349 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4350 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4351 wpa_s->p2p_persistent_group, 0, 0, 0,
4352 wpa_s->p2p_go_intent,
4353 wpa_s->p2p_connect_freq,
4354 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004355 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004356 wpa_s->p2p_go_ht40,
4357 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004358 return;
4359 }
4360
4361 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4362 "try to join the group", join ? "" :
4363 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004364 if (!join) {
4365 wpa_msg_global(wpa_s->parent, MSG_INFO,
4366 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004367 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004368 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 }
4370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004371 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4372 wpa_s->pending_join_iface_addr);
4373 if (freq < 0 &&
4374 p2p_get_interface_addr(wpa_s->global->p2p,
4375 wpa_s->pending_join_dev_addr,
4376 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004377 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4378 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4380 "address for join from " MACSTR " to " MACSTR
4381 " based on newly discovered P2P peer entry",
4382 MAC2STR(wpa_s->pending_join_iface_addr),
4383 MAC2STR(iface_addr));
4384 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4385 ETH_ALEN);
4386
4387 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4388 wpa_s->pending_join_iface_addr);
4389 }
4390 if (freq >= 0) {
4391 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4392 "from P2P peer table: %d MHz", freq);
4393 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004394 if (wpa_s->p2p_join_ssid_len) {
4395 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4396 MACSTR " and SSID %s",
4397 MAC2STR(wpa_s->pending_join_iface_addr),
4398 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4399 wpa_s->p2p_join_ssid_len));
4400 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4401 wpa_s->p2p_join_ssid,
4402 wpa_s->p2p_join_ssid_len);
4403 }
4404 if (!bss) {
4405 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4406 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4407 bss = wpa_bss_get_bssid_latest(wpa_s,
4408 wpa_s->pending_join_iface_addr);
4409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004410 if (bss) {
4411 freq = bss->freq;
4412 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004413 "from BSS table: %d MHz (SSID %s)", freq,
4414 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415 }
4416 if (freq > 0) {
4417 u16 method;
4418
Dmitry Shmidt04949592012-07-19 12:16:46 -07004419 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004420 wpa_msg_global(wpa_s->parent, MSG_INFO,
4421 P2P_EVENT_GROUP_FORMATION_FAILURE
4422 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004423 return;
4424 }
4425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4427 "prior to joining an existing group (GO " MACSTR
4428 " freq=%u MHz)",
4429 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4430 wpa_s->pending_pd_before_join = 1;
4431
4432 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 case WPS_PIN_DISPLAY:
4434 method = WPS_CONFIG_KEYPAD;
4435 break;
4436 case WPS_PIN_KEYPAD:
4437 method = WPS_CONFIG_DISPLAY;
4438 break;
4439 case WPS_PBC:
4440 method = WPS_CONFIG_PUSHBUTTON;
4441 break;
4442 default:
4443 method = 0;
4444 break;
4445 }
4446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004447 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4448 wpa_s->pending_join_dev_addr) ==
4449 method)) {
4450 /*
4451 * We have already performed provision discovery for
4452 * joining the group. Proceed directly to join
4453 * operation without duplicated provision discovery. */
4454 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4455 "with " MACSTR " already done - proceed to "
4456 "join",
4457 MAC2STR(wpa_s->pending_join_dev_addr));
4458 wpa_s->pending_pd_before_join = 0;
4459 goto start;
4460 }
4461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004463 wpa_s->pending_join_dev_addr,
4464 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004465 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4467 "Discovery Request before joining an "
4468 "existing group");
4469 wpa_s->pending_pd_before_join = 0;
4470 goto start;
4471 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004472 return;
4473 }
4474
4475 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4476 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4477 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4478 wpas_p2p_check_join_scan_limit(wpa_s);
4479 return;
4480
4481start:
4482 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004483 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004484}
4485
4486
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004487static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4488 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004489{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490 int ret;
4491 struct wpa_driver_scan_params params;
4492 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004493 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004494 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495
4496 os_memset(&params, 0, sizeof(params));
4497
4498 /* P2P Wildcard SSID */
4499 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004500 if (ssid && ssid_len) {
4501 params.ssids[0].ssid = ssid;
4502 params.ssids[0].ssid_len = ssid_len;
4503 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4504 wpa_s->p2p_join_ssid_len = ssid_len;
4505 } else {
4506 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4507 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4508 wpa_s->p2p_join_ssid_len = 0;
4509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510
4511 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004512 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4513 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4514 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 if (wps_ie == NULL) {
4516 wpas_p2p_scan_res_join(wpa_s, NULL);
4517 return;
4518 }
4519
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004520 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4521 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522 if (ies == NULL) {
4523 wpabuf_free(wps_ie);
4524 wpas_p2p_scan_res_join(wpa_s, NULL);
4525 return;
4526 }
4527 wpabuf_put_buf(ies, wps_ie);
4528 wpabuf_free(wps_ie);
4529
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004530 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004532 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533 params.extra_ies = wpabuf_head(ies);
4534 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004535
4536 if (!freq) {
4537 int oper_freq;
4538 /*
4539 * If freq is not provided, check the operating freq of the GO
4540 * and use a single channel scan on if possible.
4541 */
4542 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4543 wpa_s->pending_join_iface_addr);
4544 if (oper_freq > 0)
4545 freq = oper_freq;
4546 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004547 if (freq > 0) {
4548 freqs[0] = freq;
4549 params.freqs = freqs;
4550 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004551
4552 /*
4553 * Run a scan to update BSS table and start Provision Discovery once
4554 * the new scan results become available.
4555 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004556 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004557 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004558 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004559 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004560 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004561 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562
4563 wpabuf_free(ies);
4564
4565 if (ret) {
4566 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4567 "try again later");
4568 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4569 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4570 wpas_p2p_check_join_scan_limit(wpa_s);
4571 }
4572}
4573
4574
Dmitry Shmidt04949592012-07-19 12:16:46 -07004575static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4576{
4577 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004578 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004579}
4580
4581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004583 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004584 int auto_join, int op_freq,
4585 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004586{
4587 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004588 MACSTR " dev " MACSTR " op_freq=%d)%s",
4589 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004590 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004591 if (ssid && ssid_len) {
4592 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4593 wpa_ssid_txt(ssid, ssid_len));
4594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595
Dmitry Shmidt04949592012-07-19 12:16:46 -07004596 wpa_s->p2p_auto_pd = 0;
4597 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004598 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4599 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4600 wpa_s->pending_join_wps_method = wps_method;
4601
4602 /* Make sure we are not running find during connection establishment */
4603 wpas_p2p_stop_find(wpa_s);
4604
4605 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004606 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004607 return 0;
4608}
4609
4610
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004611static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4612 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004613{
4614 struct wpa_supplicant *group;
4615 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004616 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617
4618 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4619 if (group == NULL)
4620 return -1;
4621 if (group != wpa_s) {
4622 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4623 sizeof(group->p2p_pin));
4624 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004625 } else {
4626 /*
4627 * Need to mark the current interface for p2p_group_formation
4628 * when a separate group interface is not used. This is needed
4629 * to allow p2p_cancel stop a pending p2p_connect-join.
4630 * wpas_p2p_init_group_interface() addresses this for the case
4631 * where a separate group interface is used.
4632 */
4633 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004634 }
4635
4636 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004637 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004638
4639 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004640 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004641 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4642 ETH_ALEN);
4643 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004644 if (freq && ssid && ssid_len) {
4645 res.freq = freq;
4646 res.ssid_len = ssid_len;
4647 os_memcpy(res.ssid, ssid, ssid_len);
4648 } else {
4649 bss = wpa_bss_get_bssid_latest(wpa_s,
4650 wpa_s->pending_join_iface_addr);
4651 if (bss) {
4652 res.freq = bss->freq;
4653 res.ssid_len = bss->ssid_len;
4654 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4655 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4656 bss->freq,
4657 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4658 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004659 }
4660
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004661 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4662 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4663 "starting client");
4664 wpa_drv_cancel_remain_on_channel(wpa_s);
4665 wpa_s->off_channel_freq = 0;
4666 wpa_s->roc_waiting_drv_freq = 0;
4667 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004668 wpas_start_wps_enrollee(group, &res);
4669
4670 /*
4671 * Allow a longer timeout for join-a-running-group than normal 15
4672 * second group formation timeout since the GO may not have authorized
4673 * our connection yet.
4674 */
4675 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4676 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4677 wpa_s, NULL);
4678
4679 return 0;
4680}
4681
4682
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004683static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004684 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004685{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004686 struct wpa_used_freq_data *freqs;
4687 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004688 unsigned int freq_in_use = 0, num, i;
4689
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004690 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4691 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004692 if (!freqs)
4693 return -1;
4694
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004695 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4696 wpa_s->num_multichan_concurrent);
4697
4698 /*
4699 * It is possible that the total number of used frequencies is bigger
4700 * than the number of frequencies used for P2P, so get the system wide
4701 * number of unused frequencies.
4702 */
4703 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4704
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004705 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004706 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4707 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004708
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004709 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004710 int ret;
4711 if (go)
4712 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4713 else
4714 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4715 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004716 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4717 ieee80211_is_dfs(freq)) {
4718 /*
4719 * If freq is a DFS channel and DFS is offloaded
4720 * to the driver, allow P2P GO to use it.
4721 */
4722 wpa_printf(MSG_DEBUG,
4723 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4724 freq);
4725 } else {
4726 wpa_printf(MSG_DEBUG,
4727 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4728 freq);
4729 res = -3;
4730 goto exit_free;
4731 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004732 }
4733
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004734 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004735 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004736 freq_in_use = 1;
4737 }
4738
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004739 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004740 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4741 freq);
4742 res = -2;
4743 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004744 }
4745 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4746 "requested channel (%u MHz)", freq);
4747 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004748 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004749 }
4750
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004751 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004752
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004753 /* We have a candidate frequency to use */
4754 if (best_freq > 0) {
4755 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004756 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004757 best_freq);
4758 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004759 } else {
4760 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 -07004761 best_freq);
4762 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004763 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004764 } else if (num_unused > 0) {
4765 wpa_printf(MSG_DEBUG,
4766 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4767 *force_freq = 0;
4768 } else {
4769 wpa_printf(MSG_DEBUG,
4770 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4771 res = -2;
4772 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004773 }
4774
4775exit_ok:
4776 res = 0;
4777exit_free:
4778 os_free(freqs);
4779 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004780}
4781
4782
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783/**
4784 * wpas_p2p_connect - Request P2P Group Formation to be started
4785 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4786 * @peer_addr: Address of the peer P2P Device
4787 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4788 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004789 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790 * @join: Whether to join an existing group (as a client) instead of starting
4791 * Group Owner negotiation; @peer_addr is BSSID in that case
4792 * @auth: Whether to only authorize the connection instead of doing that and
4793 * initiating Group Owner negotiation
4794 * @go_intent: GO Intent or -1 to use default
4795 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004796 * @persistent_id: Persistent group credentials to use for forcing GO
4797 * parameters or -1 to generate new values (SSID/passphrase)
4798 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4799 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004800 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004801 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4803 * failure, -2 on failure due to channel not currently available,
4804 * -3 if forced channel is not supported
4805 */
4806int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4807 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004808 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004809 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004810 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004811{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004812 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004813 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004815 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004816 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004817
4818 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4819 return -1;
4820
Dmitry Shmidt04949592012-07-19 12:16:46 -07004821 if (persistent_id >= 0) {
4822 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4823 if (ssid == NULL || ssid->disabled != 2 ||
4824 ssid->mode != WPAS_MODE_P2P_GO)
4825 return -1;
4826 }
4827
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004828 os_free(wpa_s->global->add_psk);
4829 wpa_s->global->add_psk = NULL;
4830
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004831 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004832 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004834 if (go_intent < 0)
4835 go_intent = wpa_s->conf->p2p_go_intent;
4836
4837 if (!auth)
4838 wpa_s->p2p_long_listen = 0;
4839
4840 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004841 wpa_s->p2p_persistent_group = !!persistent_group;
4842 wpa_s->p2p_persistent_id = persistent_id;
4843 wpa_s->p2p_go_intent = go_intent;
4844 wpa_s->p2p_connect_freq = freq;
4845 wpa_s->p2p_fallback_to_go_neg = 0;
4846 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004847 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004848 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004849
4850 if (pin)
4851 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4852 else if (wps_method == WPS_PIN_DISPLAY) {
4853 ret = wps_generate_pin();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004854 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4855 "%08d", ret);
4856 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
4857 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4859 wpa_s->p2p_pin);
4860 } else
4861 wpa_s->p2p_pin[0] = '\0';
4862
Dmitry Shmidt04949592012-07-19 12:16:46 -07004863 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4865 if (auth) {
4866 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4867 "connect a running group from " MACSTR,
4868 MAC2STR(peer_addr));
4869 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4870 return ret;
4871 }
4872 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4873 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4874 iface_addr) < 0) {
4875 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4876 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4877 dev_addr);
4878 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004879 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004880 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004881 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4882 "%ld.%06ld",
4883 wpa_s->p2p_auto_started.sec,
4884 wpa_s->p2p_auto_started.usec);
4885 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004886 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004887 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004888 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004889 return -1;
4890 return ret;
4891 }
4892
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004893 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4894 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004895 if (res)
4896 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004897 wpas_p2p_set_own_freq_preference(wpa_s,
4898 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004899
4900 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4901
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004902 if (wpa_s->create_p2p_iface) {
4903 /* Prepare to add a new interface for the group */
4904 iftype = WPA_IF_P2P_GROUP;
4905 if (go_intent == 15)
4906 iftype = WPA_IF_P2P_GO;
4907 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4908 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4909 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004910 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004913 if_addr = wpa_s->pending_interface_addr;
4914 } else
4915 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916
4917 if (auth) {
4918 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004919 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004920 force_freq, persistent_group, ssid,
4921 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 return -1;
4923 return ret;
4924 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004925
4926 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4927 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004928 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004929 if (wpa_s->create_p2p_iface)
4930 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004931 return -1;
4932 }
4933 return ret;
4934}
4935
4936
4937/**
4938 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4939 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4940 * @freq: Frequency of the channel in MHz
4941 * @duration: Duration of the stay on the channel in milliseconds
4942 *
4943 * This callback is called when the driver indicates that it has started the
4944 * requested remain-on-channel duration.
4945 */
4946void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4947 unsigned int freq, unsigned int duration)
4948{
4949 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4950 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004951 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)",
4952 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4953 wpa_s->roc_waiting_drv_freq, freq, duration);
4954 if (wpa_s->off_channel_freq &&
4955 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4957 wpa_s->pending_listen_duration);
4958 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004959 } else {
4960 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4961 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4962 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004963 }
4964}
4965
4966
Jithu Jance57cea1a2014-08-20 10:22:04 -07004967int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968{
4969 /* Limit maximum Listen state time based on driver limitation. */
4970 if (timeout > wpa_s->max_remain_on_chan)
4971 timeout = wpa_s->max_remain_on_chan;
4972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 return p2p_listen(wpa_s->global->p2p, timeout);
4974}
4975
4976
4977/**
4978 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4979 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4980 * @freq: Frequency of the channel in MHz
4981 *
4982 * This callback is called when the driver indicates that a remain-on-channel
4983 * operation has been completed, i.e., the duration on the requested channel
4984 * has timed out.
4985 */
4986void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4987 unsigned int freq)
4988{
4989 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4990 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004991 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004992 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4994 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07004995 if (wpa_s->p2p_long_listen > 0)
4996 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4998 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004999 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005000 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001 if (wpa_s->p2p_long_listen > 0) {
5002 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5003 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005004 } else {
5005 /*
5006 * When listen duration is over, stop listen & update p2p_state
5007 * to IDLE.
5008 */
5009 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 }
5011}
5012
5013
5014/**
5015 * wpas_p2p_group_remove - Remove a P2P group
5016 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5017 * @ifname: Network interface name of the group interface or "*" to remove all
5018 * groups
5019 * Returns: 0 on success, -1 on failure
5020 *
5021 * This function is used to remove a P2P group. This can be used to disconnect
5022 * from a group in which the local end is a P2P Client or to end a P2P Group in
5023 * case the local end is the Group Owner. If a virtual network interface was
5024 * created for this group, that interface will be removed. Otherwise, only the
5025 * configured P2P group network will be removed from the interface.
5026 */
5027int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5028{
5029 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005030 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005031
5032 if (os_strcmp(ifname, "*") == 0) {
5033 struct wpa_supplicant *prev;
5034 wpa_s = global->ifaces;
5035 while (wpa_s) {
5036 prev = wpa_s;
5037 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005038 if (prev->p2p_group_interface !=
5039 NOT_P2P_GROUP_INTERFACE ||
5040 (prev->current_ssid &&
5041 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005042 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 }
5044 return 0;
5045 }
5046
5047 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5048 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5049 break;
5050 }
5051
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005052 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053}
5054
5055
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005056static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5057{
5058 unsigned int r;
5059
5060 if (freq == 2) {
5061 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5062 "band");
5063 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005064 p2p_supported_freq_go(wpa_s->global->p2p,
5065 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005066 freq = wpa_s->best_24_freq;
5067 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5068 "channel: %d MHz", freq);
5069 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005070 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5071 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005072 freq = 2412 + (r % 3) * 25;
5073 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5074 "channel: %d MHz", freq);
5075 }
5076 }
5077
5078 if (freq == 5) {
5079 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5080 "band");
5081 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005082 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005083 wpa_s->best_5_freq)) {
5084 freq = wpa_s->best_5_freq;
5085 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5086 "channel: %d MHz", freq);
5087 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005088 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5089 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005090 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005091 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005092 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5093 "5 GHz channel for P2P group");
5094 return -1;
5095 }
5096 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5097 "channel: %d MHz", freq);
5098 }
5099 }
5100
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005101 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005102 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5103 ieee80211_is_dfs(freq)) {
5104 /*
5105 * If freq is a DFS channel and DFS is offloaded to the
5106 * driver, allow P2P GO to use it.
5107 */
5108 wpa_printf(MSG_DEBUG, "P2P: "
5109 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5110 __func__, freq);
5111 return freq;
5112 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005113 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5114 "(%u MHz) is not supported for P2P uses",
5115 freq);
5116 return -1;
5117 }
5118
5119 return freq;
5120}
5121
5122
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005123static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5124 struct p2p_go_neg_results *params,
5125 const struct p2p_channels *channels)
5126{
5127 unsigned int i, r;
5128
5129 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005130 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5131 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005132
5133 for (i = 0; i < 3; i++) {
5134 params->freq = 2412 + ((r + i) % 3) * 25;
5135 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005136 freq_included(channels, params->freq) &&
5137 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005138 goto out;
5139 }
5140
5141 /* try all channels in reg. class 81 */
5142 for (i = 0; i < 11; i++) {
5143 params->freq = 2412 + i * 5;
5144 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005145 freq_included(channels, params->freq) &&
5146 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005147 goto out;
5148 }
5149
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005150 /* try all channels in operating class 115 */
5151 for (i = 0; i < 4; i++) {
5152 params->freq = 5180 + i * 20;
5153 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5154 freq_included(channels, params->freq) &&
5155 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5156 goto out;
5157 }
5158
5159 /* try all channels in operating class 124 */
5160 for (i = 0; i < 4; i++) {
5161 params->freq = 5745 + i * 20;
5162 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5163 freq_included(channels, params->freq) &&
5164 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5165 goto out;
5166 }
5167
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005168 /* try social channel class 180 channel 2 */
5169 params->freq = 58320 + 1 * 2160;
5170 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5171 freq_included(channels, params->freq) &&
5172 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5173 goto out;
5174
5175 /* try all channels in reg. class 180 */
5176 for (i = 0; i < 4; i++) {
5177 params->freq = 58320 + i * 2160;
5178 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5179 freq_included(channels, params->freq) &&
5180 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5181 goto out;
5182 }
5183
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005184 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005185 return -1;
5186out:
5187 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5188 params->freq);
5189 return 0;
5190}
5191
5192
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005193static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5194 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005195 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005196 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005197{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005198 struct wpa_used_freq_data *freqs;
5199 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005200 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005201
5202 os_memset(params, 0, sizeof(*params));
5203 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005204 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005205 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005206 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005207 if (!freq_included(channels, freq)) {
5208 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5209 "accepted", freq);
5210 return -1;
5211 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005212 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5213 "frequency %d MHz", freq);
5214 params->freq = freq;
5215 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5216 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005217 wpa_s->conf->p2p_oper_channel <= 11 &&
5218 freq_included(channels,
5219 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005220 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5221 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5222 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005223 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5224 wpa_s->conf->p2p_oper_reg_class == 116 ||
5225 wpa_s->conf->p2p_oper_reg_class == 117 ||
5226 wpa_s->conf->p2p_oper_reg_class == 124 ||
5227 wpa_s->conf->p2p_oper_reg_class == 126 ||
5228 wpa_s->conf->p2p_oper_reg_class == 127) &&
5229 freq_included(channels,
5230 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5232 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5233 "frequency %d MHz", params->freq);
5234 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5235 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005236 p2p_supported_freq_go(wpa_s->global->p2p,
5237 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005238 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239 params->freq = wpa_s->best_overall_freq;
5240 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5241 "channel %d MHz", params->freq);
5242 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5243 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005244 p2p_supported_freq_go(wpa_s->global->p2p,
5245 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005246 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247 params->freq = wpa_s->best_24_freq;
5248 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5249 "channel %d MHz", params->freq);
5250 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5251 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005252 p2p_supported_freq_go(wpa_s->global->p2p,
5253 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005254 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 params->freq = wpa_s->best_5_freq;
5256 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5257 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005258 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5259 channels))) {
5260 params->freq = pref_freq;
5261 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5262 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005263 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005264 /* no preference, select some channel */
5265 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005266 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267 }
5268
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005269 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5270 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005271 if (!freqs)
5272 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005274 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005275 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005276
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005277 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5278
5279 /* First try the best used frequency if possible */
5280 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5281 params->freq = cand_freq;
5282 } else if (!freq) {
5283 /* Try any of the used frequencies */
5284 for (i = 0; i < num; i++) {
5285 if (freq_included(channels, freqs[i].freq)) {
5286 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5287 freqs[i].freq);
5288 params->freq = freqs[i].freq;
5289 break;
5290 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005291 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005292
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005293 if (i == num) {
5294 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005295 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005296 os_free(freqs);
5297 return -1;
5298 } else {
5299 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5300 }
5301 }
5302 } else {
5303 for (i = 0; i < num; i++) {
5304 if (freqs[i].freq == freq)
5305 break;
5306 }
5307
5308 if (i == num) {
5309 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5310 if (freq)
5311 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5312 os_free(freqs);
5313 return -1;
5314 } else {
5315 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5316 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005317 }
5318 }
5319
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005320 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005321 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005322}
5323
5324
5325static struct wpa_supplicant *
5326wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5327 int go)
5328{
5329 struct wpa_supplicant *group_wpa_s;
5330
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005331 if (!wpas_p2p_create_iface(wpa_s)) {
5332 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5333 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005334 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337
5338 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005339 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005340 wpa_msg_global(wpa_s, MSG_ERROR,
5341 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005342 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005344 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5345 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005346 wpa_msg_global(wpa_s, MSG_ERROR,
5347 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005348 wpas_p2p_remove_pending_group_interface(wpa_s);
5349 return NULL;
5350 }
5351
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005352 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5353 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005354 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005355 return group_wpa_s;
5356}
5357
5358
5359/**
5360 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5361 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5362 * @persistent_group: Whether to create a persistent group
5363 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005364 * @ht40: Start GO with 40 MHz channel width
5365 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005366 * Returns: 0 on success, -1 on failure
5367 *
5368 * This function creates a new P2P group with the local end as the Group Owner,
5369 * i.e., without using Group Owner Negotiation.
5370 */
5371int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005372 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373{
5374 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375
5376 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5377 return -1;
5378
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005379 os_free(wpa_s->global->add_psk);
5380 wpa_s->global->add_psk = NULL;
5381
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005382 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005383 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005384 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005385
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005386 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5387 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005388 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005390 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005391 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005392 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005393 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005394 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5395 ieee80211_is_dfs(params.freq)) {
5396 /*
5397 * If freq is a DFS channel and DFS is offloaded to the
5398 * driver, allow P2P GO to use it.
5399 */
5400 wpa_printf(MSG_DEBUG,
5401 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5402 __func__, params.freq);
5403 } else {
5404 wpa_printf(MSG_DEBUG,
5405 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5406 params.freq);
5407 return -1;
5408 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005409 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005410 p2p_go_params(wpa_s->global->p2p, &params);
5411 params.persistent_group = persistent_group;
5412
5413 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5414 if (wpa_s == NULL)
5415 return -1;
5416 wpas_start_wps_go(wpa_s, &params, 0);
5417
5418 return 0;
5419}
5420
5421
5422static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005423 struct wpa_ssid *params, int addr_allocated,
5424 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425{
5426 struct wpa_ssid *ssid;
5427
5428 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5429 if (wpa_s == NULL)
5430 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005431 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432
5433 wpa_supplicant_ap_deinit(wpa_s);
5434
5435 ssid = wpa_config_add_network(wpa_s->conf);
5436 if (ssid == NULL)
5437 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438 wpa_config_set_network_defaults(ssid);
5439 ssid->temporary = 1;
5440 ssid->proto = WPA_PROTO_RSN;
5441 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5442 ssid->group_cipher = WPA_CIPHER_CCMP;
5443 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5444 ssid->ssid = os_malloc(params->ssid_len);
5445 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005446 wpa_config_remove_network(wpa_s->conf, ssid->id);
5447 return -1;
5448 }
5449 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5450 ssid->ssid_len = params->ssid_len;
5451 ssid->p2p_group = 1;
5452 ssid->export_keys = 1;
5453 if (params->psk_set) {
5454 os_memcpy(ssid->psk, params->psk, 32);
5455 ssid->psk_set = 1;
5456 }
5457 if (params->passphrase)
5458 ssid->passphrase = os_strdup(params->passphrase);
5459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005460 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005461 wpa_s->p2p_in_invitation = 1;
5462 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005463
Dmitry Shmidt15907092014-03-25 10:42:57 -07005464 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5465 NULL);
5466 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5467 wpas_p2p_group_formation_timeout,
5468 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005469 wpa_supplicant_select_network(wpa_s, ssid);
5470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005471 return 0;
5472}
5473
5474
5475int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5476 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005477 int force_freq, int neg_freq, int ht40,
5478 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005479 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480{
5481 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005482 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005483
5484 if (ssid->disabled != 2 || ssid->ssid == NULL)
5485 return -1;
5486
5487 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5488 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5489 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5490 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07005491 if (go == 0 &&
5492 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5493 wpa_s->parent, NULL)) {
5494 /*
5495 * This can happen if Invitation Response frame was lost
5496 * and the peer (GO of a persistent group) tries to
5497 * invite us again. Reschedule the timeout to avoid
5498 * terminating the wait for the connection too early
5499 * since we now know that the peer is still trying to
5500 * invite us instead of having already started the GO.
5501 */
5502 wpa_printf(MSG_DEBUG,
5503 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5504 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5505 wpas_p2p_group_formation_timeout,
5506 wpa_s->parent, NULL);
5507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508 return 0;
5509 }
5510
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005511 os_free(wpa_s->global->add_psk);
5512 wpa_s->global->add_psk = NULL;
5513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005515 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516
Dmitry Shmidt04949592012-07-19 12:16:46 -07005517 wpa_s->p2p_fallback_to_go_neg = 0;
5518
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005519 if (ssid->mode == WPAS_MODE_P2P_GO) {
5520 if (force_freq > 0) {
5521 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5522 if (freq < 0)
5523 return -1;
5524 } else {
5525 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5526 if (freq < 0 ||
5527 (freq > 0 && !freq_included(channels, freq)))
5528 freq = 0;
5529 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005530 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005531 freq = neg_freq;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005532 if (freq <= 0 || !freq_included(channels, freq)) {
5533 struct os_reltime now;
5534 struct wpa_bss *bss =
5535 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005536
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005537 os_get_reltime(&now);
5538 if (bss &&
5539 !os_reltime_expired(&now, &bss->last_update, 5) &&
5540 freq_included(channels, bss->freq))
5541 freq = bss->freq;
5542 else
5543 freq = 0;
5544 }
5545
Dmitry Shmidt15907092014-03-25 10:42:57 -07005546 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005547 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07005548 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005549 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005550
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005551 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005552 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005553
5554 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005555 params.psk_set = ssid->psk_set;
5556 if (params.psk_set)
5557 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005558 if (ssid->passphrase) {
5559 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5560 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5561 "persistent group");
5562 return -1;
5563 }
5564 os_strlcpy(params.passphrase, ssid->passphrase,
5565 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005566 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5568 params.ssid_len = ssid->ssid_len;
5569 params.persistent_group = 1;
5570
5571 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5572 if (wpa_s == NULL)
5573 return -1;
5574
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005575 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5576
Dmitry Shmidt56052862013-10-04 10:23:25 -07005577 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 wpas_start_wps_go(wpa_s, &params, 0);
5579
5580 return 0;
5581}
5582
5583
5584static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5585 struct wpabuf *proberesp_ies)
5586{
5587 struct wpa_supplicant *wpa_s = ctx;
5588 if (wpa_s->ap_iface) {
5589 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005590 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5591 wpabuf_free(beacon_ies);
5592 wpabuf_free(proberesp_ies);
5593 return;
5594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595 if (beacon_ies) {
5596 wpabuf_free(hapd->p2p_beacon_ie);
5597 hapd->p2p_beacon_ie = beacon_ies;
5598 }
5599 wpabuf_free(hapd->p2p_probe_resp_ie);
5600 hapd->p2p_probe_resp_ie = proberesp_ies;
5601 } else {
5602 wpabuf_free(beacon_ies);
5603 wpabuf_free(proberesp_ies);
5604 }
5605 wpa_supplicant_ap_update_beacon(wpa_s);
5606}
5607
5608
5609static void wpas_p2p_idle_update(void *ctx, int idle)
5610{
5611 struct wpa_supplicant *wpa_s = ctx;
5612 if (!wpa_s->ap_iface)
5613 return;
5614 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005615 if (idle) {
5616 if (wpa_s->global->p2p_fail_on_wps_complete &&
5617 wpa_s->p2p_in_provisioning) {
5618 wpas_p2p_grpform_fail_after_wps(wpa_s);
5619 return;
5620 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005622 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5624}
5625
5626
5627struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005628 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629{
5630 struct p2p_group *group;
5631 struct p2p_group_config *cfg;
5632
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005633 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5634 return NULL;
5635
5636 cfg = os_zalloc(sizeof(*cfg));
5637 if (cfg == NULL)
5638 return NULL;
5639
Dmitry Shmidt04949592012-07-19 12:16:46 -07005640 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005641 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005642 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005644 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5645 if (wpa_s->max_stations &&
5646 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5647 cfg->max_clients = wpa_s->max_stations;
5648 else
5649 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005650 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5651 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005652 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653 cfg->cb_ctx = wpa_s;
5654 cfg->ie_update = wpas_p2p_ie_update;
5655 cfg->idle_update = wpas_p2p_idle_update;
5656
5657 group = p2p_group_init(wpa_s->global->p2p, cfg);
5658 if (group == NULL)
5659 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005660 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661 p2p_group_notif_formation_done(group);
5662 wpa_s->p2p_group = group;
5663 return group;
5664}
5665
5666
5667void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5668 int registrar)
5669{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005670 struct wpa_ssid *ssid = wpa_s->current_ssid;
5671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 if (!wpa_s->p2p_in_provisioning) {
5673 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5674 "provisioning not in progress");
5675 return;
5676 }
5677
Dmitry Shmidt04949592012-07-19 12:16:46 -07005678 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5679 u8 go_dev_addr[ETH_ALEN];
5680 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5681 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5682 ssid->ssid_len);
5683 /* Clear any stored provisioning info */
5684 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5685 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5688 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005689 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005690 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5691 /*
5692 * Use a separate timeout for initial data connection to
5693 * complete to allow the group to be removed automatically if
5694 * something goes wrong in this step before the P2P group idle
5695 * timeout mechanism is taken into use.
5696 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005697 wpa_dbg(wpa_s, MSG_DEBUG,
5698 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5699 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005700 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5701 wpas_p2p_group_formation_timeout,
5702 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005703 } else if (ssid) {
5704 /*
5705 * Use a separate timeout for initial data connection to
5706 * complete to allow the group to be removed automatically if
5707 * the client does not complete data connection successfully.
5708 */
5709 wpa_dbg(wpa_s, MSG_DEBUG,
5710 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5711 P2P_MAX_INITIAL_CONN_WAIT_GO);
5712 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5713 wpas_p2p_group_formation_timeout,
5714 wpa_s->parent, NULL);
5715 /*
5716 * Complete group formation on first successful data connection
5717 */
5718 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005719 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005720 if (wpa_s->global->p2p)
5721 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722 wpas_group_formation_completed(wpa_s, 1);
5723}
5724
5725
Jouni Malinen75ecf522011-06-27 15:19:46 -07005726void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5727 struct wps_event_fail *fail)
5728{
5729 if (!wpa_s->p2p_in_provisioning) {
5730 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5731 "provisioning not in progress");
5732 return;
5733 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005734
5735 if (wpa_s->go_params) {
5736 p2p_clear_provisioning_info(
5737 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005738 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005739 }
5740
Jouni Malinen75ecf522011-06-27 15:19:46 -07005741 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005742
5743 if (wpa_s == wpa_s->global->p2p_group_formation) {
5744 /*
5745 * Allow some time for the failed WPS negotiation exchange to
5746 * complete, but remove the group since group formation cannot
5747 * succeed after provisioning failure.
5748 */
5749 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5750 wpa_s->global->p2p_fail_on_wps_complete = 1;
5751 eloop_deplete_timeout(0, 50000,
5752 wpas_p2p_group_formation_timeout,
5753 wpa_s->parent, NULL);
5754 }
5755}
5756
5757
5758int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5759{
5760 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5761 !wpa_s->p2p_in_provisioning)
5762 return 0;
5763
5764 wpas_p2p_grpform_fail_after_wps(wpa_s);
5765
5766 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005767}
5768
5769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005771 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005772 enum wpas_p2p_prov_disc_use use,
5773 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774{
5775 u16 config_methods;
5776
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005777 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005778 wpa_s->p2p_fallback_to_go_neg = 0;
5779 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005780 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
5781 p2ps_prov->conncap = p2ps_group_capability(
5782 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
5783 wpa_printf(MSG_DEBUG,
5784 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
5785 __func__, p2ps_prov->conncap,
5786 p2ps_prov->adv_id, p2ps_prov->conncap,
5787 p2ps_prov->status, p2ps_prov->info);
5788
5789 config_methods = 0;
5790 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005792 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005794 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5795 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005796 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005797 else {
5798 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005799 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005801 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802
Dmitry Shmidt04949592012-07-19 12:16:46 -07005803 if (use == WPAS_P2P_PD_AUTO) {
5804 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5805 wpa_s->pending_pd_config_methods = config_methods;
5806 wpa_s->p2p_auto_pd = 1;
5807 wpa_s->p2p_auto_join = 0;
5808 wpa_s->pending_pd_before_join = 0;
5809 wpa_s->auto_pd_scan_retry = 0;
5810 wpas_p2p_stop_find(wpa_s);
5811 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005812 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005813 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5814 wpa_s->p2p_auto_started.sec,
5815 wpa_s->p2p_auto_started.usec);
5816 wpas_p2p_join_scan(wpa_s, NULL);
5817 return 0;
5818 }
5819
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005820 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
5821 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005824
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005825 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005826 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005827 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005828}
5829
5830
5831int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5832 char *end)
5833{
5834 return p2p_scan_result_text(ies, ies_len, buf, end);
5835}
5836
5837
5838static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5839{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005840 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005841 return;
5842
Dmitry Shmidt03658832014-08-13 11:03:49 -07005843 wpas_p2p_action_tx_clear(wpa_s);
5844
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005845 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5846 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005847 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005848}
5849
5850
5851int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5852 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005853 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005854 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005855 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856{
5857 wpas_p2p_clear_pending_action_tx(wpa_s);
5858 wpa_s->p2p_long_listen = 0;
5859
Dmitry Shmidt04949592012-07-19 12:16:46 -07005860 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5861 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862 return -1;
5863
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005864 wpa_supplicant_cancel_sched_scan(wpa_s);
5865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005866 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005867 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005868 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869}
5870
5871
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005872static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5873 struct wpa_scan_results *scan_res)
5874{
5875 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5876
5877 if (wpa_s->p2p_scan_work) {
5878 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5879 wpa_s->p2p_scan_work = NULL;
5880 radio_work_done(work);
5881 }
5882
5883 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5884 return;
5885
5886 /*
5887 * Indicate that results have been processed so that the P2P module can
5888 * continue pending tasks.
5889 */
5890 p2p_scan_res_handled(wpa_s->global->p2p);
5891}
5892
5893
5894static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005895{
5896 wpas_p2p_clear_pending_action_tx(wpa_s);
5897 wpa_s->p2p_long_listen = 0;
5898 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5899 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005900
5901 if (wpa_s->global->p2p)
5902 p2p_stop_find(wpa_s->global->p2p);
5903
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005904 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5905 wpa_printf(MSG_DEBUG,
5906 "P2P: Do not consider the scan results after stop_find");
5907 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5908 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005909}
5910
5911
5912void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5913{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005914 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005915 if (!wpa_s->global->pending_group_iface_for_p2ps)
5916 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917}
5918
5919
5920static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5921{
5922 struct wpa_supplicant *wpa_s = eloop_ctx;
5923 wpa_s->p2p_long_listen = 0;
5924}
5925
5926
5927int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5928{
5929 int res;
5930
5931 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5932 return -1;
5933
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005934 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 wpas_p2p_clear_pending_action_tx(wpa_s);
5936
5937 if (timeout == 0) {
5938 /*
5939 * This is a request for unlimited Listen state. However, at
5940 * least for now, this is mapped to a Listen state for one
5941 * hour.
5942 */
5943 timeout = 3600;
5944 }
5945 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5946 wpa_s->p2p_long_listen = 0;
5947
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005948 /*
5949 * Stop previous find/listen operation to avoid trying to request a new
5950 * remain-on-channel operation while the driver is still running the
5951 * previous one.
5952 */
5953 if (wpa_s->global->p2p)
5954 p2p_stop_find(wpa_s->global->p2p);
5955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5957 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5958 wpa_s->p2p_long_listen = timeout * 1000;
5959 eloop_register_timeout(timeout, 0,
5960 wpas_p2p_long_listen_timeout,
5961 wpa_s, NULL);
5962 }
5963
5964 return res;
5965}
5966
5967
5968int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5969 u8 *buf, size_t len, int p2p_group)
5970{
5971 struct wpabuf *p2p_ie;
5972 int ret;
5973
5974 if (wpa_s->global->p2p_disabled)
5975 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07005976 /*
5977 * Advertize mandatory cross connection capability even on
5978 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
5979 */
5980 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005981 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005982 if (wpa_s->global->p2p == NULL)
5983 return -1;
5984 if (bss == NULL)
5985 return -1;
5986
5987 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5988 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5989 p2p_group, p2p_ie);
5990 wpabuf_free(p2p_ie);
5991
5992 return ret;
5993}
5994
5995
5996int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005997 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005998 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005999{
6000 if (wpa_s->global->p2p_disabled)
6001 return 0;
6002 if (wpa_s->global->p2p == NULL)
6003 return 0;
6004
Dmitry Shmidt04949592012-07-19 12:16:46 -07006005 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6006 ie, ie_len)) {
6007 case P2P_PREQ_NOT_P2P:
6008 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6009 ssi_signal);
6010 /* fall through */
6011 case P2P_PREQ_MALFORMED:
6012 case P2P_PREQ_NOT_LISTEN:
6013 case P2P_PREQ_NOT_PROCESSED:
6014 default: /* make gcc happy */
6015 return 0;
6016 case P2P_PREQ_PROCESSED:
6017 return 1;
6018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006019}
6020
6021
6022void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6023 const u8 *sa, const u8 *bssid,
6024 u8 category, const u8 *data, size_t len, int freq)
6025{
6026 if (wpa_s->global->p2p_disabled)
6027 return;
6028 if (wpa_s->global->p2p == NULL)
6029 return;
6030
6031 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6032 freq);
6033}
6034
6035
6036void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6037{
6038 if (wpa_s->global->p2p_disabled)
6039 return;
6040 if (wpa_s->global->p2p == NULL)
6041 return;
6042
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006043 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006044}
6045
6046
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006047static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006048{
6049 p2p_group_deinit(wpa_s->p2p_group);
6050 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006051
6052 wpa_s->ap_configured_cb = NULL;
6053 wpa_s->ap_configured_cb_ctx = NULL;
6054 wpa_s->ap_configured_cb_data = NULL;
6055 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056}
6057
6058
6059int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6060{
6061 wpa_s->p2p_long_listen = 0;
6062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6064 return -1;
6065
6066 return p2p_reject(wpa_s->global->p2p, addr);
6067}
6068
6069
6070/* Invite to reinvoke a persistent group */
6071int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006072 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006073 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006074{
6075 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006076 u8 *bssid = NULL;
6077 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006078 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006079 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006080
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006081 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006082 if (peer_addr)
6083 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6084 else
6085 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006086
Jouni Malinen31be0a42012-08-31 21:20:51 +03006087 wpa_s->p2p_persistent_go_freq = freq;
6088 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089 if (ssid->mode == WPAS_MODE_P2P_GO) {
6090 role = P2P_INVITE_ROLE_GO;
6091 if (peer_addr == NULL) {
6092 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6093 "address in invitation command");
6094 return -1;
6095 }
6096 if (wpas_p2p_create_iface(wpa_s)) {
6097 if (wpas_p2p_add_group_interface(wpa_s,
6098 WPA_IF_P2P_GO) < 0) {
6099 wpa_printf(MSG_ERROR, "P2P: Failed to "
6100 "allocate a new interface for the "
6101 "group");
6102 return -1;
6103 }
6104 bssid = wpa_s->pending_interface_addr;
6105 } else
6106 bssid = wpa_s->own_addr;
6107 } else {
6108 role = P2P_INVITE_ROLE_CLIENT;
6109 peer_addr = ssid->bssid;
6110 }
6111 wpa_s->pending_invite_ssid_id = ssid->id;
6112
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006113 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6114 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006115 if (res)
6116 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006117
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006118 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6119 return -1;
6120
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006121 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6122 no_pref_freq_given && pref_freq > 0 &&
6123 wpa_s->num_multichan_concurrent > 1 &&
6124 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6125 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6126 pref_freq);
6127 pref_freq = 0;
6128 }
6129
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006130 /*
6131 * Stop any find/listen operations before invitation and possibly
6132 * connection establishment.
6133 */
6134 wpas_p2p_stop_find_oper(wpa_s);
6135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006136 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006137 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006138 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006139}
6140
6141
6142/* Invite to join an active group */
6143int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6144 const u8 *peer_addr, const u8 *go_dev_addr)
6145{
6146 struct wpa_global *global = wpa_s->global;
6147 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006148 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006149 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006150 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006151 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006152 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153
Jouni Malinen31be0a42012-08-31 21:20:51 +03006154 wpa_s->p2p_persistent_go_freq = 0;
6155 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006156 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006158 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6159 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6160 break;
6161 }
6162 if (wpa_s == NULL) {
6163 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6164 return -1;
6165 }
6166
6167 ssid = wpa_s->current_ssid;
6168 if (ssid == NULL) {
6169 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6170 "invitation");
6171 return -1;
6172 }
6173
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006174 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006175 persistent = ssid->p2p_persistent_group &&
6176 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6177 ssid->ssid, ssid->ssid_len);
6178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006179 if (ssid->mode == WPAS_MODE_P2P_GO) {
6180 role = P2P_INVITE_ROLE_ACTIVE_GO;
6181 bssid = wpa_s->own_addr;
6182 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006183 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006184 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006185 } else {
6186 role = P2P_INVITE_ROLE_CLIENT;
6187 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6188 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6189 "invite to current group");
6190 return -1;
6191 }
6192 bssid = wpa_s->bssid;
6193 if (go_dev_addr == NULL &&
6194 !is_zero_ether_addr(wpa_s->go_dev_addr))
6195 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006196 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6197 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 }
6199 wpa_s->parent->pending_invite_ssid_id = -1;
6200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006201 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6202 return -1;
6203
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006204 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6205 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006206 if (res)
6207 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006208 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006210 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006211 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006212 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006213}
6214
6215
6216void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6217{
6218 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006219 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006220 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006222 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006223 u8 ip[3 * 4];
6224 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006225
Dmitry Shmidt04949592012-07-19 12:16:46 -07006226 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6227 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6228 wpa_s->parent, NULL);
6229 }
6230
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006231 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006232 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006233
6234 wpa_s->show_group_started = 0;
6235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 os_memset(go_dev_addr, 0, ETH_ALEN);
6237 if (ssid->bssid_set)
6238 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6239 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6240 ssid->ssid_len);
6241 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6242
6243 if (wpa_s->global->p2p_group_formation == wpa_s)
6244 wpa_s->global->p2p_group_formation = NULL;
6245
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006246 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6247 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006248
6249 ip_addr[0] = '\0';
6250 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006251 int res;
6252
6253 res = os_snprintf(ip_addr, sizeof(ip_addr),
6254 " ip_addr=%u.%u.%u.%u "
6255 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6256 ip[0], ip[1], ip[2], ip[3],
6257 ip[4], ip[5], ip[6], ip[7],
6258 ip[8], ip[9], ip[10], ip[11]);
6259 if (os_snprintf_error(sizeof(ip_addr), res))
6260 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006261 }
6262
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006263 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6264 ssid->passphrase == NULL && ssid->psk_set ?
6265 ssid->psk : NULL,
6266 ssid->passphrase, go_dev_addr, persistent,
6267 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268
6269 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006270 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6271 ssid, go_dev_addr);
6272 if (network_id < 0)
6273 network_id = ssid->id;
6274 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006275}
6276
6277
6278int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6279 u32 interval1, u32 duration2, u32 interval2)
6280{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006281 int ret;
6282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006283 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6284 return -1;
6285
6286 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6287 wpa_s->current_ssid == NULL ||
6288 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6289 return -1;
6290
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006291 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6292 wpa_s->own_addr, wpa_s->assoc_freq,
6293 duration1, interval1, duration2, interval2);
6294 if (ret == 0)
6295 wpa_s->waiting_presence_resp = 1;
6296
6297 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006298}
6299
6300
6301int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6302 unsigned int interval)
6303{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6305 return -1;
6306
6307 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6308}
6309
6310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006311static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6312{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006313 if (wpa_s->current_ssid == NULL) {
6314 /*
6315 * current_ssid can be cleared when P2P client interface gets
6316 * disconnected, so assume this interface was used as P2P
6317 * client.
6318 */
6319 return 1;
6320 }
6321 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006322 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6323}
6324
6325
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006326static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6327{
6328 struct wpa_supplicant *wpa_s = eloop_ctx;
6329
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006330 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006331 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6332 "disabled");
6333 return;
6334 }
6335
Dmitry Shmidt04949592012-07-19 12:16:46 -07006336 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6337 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006338 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006339}
6340
6341
6342static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6343{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006344 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006345
Dmitry Shmidt04949592012-07-19 12:16:46 -07006346 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6347 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6350 return;
6351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006352 timeout = wpa_s->conf->p2p_group_idle;
6353 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6354 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6355 timeout = P2P_MAX_CLIENT_IDLE;
6356
6357 if (timeout == 0)
6358 return;
6359
Dmitry Shmidt04949592012-07-19 12:16:46 -07006360 if (timeout < 0) {
6361 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6362 timeout = 0; /* special client mode no-timeout */
6363 else
6364 return;
6365 }
6366
6367 if (wpa_s->p2p_in_provisioning) {
6368 /*
6369 * Use the normal group formation timeout during the
6370 * provisioning phase to avoid terminating this process too
6371 * early due to group idle timeout.
6372 */
6373 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6374 "during provisioning");
6375 return;
6376 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306377
Dmitry Shmidt04949592012-07-19 12:16:46 -07006378 if (wpa_s->show_group_started) {
6379 /*
6380 * Use the normal group formation timeout between the end of
6381 * the provisioning phase and completion of 4-way handshake to
6382 * avoid terminating this process too early due to group idle
6383 * timeout.
6384 */
6385 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6386 "while waiting for initial 4-way handshake to "
6387 "complete");
6388 return;
6389 }
6390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006391 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006392 timeout);
6393 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6394 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006395}
6396
6397
Jouni Malinen2b89da82012-08-31 22:04:41 +03006398/* Returns 1 if the interface was removed */
6399int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6400 u16 reason_code, const u8 *ie, size_t ie_len,
6401 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006402{
6403 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006404 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006405
Dmitry Shmidt04949592012-07-19 12:16:46 -07006406 if (!locally_generated)
6407 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6408 ie_len);
6409
6410 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6411 wpa_s->current_ssid &&
6412 wpa_s->current_ssid->p2p_group &&
6413 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6414 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6415 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006416 if (wpas_p2p_group_delete(wpa_s,
6417 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6418 > 0)
6419 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006420 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006421
6422 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006423}
6424
6425
6426void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006427 u16 reason_code, const u8 *ie, size_t ie_len,
6428 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429{
6430 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6431 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432
Dmitry Shmidt04949592012-07-19 12:16:46 -07006433 if (!locally_generated)
6434 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6435 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006436}
6437
6438
6439void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6440{
6441 struct p2p_data *p2p = wpa_s->global->p2p;
6442
6443 if (p2p == NULL)
6444 return;
6445
6446 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6447 return;
6448
6449 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6450 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6451
6452 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6453 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6454
6455 if (wpa_s->wps &&
6456 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6457 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6458
6459 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6460 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6461
6462 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6463 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6464 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6465 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6466 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6467 }
6468
6469 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6470 p2p_set_sec_dev_types(p2p,
6471 (void *) wpa_s->conf->sec_device_type,
6472 wpa_s->conf->num_sec_device_types);
6473
6474 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6475 int i;
6476 p2p_remove_wps_vendor_extensions(p2p);
6477 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6478 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6479 continue;
6480 p2p_add_wps_vendor_extension(
6481 p2p, wpa_s->conf->wps_vendor_ext[i]);
6482 }
6483 }
6484
6485 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6486 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6487 char country[3];
6488 country[0] = wpa_s->conf->country[0];
6489 country[1] = wpa_s->conf->country[1];
6490 country[2] = 0x04;
6491 p2p_set_country(p2p, country);
6492 }
6493
6494 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6495 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6496 wpa_s->conf->p2p_ssid_postfix ?
6497 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6498 0);
6499 }
6500
6501 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6502 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006503
6504 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6505 u8 reg_class, channel;
6506 int ret;
6507 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006508 u8 channel_forced;
6509
Jouni Malinen75ecf522011-06-27 15:19:46 -07006510 if (wpa_s->conf->p2p_listen_reg_class &&
6511 wpa_s->conf->p2p_listen_channel) {
6512 reg_class = wpa_s->conf->p2p_listen_reg_class;
6513 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006514 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006515 } else {
6516 reg_class = 81;
6517 /*
6518 * Pick one of the social channels randomly as the
6519 * listen channel.
6520 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006521 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6522 channel = 1;
6523 else
6524 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006525 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006526 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006527 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6528 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006529 if (ret)
6530 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6531 "failed: %d", ret);
6532 }
6533 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6534 u8 op_reg_class, op_channel, cfg_op_channel;
6535 int ret = 0;
6536 unsigned int r;
6537 if (wpa_s->conf->p2p_oper_reg_class &&
6538 wpa_s->conf->p2p_oper_channel) {
6539 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6540 op_channel = wpa_s->conf->p2p_oper_channel;
6541 cfg_op_channel = 1;
6542 } else {
6543 op_reg_class = 81;
6544 /*
6545 * Use random operation channel from (1, 6, 11)
6546 *if no other preference is indicated.
6547 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006548 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6549 op_channel = 1;
6550 else
6551 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006552 cfg_op_channel = 0;
6553 }
6554 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6555 cfg_op_channel);
6556 if (ret)
6557 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6558 "failed: %d", ret);
6559 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006560
6561 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6562 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6563 wpa_s->conf->p2p_pref_chan) < 0) {
6564 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6565 "update failed");
6566 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006567
6568 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6569 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6570 "update failed");
6571 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006572 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006573
6574 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6575 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006576}
6577
6578
6579int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6580 int duration)
6581{
6582 if (!wpa_s->ap_iface)
6583 return -1;
6584 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6585 duration);
6586}
6587
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006588
6589int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6590{
6591 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6592 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006593
6594 wpa_s->global->cross_connection = enabled;
6595 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6596
6597 if (!enabled) {
6598 struct wpa_supplicant *iface;
6599
6600 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6601 {
6602 if (iface->cross_connect_enabled == 0)
6603 continue;
6604
6605 iface->cross_connect_enabled = 0;
6606 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006607 wpa_msg_global(iface->parent, MSG_INFO,
6608 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6609 iface->ifname,
6610 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611 }
6612 }
6613
6614 return 0;
6615}
6616
6617
6618static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6619{
6620 struct wpa_supplicant *iface;
6621
6622 if (!uplink->global->cross_connection)
6623 return;
6624
6625 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6626 if (!iface->cross_connect_enabled)
6627 continue;
6628 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6629 0)
6630 continue;
6631 if (iface->ap_iface == NULL)
6632 continue;
6633 if (iface->cross_connect_in_use)
6634 continue;
6635
6636 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006637 wpa_msg_global(iface->parent, MSG_INFO,
6638 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6639 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006640 }
6641}
6642
6643
6644static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6645{
6646 struct wpa_supplicant *iface;
6647
6648 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6649 if (!iface->cross_connect_enabled)
6650 continue;
6651 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6652 0)
6653 continue;
6654 if (!iface->cross_connect_in_use)
6655 continue;
6656
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006657 wpa_msg_global(iface->parent, MSG_INFO,
6658 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6659 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006660 iface->cross_connect_in_use = 0;
6661 }
6662}
6663
6664
6665void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6666{
6667 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6668 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6669 wpa_s->cross_connect_disallowed)
6670 wpas_p2p_disable_cross_connect(wpa_s);
6671 else
6672 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006673 if (!wpa_s->ap_iface &&
6674 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6675 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006676}
6677
6678
6679void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6680{
6681 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006682 if (!wpa_s->ap_iface &&
6683 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6684 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006685 wpas_p2p_set_group_idle_timeout(wpa_s);
6686}
6687
6688
6689static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6690{
6691 struct wpa_supplicant *iface;
6692
6693 if (!wpa_s->global->cross_connection)
6694 return;
6695
6696 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6697 if (iface == wpa_s)
6698 continue;
6699 if (iface->drv_flags &
6700 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6701 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006702 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6703 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006704 continue;
6705
6706 wpa_s->cross_connect_enabled = 1;
6707 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6708 sizeof(wpa_s->cross_connect_uplink));
6709 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6710 "%s to %s whenever uplink is available",
6711 wpa_s->ifname, wpa_s->cross_connect_uplink);
6712
6713 if (iface->ap_iface || iface->current_ssid == NULL ||
6714 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6715 iface->cross_connect_disallowed ||
6716 iface->wpa_state != WPA_COMPLETED)
6717 break;
6718
6719 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006720 wpa_msg_global(wpa_s->parent, MSG_INFO,
6721 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6722 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006723 break;
6724 }
6725}
6726
6727
6728int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6729{
6730 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6731 !wpa_s->p2p_in_provisioning)
6732 return 0; /* not P2P client operation */
6733
6734 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6735 "session overlap");
6736 if (wpa_s != wpa_s->parent)
6737 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006738 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006739 return 1;
6740}
6741
6742
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006743void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6744{
6745 struct wpa_supplicant *wpa_s = eloop_ctx;
6746 wpas_p2p_notif_pbc_overlap(wpa_s);
6747}
6748
6749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006750void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6751{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006752 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006753 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006754
6755 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6756 return;
6757
6758 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006759 os_memset(&cli_chan, 0, sizeof(cli_chan));
6760 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006761 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6762 "channel list");
6763 return;
6764 }
6765
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006766 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006767
6768 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6769 int freq;
6770 if (!ifs->current_ssid ||
6771 !ifs->current_ssid->p2p_group ||
6772 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6773 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6774 continue;
6775 freq = ifs->current_ssid->frequency;
6776 if (freq_included(&chan, freq)) {
6777 wpa_dbg(ifs, MSG_DEBUG,
6778 "P2P GO operating frequency %d MHz in valid range",
6779 freq);
6780 continue;
6781 }
6782
6783 wpa_dbg(ifs, MSG_DEBUG,
6784 "P2P GO operating in invalid frequency %d MHz", freq);
6785 /* TODO: Consider using CSA or removing the group within
6786 * wpa_supplicant */
6787 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6788 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006789}
6790
6791
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006792static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6793 struct wpa_scan_results *scan_res)
6794{
6795 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6796}
6797
6798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006799int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6800{
6801 struct wpa_global *global = wpa_s->global;
6802 int found = 0;
6803 const u8 *peer;
6804
6805 if (global->p2p == NULL)
6806 return -1;
6807
6808 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6809
6810 if (wpa_s->pending_interface_name[0] &&
6811 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6812 found = 1;
6813
6814 peer = p2p_get_go_neg_peer(global->p2p);
6815 if (peer) {
6816 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6817 MACSTR, MAC2STR(peer));
6818 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006819 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006820 }
6821
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006822 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6823 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6824 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6825 found = 1;
6826 }
6827
6828 if (wpa_s->pending_pd_before_join) {
6829 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6830 wpa_s->pending_pd_before_join = 0;
6831 found = 1;
6832 }
6833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006834 wpas_p2p_stop_find(wpa_s);
6835
6836 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6837 if (wpa_s == global->p2p_group_formation &&
6838 (wpa_s->p2p_in_provisioning ||
6839 wpa_s->parent->pending_interface_type ==
6840 WPA_IF_P2P_CLIENT)) {
6841 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6842 "formation found - cancelling",
6843 wpa_s->ifname);
6844 found = 1;
6845 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6846 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006847 if (wpa_s->p2p_in_provisioning) {
6848 wpas_group_formation_completed(wpa_s, 0);
6849 break;
6850 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006851 wpas_p2p_group_delete(wpa_s,
6852 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006854 } else if (wpa_s->p2p_in_invitation) {
6855 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6856 wpa_s->ifname);
6857 found = 1;
6858 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006859 }
6860 }
6861
6862 if (!found) {
6863 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6864 return -1;
6865 }
6866
6867 return 0;
6868}
6869
6870
6871void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6872{
6873 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6874 return;
6875
6876 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6877 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006878 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006879}
6880
6881
6882void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6883 int freq_24, int freq_5, int freq_overall)
6884{
6885 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006886 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006887 return;
6888 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6889}
6890
6891
6892int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6893{
6894 u8 peer[ETH_ALEN];
6895 struct p2p_data *p2p = wpa_s->global->p2p;
6896
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006897 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006898 return -1;
6899
6900 if (hwaddr_aton(addr, peer))
6901 return -1;
6902
6903 return p2p_unauthorize(p2p, peer);
6904}
6905
6906
6907/**
6908 * wpas_p2p_disconnect - Disconnect from a P2P Group
6909 * @wpa_s: Pointer to wpa_supplicant data
6910 * Returns: 0 on success, -1 on failure
6911 *
6912 * This can be used to disconnect from a group in which the local end is a P2P
6913 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6914 * virtual network interface was created for this group, that interface will be
6915 * removed. Otherwise, only the configured P2P group network will be removed
6916 * from the interface.
6917 */
6918int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6919{
6920
6921 if (wpa_s == NULL)
6922 return -1;
6923
Jouni Malinen2b89da82012-08-31 22:04:41 +03006924 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6925 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006927
6928
6929int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6930{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006931 int ret;
6932
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006933 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6934 return 0;
6935
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006936 ret = p2p_in_progress(wpa_s->global->p2p);
6937 if (ret == 0) {
6938 /*
6939 * Check whether there is an ongoing WPS provisioning step (or
6940 * other parts of group formation) on another interface since
6941 * p2p_in_progress() does not report this to avoid issues for
6942 * scans during such provisioning step.
6943 */
6944 if (wpa_s->global->p2p_group_formation &&
6945 wpa_s->global->p2p_group_formation != wpa_s) {
6946 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6947 "in group formation",
6948 wpa_s->global->p2p_group_formation->ifname);
6949 ret = 1;
6950 }
6951 }
6952
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006953 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006954 struct os_reltime now;
6955 os_get_reltime(&now);
6956 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6957 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006958 /* Wait for the first client has expired */
6959 wpa_s->global->p2p_go_wait_client.sec = 0;
6960 } else {
6961 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6962 ret = 1;
6963 }
6964 }
6965
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006966 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006967}
6968
6969
6970void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6971 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006972{
6973 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6974 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6975 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006976 /**
6977 * Remove the network by scheduling the group formation
6978 * timeout to happen immediately. The teardown code
6979 * needs to be scheduled to run asynch later so that we
6980 * don't delete data from under ourselves unexpectedly.
6981 * Calling wpas_p2p_group_formation_timeout directly
6982 * causes a series of crashes in WPS failure scenarios.
6983 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006984 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6985 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006986 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6987 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006988 }
6989}
6990
6991
6992struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006993 const u8 *addr, const u8 *ssid,
6994 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006995{
6996 struct wpa_ssid *s;
6997 size_t i;
6998
6999 for (s = wpa_s->conf->ssid; s; s = s->next) {
7000 if (s->disabled != 2)
7001 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007002 if (ssid &&
7003 (ssid_len != s->ssid_len ||
7004 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7005 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007006 if (addr == NULL) {
7007 if (s->mode == WPAS_MODE_P2P_GO)
7008 return s;
7009 continue;
7010 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007011 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7012 return s; /* peer is GO in the persistent group */
7013 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7014 continue;
7015 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007016 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007017 addr, ETH_ALEN) == 0)
7018 return s; /* peer is P2P client in persistent
7019 * group */
7020 }
7021 }
7022
7023 return NULL;
7024}
7025
7026
7027void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7028 const u8 *addr)
7029{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007030 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7031 wpa_s->parent, NULL) > 0) {
7032 /*
7033 * This can happen if WPS provisioning step is not terminated
7034 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7035 * peer was able to connect, there is no need to time out group
7036 * formation after this, though. In addition, this is used with
7037 * the initial connection wait on the GO as a separate formation
7038 * timeout and as such, expected to be hit after the initial WPS
7039 * provisioning step.
7040 */
7041 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007042
7043 if (!wpa_s->p2p_go_group_formation_completed &&
7044 !wpa_s->group_formation_reported) {
7045 /*
7046 * GO has not yet notified group formation success since
7047 * the WPS step was not completed cleanly. Do that
7048 * notification now since the P2P Client was able to
7049 * connect and as such, must have received the
7050 * credential from the WPS step.
7051 */
7052 if (wpa_s->global->p2p)
7053 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7054 wpas_group_formation_completed(wpa_s, 1);
7055 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007056 }
7057 if (!wpa_s->p2p_go_group_formation_completed) {
7058 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7059 wpa_s->p2p_go_group_formation_completed = 1;
7060 wpa_s->global->p2p_group_formation = NULL;
7061 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007062 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007063 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007064 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007065 if (addr == NULL)
7066 return;
7067 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7068}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007069
Dmitry Shmidt04949592012-07-19 12:16:46 -07007070
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007071static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7072 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007073{
7074 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007075 int ret = 0;
7076
Dmitry Shmidt04949592012-07-19 12:16:46 -07007077 if (wpa_s->global->p2p_group_formation)
7078 group = wpa_s->global->p2p_group_formation;
7079 wpa_s = wpa_s->parent;
7080 offchannel_send_action_done(wpa_s);
7081 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007082 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007083 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7084 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7085 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7086 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7087 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007088 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007089 wpa_s->p2p_go_ht40,
7090 wpa_s->p2p_go_vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007091 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007092}
7093
7094
7095int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7096{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007097 int res;
7098
Dmitry Shmidt04949592012-07-19 12:16:46 -07007099 if (!wpa_s->p2p_fallback_to_go_neg ||
7100 wpa_s->p2p_in_provisioning <= 5)
7101 return 0;
7102
7103 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7104 return 0; /* peer operating as a GO */
7105
7106 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7107 "fallback to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007108 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7109 "reason=GO-not-found");
7110 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007111
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007112 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007113}
7114
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007115
7116unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7117{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007118 struct wpa_supplicant *ifs;
7119
7120 if (wpa_s->wpa_state > WPA_SCANNING) {
7121 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7122 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007123 wpa_s->conf->p2p_search_delay);
7124 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007125 }
7126
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007127 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7128 radio_list) {
7129 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007130 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7131 "delay due to concurrent operation on "
7132 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007133 wpa_s->conf->p2p_search_delay,
7134 ifs->ifname);
7135 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007136 }
7137 }
7138
7139 return 0;
7140}
7141
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007142
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007143static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7144 struct wpa_ssid *s, const u8 *addr,
7145 int iface_addr)
7146{
7147 struct psk_list_entry *psk, *tmp;
7148 int changed = 0;
7149
7150 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7151 list) {
7152 if ((iface_addr && !psk->p2p &&
7153 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7154 (!iface_addr && psk->p2p &&
7155 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7156 wpa_dbg(wpa_s, MSG_DEBUG,
7157 "P2P: Remove persistent group PSK list entry for "
7158 MACSTR " p2p=%u",
7159 MAC2STR(psk->addr), psk->p2p);
7160 dl_list_del(&psk->list);
7161 os_free(psk);
7162 changed++;
7163 }
7164 }
7165
7166 return changed;
7167}
7168
7169
7170void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7171 const u8 *p2p_dev_addr,
7172 const u8 *psk, size_t psk_len)
7173{
7174 struct wpa_ssid *ssid = wpa_s->current_ssid;
7175 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007176 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007177
7178 if (psk_len != sizeof(p->psk))
7179 return;
7180
7181 if (p2p_dev_addr) {
7182 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7183 " p2p_dev_addr=" MACSTR,
7184 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7185 if (is_zero_ether_addr(p2p_dev_addr))
7186 p2p_dev_addr = NULL;
7187 } else {
7188 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7189 MAC2STR(mac_addr));
7190 }
7191
7192 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7193 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7194 /* To be added to persistent group once created */
7195 if (wpa_s->global->add_psk == NULL) {
7196 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7197 if (wpa_s->global->add_psk == NULL)
7198 return;
7199 }
7200 p = wpa_s->global->add_psk;
7201 if (p2p_dev_addr) {
7202 p->p2p = 1;
7203 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7204 } else {
7205 p->p2p = 0;
7206 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7207 }
7208 os_memcpy(p->psk, psk, psk_len);
7209 return;
7210 }
7211
7212 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7213 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7214 return;
7215 }
7216
7217 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7218 ssid->ssid_len);
7219 if (!persistent) {
7220 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7221 return;
7222 }
7223
7224 p = os_zalloc(sizeof(*p));
7225 if (p == NULL)
7226 return;
7227 if (p2p_dev_addr) {
7228 p->p2p = 1;
7229 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7230 } else {
7231 p->p2p = 0;
7232 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7233 }
7234 os_memcpy(p->psk, psk, psk_len);
7235
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007236 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7237 (last = dl_list_last(&persistent->psk_list,
7238 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007239 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7240 MACSTR " (p2p=%u) to make room for a new one",
7241 MAC2STR(last->addr), last->p2p);
7242 dl_list_del(&last->list);
7243 os_free(last);
7244 }
7245
7246 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7247 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7248 p2p_dev_addr == NULL);
7249 if (p2p_dev_addr) {
7250 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7251 MACSTR, MAC2STR(p2p_dev_addr));
7252 } else {
7253 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7254 MAC2STR(mac_addr));
7255 }
7256 dl_list_add(&persistent->psk_list, &p->list);
7257
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007258 if (wpa_s->parent->conf->update_config &&
7259 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7260 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007261}
7262
7263
7264static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7265 struct wpa_ssid *s, const u8 *addr,
7266 int iface_addr)
7267{
7268 int res;
7269
7270 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007271 if (res > 0 && wpa_s->conf->update_config &&
7272 wpa_config_write(wpa_s->confname, wpa_s->conf))
7273 wpa_dbg(wpa_s, MSG_DEBUG,
7274 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007275}
7276
7277
7278static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7279 const u8 *peer, int iface_addr)
7280{
7281 struct hostapd_data *hapd;
7282 struct hostapd_wpa_psk *psk, *prev, *rem;
7283 struct sta_info *sta;
7284
7285 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7286 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7287 return;
7288
7289 /* Remove per-station PSK entry */
7290 hapd = wpa_s->ap_iface->bss[0];
7291 prev = NULL;
7292 psk = hapd->conf->ssid.wpa_psk;
7293 while (psk) {
7294 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7295 (!iface_addr &&
7296 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7298 MACSTR " iface_addr=%d",
7299 MAC2STR(peer), iface_addr);
7300 if (prev)
7301 prev->next = psk->next;
7302 else
7303 hapd->conf->ssid.wpa_psk = psk->next;
7304 rem = psk;
7305 psk = psk->next;
7306 os_free(rem);
7307 } else {
7308 prev = psk;
7309 psk = psk->next;
7310 }
7311 }
7312
7313 /* Disconnect from group */
7314 if (iface_addr)
7315 sta = ap_get_sta(hapd, peer);
7316 else
7317 sta = ap_get_sta_p2p(hapd, peer);
7318 if (sta) {
7319 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7320 " (iface_addr=%d) from group",
7321 MAC2STR(peer), iface_addr);
7322 hostapd_drv_sta_deauth(hapd, sta->addr,
7323 WLAN_REASON_DEAUTH_LEAVING);
7324 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7325 }
7326}
7327
7328
7329void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7330 int iface_addr)
7331{
7332 struct wpa_ssid *s;
7333 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007334 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007335
7336 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7337
7338 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007339 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007340 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7341 continue;
7342 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007343 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7344 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007345 }
7346
7347 /* Remove from any operating group */
7348 for (w = wpa_s->global->ifaces; w; w = w->next)
7349 wpas_p2p_remove_client_go(w, peer, iface_addr);
7350}
7351
7352
7353static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7354{
7355 struct wpa_supplicant *wpa_s = eloop_ctx;
7356 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7357}
7358
7359
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007360static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7361{
7362 struct wpa_supplicant *wpa_s = eloop_ctx;
7363
7364 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7365 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7366}
7367
7368
7369int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7370 struct wpa_ssid *ssid)
7371{
7372 struct wpa_supplicant *iface;
7373
7374 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7375 if (!iface->current_ssid ||
7376 iface->current_ssid->frequency == freq ||
7377 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7378 !iface->current_ssid->p2p_group))
7379 continue;
7380
7381 /* Remove the connection with least priority */
7382 if (!wpas_is_p2p_prioritized(iface)) {
7383 /* STA connection has priority over existing
7384 * P2P connection, so remove the interface. */
7385 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7386 eloop_register_timeout(0, 0,
7387 wpas_p2p_group_freq_conflict,
7388 iface, NULL);
7389 /* If connection in progress is P2P connection, do not
7390 * proceed for the connection. */
7391 if (wpa_s == iface)
7392 return -1;
7393 else
7394 return 0;
7395 } else {
7396 /* P2P connection has priority, disable the STA network
7397 */
7398 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7399 ssid);
7400 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7401 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7402 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7403 ETH_ALEN);
7404 /* If P2P connection is in progress, continue
7405 * connecting...*/
7406 if (wpa_s == iface)
7407 return 0;
7408 else
7409 return -1;
7410 }
7411 }
7412
7413 return 0;
7414}
7415
7416
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007417int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7418{
7419 struct wpa_ssid *ssid = wpa_s->current_ssid;
7420
7421 if (ssid == NULL || !ssid->p2p_group)
7422 return 0;
7423
7424 if (wpa_s->p2p_last_4way_hs_fail &&
7425 wpa_s->p2p_last_4way_hs_fail == ssid) {
7426 u8 go_dev_addr[ETH_ALEN];
7427 struct wpa_ssid *persistent;
7428
7429 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7430 ssid->ssid,
7431 ssid->ssid_len) <= 0) {
7432 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7433 goto disconnect;
7434 }
7435
7436 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7437 MACSTR, MAC2STR(go_dev_addr));
7438 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7439 ssid->ssid,
7440 ssid->ssid_len);
7441 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7442 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7443 goto disconnect;
7444 }
7445 wpa_msg_global(wpa_s->parent, MSG_INFO,
7446 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7447 persistent->id);
7448 disconnect:
7449 wpa_s->p2p_last_4way_hs_fail = NULL;
7450 /*
7451 * Remove the group from a timeout to avoid issues with caller
7452 * continuing to use the interface if this is on a P2P group
7453 * interface.
7454 */
7455 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7456 wpa_s, NULL);
7457 return 1;
7458 }
7459
7460 wpa_s->p2p_last_4way_hs_fail = ssid;
7461 return 0;
7462}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007463
7464
7465#ifdef CONFIG_WPS_NFC
7466
7467static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7468 struct wpabuf *p2p)
7469{
7470 struct wpabuf *ret;
7471 size_t wsc_len;
7472
7473 if (p2p == NULL) {
7474 wpabuf_free(wsc);
7475 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7476 return NULL;
7477 }
7478
7479 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7480 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7481 if (ret == NULL) {
7482 wpabuf_free(wsc);
7483 wpabuf_free(p2p);
7484 return NULL;
7485 }
7486
7487 wpabuf_put_be16(ret, wsc_len);
7488 if (wsc)
7489 wpabuf_put_buf(ret, wsc);
7490 wpabuf_put_be16(ret, wpabuf_len(p2p));
7491 wpabuf_put_buf(ret, p2p);
7492
7493 wpabuf_free(wsc);
7494 wpabuf_free(p2p);
7495 wpa_hexdump_buf(MSG_DEBUG,
7496 "P2P: Generated NFC connection handover message", ret);
7497
7498 if (ndef && ret) {
7499 struct wpabuf *tmp;
7500 tmp = ndef_build_p2p(ret);
7501 wpabuf_free(ret);
7502 if (tmp == NULL) {
7503 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7504 return NULL;
7505 }
7506 ret = tmp;
7507 }
7508
7509 return ret;
7510}
7511
7512
7513static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7514 struct wpa_ssid **ssid, u8 *go_dev_addr)
7515{
7516 struct wpa_supplicant *iface;
7517
7518 if (go_dev_addr)
7519 os_memset(go_dev_addr, 0, ETH_ALEN);
7520 if (ssid)
7521 *ssid = NULL;
7522 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7523 if (iface->wpa_state < WPA_ASSOCIATING ||
7524 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7525 !iface->current_ssid->p2p_group ||
7526 iface->current_ssid->mode != WPAS_MODE_INFRA)
7527 continue;
7528 if (ssid)
7529 *ssid = iface->current_ssid;
7530 if (go_dev_addr)
7531 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7532 return iface->assoc_freq;
7533 }
7534 return 0;
7535}
7536
7537
7538struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7539 int ndef)
7540{
7541 struct wpabuf *wsc, *p2p;
7542 struct wpa_ssid *ssid;
7543 u8 go_dev_addr[ETH_ALEN];
7544 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7545
7546 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7547 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7548 return NULL;
7549 }
7550
7551 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7552 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7553 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7554 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7555 return NULL;
7556 }
7557
7558 if (cli_freq == 0) {
7559 wsc = wps_build_nfc_handover_req_p2p(
7560 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7561 } else
7562 wsc = NULL;
7563 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7564 go_dev_addr, ssid ? ssid->ssid : NULL,
7565 ssid ? ssid->ssid_len : 0);
7566
7567 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7568}
7569
7570
7571struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7572 int ndef, int tag)
7573{
7574 struct wpabuf *wsc, *p2p;
7575 struct wpa_ssid *ssid;
7576 u8 go_dev_addr[ETH_ALEN];
7577 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7578
7579 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7580 return NULL;
7581
7582 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7583 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7584 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7585 return NULL;
7586
7587 if (cli_freq == 0) {
7588 wsc = wps_build_nfc_handover_sel_p2p(
7589 wpa_s->parent->wps,
7590 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7591 DEV_PW_NFC_CONNECTION_HANDOVER,
7592 wpa_s->conf->wps_nfc_dh_pubkey,
7593 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7594 } else
7595 wsc = NULL;
7596 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7597 go_dev_addr, ssid ? ssid->ssid : NULL,
7598 ssid ? ssid->ssid_len : 0);
7599
7600 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7601}
7602
7603
7604static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7605 struct p2p_nfc_params *params)
7606{
7607 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7608 "connection handover (freq=%d)",
7609 params->go_freq);
7610
7611 if (params->go_freq && params->go_ssid_len) {
7612 wpa_s->p2p_wps_method = WPS_NFC;
7613 wpa_s->pending_join_wps_method = WPS_NFC;
7614 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7615 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7616 ETH_ALEN);
7617 return wpas_p2p_join_start(wpa_s, params->go_freq,
7618 params->go_ssid,
7619 params->go_ssid_len);
7620 }
7621
7622 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7623 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7624 params->go_freq, -1, 0, 1, 1);
7625}
7626
7627
7628static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7629 struct p2p_nfc_params *params, int tag)
7630{
7631 int res, persistent;
7632 struct wpa_ssid *ssid;
7633
7634 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7635 "connection handover");
7636 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7637 ssid = wpa_s->current_ssid;
7638 if (ssid == NULL)
7639 continue;
7640 if (ssid->mode != WPAS_MODE_P2P_GO)
7641 continue;
7642 if (wpa_s->ap_iface == NULL)
7643 continue;
7644 break;
7645 }
7646 if (wpa_s == NULL) {
7647 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7648 return -1;
7649 }
7650
7651 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7652 DEV_PW_NFC_CONNECTION_HANDOVER &&
7653 !wpa_s->parent->p2p_oob_dev_pw) {
7654 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7655 return -1;
7656 }
7657 res = wpas_ap_wps_add_nfc_pw(
7658 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7659 wpa_s->parent->p2p_oob_dev_pw,
7660 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7661 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7662 if (res)
7663 return res;
7664
7665 if (!tag) {
7666 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7667 return 0;
7668 }
7669
7670 if (!params->peer ||
7671 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7672 return 0;
7673
7674 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7675 " to join", MAC2STR(params->peer->p2p_device_addr));
7676
7677 wpa_s->global->p2p_invite_group = wpa_s;
7678 persistent = ssid->p2p_persistent_group &&
7679 wpas_p2p_get_persistent(wpa_s->parent,
7680 params->peer->p2p_device_addr,
7681 ssid->ssid, ssid->ssid_len);
7682 wpa_s->parent->pending_invite_ssid_id = -1;
7683
7684 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7685 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7686 ssid->ssid, ssid->ssid_len, ssid->frequency,
7687 wpa_s->global->p2p_dev_addr, persistent, 0,
7688 wpa_s->parent->p2p_oob_dev_pw_id);
7689}
7690
7691
7692static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7693 struct p2p_nfc_params *params,
7694 int forced_freq)
7695{
7696 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7697 "connection handover");
7698 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7699 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7700 forced_freq, -1, 0, 1, 1);
7701}
7702
7703
7704static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7705 struct p2p_nfc_params *params,
7706 int forced_freq)
7707{
7708 int res;
7709
7710 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7711 "connection handover");
7712 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7713 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7714 forced_freq, -1, 0, 1, 1);
7715 if (res)
7716 return res;
7717
7718 res = wpas_p2p_listen(wpa_s, 60);
7719 if (res) {
7720 p2p_unauthorize(wpa_s->global->p2p,
7721 params->peer->p2p_device_addr);
7722 }
7723
7724 return res;
7725}
7726
7727
7728static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7729 const struct wpabuf *data,
7730 int sel, int tag, int forced_freq)
7731{
7732 const u8 *pos, *end;
7733 u16 len, id;
7734 struct p2p_nfc_params params;
7735 int res;
7736
7737 os_memset(&params, 0, sizeof(params));
7738 params.sel = sel;
7739
7740 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7741
7742 pos = wpabuf_head(data);
7743 end = pos + wpabuf_len(data);
7744
7745 if (end - pos < 2) {
7746 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7747 "attributes");
7748 return -1;
7749 }
7750 len = WPA_GET_BE16(pos);
7751 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007752 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007753 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7754 "attributes");
7755 return -1;
7756 }
7757 params.wsc_attr = pos;
7758 params.wsc_len = len;
7759 pos += len;
7760
7761 if (end - pos < 2) {
7762 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7763 "attributes");
7764 return -1;
7765 }
7766 len = WPA_GET_BE16(pos);
7767 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007768 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007769 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7770 "attributes");
7771 return -1;
7772 }
7773 params.p2p_attr = pos;
7774 params.p2p_len = len;
7775 pos += len;
7776
7777 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7778 params.wsc_attr, params.wsc_len);
7779 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7780 params.p2p_attr, params.p2p_len);
7781 if (pos < end) {
7782 wpa_hexdump(MSG_DEBUG,
7783 "P2P: Ignored extra data after P2P attributes",
7784 pos, end - pos);
7785 }
7786
7787 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7788 if (res)
7789 return res;
7790
7791 if (params.next_step == NO_ACTION)
7792 return 0;
7793
7794 if (params.next_step == BOTH_GO) {
7795 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7796 MAC2STR(params.peer->p2p_device_addr));
7797 return 0;
7798 }
7799
7800 if (params.next_step == PEER_CLIENT) {
7801 if (!is_zero_ether_addr(params.go_dev_addr)) {
7802 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7803 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7804 " ssid=\"%s\"",
7805 MAC2STR(params.peer->p2p_device_addr),
7806 params.go_freq,
7807 MAC2STR(params.go_dev_addr),
7808 wpa_ssid_txt(params.go_ssid,
7809 params.go_ssid_len));
7810 } else {
7811 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7812 "peer=" MACSTR " freq=%d",
7813 MAC2STR(params.peer->p2p_device_addr),
7814 params.go_freq);
7815 }
7816 return 0;
7817 }
7818
7819 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7820 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7821 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7822 return 0;
7823 }
7824
7825 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7826 wpa_s->p2p_oob_dev_pw = NULL;
7827
7828 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7829 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7830 "received");
7831 return -1;
7832 }
7833
7834 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7835 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7836 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7837 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7838 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7839 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7840 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7841
7842 if (tag) {
7843 if (id < 0x10) {
7844 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7845 "peer OOB Device Password Id %u", id);
7846 return -1;
7847 }
7848 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7849 "Device Password Id %u", id);
7850 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7851 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7852 params.oob_dev_pw_len -
7853 WPS_OOB_PUBKEY_HASH_LEN - 2);
7854 wpa_s->p2p_oob_dev_pw_id = id;
7855 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7856 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7857 params.oob_dev_pw_len -
7858 WPS_OOB_PUBKEY_HASH_LEN - 2);
7859 if (wpa_s->p2p_oob_dev_pw == NULL)
7860 return -1;
7861
7862 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7863 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7864 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7865 return -1;
7866 } else {
7867 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7868 "without Device Password");
7869 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7870 }
7871
7872 switch (params.next_step) {
7873 case NO_ACTION:
7874 case BOTH_GO:
7875 case PEER_CLIENT:
7876 /* already covered above */
7877 return 0;
7878 case JOIN_GROUP:
7879 return wpas_p2p_nfc_join_group(wpa_s, &params);
7880 case AUTH_JOIN:
7881 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7882 case INIT_GO_NEG:
7883 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7884 case RESP_GO_NEG:
7885 /* TODO: use own OOB Dev Pw */
7886 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7887 }
7888
7889 return -1;
7890}
7891
7892
7893int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7894 const struct wpabuf *data, int forced_freq)
7895{
7896 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7897 return -1;
7898
7899 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7900}
7901
7902
7903int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7904 const struct wpabuf *req,
7905 const struct wpabuf *sel, int forced_freq)
7906{
7907 struct wpabuf *tmp;
7908 int ret;
7909
7910 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7911 return -1;
7912
7913 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7914
7915 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7916 wpabuf_head(req), wpabuf_len(req));
7917 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7918 wpabuf_head(sel), wpabuf_len(sel));
7919 if (forced_freq)
7920 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7921 tmp = ndef_parse_p2p(init ? sel : req);
7922 if (tmp == NULL) {
7923 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7924 return -1;
7925 }
7926
7927 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7928 forced_freq);
7929 wpabuf_free(tmp);
7930
7931 return ret;
7932}
7933
7934
7935int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7936{
7937 const u8 *if_addr;
7938 int go_intent = wpa_s->conf->p2p_go_intent;
7939 struct wpa_supplicant *iface;
7940
7941 if (wpa_s->global->p2p == NULL)
7942 return -1;
7943
7944 if (!enabled) {
7945 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7946 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7947 {
7948 if (!iface->ap_iface)
7949 continue;
7950 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7951 }
7952 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7953 0, NULL);
7954 if (wpa_s->p2p_nfc_tag_enabled)
7955 wpas_p2p_remove_pending_group_interface(wpa_s);
7956 wpa_s->p2p_nfc_tag_enabled = 0;
7957 return 0;
7958 }
7959
7960 if (wpa_s->global->p2p_disabled)
7961 return -1;
7962
7963 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7964 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7965 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7966 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7967 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7968 "to allow static handover cases");
7969 return -1;
7970 }
7971
7972 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7973
7974 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7975 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7976 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7977 if (wpa_s->p2p_oob_dev_pw == NULL)
7978 return -1;
7979 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7980
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007981 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7982 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7983 /*
7984 * P2P Group Interface present and the command came on group
7985 * interface, so enable the token for the current interface.
7986 */
7987 wpa_s->create_p2p_iface = 0;
7988 } else {
7989 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7990 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007991
7992 if (wpa_s->create_p2p_iface) {
7993 enum wpa_driver_if_type iftype;
7994 /* Prepare to add a new interface for the group */
7995 iftype = WPA_IF_P2P_GROUP;
7996 if (go_intent == 15)
7997 iftype = WPA_IF_P2P_GO;
7998 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7999 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8000 "interface for the group");
8001 return -1;
8002 }
8003
8004 if_addr = wpa_s->pending_interface_addr;
8005 } else
8006 if_addr = wpa_s->own_addr;
8007
8008 wpa_s->p2p_nfc_tag_enabled = enabled;
8009
8010 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8011 struct hostapd_data *hapd;
8012 if (iface->ap_iface == NULL)
8013 continue;
8014 hapd = iface->ap_iface->bss[0];
8015 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8016 hapd->conf->wps_nfc_dh_pubkey =
8017 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8018 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8019 hapd->conf->wps_nfc_dh_privkey =
8020 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8021 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8022 hapd->conf->wps_nfc_dev_pw =
8023 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8024 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8025
8026 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8027 wpa_dbg(iface, MSG_DEBUG,
8028 "P2P: Failed to enable NFC Tag for GO");
8029 }
8030 }
8031 p2p_set_authorized_oob_dev_pw_id(
8032 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8033 if_addr);
8034
8035 return 0;
8036}
8037
8038#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008039
8040
8041static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8042 struct wpa_used_freq_data *freqs,
8043 unsigned int num)
8044{
8045 u8 curr_chan, cand, chan;
8046 unsigned int i;
8047
8048 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8049 for (i = 0, cand = 0; i < num; i++) {
8050 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8051 if (curr_chan == chan) {
8052 cand = 0;
8053 break;
8054 }
8055
8056 if (chan == 1 || chan == 6 || chan == 11)
8057 cand = chan;
8058 }
8059
8060 if (cand) {
8061 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008062 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008063 cand);
8064 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8065 }
8066}
8067
8068
8069void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8070{
8071 struct wpa_used_freq_data *freqs;
8072 unsigned int num = wpa_s->num_multichan_concurrent;
8073
8074 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8075 return;
8076
8077 /*
8078 * If possible, optimize the Listen channel to be a channel that is
8079 * already used by one of the other interfaces.
8080 */
8081 if (!wpa_s->conf->p2p_optimize_listen_chan)
8082 return;
8083
8084 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8085 return;
8086
8087 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8088 if (!freqs)
8089 return;
8090
8091 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8092
8093 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8094 os_free(freqs);
8095}
8096
8097
8098void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8099{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008100 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8101 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8102 "the management interface is being removed");
8103 wpas_p2p_deinit_global(wpa_s->global);
8104 }
8105}
8106
8107
8108void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8109{
8110 if (wpa_s->ap_iface->bss)
8111 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8112 wpas_p2p_group_deinit(wpa_s);
8113}