blob: 5c8a3b21441f28e7f4567474109ad95202100885 [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 Shmidt7a53dbb2015-06-11 13:13:53 -07002149static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2150 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151{
2152 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002153 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002154 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2155 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002157 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158}
2159
2160
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002161static void wpas_dev_found(void *ctx, const u8 *addr,
2162 const struct p2p_peer_info *info,
2163 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002165#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 struct wpa_supplicant *wpa_s = ctx;
2167 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002168 char *wfd_dev_info_hex = NULL;
2169
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002170#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002171 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2172 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002173#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002174
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002175 if (info->p2ps_instance) {
2176 char str[256];
2177 const u8 *buf = wpabuf_head(info->p2ps_instance);
2178 size_t len = wpabuf_len(info->p2ps_instance);
2179
2180 while (len) {
2181 u32 id;
2182 u16 methods;
2183 u8 str_len;
2184
2185 if (len < 4 + 2 + 1)
2186 break;
2187 id = WPA_GET_LE32(buf);
2188 buf += sizeof(u32);
2189 methods = WPA_GET_BE16(buf);
2190 buf += sizeof(u16);
2191 str_len = *buf++;
2192 if (str_len > len - 4 - 2 - 1)
2193 break;
2194 os_memcpy(str, buf, str_len);
2195 str[str_len] = '\0';
2196 buf += str_len;
2197 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2198
2199 wpa_msg_global(wpa_s, MSG_INFO,
2200 P2P_EVENT_DEVICE_FOUND MACSTR
2201 " p2p_dev_addr=" MACSTR
2202 " pri_dev_type=%s name='%s'"
2203 " config_methods=0x%x"
2204 " dev_capab=0x%x"
2205 " group_capab=0x%x"
2206 " adv_id=%x asp_svc=%s%s",
2207 MAC2STR(addr),
2208 MAC2STR(info->p2p_device_addr),
2209 wps_dev_type_bin2str(
2210 info->pri_dev_type,
2211 devtype, sizeof(devtype)),
2212 info->device_name, methods,
2213 info->dev_capab, info->group_capab,
2214 id, str,
2215 info->vendor_elems ?
2216 " vendor_elems=1" : "");
2217 }
2218 goto done;
2219 }
2220
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002221 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2222 " p2p_dev_addr=" MACSTR
2223 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002224 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002225 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2226 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2227 sizeof(devtype)),
2228 info->device_name, info->config_methods,
2229 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002230 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002231 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002232 info->vendor_elems ? " vendor_elems=1" : "",
2233 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002234
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002235done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002236 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002237#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2240}
2241
2242
2243static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2244{
2245 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002246
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002247 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2248 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2251}
2252
2253
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002254static void wpas_find_stopped(void *ctx)
2255{
2256 struct wpa_supplicant *wpa_s = ctx;
2257 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002258 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002259}
2260
2261
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002262struct wpas_p2p_listen_work {
2263 unsigned int freq;
2264 unsigned int duration;
2265 struct wpabuf *probe_resp_ie;
2266};
2267
2268
2269static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2270{
2271 if (lwork == NULL)
2272 return;
2273 wpabuf_free(lwork->probe_resp_ie);
2274 os_free(lwork);
2275}
2276
2277
2278static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2279{
2280 struct wpas_p2p_listen_work *lwork;
2281
2282 if (!wpa_s->p2p_listen_work)
2283 return;
2284
2285 lwork = wpa_s->p2p_listen_work->ctx;
2286 wpas_p2p_listen_work_free(lwork);
2287 radio_work_done(wpa_s->p2p_listen_work);
2288 wpa_s->p2p_listen_work = NULL;
2289}
2290
2291
2292static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2293{
2294 struct wpa_supplicant *wpa_s = work->wpa_s;
2295 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002296 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002297
2298 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002299 if (work->started) {
2300 wpa_s->p2p_listen_work = NULL;
2301 wpas_stop_listen(wpa_s);
2302 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002303 wpas_p2p_listen_work_free(lwork);
2304 return;
2305 }
2306
2307 wpa_s->p2p_listen_work = work;
2308
2309 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2310
2311 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2312 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2313 "report received Probe Request frames");
2314 wpas_p2p_listen_work_done(wpa_s);
2315 return;
2316 }
2317
2318 wpa_s->pending_listen_freq = lwork->freq;
2319 wpa_s->pending_listen_duration = lwork->duration;
2320
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002321 duration = lwork->duration;
2322#ifdef CONFIG_TESTING_OPTIONS
2323 if (wpa_s->extra_roc_dur) {
2324 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2325 duration, duration + wpa_s->extra_roc_dur);
2326 duration += wpa_s->extra_roc_dur;
2327 }
2328#endif /* CONFIG_TESTING_OPTIONS */
2329
2330 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002331 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2332 "to remain on channel (%u MHz) for Listen "
2333 "state", lwork->freq);
2334 wpas_p2p_listen_work_done(wpa_s);
2335 wpa_s->pending_listen_freq = 0;
2336 return;
2337 }
2338 wpa_s->off_channel_freq = 0;
2339 wpa_s->roc_waiting_drv_freq = lwork->freq;
2340}
2341
2342
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343static int wpas_start_listen(void *ctx, unsigned int freq,
2344 unsigned int duration,
2345 const struct wpabuf *probe_resp_ie)
2346{
2347 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002348 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002350 if (wpa_s->p2p_listen_work) {
2351 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352 return -1;
2353 }
2354
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002355 lwork = os_zalloc(sizeof(*lwork));
2356 if (lwork == NULL)
2357 return -1;
2358 lwork->freq = freq;
2359 lwork->duration = duration;
2360 if (probe_resp_ie) {
2361 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2362 if (lwork->probe_resp_ie == NULL) {
2363 wpas_p2p_listen_work_free(lwork);
2364 return -1;
2365 }
2366 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002368 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2369 lwork) < 0) {
2370 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002371 return -1;
2372 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373
2374 return 0;
2375}
2376
2377
2378static void wpas_stop_listen(void *ctx)
2379{
2380 struct wpa_supplicant *wpa_s = ctx;
2381 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2382 wpa_drv_cancel_remain_on_channel(wpa_s);
2383 wpa_s->off_channel_freq = 0;
2384 wpa_s->roc_waiting_drv_freq = 0;
2385 }
2386 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2387 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002388 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002389}
2390
2391
2392static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2393{
2394 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002395 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396}
2397
2398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002399static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2400 const u8 *peer, const char *params,
2401 unsigned int generated_pin)
2402{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002403 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2404 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405}
2406
2407
2408static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2409 const u8 *peer, const char *params)
2410{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002411 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2412 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413}
2414
2415
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002416static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2417 const u8 *dev_addr, const u8 *pri_dev_type,
2418 const char *dev_name, u16 supp_config_methods,
2419 u8 dev_capab, u8 group_capab, const u8 *group_id,
2420 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421{
2422 struct wpa_supplicant *wpa_s = ctx;
2423 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002424 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002425 u8 empty_dev_type[8];
2426 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002427 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002428 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002429
2430 if (group_id) {
2431 for (group = wpa_s->global->ifaces; group; group = group->next)
2432 {
2433 struct wpa_ssid *s = group->current_ssid;
2434 if (s != NULL &&
2435 s->mode == WPAS_MODE_P2P_GO &&
2436 group_id_len - ETH_ALEN == s->ssid_len &&
2437 os_memcmp(group_id + ETH_ALEN, s->ssid,
2438 s->ssid_len) == 0)
2439 break;
2440 }
2441 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442
2443 if (pri_dev_type == NULL) {
2444 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2445 pri_dev_type = empty_dev_type;
2446 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002447 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2448 " pri_dev_type=%s name='%s' config_methods=0x%x "
2449 "dev_capab=0x%x group_capab=0x%x%s%s",
2450 MAC2STR(dev_addr),
2451 wps_dev_type_bin2str(pri_dev_type, devtype,
2452 sizeof(devtype)),
2453 dev_name, supp_config_methods, dev_capab, group_capab,
2454 group ? " group=" : "",
2455 group ? group->ifname : "");
2456 if (os_snprintf_error(sizeof(params), res))
2457 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002460 if (config_methods & WPS_CONFIG_DISPLAY) {
2461 generated_pin = wps_generate_pin();
2462 wpas_prov_disc_local_display(wpa_s, peer, params,
2463 generated_pin);
2464 } else if (config_methods & WPS_CONFIG_KEYPAD)
2465 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2466 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002467 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2468 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002469
2470 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2471 P2P_PROV_DISC_SUCCESS,
2472 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473}
2474
2475
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002476static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002477{
2478 struct wpa_supplicant *wpa_s = ctx;
2479 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002480 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002482 if (wpa_s->pending_pd_before_join &&
2483 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2484 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2485 wpa_s->pending_pd_before_join = 0;
2486 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2487 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002488 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002489 return;
2490 }
2491
Dmitry Shmidt04949592012-07-19 12:16:46 -07002492 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002493 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2494 int res;
2495
2496 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2497 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2498 if (os_snprintf_error(sizeof(params), res))
2499 params[sizeof(params) - 1] = '\0';
2500 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002501 params[0] = '\0';
2502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002504 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 else if (config_methods & WPS_CONFIG_KEYPAD) {
2506 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507 wpas_prov_disc_local_display(wpa_s, peer, params,
2508 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002510 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2511 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002512
Jouni Malinen75ecf522011-06-27 15:19:46 -07002513 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2514 P2P_PROV_DISC_SUCCESS,
2515 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516}
2517
2518
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002519static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002520 enum p2p_prov_disc_status status,
2521 u32 adv_id, const u8 *adv_mac,
2522 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002523{
2524 struct wpa_supplicant *wpa_s = ctx;
2525
Dmitry Shmidt04949592012-07-19 12:16:46 -07002526 if (wpa_s->p2p_fallback_to_go_neg) {
2527 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2528 "failed - fall back to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002529 wpa_msg_global(wpa_s->parent, MSG_INFO,
2530 P2P_EVENT_FALLBACK_TO_GO_NEG
2531 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002532 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2533 return;
2534 }
2535
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002536 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002537 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002538 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2539 "join-existing-group operation (no ACK for PD "
2540 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002541 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002542 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002543 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002544
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002545 if (adv_id && adv_mac && deferred_session_resp) {
2546 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2547 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2548 " deferred_session_resp='%s'",
2549 MAC2STR(peer), status, adv_id,
2550 deferred_session_resp);
2551 } else if (adv_id && adv_mac) {
2552 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2553 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2554 MAC2STR(peer), status, adv_id);
2555 } else {
2556 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2557 " p2p_dev_addr=" MACSTR " status=%d",
2558 MAC2STR(peer), status);
2559 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002560
Jouni Malinen75ecf522011-06-27 15:19:46 -07002561 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2562 status, 0, 0);
2563}
2564
2565
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002566static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2567{
2568 if (channels == NULL)
2569 return 1; /* Assume no restrictions */
2570 return p2p_channels_includes_freq(channels, freq);
2571
2572}
2573
2574
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002575/**
2576 * Pick the best frequency to use from all the currently used frequencies.
2577 */
2578static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2579 struct wpa_used_freq_data *freqs,
2580 unsigned int num)
2581{
2582 unsigned int i, c;
2583
2584 /* find a candidate freq that is supported by P2P */
2585 for (c = 0; c < num; c++)
2586 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2587 break;
2588
2589 if (c == num)
2590 return 0;
2591
2592 /* once we have a candidate, try to find a 'better' one */
2593 for (i = c + 1; i < num; i++) {
2594 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2595 continue;
2596
2597 /*
2598 * 1. Infrastructure station interfaces have higher preference.
2599 * 2. P2P Clients have higher preference.
2600 * 3. All others.
2601 */
2602 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2603 c = i;
2604 break;
2605 }
2606
2607 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2608 c = i;
2609 }
2610 return freqs[c].freq;
2611}
2612
2613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002614static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2615 const u8 *go_dev_addr, const u8 *ssid,
2616 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002617 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002618 const struct p2p_channels *channels,
2619 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620{
2621 struct wpa_supplicant *wpa_s = ctx;
2622 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002623 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002625 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626
2627 if (!persistent_group) {
2628 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002629 " to join an active group (SSID: %s)",
2630 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2632 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2633 == 0 ||
2634 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2635 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2636 "authorized invitation");
2637 goto accept_inv;
2638 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002639
2640#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002641 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2642 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002643 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002644 wpa_s->p2p_wps_method = WPS_NFC;
2645 wpa_s->pending_join_wps_method = WPS_NFC;
2646 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002647 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002648 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002649 bssid, ETH_ALEN);
2650 goto accept_inv;
2651 }
2652#endif /* CONFIG_WPS_NFC */
2653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 /*
2655 * Do not accept the invitation automatically; notify user and
2656 * request approval.
2657 */
2658 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2659 }
2660
2661 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2662 if (grp) {
2663 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2664 "running persistent group");
2665 if (*go)
2666 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2667 goto accept_inv;
2668 }
2669
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002670 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2671 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2672 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2673 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002674 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002675 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2677
2678 for (s = wpa_s->conf->ssid; s; s = s->next) {
2679 if (s->disabled == 2 &&
2680 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2681 s->ssid_len == ssid_len &&
2682 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2683 break;
2684 }
2685
2686 if (!s) {
2687 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2688 " requested reinvocation of an unknown group",
2689 MAC2STR(sa));
2690 return P2P_SC_FAIL_UNKNOWN_GROUP;
2691 }
2692
2693 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2694 *go = 1;
2695 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2696 wpa_printf(MSG_DEBUG, "P2P: The only available "
2697 "interface is already in use - reject "
2698 "invitation");
2699 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2700 }
2701 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2702 } else if (s->mode == WPAS_MODE_P2P_GO) {
2703 *go = 1;
2704 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2705 {
2706 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2707 "interface address for the group");
2708 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2709 }
2710 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2711 ETH_ALEN);
2712 }
2713
2714accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002715 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2716
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002717 best_freq = 0;
2718 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2719 sizeof(struct wpa_used_freq_data));
2720 if (freqs) {
2721 int num_channels = wpa_s->num_multichan_concurrent;
2722 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2723 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2724 os_free(freqs);
2725 }
2726
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002727 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002728 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002729 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002730 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002731
2732 if (wpa_s->num_multichan_concurrent < 2 ||
2733 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2734 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 -07002735 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002736 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 }
2738
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002739 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2740 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002741 if (*go == 0) {
2742 /* We are the client */
2743 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2744 "running a GO but we are capable of MCC, "
2745 "figure out the best channel to use");
2746 *force_freq = 0;
2747 } else if (!freq_included(channels, *force_freq)) {
2748 /* We are the GO, and *force_freq is not in the
2749 * intersection */
2750 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2751 "in intersection but we are capable of MCC, "
2752 "figure out the best channel to use",
2753 *force_freq);
2754 *force_freq = 0;
2755 }
2756 }
2757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 return P2P_SC_SUCCESS;
2759}
2760
2761
2762static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2763 const u8 *ssid, size_t ssid_len,
2764 const u8 *go_dev_addr, u8 status,
2765 int op_freq)
2766{
2767 struct wpa_supplicant *wpa_s = ctx;
2768 struct wpa_ssid *s;
2769
2770 for (s = wpa_s->conf->ssid; s; s = s->next) {
2771 if (s->disabled == 2 &&
2772 s->ssid_len == ssid_len &&
2773 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2774 break;
2775 }
2776
2777 if (status == P2P_SC_SUCCESS) {
2778 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002779 " was accepted; op_freq=%d MHz, SSID=%s",
2780 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002782 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07002784 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002785 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002787 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002788 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002789 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002790 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 }
2792 return;
2793 }
2794
2795 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2796 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2797 " was rejected (status %u)", MAC2STR(sa), status);
2798 return;
2799 }
2800
2801 if (!s) {
2802 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002803 wpa_msg_global(wpa_s, MSG_INFO,
2804 P2P_EVENT_INVITATION_RECEIVED
2805 "sa=" MACSTR " go_dev_addr=" MACSTR
2806 " bssid=" MACSTR " unknown-network",
2807 MAC2STR(sa), MAC2STR(go_dev_addr),
2808 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002810 wpa_msg_global(wpa_s, MSG_INFO,
2811 P2P_EVENT_INVITATION_RECEIVED
2812 "sa=" MACSTR " go_dev_addr=" MACSTR
2813 " unknown-network",
2814 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002815 }
2816 return;
2817 }
2818
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002819 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002820 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2821 "sa=" MACSTR " persistent=%d freq=%d",
2822 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002823 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002824 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2825 "sa=" MACSTR " persistent=%d",
2826 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002827 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828}
2829
2830
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002831static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2832 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002833 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002834{
2835 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002836 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002837
2838 if (ssid == NULL)
2839 return;
2840
2841 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002842 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002843 ETH_ALEN) == 0)
2844 break;
2845 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002846 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002847 if (ssid->mode != WPAS_MODE_P2P_GO &&
2848 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2849 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2850 "due to invitation result", ssid->id);
2851 wpas_notify_network_removed(wpa_s, ssid);
2852 wpa_config_remove_network(wpa_s->conf, ssid->id);
2853 return;
2854 }
2855 return; /* Peer not found in client list */
2856 }
2857
2858 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002859 "group %d client list%s",
2860 MAC2STR(peer), ssid->id,
2861 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002862 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2863 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2864 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002865 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002866 if (p2p_wpa_s->conf->update_config &&
2867 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002868 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002869}
2870
2871
2872static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2873 const u8 *peer)
2874{
2875 struct wpa_ssid *ssid;
2876
2877 wpa_s = wpa_s->global->p2p_invite_group;
2878 if (wpa_s == NULL)
2879 return; /* No known invitation group */
2880 ssid = wpa_s->current_ssid;
2881 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2882 !ssid->p2p_persistent_group)
2883 return; /* Not operating as a GO in persistent group */
2884 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2885 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002886 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002887}
2888
2889
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002890static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002891 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002892 const u8 *peer, int neg_freq,
2893 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002894{
2895 struct wpa_supplicant *wpa_s = ctx;
2896 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07002897 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898
2899 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002900 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2901 "status=%d " MACSTR,
2902 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002904 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2905 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906 }
2907 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2908
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002909 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2910 status, MAC2STR(peer));
2911 if (wpa_s->pending_invite_ssid_id == -1) {
2912 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2913 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002914 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002917 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2918 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2919 "invitation exchange to indicate readiness for "
2920 "re-invocation");
2921 }
2922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002923 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002924 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2925 ssid = wpa_config_get_network(
2926 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002927 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002928 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 wpas_p2p_remove_pending_group_interface(wpa_s);
2930 return;
2931 }
2932
2933 ssid = wpa_config_get_network(wpa_s->conf,
2934 wpa_s->pending_invite_ssid_id);
2935 if (ssid == NULL) {
2936 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2937 "data matching with invitation");
2938 return;
2939 }
2940
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002941 /*
2942 * The peer could have missed our ctrl::ack frame for Invitation
2943 * Response and continue retransmitting the frame. To reduce the
2944 * likelihood of the peer not getting successful TX status for the
2945 * Invitation Response frame, wait a short time here before starting
2946 * the persistent group so that we will remain on the current channel to
2947 * acknowledge any possible retransmission from the peer.
2948 */
2949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2950 "starting persistent group");
2951 os_sleep(0, 50000);
2952
Dmitry Shmidt15907092014-03-25 10:42:57 -07002953 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2954 freq_included(channels, neg_freq))
2955 freq = neg_freq;
2956 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
2957 freq_included(channels, peer_oper_freq))
2958 freq = peer_oper_freq;
2959 else
2960 freq = 0;
2961
2962 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
2963 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002965 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002966 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002967 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002968 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2969 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002970 ssid->mode == WPAS_MODE_P2P_GO ?
2971 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2972 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973}
2974
2975
Dmitry Shmidt04949592012-07-19 12:16:46 -07002976static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2977 unsigned int freq)
2978{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002979 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
2980 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002981 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002982}
2983
2984
2985static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2986{
2987 reg->channel[reg->channels] = chan;
2988 reg->channels++;
2989}
2990
2991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002992static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002993 struct p2p_channels *chan,
2994 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995{
2996 int i, cla = 0;
2997
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 wpa_s->global->p2p_24ghz_social_channels = 1;
2999
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003000 os_memset(cli_chan, 0, sizeof(*cli_chan));
3001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003002 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3003 "band");
3004
3005 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3006 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003007 chan->reg_class[cla].channels = 0;
3008 for (i = 0; i < 11; i++) {
3009 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3010 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3011 }
3012 if (chan->reg_class[cla].channels)
3013 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003014
3015 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3016 "band");
3017
3018 /* Operating class 115 - 5 GHz, channels 36-48 */
3019 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003020 chan->reg_class[cla].channels = 0;
3021 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3022 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3023 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3024 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3025 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3026 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3027 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3028 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3029 if (chan->reg_class[cla].channels)
3030 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003031
3032 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3033 "band");
3034
3035 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3036 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003037 chan->reg_class[cla].channels = 0;
3038 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3039 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3040 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3041 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3042 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3043 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3044 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3045 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3046 if (chan->reg_class[cla].channels)
3047 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048
3049 chan->reg_classes = cla;
3050 return 0;
3051}
3052
3053
3054static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3055 u16 num_modes,
3056 enum hostapd_hw_mode mode)
3057{
3058 u16 i;
3059
3060 for (i = 0; i < num_modes; i++) {
3061 if (modes[i].mode == mode)
3062 return &modes[i];
3063 }
3064
3065 return NULL;
3066}
3067
3068
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003069enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003070 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003071};
3072
Dmitry Shmidt04949592012-07-19 12:16:46 -07003073static int has_channel(struct wpa_global *global,
3074 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075{
3076 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003077 unsigned int freq;
3078
3079 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3080 chan * 5;
3081 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003082 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083
3084 for (i = 0; i < mode->num_channels; i++) {
3085 if (mode->channels[i].chan == chan) {
3086 if (flags)
3087 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003088 if (mode->channels[i].flag &
3089 (HOSTAPD_CHAN_DISABLED |
3090 HOSTAPD_CHAN_RADAR))
3091 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003092 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3093 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003094 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 }
3096 }
3097
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003098 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099}
3100
3101
3102struct p2p_oper_class_map {
3103 enum hostapd_hw_mode mode;
3104 u8 op_class;
3105 u8 min_chan;
3106 u8 max_chan;
3107 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003108 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003109};
3110
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003111static const struct p2p_oper_class_map op_class[] = {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003112 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003113#if 0 /* Do not enable HT40 on 2 GHz for now */
3114 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3115 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3116#endif
3117 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3118 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3119 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3120 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3121 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3122 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003123
3124 /*
3125 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3126 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3127 * 80 MHz, but currently use the following definition for simplicity
3128 * (these center frequencies are not actual channels, which makes
3129 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3130 * removing invalid channels.
3131 */
3132 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003133 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003134 { -1, 0, 0, 0, 0, BW20 }
3135};
3136
3137
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003138static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3139 struct hostapd_hw_modes *mode,
3140 u8 channel)
3141{
3142 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3143 unsigned int i;
3144
3145 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3146 return 0;
3147
3148 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3149 /*
3150 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3151 * so the center channel is 6 channels away from the start/end.
3152 */
3153 if (channel >= center_channels[i] - 6 &&
3154 channel <= center_channels[i] + 6)
3155 return center_channels[i];
3156
3157 return 0;
3158}
3159
3160
3161static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3162 struct hostapd_hw_modes *mode,
3163 u8 channel, u8 bw)
3164{
3165 u8 center_chan;
3166 int i, flags;
3167 enum chan_allowed res, ret = ALLOWED;
3168
3169 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3170 if (!center_chan)
3171 return NOT_ALLOWED;
3172 if (center_chan >= 58 && center_chan <= 138)
3173 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3174
3175 /* check all the channels are available */
3176 for (i = 0; i < 4; i++) {
3177 int adj_chan = center_chan - 6 + i * 4;
3178
3179 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3180 if (res == NOT_ALLOWED)
3181 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003182 if (res == NO_IR)
3183 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003184
3185 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3186 return NOT_ALLOWED;
3187 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3188 return NOT_ALLOWED;
3189 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3190 return NOT_ALLOWED;
3191 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3192 return NOT_ALLOWED;
3193 }
3194
3195 return ret;
3196}
3197
3198
3199static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3200 struct hostapd_hw_modes *mode,
3201 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003202{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003203 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003204 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003205
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003206 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3207 if (bw == BW40MINUS) {
3208 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3209 return NOT_ALLOWED;
3210 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3211 } else if (bw == BW40PLUS) {
3212 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3213 return NOT_ALLOWED;
3214 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3215 } else if (bw == BW80) {
3216 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3217 }
3218
3219 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3220 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003221 if (res == NO_IR || res2 == NO_IR)
3222 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003223 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003224}
3225
3226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003227static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003228 struct p2p_channels *chan,
3229 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003230{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003231 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003232 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003233
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003234 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3236 "of all supported channels; assume dualband "
3237 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003238 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239 }
3240
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003241 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242
3243 for (op = 0; op_class[op].op_class; op++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003244 const struct p2p_oper_class_map *o = &op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003246 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003248 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 if (mode == NULL)
3250 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003251 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3252 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003254 enum chan_allowed res;
3255 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3256 if (res == ALLOWED) {
3257 if (reg == NULL) {
3258 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3259 o->op_class);
3260 reg = &chan->reg_class[cla];
3261 cla++;
3262 reg->reg_class = o->op_class;
3263 }
3264 reg->channel[reg->channels] = ch;
3265 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003266 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003267 wpa_s->conf->p2p_add_cli_chan) {
3268 if (cli_reg == NULL) {
3269 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3270 o->op_class);
3271 cli_reg = &cli_chan->reg_class[cli_cla];
3272 cli_cla++;
3273 cli_reg->reg_class = o->op_class;
3274 }
3275 cli_reg->channel[cli_reg->channels] = ch;
3276 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 }
3279 if (reg) {
3280 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3281 reg->channel, reg->channels);
3282 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003283 if (cli_reg) {
3284 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3285 cli_reg->channel, cli_reg->channels);
3286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287 }
3288
3289 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003290 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 return 0;
3293}
3294
3295
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003296int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3297 struct hostapd_hw_modes *mode, u8 channel)
3298{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003299 int op;
3300 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003301
3302 for (op = 0; op_class[op].op_class; op++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003303 const struct p2p_oper_class_map *o = &op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003304 u8 ch;
3305
3306 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3307 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3308 o->bw == BW20 || ch != channel)
3309 continue;
3310 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003311 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003312 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003313 }
3314 }
3315 return 0;
3316}
3317
3318
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003319int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3320 struct hostapd_hw_modes *mode, u8 channel)
3321{
3322 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3323 return 0;
3324
3325 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3326}
3327
3328
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003329static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3330 size_t buf_len)
3331{
3332 struct wpa_supplicant *wpa_s = ctx;
3333
3334 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3335 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3336 break;
3337 }
3338 if (wpa_s == NULL)
3339 return -1;
3340
3341 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3342}
3343
3344
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003345struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3346 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003347{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003348 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3349 struct wpa_ssid *s = wpa_s->current_ssid;
3350 if (s == NULL)
3351 continue;
3352 if (s->mode != WPAS_MODE_P2P_GO &&
3353 s->mode != WPAS_MODE_AP &&
3354 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3355 continue;
3356 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003357 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003358 continue;
3359 return wpa_s;
3360 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003361
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003362 return NULL;
3363
3364}
3365
3366
3367struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3368 const u8 *peer_dev_addr)
3369{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003370 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3371 struct wpa_ssid *ssid = wpa_s->current_ssid;
3372 if (ssid == NULL)
3373 continue;
3374 if (ssid->mode != WPAS_MODE_INFRA)
3375 continue;
3376 if (wpa_s->wpa_state != WPA_COMPLETED &&
3377 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3378 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003379 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3380 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003381 }
3382
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003383 return NULL;
3384}
3385
3386
3387static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3388{
3389 struct wpa_supplicant *wpa_s = ctx;
3390
3391 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003392}
3393
3394
Dmitry Shmidt18463232014-01-24 12:29:41 -08003395static int wpas_is_concurrent_session_active(void *ctx)
3396{
3397 struct wpa_supplicant *wpa_s = ctx;
3398 struct wpa_supplicant *ifs;
3399
3400 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3401 if (ifs == wpa_s)
3402 continue;
3403 if (ifs->wpa_state > WPA_ASSOCIATED)
3404 return 1;
3405 }
3406 return 0;
3407}
3408
3409
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003410static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3411{
3412 struct wpa_supplicant *wpa_s = ctx;
3413 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3414}
3415
3416
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003417int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3418 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003419{
3420 struct wpa_interface iface;
3421 struct wpa_supplicant *p2pdev_wpa_s;
3422 char ifname[100];
3423 char force_name[100];
3424 int ret;
3425
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003426 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3427 wpa_s->ifname);
3428 if (os_snprintf_error(sizeof(ifname), ret))
3429 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003430 force_name[0] = '\0';
3431 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3432 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3433 force_name, wpa_s->pending_interface_addr, NULL);
3434 if (ret < 0) {
3435 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3436 return ret;
3437 }
3438 os_strlcpy(wpa_s->pending_interface_name, ifname,
3439 sizeof(wpa_s->pending_interface_name));
3440
3441 os_memset(&iface, 0, sizeof(iface));
3442 iface.p2p_mgmt = 1;
3443 iface.ifname = wpa_s->pending_interface_name;
3444 iface.driver = wpa_s->driver->name;
3445 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003446
3447 /*
3448 * If a P2P Device configuration file was given, use it as the interface
3449 * configuration file (instead of using parent's configuration file.
3450 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003451 if (conf_p2p_dev) {
3452 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003453 iface.ctrl_interface = NULL;
3454 } else {
3455 iface.confname = wpa_s->confname;
3456 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3457 }
3458 iface.conf_p2p_dev = NULL;
3459
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003460 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003461 if (!p2pdev_wpa_s) {
3462 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3463 return -1;
3464 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003465
3466 wpa_s->pending_interface_name[0] = '\0';
3467 return 0;
3468}
3469
3470
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003471static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3472 const u8 *noa, size_t noa_len)
3473{
3474 struct wpa_supplicant *wpa_s, *intf = ctx;
3475 char hex[100];
3476
3477 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3478 if (wpa_s->waiting_presence_resp)
3479 break;
3480 }
3481 if (!wpa_s) {
3482 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3483 return;
3484 }
3485 wpa_s->waiting_presence_resp = 0;
3486
3487 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3488 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3489 " status=%u noa=%s", MAC2STR(src), status, hex);
3490}
3491
3492
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003493static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3494 size_t ssid_len, u8 *go_dev_addr,
3495 u8 *ret_ssid, size_t *ret_ssid_len)
3496{
3497 struct wpa_supplicant *wpa_s = ctx;
3498 struct wpa_ssid *s;
3499
3500 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3501 if (s) {
3502 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3503 *ret_ssid_len = s->ssid_len;
3504 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3505 return 1;
3506 }
3507
3508 return 0;
3509}
3510
3511
3512static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3513 u8 *ssid, size_t *ssid_len, int *group_iface)
3514{
3515 struct wpa_supplicant *wpa_s = ctx;
3516 struct wpa_ssid *s;
3517 u8 bssid[ETH_ALEN];
3518
3519 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3520 if (!s) {
3521 s = wpas_p2p_get_persistent_go(wpa_s);
3522 if (s)
3523 os_memcpy(bssid, s->bssid, ETH_ALEN);
3524 }
3525
3526 *group_iface = wpas_p2p_create_iface(wpa_s);
3527 if (!s)
3528 return 0;
3529
3530 os_memcpy(intended_addr, bssid, ETH_ALEN);
3531 os_memcpy(ssid, s->ssid, s->ssid_len);
3532 *ssid_len = s->ssid_len;
3533
3534 return 1;
3535}
3536
3537
3538static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3539 const u8 *ssid, size_t ssid_len)
3540{
3541 struct wpa_supplicant *wpa_s = ctx;
3542 struct wpa_ssid *s;
3543 int save_config = 0;
3544 size_t i;
3545
3546 /* Start with our first choice of Persistent Groups */
3547 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3548 if (go && ssid && ssid_len &&
3549 s->ssid_len == ssid_len &&
3550 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3551 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3552 break;
3553
3554 /* Remove stale persistent group */
3555 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3556 wpa_config_remove_network(wpa_s->conf, s->id);
3557 save_config = 1;
3558 continue;
3559 }
3560
3561 for (i = 0; i < s->num_p2p_clients; i++) {
3562 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3563 peer, ETH_ALEN) != 0)
3564 continue;
3565
3566 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3567 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3568 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3569 break;
3570 }
3571 s->num_p2p_clients--;
3572 save_config = 1;
3573 }
3574
3575 if (save_config)
3576 p2p_config_write(wpa_s);
3577
3578 /* Return TRUE if valid SSID remains */
3579 return s != NULL;
3580}
3581
3582
3583static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3584 const u8 *adv_mac, const u8 *ses_mac,
3585 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3586 u8 conncap, int passwd_id,
3587 const u8 *persist_ssid,
3588 size_t persist_ssid_size, int response_done,
3589 int prov_start, const char *session_info)
3590{
3591 struct wpa_supplicant *wpa_s = ctx;
3592 u8 mac[ETH_ALEN];
3593 struct wpa_ssid *persistent_go, *stale, *s;
3594 int save_config = 0;
3595 struct wpa_supplicant *go_wpa_s;
3596
3597 if (!dev)
3598 return;
3599
3600 os_memset(mac, 0, ETH_ALEN);
3601 if (!adv_mac)
3602 adv_mac = mac;
3603 if (!ses_mac)
3604 ses_mac = mac;
3605 if (!grp_mac)
3606 grp_mac = mac;
3607
3608 if (prov_start) {
3609 if (session_info == NULL) {
3610 wpa_msg_global(wpa_s, MSG_INFO,
3611 P2P_EVENT_P2PS_PROVISION_START MACSTR
3612 " adv_id=%x conncap=%x"
3613 " adv_mac=" MACSTR
3614 " session=%x mac=" MACSTR
3615 " dev_passwd_id=%d",
3616 MAC2STR(dev), adv_id, conncap,
3617 MAC2STR(adv_mac),
3618 ses_id, MAC2STR(ses_mac),
3619 passwd_id);
3620 } else {
3621 wpa_msg_global(wpa_s, MSG_INFO,
3622 P2P_EVENT_P2PS_PROVISION_START MACSTR
3623 " adv_id=%x conncap=%x"
3624 " adv_mac=" MACSTR
3625 " session=%x mac=" MACSTR
3626 " dev_passwd_id=%d info='%s'",
3627 MAC2STR(dev), adv_id, conncap,
3628 MAC2STR(adv_mac),
3629 ses_id, MAC2STR(ses_mac),
3630 passwd_id, session_info);
3631 }
3632 return;
3633 }
3634
3635 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3636 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3637
3638 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3639 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3640 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3641
3642 if (persistent_go && !persistent_go->num_p2p_clients) {
3643 /* remove empty persistent GO */
3644 wpa_config_remove_network(wpa_s->conf,
3645 persistent_go->id);
3646 }
3647
3648 wpa_msg_global(wpa_s, MSG_INFO,
3649 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3650 " status=%d"
3651 " adv_id=%x adv_mac=" MACSTR
3652 " session=%x mac=" MACSTR,
3653 MAC2STR(dev), status,
3654 adv_id, MAC2STR(adv_mac),
3655 ses_id, MAC2STR(ses_mac));
3656 return;
3657 }
3658
3659 /* Clean up stale persistent groups with this device */
3660 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3661 persist_ssid_size);
3662 for (;;) {
3663 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3664 if (!stale)
3665 break;
3666
3667 if (s && s->ssid_len == stale->ssid_len &&
3668 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3669 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3670 break;
3671
3672 /* Remove stale persistent group */
3673 if (stale->mode != WPAS_MODE_P2P_GO ||
3674 stale->num_p2p_clients <= 1) {
3675 wpa_config_remove_network(wpa_s->conf, stale->id);
3676 } else {
3677 size_t i;
3678
3679 for (i = 0; i < stale->num_p2p_clients; i++) {
3680 if (os_memcmp(stale->p2p_client_list +
3681 i * ETH_ALEN,
3682 dev, ETH_ALEN) == 0) {
3683 os_memmove(stale->p2p_client_list +
3684 i * ETH_ALEN,
3685 stale->p2p_client_list +
3686 (i + 1) * ETH_ALEN,
3687 (stale->num_p2p_clients -
3688 i - 1) * ETH_ALEN);
3689 break;
3690 }
3691 }
3692 stale->num_p2p_clients--;
3693 }
3694 save_config = 1;
3695 }
3696
3697 if (save_config)
3698 p2p_config_write(wpa_s);
3699
3700 if (s) {
3701 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3702 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3703
3704 if (persistent_go && s != persistent_go &&
3705 !persistent_go->num_p2p_clients) {
3706 /* remove empty persistent GO */
3707 wpa_config_remove_network(wpa_s->conf,
3708 persistent_go->id);
3709 /* Save config */
3710 }
3711
3712 wpa_msg_global(wpa_s, MSG_INFO,
3713 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3714 " status=%d"
3715 " adv_id=%x adv_mac=" MACSTR
3716 " session=%x mac=" MACSTR
3717 " persist=%d",
3718 MAC2STR(dev), status,
3719 adv_id, MAC2STR(adv_mac),
3720 ses_id, MAC2STR(ses_mac), s->id);
3721 return;
3722 }
3723
3724 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3725 const char *go_ifname = NULL;
3726 if (!go_wpa_s) {
3727 wpa_s->global->pending_p2ps_group = 1;
3728
3729 if (wpa_s->conf->p2p_no_group_iface)
3730 go_ifname = wpa_s->ifname;
3731 else if (wpa_s->pending_interface_name[0])
3732 go_ifname = wpa_s->pending_interface_name;
3733
3734 if (!go_ifname) {
3735 wpas_p2ps_prov_complete(
3736 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3737 dev, adv_mac, ses_mac,
3738 NULL, adv_id, ses_id, 0, 0,
3739 NULL, 0, 0, 0, NULL);
3740 return;
3741 }
3742
3743 /* If PD Resp complete, start up the GO */
3744 if (response_done && persistent_go) {
3745 wpas_p2p_group_add_persistent(
3746 wpa_s, persistent_go,
3747 0, 0, 0, 0, 0, NULL,
3748 persistent_go->mode ==
3749 WPAS_MODE_P2P_GO ?
3750 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3751 0);
3752 } else if (response_done) {
3753 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3754 }
3755
3756 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3757 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3758 wpa_s->p2ps_join_addr_valid = 1;
3759 wpa_dbg(wpa_s, MSG_DEBUG,
3760 "P2PS: Saving PIN for " MACSTR,
3761 MAC2STR(dev));
3762 }
3763 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3764 go_ifname = go_wpa_s->ifname;
3765
3766 wpa_dbg(go_wpa_s, MSG_DEBUG,
3767 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3768 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3769 NULL, 0, 0);
3770
3771 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3772 wpa_s->p2ps_join_addr_valid = 1;
3773 wpa_dbg(wpa_s, MSG_DEBUG,
3774 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3775 }
3776
3777 wpa_msg_global(wpa_s, MSG_INFO,
3778 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3779 " status=%d conncap=%x"
3780 " adv_id=%x adv_mac=" MACSTR
3781 " session=%x mac=" MACSTR
3782 " dev_passwd_id=%d go=%s",
3783 MAC2STR(dev), status, conncap,
3784 adv_id, MAC2STR(adv_mac),
3785 ses_id, MAC2STR(ses_mac),
3786 passwd_id, go_ifname);
3787 return;
3788 }
3789
3790 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3791 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3792
3793 if (persistent_go && !persistent_go->num_p2p_clients) {
3794 /* remove empty persistent GO */
3795 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3796 }
3797
3798 if (conncap == P2PS_SETUP_CLIENT) {
3799 wpa_msg_global(wpa_s, MSG_INFO,
3800 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3801 " status=%d conncap=%x"
3802 " adv_id=%x adv_mac=" MACSTR
3803 " session=%x mac=" MACSTR
3804 " dev_passwd_id=%d join=" MACSTR,
3805 MAC2STR(dev), status, conncap,
3806 adv_id, MAC2STR(adv_mac),
3807 ses_id, MAC2STR(ses_mac),
3808 passwd_id, MAC2STR(grp_mac));
3809 } else {
3810 wpa_msg_global(wpa_s, MSG_INFO,
3811 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3812 " status=%d conncap=%x"
3813 " adv_id=%x adv_mac=" MACSTR
3814 " session=%x mac=" MACSTR
3815 " dev_passwd_id=%d",
3816 MAC2STR(dev), status, conncap,
3817 adv_id, MAC2STR(adv_mac),
3818 ses_id, MAC2STR(ses_mac),
3819 passwd_id);
3820 }
3821}
3822
3823
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003824static int _wpas_p2p_in_progress(void *ctx)
3825{
3826 struct wpa_supplicant *wpa_s = ctx;
3827 return wpas_p2p_in_progress(wpa_s);
3828}
3829
3830
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003831static int wpas_prov_disc_resp_cb(void *ctx)
3832{
3833 struct wpa_supplicant *wpa_s = ctx;
3834 struct wpa_ssid *persistent_go;
3835
3836 if (!wpa_s->global->pending_p2ps_group)
3837 return 0;
3838
3839 wpa_s->global->pending_p2ps_group = 0;
3840
3841 if (wpas_p2p_get_go_group(wpa_s))
3842 return 0;
3843 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3844
3845 if (persistent_go) {
3846 wpas_p2p_group_add_persistent(
3847 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
3848 persistent_go->mode == WPAS_MODE_P2P_GO ?
3849 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3850 } else {
3851 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3852 }
3853
3854 return 1;
3855}
3856
3857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858/**
3859 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3860 * @global: Pointer to global data from wpa_supplicant_init()
3861 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3862 * Returns: 0 on success, -1 on failure
3863 */
3864int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3865{
3866 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867 int i;
3868
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003869 if (wpa_s->conf->p2p_disabled)
3870 return 0;
3871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3873 return 0;
3874
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875 if (global->p2p)
3876 return 0;
3877
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003878 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003879 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003880 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881 p2p.p2p_scan = wpas_p2p_scan;
3882 p2p.send_action = wpas_send_action;
3883 p2p.send_action_done = wpas_send_action_done;
3884 p2p.go_neg_completed = wpas_go_neg_completed;
3885 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3886 p2p.dev_found = wpas_dev_found;
3887 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003888 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 p2p.start_listen = wpas_start_listen;
3890 p2p.stop_listen = wpas_stop_listen;
3891 p2p.send_probe_resp = wpas_send_probe_resp;
3892 p2p.sd_request = wpas_sd_request;
3893 p2p.sd_response = wpas_sd_response;
3894 p2p.prov_disc_req = wpas_prov_disc_req;
3895 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003896 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 p2p.invitation_process = wpas_invitation_process;
3898 p2p.invitation_received = wpas_invitation_received;
3899 p2p.invitation_result = wpas_invitation_result;
3900 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003901 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003902 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003903 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003904 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003905 p2p.get_persistent_group = wpas_get_persistent_group;
3906 p2p.get_go_info = wpas_get_go_info;
3907 p2p.remove_stale_groups = wpas_remove_stale_groups;
3908 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
3909 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
3910 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003911
3912 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003913 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914 p2p.dev_name = wpa_s->conf->device_name;
3915 p2p.manufacturer = wpa_s->conf->manufacturer;
3916 p2p.model_name = wpa_s->conf->model_name;
3917 p2p.model_number = wpa_s->conf->model_number;
3918 p2p.serial_number = wpa_s->conf->serial_number;
3919 if (wpa_s->wps) {
3920 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3921 p2p.config_methods = wpa_s->wps->config_methods;
3922 }
3923
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003924 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3925 wpa_printf(MSG_ERROR,
3926 "P2P: Failed to configure supported channel list");
3927 return -1;
3928 }
3929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930 if (wpa_s->conf->p2p_listen_reg_class &&
3931 wpa_s->conf->p2p_listen_channel) {
3932 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3933 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003934 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003935 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003936 /*
3937 * Pick one of the social channels randomly as the listen
3938 * channel.
3939 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003940 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
3941 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003942 wpa_printf(MSG_INFO,
3943 "P2P: No social channels supported by the driver - do not enable P2P");
3944 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003945 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003946 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003948 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
3949 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950
3951 if (wpa_s->conf->p2p_oper_reg_class &&
3952 wpa_s->conf->p2p_oper_channel) {
3953 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3954 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3955 p2p.cfg_op_channel = 1;
3956 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3957 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3958
3959 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003961 * Use random operation channel from 2.4 GHz band social
3962 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
3963 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003965 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
3966 &p2p.op_channel) != 0) {
3967 wpa_printf(MSG_ERROR,
3968 "P2P: Failed to select random social channel as operation channel");
3969 return -1;
3970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 p2p.cfg_op_channel = 0;
3972 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3973 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3974 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003975
3976 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3977 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3978 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3979 }
3980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3982 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3983 p2p.country[2] = 0x04;
3984 } else
3985 os_memcpy(p2p.country, "XX\x04", 3);
3986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3988 WPS_DEV_TYPE_LEN);
3989
3990 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3991 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3992 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3993
3994 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3995 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3996
3997 p2p.max_peers = 100;
3998
3999 if (wpa_s->conf->p2p_ssid_postfix) {
4000 p2p.ssid_postfix_len =
4001 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4002 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4003 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4004 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4005 p2p.ssid_postfix_len);
4006 }
4007
4008 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4009
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004010 p2p.max_listen = wpa_s->max_remain_on_chan;
4011
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004012 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4013 wpa_s->conf->p2p_passphrase_len <= 63)
4014 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4015 else
4016 p2p.passphrase_len = 8;
4017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004018 global->p2p = p2p_init(&p2p);
4019 if (global->p2p == NULL)
4020 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004021 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004022
4023 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4024 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4025 continue;
4026 p2p_add_wps_vendor_extension(
4027 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4028 }
4029
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004030 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032 return 0;
4033}
4034
4035
4036/**
4037 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4038 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4039 *
4040 * This function deinitialize per-interface P2P data.
4041 */
4042void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4043{
4044 if (wpa_s->driver && wpa_s->drv_priv)
4045 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004046
4047 if (wpa_s->go_params) {
4048 /* Clear any stored provisioning info */
4049 p2p_clear_provisioning_info(
4050 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004051 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004052 }
4053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004054 os_free(wpa_s->go_params);
4055 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004056 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4058 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4059 wpa_s->p2p_long_listen = 0;
4060 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4061 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4062 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004063 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004064 wpas_p2p_listen_work_done(wpa_s);
4065 if (wpa_s->p2p_send_action_work) {
4066 os_free(wpa_s->p2p_send_action_work->ctx);
4067 radio_work_done(wpa_s->p2p_send_action_work);
4068 wpa_s->p2p_send_action_work = NULL;
4069 }
4070 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004071
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004072 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4073 wpa_s->p2p_oob_dev_pw = NULL;
4074
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004075 os_free(wpa_s->p2p_group_common_freqs);
4076 wpa_s->p2p_group_common_freqs = NULL;
4077 wpa_s->p2p_group_common_freqs_num = 0;
4078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004079 /* TODO: remove group interface from the driver if this wpa_s instance
4080 * is on top of a P2P group interface */
4081}
4082
4083
4084/**
4085 * wpas_p2p_deinit_global - Deinitialize global P2P module
4086 * @global: Pointer to global data from wpa_supplicant_init()
4087 *
4088 * This function deinitializes the global (per device) P2P module.
4089 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004090static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091{
4092 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004094 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004095
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004096 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004097
4098 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4100 wpa_s = wpa_s->next;
4101 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 tmp = global->ifaces;
4103 while (tmp &&
4104 (tmp == wpa_s ||
4105 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4106 tmp = tmp->next;
4107 }
4108 if (tmp == NULL)
4109 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004110 /* Disconnect from the P2P group and deinit the interface */
4111 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004112 }
4113
4114 /*
4115 * Deinit GO data on any possibly remaining interface (if main
4116 * interface is used as GO).
4117 */
4118 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4119 if (wpa_s->ap_iface)
4120 wpas_p2p_group_deinit(wpa_s);
4121 }
4122
4123 p2p_deinit(global->p2p);
4124 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004125 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004126}
4127
4128
4129static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4130{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004131 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4132 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004133 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 if (wpa_s->drv_flags &
4135 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4136 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4137 return 1; /* P2P group requires a new interface in every case
4138 */
4139 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4140 return 0; /* driver does not support concurrent operations */
4141 if (wpa_s->global->ifaces->next)
4142 return 1; /* more that one interface already in use */
4143 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4144 return 1; /* this interface is already in use */
4145 return 0;
4146}
4147
4148
4149static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4150 const u8 *peer_addr,
4151 enum p2p_wps_method wps_method,
4152 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004153 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004154 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004156 if (persistent_group && wpa_s->conf->persistent_reconnect)
4157 persistent_group = 2;
4158
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004159 /*
4160 * Increase GO config timeout if HT40 is used since it takes some time
4161 * to scan channels for coex purposes before the BSS can be started.
4162 */
4163 p2p_set_config_timeout(wpa_s->global->p2p,
4164 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4165
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4167 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004168 persistent_group, ssid ? ssid->ssid : NULL,
4169 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004170 wpa_s->p2p_pd_before_go_neg, pref_freq,
4171 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4172 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004173}
4174
4175
4176static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4177 const u8 *peer_addr,
4178 enum p2p_wps_method wps_method,
4179 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004181 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004183 if (persistent_group && wpa_s->conf->persistent_reconnect)
4184 persistent_group = 2;
4185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004186 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4187 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004188 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004189 ssid ? ssid->ssid_len : 0, pref_freq,
4190 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4191 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004192}
4193
4194
4195static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4196{
4197 wpa_s->p2p_join_scan_count++;
4198 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4199 wpa_s->p2p_join_scan_count);
4200 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4201 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4202 " for join operationg - stop join attempt",
4203 MAC2STR(wpa_s->pending_join_iface_addr));
4204 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004205 if (wpa_s->p2p_auto_pd) {
4206 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004207 wpa_msg_global(wpa_s, MSG_INFO,
4208 P2P_EVENT_PROV_DISC_FAILURE
4209 " p2p_dev_addr=" MACSTR " status=N/A",
4210 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004211 return;
4212 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004213 wpa_msg_global(wpa_s->parent, MSG_INFO,
4214 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215 }
4216}
4217
4218
Dmitry Shmidt04949592012-07-19 12:16:46 -07004219static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4220{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004221 int res;
4222 unsigned int num, i;
4223 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004224
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004225 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4226 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004227 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004228 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004229
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004230 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4231 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004232 if (!freqs)
4233 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004234
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004235 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4236 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004237
4238 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004239 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004240 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4241 freq);
4242 res = 0;
4243 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004244 }
4245 }
4246
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004247 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004248 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004249
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004250exit_free:
4251 os_free(freqs);
4252 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004253}
4254
4255
4256static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4257 const u8 *peer_dev_addr)
4258{
4259 struct wpa_bss *bss;
4260 int updated;
4261
4262 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4263 if (bss == NULL)
4264 return -1;
4265 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4266 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4267 "last scan");
4268 return 0;
4269 }
4270
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004271 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4272 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004273 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4274 "%ld.%06ld (%supdated in last scan)",
4275 bss->last_update.sec, bss->last_update.usec,
4276 updated ? "": "not ");
4277
4278 return updated;
4279}
4280
4281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4283 struct wpa_scan_results *scan_res)
4284{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004285 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 int freq;
4287 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004288
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4290
4291 if (wpa_s->global->p2p_disabled)
4292 return;
4293
Dmitry Shmidt04949592012-07-19 12:16:46 -07004294 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4295 scan_res ? (int) scan_res->num : -1,
4296 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297
4298 if (scan_res)
4299 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4300
Dmitry Shmidt04949592012-07-19 12:16:46 -07004301 if (wpa_s->p2p_auto_pd) {
4302 int join = wpas_p2p_peer_go(wpa_s,
4303 wpa_s->pending_join_dev_addr);
4304 if (join == 0 &&
4305 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4306 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004307 bss = wpa_bss_get_bssid_latest(
4308 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004309 if (bss) {
4310 freq = bss->freq;
4311 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4312 "the peer " MACSTR " at %d MHz",
4313 wpa_s->auto_pd_scan_retry,
4314 MAC2STR(wpa_s->
4315 pending_join_dev_addr),
4316 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004317 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004318 return;
4319 }
4320 }
4321
4322 if (join < 0)
4323 join = 0;
4324
4325 wpa_s->p2p_auto_pd = 0;
4326 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4327 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4328 MAC2STR(wpa_s->pending_join_dev_addr), join);
4329 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004330 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004331 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004332 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004333 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004334 wpa_msg_global(wpa_s, MSG_INFO,
4335 P2P_EVENT_PROV_DISC_FAILURE
4336 " p2p_dev_addr=" MACSTR " status=N/A",
4337 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004338 }
4339 return;
4340 }
4341
4342 if (wpa_s->p2p_auto_join) {
4343 int join = wpas_p2p_peer_go(wpa_s,
4344 wpa_s->pending_join_dev_addr);
4345 if (join < 0) {
4346 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4347 "running a GO -> use GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004348 wpa_msg_global(wpa_s->parent, MSG_INFO,
4349 P2P_EVENT_FALLBACK_TO_GO_NEG
4350 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004351 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4352 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4353 wpa_s->p2p_persistent_group, 0, 0, 0,
4354 wpa_s->p2p_go_intent,
4355 wpa_s->p2p_connect_freq,
4356 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004357 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004358 wpa_s->p2p_go_ht40,
4359 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004360 return;
4361 }
4362
4363 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4364 "try to join the group", join ? "" :
4365 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004366 if (!join) {
4367 wpa_msg_global(wpa_s->parent, MSG_INFO,
4368 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004370 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004371 }
4372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4374 wpa_s->pending_join_iface_addr);
4375 if (freq < 0 &&
4376 p2p_get_interface_addr(wpa_s->global->p2p,
4377 wpa_s->pending_join_dev_addr,
4378 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004379 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4380 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004381 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4382 "address for join from " MACSTR " to " MACSTR
4383 " based on newly discovered P2P peer entry",
4384 MAC2STR(wpa_s->pending_join_iface_addr),
4385 MAC2STR(iface_addr));
4386 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4387 ETH_ALEN);
4388
4389 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4390 wpa_s->pending_join_iface_addr);
4391 }
4392 if (freq >= 0) {
4393 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4394 "from P2P peer table: %d MHz", freq);
4395 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004396 if (wpa_s->p2p_join_ssid_len) {
4397 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4398 MACSTR " and SSID %s",
4399 MAC2STR(wpa_s->pending_join_iface_addr),
4400 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4401 wpa_s->p2p_join_ssid_len));
4402 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4403 wpa_s->p2p_join_ssid,
4404 wpa_s->p2p_join_ssid_len);
4405 }
4406 if (!bss) {
4407 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4408 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4409 bss = wpa_bss_get_bssid_latest(wpa_s,
4410 wpa_s->pending_join_iface_addr);
4411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412 if (bss) {
4413 freq = bss->freq;
4414 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004415 "from BSS table: %d MHz (SSID %s)", freq,
4416 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417 }
4418 if (freq > 0) {
4419 u16 method;
4420
Dmitry Shmidt04949592012-07-19 12:16:46 -07004421 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004422 wpa_msg_global(wpa_s->parent, MSG_INFO,
4423 P2P_EVENT_GROUP_FORMATION_FAILURE
4424 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004425 return;
4426 }
4427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4429 "prior to joining an existing group (GO " MACSTR
4430 " freq=%u MHz)",
4431 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4432 wpa_s->pending_pd_before_join = 1;
4433
4434 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 case WPS_PIN_DISPLAY:
4436 method = WPS_CONFIG_KEYPAD;
4437 break;
4438 case WPS_PIN_KEYPAD:
4439 method = WPS_CONFIG_DISPLAY;
4440 break;
4441 case WPS_PBC:
4442 method = WPS_CONFIG_PUSHBUTTON;
4443 break;
4444 default:
4445 method = 0;
4446 break;
4447 }
4448
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004449 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4450 wpa_s->pending_join_dev_addr) ==
4451 method)) {
4452 /*
4453 * We have already performed provision discovery for
4454 * joining the group. Proceed directly to join
4455 * operation without duplicated provision discovery. */
4456 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4457 "with " MACSTR " already done - proceed to "
4458 "join",
4459 MAC2STR(wpa_s->pending_join_dev_addr));
4460 wpa_s->pending_pd_before_join = 0;
4461 goto start;
4462 }
4463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004465 wpa_s->pending_join_dev_addr,
4466 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004467 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004468 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4469 "Discovery Request before joining an "
4470 "existing group");
4471 wpa_s->pending_pd_before_join = 0;
4472 goto start;
4473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004474 return;
4475 }
4476
4477 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4478 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4479 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4480 wpas_p2p_check_join_scan_limit(wpa_s);
4481 return;
4482
4483start:
4484 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004485 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004486}
4487
4488
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004489static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4490 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004491{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 int ret;
4493 struct wpa_driver_scan_params params;
4494 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004495 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004496 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497
4498 os_memset(&params, 0, sizeof(params));
4499
4500 /* P2P Wildcard SSID */
4501 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004502 if (ssid && ssid_len) {
4503 params.ssids[0].ssid = ssid;
4504 params.ssids[0].ssid_len = ssid_len;
4505 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4506 wpa_s->p2p_join_ssid_len = ssid_len;
4507 } else {
4508 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4509 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4510 wpa_s->p2p_join_ssid_len = 0;
4511 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512
4513 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004514 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4515 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4516 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517 if (wps_ie == NULL) {
4518 wpas_p2p_scan_res_join(wpa_s, NULL);
4519 return;
4520 }
4521
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004522 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4523 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004524 if (ies == NULL) {
4525 wpabuf_free(wps_ie);
4526 wpas_p2p_scan_res_join(wpa_s, NULL);
4527 return;
4528 }
4529 wpabuf_put_buf(ies, wps_ie);
4530 wpabuf_free(wps_ie);
4531
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004532 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004534 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 params.extra_ies = wpabuf_head(ies);
4536 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004537
4538 if (!freq) {
4539 int oper_freq;
4540 /*
4541 * If freq is not provided, check the operating freq of the GO
4542 * and use a single channel scan on if possible.
4543 */
4544 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4545 wpa_s->pending_join_iface_addr);
4546 if (oper_freq > 0)
4547 freq = oper_freq;
4548 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004549 if (freq > 0) {
4550 freqs[0] = freq;
4551 params.freqs = freqs;
4552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553
4554 /*
4555 * Run a scan to update BSS table and start Provision Discovery once
4556 * the new scan results become available.
4557 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004558 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004559 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004560 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004561 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004562 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004563 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564
4565 wpabuf_free(ies);
4566
4567 if (ret) {
4568 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4569 "try again later");
4570 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4571 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4572 wpas_p2p_check_join_scan_limit(wpa_s);
4573 }
4574}
4575
4576
Dmitry Shmidt04949592012-07-19 12:16:46 -07004577static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4578{
4579 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004580 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004581}
4582
4583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004585 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004586 int auto_join, int op_freq,
4587 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004588{
4589 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004590 MACSTR " dev " MACSTR " op_freq=%d)%s",
4591 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004592 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004593 if (ssid && ssid_len) {
4594 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4595 wpa_ssid_txt(ssid, ssid_len));
4596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004597
Dmitry Shmidt04949592012-07-19 12:16:46 -07004598 wpa_s->p2p_auto_pd = 0;
4599 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004600 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4601 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4602 wpa_s->pending_join_wps_method = wps_method;
4603
4604 /* Make sure we are not running find during connection establishment */
4605 wpas_p2p_stop_find(wpa_s);
4606
4607 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004608 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 return 0;
4610}
4611
4612
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004613static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4614 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615{
4616 struct wpa_supplicant *group;
4617 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619
4620 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4621 if (group == NULL)
4622 return -1;
4623 if (group != wpa_s) {
4624 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4625 sizeof(group->p2p_pin));
4626 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004627 } else {
4628 /*
4629 * Need to mark the current interface for p2p_group_formation
4630 * when a separate group interface is not used. This is needed
4631 * to allow p2p_cancel stop a pending p2p_connect-join.
4632 * wpas_p2p_init_group_interface() addresses this for the case
4633 * where a separate group interface is used.
4634 */
4635 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 }
4637
4638 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004639 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640
4641 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004642 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4644 ETH_ALEN);
4645 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004646 if (freq && ssid && ssid_len) {
4647 res.freq = freq;
4648 res.ssid_len = ssid_len;
4649 os_memcpy(res.ssid, ssid, ssid_len);
4650 } else {
4651 bss = wpa_bss_get_bssid_latest(wpa_s,
4652 wpa_s->pending_join_iface_addr);
4653 if (bss) {
4654 res.freq = bss->freq;
4655 res.ssid_len = bss->ssid_len;
4656 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4657 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4658 bss->freq,
4659 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4660 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004661 }
4662
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004663 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4664 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4665 "starting client");
4666 wpa_drv_cancel_remain_on_channel(wpa_s);
4667 wpa_s->off_channel_freq = 0;
4668 wpa_s->roc_waiting_drv_freq = 0;
4669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670 wpas_start_wps_enrollee(group, &res);
4671
4672 /*
4673 * Allow a longer timeout for join-a-running-group than normal 15
4674 * second group formation timeout since the GO may not have authorized
4675 * our connection yet.
4676 */
4677 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4678 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4679 wpa_s, NULL);
4680
4681 return 0;
4682}
4683
4684
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004685static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004686 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004687{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004688 struct wpa_used_freq_data *freqs;
4689 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004690 unsigned int freq_in_use = 0, num, i;
4691
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004692 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4693 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004694 if (!freqs)
4695 return -1;
4696
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004697 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4698 wpa_s->num_multichan_concurrent);
4699
4700 /*
4701 * It is possible that the total number of used frequencies is bigger
4702 * than the number of frequencies used for P2P, so get the system wide
4703 * number of unused frequencies.
4704 */
4705 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4706
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004707 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004708 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4709 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004710
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004711 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004712 int ret;
4713 if (go)
4714 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4715 else
4716 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4717 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004718 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4719 ieee80211_is_dfs(freq)) {
4720 /*
4721 * If freq is a DFS channel and DFS is offloaded
4722 * to the driver, allow P2P GO to use it.
4723 */
4724 wpa_printf(MSG_DEBUG,
4725 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4726 freq);
4727 } else {
4728 wpa_printf(MSG_DEBUG,
4729 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4730 freq);
4731 res = -3;
4732 goto exit_free;
4733 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004734 }
4735
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004736 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004737 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004738 freq_in_use = 1;
4739 }
4740
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004741 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004742 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4743 freq);
4744 res = -2;
4745 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004746 }
4747 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4748 "requested channel (%u MHz)", freq);
4749 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004750 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004751 }
4752
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004753 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004754
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004755 /* We have a candidate frequency to use */
4756 if (best_freq > 0) {
4757 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004758 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004759 best_freq);
4760 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004761 } else {
4762 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 -07004763 best_freq);
4764 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004765 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004766 } else if (num_unused > 0) {
4767 wpa_printf(MSG_DEBUG,
4768 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4769 *force_freq = 0;
4770 } else {
4771 wpa_printf(MSG_DEBUG,
4772 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4773 res = -2;
4774 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004775 }
4776
4777exit_ok:
4778 res = 0;
4779exit_free:
4780 os_free(freqs);
4781 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004782}
4783
4784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785/**
4786 * wpas_p2p_connect - Request P2P Group Formation to be started
4787 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4788 * @peer_addr: Address of the peer P2P Device
4789 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4790 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004791 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004792 * @join: Whether to join an existing group (as a client) instead of starting
4793 * Group Owner negotiation; @peer_addr is BSSID in that case
4794 * @auth: Whether to only authorize the connection instead of doing that and
4795 * initiating Group Owner negotiation
4796 * @go_intent: GO Intent or -1 to use default
4797 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004798 * @persistent_id: Persistent group credentials to use for forcing GO
4799 * parameters or -1 to generate new values (SSID/passphrase)
4800 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4801 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004802 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004803 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4805 * failure, -2 on failure due to channel not currently available,
4806 * -3 if forced channel is not supported
4807 */
4808int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4809 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004810 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004811 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004812 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004813{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004814 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004815 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004817 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004818 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004819
4820 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4821 return -1;
4822
Dmitry Shmidt04949592012-07-19 12:16:46 -07004823 if (persistent_id >= 0) {
4824 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4825 if (ssid == NULL || ssid->disabled != 2 ||
4826 ssid->mode != WPAS_MODE_P2P_GO)
4827 return -1;
4828 }
4829
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004830 os_free(wpa_s->global->add_psk);
4831 wpa_s->global->add_psk = NULL;
4832
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004833 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004834 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 if (go_intent < 0)
4837 go_intent = wpa_s->conf->p2p_go_intent;
4838
4839 if (!auth)
4840 wpa_s->p2p_long_listen = 0;
4841
4842 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004843 wpa_s->p2p_persistent_group = !!persistent_group;
4844 wpa_s->p2p_persistent_id = persistent_id;
4845 wpa_s->p2p_go_intent = go_intent;
4846 wpa_s->p2p_connect_freq = freq;
4847 wpa_s->p2p_fallback_to_go_neg = 0;
4848 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004849 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004850 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851
4852 if (pin)
4853 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4854 else if (wps_method == WPS_PIN_DISPLAY) {
4855 ret = wps_generate_pin();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004856 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4857 "%08d", ret);
4858 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
4859 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004860 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4861 wpa_s->p2p_pin);
4862 } else
4863 wpa_s->p2p_pin[0] = '\0';
4864
Dmitry Shmidt04949592012-07-19 12:16:46 -07004865 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4867 if (auth) {
4868 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4869 "connect a running group from " MACSTR,
4870 MAC2STR(peer_addr));
4871 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4872 return ret;
4873 }
4874 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4875 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4876 iface_addr) < 0) {
4877 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4878 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4879 dev_addr);
4880 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004881 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004882 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004883 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4884 "%ld.%06ld",
4885 wpa_s->p2p_auto_started.sec,
4886 wpa_s->p2p_auto_started.usec);
4887 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004888 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004889 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004890 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 return -1;
4892 return ret;
4893 }
4894
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004895 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4896 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004897 if (res)
4898 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004899 wpas_p2p_set_own_freq_preference(wpa_s,
4900 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004901
4902 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004904 if (wpa_s->create_p2p_iface) {
4905 /* Prepare to add a new interface for the group */
4906 iftype = WPA_IF_P2P_GROUP;
4907 if (go_intent == 15)
4908 iftype = WPA_IF_P2P_GO;
4909 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4910 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4911 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004912 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004913 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004914
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004915 if_addr = wpa_s->pending_interface_addr;
4916 } else
4917 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004918
4919 if (auth) {
4920 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004921 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004922 force_freq, persistent_group, ssid,
4923 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004924 return -1;
4925 return ret;
4926 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004927
4928 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4929 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004930 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004931 if (wpa_s->create_p2p_iface)
4932 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933 return -1;
4934 }
4935 return ret;
4936}
4937
4938
4939/**
4940 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4941 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4942 * @freq: Frequency of the channel in MHz
4943 * @duration: Duration of the stay on the channel in milliseconds
4944 *
4945 * This callback is called when the driver indicates that it has started the
4946 * requested remain-on-channel duration.
4947 */
4948void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4949 unsigned int freq, unsigned int duration)
4950{
4951 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4952 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004953 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)",
4954 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4955 wpa_s->roc_waiting_drv_freq, freq, duration);
4956 if (wpa_s->off_channel_freq &&
4957 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004958 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4959 wpa_s->pending_listen_duration);
4960 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004961 } else {
4962 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4963 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4964 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965 }
4966}
4967
4968
Jithu Jance57cea1a2014-08-20 10:22:04 -07004969int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970{
4971 /* Limit maximum Listen state time based on driver limitation. */
4972 if (timeout > wpa_s->max_remain_on_chan)
4973 timeout = wpa_s->max_remain_on_chan;
4974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 return p2p_listen(wpa_s->global->p2p, timeout);
4976}
4977
4978
4979/**
4980 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4981 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4982 * @freq: Frequency of the channel in MHz
4983 *
4984 * This callback is called when the driver indicates that a remain-on-channel
4985 * operation has been completed, i.e., the duration on the requested channel
4986 * has timed out.
4987 */
4988void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4989 unsigned int freq)
4990{
4991 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4992 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004993 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004994 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4996 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07004997 if (wpa_s->p2p_long_listen > 0)
4998 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5000 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005001 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005002 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005003 if (wpa_s->p2p_long_listen > 0) {
5004 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5005 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005006 } else {
5007 /*
5008 * When listen duration is over, stop listen & update p2p_state
5009 * to IDLE.
5010 */
5011 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005012 }
5013}
5014
5015
5016/**
5017 * wpas_p2p_group_remove - Remove a P2P group
5018 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5019 * @ifname: Network interface name of the group interface or "*" to remove all
5020 * groups
5021 * Returns: 0 on success, -1 on failure
5022 *
5023 * This function is used to remove a P2P group. This can be used to disconnect
5024 * from a group in which the local end is a P2P Client or to end a P2P Group in
5025 * case the local end is the Group Owner. If a virtual network interface was
5026 * created for this group, that interface will be removed. Otherwise, only the
5027 * configured P2P group network will be removed from the interface.
5028 */
5029int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5030{
5031 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005032 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033
5034 if (os_strcmp(ifname, "*") == 0) {
5035 struct wpa_supplicant *prev;
5036 wpa_s = global->ifaces;
5037 while (wpa_s) {
5038 prev = wpa_s;
5039 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005040 if (prev->p2p_group_interface !=
5041 NOT_P2P_GROUP_INTERFACE ||
5042 (prev->current_ssid &&
5043 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005044 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005045 }
5046 return 0;
5047 }
5048
5049 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5050 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5051 break;
5052 }
5053
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005054 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005055}
5056
5057
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005058static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5059{
5060 unsigned int r;
5061
5062 if (freq == 2) {
5063 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5064 "band");
5065 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005066 p2p_supported_freq_go(wpa_s->global->p2p,
5067 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005068 freq = wpa_s->best_24_freq;
5069 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5070 "channel: %d MHz", freq);
5071 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005072 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5073 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005074 freq = 2412 + (r % 3) * 25;
5075 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5076 "channel: %d MHz", freq);
5077 }
5078 }
5079
5080 if (freq == 5) {
5081 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5082 "band");
5083 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005084 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005085 wpa_s->best_5_freq)) {
5086 freq = wpa_s->best_5_freq;
5087 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5088 "channel: %d MHz", freq);
5089 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005090 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5091 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005092 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005093 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005094 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5095 "5 GHz channel for P2P group");
5096 return -1;
5097 }
5098 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5099 "channel: %d MHz", freq);
5100 }
5101 }
5102
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005103 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005104 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5105 ieee80211_is_dfs(freq)) {
5106 /*
5107 * If freq is a DFS channel and DFS is offloaded to the
5108 * driver, allow P2P GO to use it.
5109 */
5110 wpa_printf(MSG_DEBUG, "P2P: "
5111 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5112 __func__, freq);
5113 return freq;
5114 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005115 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5116 "(%u MHz) is not supported for P2P uses",
5117 freq);
5118 return -1;
5119 }
5120
5121 return freq;
5122}
5123
5124
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005125static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5126 struct p2p_go_neg_results *params,
5127 const struct p2p_channels *channels)
5128{
5129 unsigned int i, r;
5130
5131 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005132 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5133 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005134
5135 for (i = 0; i < 3; i++) {
5136 params->freq = 2412 + ((r + i) % 3) * 25;
5137 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005138 freq_included(channels, params->freq) &&
5139 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005140 goto out;
5141 }
5142
5143 /* try all channels in reg. class 81 */
5144 for (i = 0; i < 11; i++) {
5145 params->freq = 2412 + i * 5;
5146 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005147 freq_included(channels, params->freq) &&
5148 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005149 goto out;
5150 }
5151
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005152 /* try all channels in operating class 115 */
5153 for (i = 0; i < 4; i++) {
5154 params->freq = 5180 + i * 20;
5155 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5156 freq_included(channels, params->freq) &&
5157 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5158 goto out;
5159 }
5160
5161 /* try all channels in operating class 124 */
5162 for (i = 0; i < 4; i++) {
5163 params->freq = 5745 + i * 20;
5164 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5165 freq_included(channels, params->freq) &&
5166 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5167 goto out;
5168 }
5169
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005170 /* try social channel class 180 channel 2 */
5171 params->freq = 58320 + 1 * 2160;
5172 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5173 freq_included(channels, params->freq) &&
5174 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5175 goto out;
5176
5177 /* try all channels in reg. class 180 */
5178 for (i = 0; i < 4; i++) {
5179 params->freq = 58320 + i * 2160;
5180 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5181 freq_included(channels, params->freq) &&
5182 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5183 goto out;
5184 }
5185
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005186 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005187 return -1;
5188out:
5189 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5190 params->freq);
5191 return 0;
5192}
5193
5194
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005195static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5196 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005197 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005198 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005199{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005200 struct wpa_used_freq_data *freqs;
5201 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005202 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005203
5204 os_memset(params, 0, sizeof(*params));
5205 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005206 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005207 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005209 if (!freq_included(channels, freq)) {
5210 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5211 "accepted", freq);
5212 return -1;
5213 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5215 "frequency %d MHz", freq);
5216 params->freq = freq;
5217 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5218 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005219 wpa_s->conf->p2p_oper_channel <= 11 &&
5220 freq_included(channels,
5221 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005222 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5223 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5224 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005225 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5226 wpa_s->conf->p2p_oper_reg_class == 116 ||
5227 wpa_s->conf->p2p_oper_reg_class == 117 ||
5228 wpa_s->conf->p2p_oper_reg_class == 124 ||
5229 wpa_s->conf->p2p_oper_reg_class == 126 ||
5230 wpa_s->conf->p2p_oper_reg_class == 127) &&
5231 freq_included(channels,
5232 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5234 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5235 "frequency %d MHz", params->freq);
5236 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5237 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005238 p2p_supported_freq_go(wpa_s->global->p2p,
5239 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005240 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005241 params->freq = wpa_s->best_overall_freq;
5242 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5243 "channel %d MHz", params->freq);
5244 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5245 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005246 p2p_supported_freq_go(wpa_s->global->p2p,
5247 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005248 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249 params->freq = wpa_s->best_24_freq;
5250 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5251 "channel %d MHz", params->freq);
5252 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5253 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005254 p2p_supported_freq_go(wpa_s->global->p2p,
5255 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005256 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 params->freq = wpa_s->best_5_freq;
5258 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5259 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005260 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5261 channels))) {
5262 params->freq = pref_freq;
5263 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5264 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005266 /* no preference, select some channel */
5267 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005268 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005269 }
5270
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005271 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5272 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005273 if (!freqs)
5274 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005275
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005276 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005277 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005278
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005279 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5280
5281 /* First try the best used frequency if possible */
5282 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5283 params->freq = cand_freq;
5284 } else if (!freq) {
5285 /* Try any of the used frequencies */
5286 for (i = 0; i < num; i++) {
5287 if (freq_included(channels, freqs[i].freq)) {
5288 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5289 freqs[i].freq);
5290 params->freq = freqs[i].freq;
5291 break;
5292 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005293 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005294
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005295 if (i == num) {
5296 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005297 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005298 os_free(freqs);
5299 return -1;
5300 } else {
5301 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5302 }
5303 }
5304 } else {
5305 for (i = 0; i < num; i++) {
5306 if (freqs[i].freq == freq)
5307 break;
5308 }
5309
5310 if (i == num) {
5311 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5312 if (freq)
5313 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5314 os_free(freqs);
5315 return -1;
5316 } else {
5317 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5318 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005319 }
5320 }
5321
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005322 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005323 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005324}
5325
5326
5327static struct wpa_supplicant *
5328wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5329 int go)
5330{
5331 struct wpa_supplicant *group_wpa_s;
5332
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005333 if (!wpas_p2p_create_iface(wpa_s)) {
5334 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5335 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005336 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005337 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339
5340 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005341 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005342 wpa_msg_global(wpa_s, MSG_ERROR,
5343 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005344 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005345 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5347 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005348 wpa_msg_global(wpa_s, MSG_ERROR,
5349 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005350 wpas_p2p_remove_pending_group_interface(wpa_s);
5351 return NULL;
5352 }
5353
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005354 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5355 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005356 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357 return group_wpa_s;
5358}
5359
5360
5361/**
5362 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5363 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5364 * @persistent_group: Whether to create a persistent group
5365 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005366 * @ht40: Start GO with 40 MHz channel width
5367 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 * Returns: 0 on success, -1 on failure
5369 *
5370 * This function creates a new P2P group with the local end as the Group Owner,
5371 * i.e., without using Group Owner Negotiation.
5372 */
5373int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005374 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005375{
5376 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377
5378 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5379 return -1;
5380
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005381 os_free(wpa_s->global->add_psk);
5382 wpa_s->global->add_psk = NULL;
5383
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005384 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005385 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005386 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005387
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005388 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5389 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005391
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005392 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005393 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005394 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005395 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005396 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5397 ieee80211_is_dfs(params.freq)) {
5398 /*
5399 * If freq is a DFS channel and DFS is offloaded to the
5400 * driver, allow P2P GO to use it.
5401 */
5402 wpa_printf(MSG_DEBUG,
5403 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5404 __func__, params.freq);
5405 } else {
5406 wpa_printf(MSG_DEBUG,
5407 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5408 params.freq);
5409 return -1;
5410 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005411 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005412 p2p_go_params(wpa_s->global->p2p, &params);
5413 params.persistent_group = persistent_group;
5414
5415 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5416 if (wpa_s == NULL)
5417 return -1;
5418 wpas_start_wps_go(wpa_s, &params, 0);
5419
5420 return 0;
5421}
5422
5423
5424static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005425 struct wpa_ssid *params, int addr_allocated,
5426 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427{
5428 struct wpa_ssid *ssid;
5429
5430 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5431 if (wpa_s == NULL)
5432 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005433 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434
5435 wpa_supplicant_ap_deinit(wpa_s);
5436
5437 ssid = wpa_config_add_network(wpa_s->conf);
5438 if (ssid == NULL)
5439 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005440 wpa_config_set_network_defaults(ssid);
5441 ssid->temporary = 1;
5442 ssid->proto = WPA_PROTO_RSN;
5443 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5444 ssid->group_cipher = WPA_CIPHER_CCMP;
5445 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5446 ssid->ssid = os_malloc(params->ssid_len);
5447 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005448 wpa_config_remove_network(wpa_s->conf, ssid->id);
5449 return -1;
5450 }
5451 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5452 ssid->ssid_len = params->ssid_len;
5453 ssid->p2p_group = 1;
5454 ssid->export_keys = 1;
5455 if (params->psk_set) {
5456 os_memcpy(ssid->psk, params->psk, 32);
5457 ssid->psk_set = 1;
5458 }
5459 if (params->passphrase)
5460 ssid->passphrase = os_strdup(params->passphrase);
5461
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005462 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005463 wpa_s->p2p_in_invitation = 1;
5464 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005465
Dmitry Shmidt15907092014-03-25 10:42:57 -07005466 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5467 NULL);
5468 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5469 wpas_p2p_group_formation_timeout,
5470 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005471 wpa_supplicant_select_network(wpa_s, ssid);
5472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 return 0;
5474}
5475
5476
5477int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5478 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005479 int force_freq, int neg_freq, int ht40,
5480 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005481 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482{
5483 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005484 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485
5486 if (ssid->disabled != 2 || ssid->ssid == NULL)
5487 return -1;
5488
5489 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5490 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5491 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5492 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07005493 if (go == 0 &&
5494 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5495 wpa_s->parent, NULL)) {
5496 /*
5497 * This can happen if Invitation Response frame was lost
5498 * and the peer (GO of a persistent group) tries to
5499 * invite us again. Reschedule the timeout to avoid
5500 * terminating the wait for the connection too early
5501 * since we now know that the peer is still trying to
5502 * invite us instead of having already started the GO.
5503 */
5504 wpa_printf(MSG_DEBUG,
5505 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5506 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5507 wpas_p2p_group_formation_timeout,
5508 wpa_s->parent, NULL);
5509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 return 0;
5511 }
5512
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005513 os_free(wpa_s->global->add_psk);
5514 wpa_s->global->add_psk = NULL;
5515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005516 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005517 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005518
Dmitry Shmidt04949592012-07-19 12:16:46 -07005519 wpa_s->p2p_fallback_to_go_neg = 0;
5520
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005521 if (ssid->mode == WPAS_MODE_P2P_GO) {
5522 if (force_freq > 0) {
5523 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5524 if (freq < 0)
5525 return -1;
5526 } else {
5527 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5528 if (freq < 0 ||
5529 (freq > 0 && !freq_included(channels, freq)))
5530 freq = 0;
5531 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005532 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005533 freq = neg_freq;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005534 if (freq <= 0 || !freq_included(channels, freq)) {
5535 struct os_reltime now;
5536 struct wpa_bss *bss =
5537 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005538
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005539 os_get_reltime(&now);
5540 if (bss &&
5541 !os_reltime_expired(&now, &bss->last_update, 5) &&
5542 freq_included(channels, bss->freq))
5543 freq = bss->freq;
5544 else
5545 freq = 0;
5546 }
5547
Dmitry Shmidt15907092014-03-25 10:42:57 -07005548 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005549 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07005550 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005551 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005552
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005553 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005554 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005555
5556 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005557 params.psk_set = ssid->psk_set;
5558 if (params.psk_set)
5559 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005560 if (ssid->passphrase) {
5561 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5562 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5563 "persistent group");
5564 return -1;
5565 }
5566 os_strlcpy(params.passphrase, ssid->passphrase,
5567 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005569 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5570 params.ssid_len = ssid->ssid_len;
5571 params.persistent_group = 1;
5572
5573 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5574 if (wpa_s == NULL)
5575 return -1;
5576
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005577 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5578
Dmitry Shmidt56052862013-10-04 10:23:25 -07005579 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 wpas_start_wps_go(wpa_s, &params, 0);
5581
5582 return 0;
5583}
5584
5585
5586static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5587 struct wpabuf *proberesp_ies)
5588{
5589 struct wpa_supplicant *wpa_s = ctx;
5590 if (wpa_s->ap_iface) {
5591 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005592 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5593 wpabuf_free(beacon_ies);
5594 wpabuf_free(proberesp_ies);
5595 return;
5596 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005597 if (beacon_ies) {
5598 wpabuf_free(hapd->p2p_beacon_ie);
5599 hapd->p2p_beacon_ie = beacon_ies;
5600 }
5601 wpabuf_free(hapd->p2p_probe_resp_ie);
5602 hapd->p2p_probe_resp_ie = proberesp_ies;
5603 } else {
5604 wpabuf_free(beacon_ies);
5605 wpabuf_free(proberesp_ies);
5606 }
5607 wpa_supplicant_ap_update_beacon(wpa_s);
5608}
5609
5610
5611static void wpas_p2p_idle_update(void *ctx, int idle)
5612{
5613 struct wpa_supplicant *wpa_s = ctx;
5614 if (!wpa_s->ap_iface)
5615 return;
5616 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005617 if (idle) {
5618 if (wpa_s->global->p2p_fail_on_wps_complete &&
5619 wpa_s->p2p_in_provisioning) {
5620 wpas_p2p_grpform_fail_after_wps(wpa_s);
5621 return;
5622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005623 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005624 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005625 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5626}
5627
5628
5629struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005630 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005631{
5632 struct p2p_group *group;
5633 struct p2p_group_config *cfg;
5634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5636 return NULL;
5637
5638 cfg = os_zalloc(sizeof(*cfg));
5639 if (cfg == NULL)
5640 return NULL;
5641
Dmitry Shmidt04949592012-07-19 12:16:46 -07005642 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005644 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005645 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5647 if (wpa_s->max_stations &&
5648 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5649 cfg->max_clients = wpa_s->max_stations;
5650 else
5651 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005652 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5653 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005654 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655 cfg->cb_ctx = wpa_s;
5656 cfg->ie_update = wpas_p2p_ie_update;
5657 cfg->idle_update = wpas_p2p_idle_update;
5658
5659 group = p2p_group_init(wpa_s->global->p2p, cfg);
5660 if (group == NULL)
5661 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005662 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 p2p_group_notif_formation_done(group);
5664 wpa_s->p2p_group = group;
5665 return group;
5666}
5667
5668
5669void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5670 int registrar)
5671{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005672 struct wpa_ssid *ssid = wpa_s->current_ssid;
5673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674 if (!wpa_s->p2p_in_provisioning) {
5675 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5676 "provisioning not in progress");
5677 return;
5678 }
5679
Dmitry Shmidt04949592012-07-19 12:16:46 -07005680 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5681 u8 go_dev_addr[ETH_ALEN];
5682 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5683 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5684 ssid->ssid_len);
5685 /* Clear any stored provisioning info */
5686 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5687 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005689 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5690 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005691 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005692 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5693 /*
5694 * Use a separate timeout for initial data connection to
5695 * complete to allow the group to be removed automatically if
5696 * something goes wrong in this step before the P2P group idle
5697 * timeout mechanism is taken into use.
5698 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005699 wpa_dbg(wpa_s, MSG_DEBUG,
5700 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5701 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005702 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5703 wpas_p2p_group_formation_timeout,
5704 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005705 } else if (ssid) {
5706 /*
5707 * Use a separate timeout for initial data connection to
5708 * complete to allow the group to be removed automatically if
5709 * the client does not complete data connection successfully.
5710 */
5711 wpa_dbg(wpa_s, MSG_DEBUG,
5712 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5713 P2P_MAX_INITIAL_CONN_WAIT_GO);
5714 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5715 wpas_p2p_group_formation_timeout,
5716 wpa_s->parent, NULL);
5717 /*
5718 * Complete group formation on first successful data connection
5719 */
5720 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005721 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005722 if (wpa_s->global->p2p)
5723 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 wpas_group_formation_completed(wpa_s, 1);
5725}
5726
5727
Jouni Malinen75ecf522011-06-27 15:19:46 -07005728void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5729 struct wps_event_fail *fail)
5730{
5731 if (!wpa_s->p2p_in_provisioning) {
5732 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5733 "provisioning not in progress");
5734 return;
5735 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005736
5737 if (wpa_s->go_params) {
5738 p2p_clear_provisioning_info(
5739 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005740 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005741 }
5742
Jouni Malinen75ecf522011-06-27 15:19:46 -07005743 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005744
5745 if (wpa_s == wpa_s->global->p2p_group_formation) {
5746 /*
5747 * Allow some time for the failed WPS negotiation exchange to
5748 * complete, but remove the group since group formation cannot
5749 * succeed after provisioning failure.
5750 */
5751 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5752 wpa_s->global->p2p_fail_on_wps_complete = 1;
5753 eloop_deplete_timeout(0, 50000,
5754 wpas_p2p_group_formation_timeout,
5755 wpa_s->parent, NULL);
5756 }
5757}
5758
5759
5760int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5761{
5762 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5763 !wpa_s->p2p_in_provisioning)
5764 return 0;
5765
5766 wpas_p2p_grpform_fail_after_wps(wpa_s);
5767
5768 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005769}
5770
5771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005772int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005773 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005774 enum wpas_p2p_prov_disc_use use,
5775 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005776{
5777 u16 config_methods;
5778
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005779 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005780 wpa_s->p2p_fallback_to_go_neg = 0;
5781 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005782 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
5783 p2ps_prov->conncap = p2ps_group_capability(
5784 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
5785 wpa_printf(MSG_DEBUG,
5786 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
5787 __func__, p2ps_prov->conncap,
5788 p2ps_prov->adv_id, p2ps_prov->conncap,
5789 p2ps_prov->status, p2ps_prov->info);
5790
5791 config_methods = 0;
5792 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005793 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005794 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005796 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5797 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005798 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005799 else {
5800 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005801 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005802 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804
Dmitry Shmidt04949592012-07-19 12:16:46 -07005805 if (use == WPAS_P2P_PD_AUTO) {
5806 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5807 wpa_s->pending_pd_config_methods = config_methods;
5808 wpa_s->p2p_auto_pd = 1;
5809 wpa_s->p2p_auto_join = 0;
5810 wpa_s->pending_pd_before_join = 0;
5811 wpa_s->auto_pd_scan_retry = 0;
5812 wpas_p2p_stop_find(wpa_s);
5813 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005814 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005815 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5816 wpa_s->p2p_auto_started.sec,
5817 wpa_s->p2p_auto_started.usec);
5818 wpas_p2p_join_scan(wpa_s, NULL);
5819 return 0;
5820 }
5821
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005822 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
5823 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005824 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005826
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005827 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005828 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005829 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830}
5831
5832
5833int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5834 char *end)
5835{
5836 return p2p_scan_result_text(ies, ies_len, buf, end);
5837}
5838
5839
5840static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5841{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005842 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843 return;
5844
Dmitry Shmidt03658832014-08-13 11:03:49 -07005845 wpas_p2p_action_tx_clear(wpa_s);
5846
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5848 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005849 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850}
5851
5852
5853int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5854 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005855 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005856 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005857 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005858{
5859 wpas_p2p_clear_pending_action_tx(wpa_s);
5860 wpa_s->p2p_long_listen = 0;
5861
Dmitry Shmidt04949592012-07-19 12:16:46 -07005862 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5863 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864 return -1;
5865
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005866 wpa_supplicant_cancel_sched_scan(wpa_s);
5867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005868 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005869 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005870 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005871}
5872
5873
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005874static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5875 struct wpa_scan_results *scan_res)
5876{
5877 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5878
5879 if (wpa_s->p2p_scan_work) {
5880 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5881 wpa_s->p2p_scan_work = NULL;
5882 radio_work_done(work);
5883 }
5884
5885 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5886 return;
5887
5888 /*
5889 * Indicate that results have been processed so that the P2P module can
5890 * continue pending tasks.
5891 */
5892 p2p_scan_res_handled(wpa_s->global->p2p);
5893}
5894
5895
5896static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897{
5898 wpas_p2p_clear_pending_action_tx(wpa_s);
5899 wpa_s->p2p_long_listen = 0;
5900 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5901 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005902
5903 if (wpa_s->global->p2p)
5904 p2p_stop_find(wpa_s->global->p2p);
5905
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005906 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5907 wpa_printf(MSG_DEBUG,
5908 "P2P: Do not consider the scan results after stop_find");
5909 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5910 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005911}
5912
5913
5914void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5915{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005916 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005917 if (!wpa_s->global->pending_group_iface_for_p2ps)
5918 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005919}
5920
5921
5922static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5923{
5924 struct wpa_supplicant *wpa_s = eloop_ctx;
5925 wpa_s->p2p_long_listen = 0;
5926}
5927
5928
5929int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5930{
5931 int res;
5932
5933 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5934 return -1;
5935
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005936 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937 wpas_p2p_clear_pending_action_tx(wpa_s);
5938
5939 if (timeout == 0) {
5940 /*
5941 * This is a request for unlimited Listen state. However, at
5942 * least for now, this is mapped to a Listen state for one
5943 * hour.
5944 */
5945 timeout = 3600;
5946 }
5947 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5948 wpa_s->p2p_long_listen = 0;
5949
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005950 /*
5951 * Stop previous find/listen operation to avoid trying to request a new
5952 * remain-on-channel operation while the driver is still running the
5953 * previous one.
5954 */
5955 if (wpa_s->global->p2p)
5956 p2p_stop_find(wpa_s->global->p2p);
5957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005958 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5959 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5960 wpa_s->p2p_long_listen = timeout * 1000;
5961 eloop_register_timeout(timeout, 0,
5962 wpas_p2p_long_listen_timeout,
5963 wpa_s, NULL);
5964 }
5965
5966 return res;
5967}
5968
5969
5970int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5971 u8 *buf, size_t len, int p2p_group)
5972{
5973 struct wpabuf *p2p_ie;
5974 int ret;
5975
5976 if (wpa_s->global->p2p_disabled)
5977 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07005978 /*
5979 * Advertize mandatory cross connection capability even on
5980 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
5981 */
5982 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005983 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984 if (wpa_s->global->p2p == NULL)
5985 return -1;
5986 if (bss == NULL)
5987 return -1;
5988
5989 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5990 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5991 p2p_group, p2p_ie);
5992 wpabuf_free(p2p_ie);
5993
5994 return ret;
5995}
5996
5997
5998int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005999 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006000 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006001{
6002 if (wpa_s->global->p2p_disabled)
6003 return 0;
6004 if (wpa_s->global->p2p == NULL)
6005 return 0;
6006
Dmitry Shmidt04949592012-07-19 12:16:46 -07006007 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6008 ie, ie_len)) {
6009 case P2P_PREQ_NOT_P2P:
6010 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6011 ssi_signal);
6012 /* fall through */
6013 case P2P_PREQ_MALFORMED:
6014 case P2P_PREQ_NOT_LISTEN:
6015 case P2P_PREQ_NOT_PROCESSED:
6016 default: /* make gcc happy */
6017 return 0;
6018 case P2P_PREQ_PROCESSED:
6019 return 1;
6020 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006021}
6022
6023
6024void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6025 const u8 *sa, const u8 *bssid,
6026 u8 category, const u8 *data, size_t len, int freq)
6027{
6028 if (wpa_s->global->p2p_disabled)
6029 return;
6030 if (wpa_s->global->p2p == NULL)
6031 return;
6032
6033 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6034 freq);
6035}
6036
6037
6038void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6039{
6040 if (wpa_s->global->p2p_disabled)
6041 return;
6042 if (wpa_s->global->p2p == NULL)
6043 return;
6044
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006045 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006046}
6047
6048
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006049static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050{
6051 p2p_group_deinit(wpa_s->p2p_group);
6052 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006053
6054 wpa_s->ap_configured_cb = NULL;
6055 wpa_s->ap_configured_cb_ctx = NULL;
6056 wpa_s->ap_configured_cb_data = NULL;
6057 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006058}
6059
6060
6061int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6062{
6063 wpa_s->p2p_long_listen = 0;
6064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6066 return -1;
6067
6068 return p2p_reject(wpa_s->global->p2p, addr);
6069}
6070
6071
6072/* Invite to reinvoke a persistent group */
6073int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006074 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006075 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076{
6077 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006078 u8 *bssid = NULL;
6079 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006080 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006081 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006082
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006083 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006084 if (peer_addr)
6085 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6086 else
6087 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088
Jouni Malinen31be0a42012-08-31 21:20:51 +03006089 wpa_s->p2p_persistent_go_freq = freq;
6090 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006091 if (ssid->mode == WPAS_MODE_P2P_GO) {
6092 role = P2P_INVITE_ROLE_GO;
6093 if (peer_addr == NULL) {
6094 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6095 "address in invitation command");
6096 return -1;
6097 }
6098 if (wpas_p2p_create_iface(wpa_s)) {
6099 if (wpas_p2p_add_group_interface(wpa_s,
6100 WPA_IF_P2P_GO) < 0) {
6101 wpa_printf(MSG_ERROR, "P2P: Failed to "
6102 "allocate a new interface for the "
6103 "group");
6104 return -1;
6105 }
6106 bssid = wpa_s->pending_interface_addr;
6107 } else
6108 bssid = wpa_s->own_addr;
6109 } else {
6110 role = P2P_INVITE_ROLE_CLIENT;
6111 peer_addr = ssid->bssid;
6112 }
6113 wpa_s->pending_invite_ssid_id = ssid->id;
6114
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006115 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6116 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006117 if (res)
6118 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6121 return -1;
6122
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006123 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6124 no_pref_freq_given && pref_freq > 0 &&
6125 wpa_s->num_multichan_concurrent > 1 &&
6126 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6127 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6128 pref_freq);
6129 pref_freq = 0;
6130 }
6131
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006132 /*
6133 * Stop any find/listen operations before invitation and possibly
6134 * connection establishment.
6135 */
6136 wpas_p2p_stop_find_oper(wpa_s);
6137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006138 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006139 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006140 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006141}
6142
6143
6144/* Invite to join an active group */
6145int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6146 const u8 *peer_addr, const u8 *go_dev_addr)
6147{
6148 struct wpa_global *global = wpa_s->global;
6149 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006150 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006152 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006153 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006154 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006155
Jouni Malinen31be0a42012-08-31 21:20:51 +03006156 wpa_s->p2p_persistent_go_freq = 0;
6157 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006158 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006160 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6161 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6162 break;
6163 }
6164 if (wpa_s == NULL) {
6165 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6166 return -1;
6167 }
6168
6169 ssid = wpa_s->current_ssid;
6170 if (ssid == NULL) {
6171 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6172 "invitation");
6173 return -1;
6174 }
6175
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006176 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006177 persistent = ssid->p2p_persistent_group &&
6178 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6179 ssid->ssid, ssid->ssid_len);
6180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006181 if (ssid->mode == WPAS_MODE_P2P_GO) {
6182 role = P2P_INVITE_ROLE_ACTIVE_GO;
6183 bssid = wpa_s->own_addr;
6184 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006185 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006186 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006187 } else {
6188 role = P2P_INVITE_ROLE_CLIENT;
6189 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6190 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6191 "invite to current group");
6192 return -1;
6193 }
6194 bssid = wpa_s->bssid;
6195 if (go_dev_addr == NULL &&
6196 !is_zero_ether_addr(wpa_s->go_dev_addr))
6197 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006198 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6199 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200 }
6201 wpa_s->parent->pending_invite_ssid_id = -1;
6202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006203 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6204 return -1;
6205
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006206 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6207 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006208 if (res)
6209 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006210 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006213 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006214 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006215}
6216
6217
6218void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6219{
6220 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006222 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006223 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006224 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006225 u8 ip[3 * 4];
6226 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227
Dmitry Shmidt04949592012-07-19 12:16:46 -07006228 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6229 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6230 wpa_s->parent, NULL);
6231 }
6232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006233 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006234 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235
6236 wpa_s->show_group_started = 0;
6237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238 os_memset(go_dev_addr, 0, ETH_ALEN);
6239 if (ssid->bssid_set)
6240 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6241 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6242 ssid->ssid_len);
6243 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6244
6245 if (wpa_s->global->p2p_group_formation == wpa_s)
6246 wpa_s->global->p2p_group_formation = NULL;
6247
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006248 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6249 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006250
6251 ip_addr[0] = '\0';
6252 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006253 int res;
6254
6255 res = os_snprintf(ip_addr, sizeof(ip_addr),
6256 " ip_addr=%u.%u.%u.%u "
6257 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6258 ip[0], ip[1], ip[2], ip[3],
6259 ip[4], ip[5], ip[6], ip[7],
6260 ip[8], ip[9], ip[10], ip[11]);
6261 if (os_snprintf_error(sizeof(ip_addr), res))
6262 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006263 }
6264
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006265 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6266 ssid->passphrase == NULL && ssid->psk_set ?
6267 ssid->psk : NULL,
6268 ssid->passphrase, go_dev_addr, persistent,
6269 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270
6271 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006272 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6273 ssid, go_dev_addr);
6274 if (network_id < 0)
6275 network_id = ssid->id;
6276 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006277}
6278
6279
6280int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6281 u32 interval1, u32 duration2, u32 interval2)
6282{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006283 int ret;
6284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006285 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6286 return -1;
6287
6288 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6289 wpa_s->current_ssid == NULL ||
6290 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6291 return -1;
6292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006293 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6294 wpa_s->own_addr, wpa_s->assoc_freq,
6295 duration1, interval1, duration2, interval2);
6296 if (ret == 0)
6297 wpa_s->waiting_presence_resp = 1;
6298
6299 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006300}
6301
6302
6303int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6304 unsigned int interval)
6305{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006306 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6307 return -1;
6308
6309 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6310}
6311
6312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006313static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6314{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006315 if (wpa_s->current_ssid == NULL) {
6316 /*
6317 * current_ssid can be cleared when P2P client interface gets
6318 * disconnected, so assume this interface was used as P2P
6319 * client.
6320 */
6321 return 1;
6322 }
6323 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006324 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6325}
6326
6327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006328static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6329{
6330 struct wpa_supplicant *wpa_s = eloop_ctx;
6331
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006332 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006333 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6334 "disabled");
6335 return;
6336 }
6337
Dmitry Shmidt04949592012-07-19 12:16:46 -07006338 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6339 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006340 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006341}
6342
6343
6344static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6345{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006346 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006347
Dmitry Shmidt04949592012-07-19 12:16:46 -07006348 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6349 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006351 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6352 return;
6353
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006354 timeout = wpa_s->conf->p2p_group_idle;
6355 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6356 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6357 timeout = P2P_MAX_CLIENT_IDLE;
6358
6359 if (timeout == 0)
6360 return;
6361
Dmitry Shmidt04949592012-07-19 12:16:46 -07006362 if (timeout < 0) {
6363 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6364 timeout = 0; /* special client mode no-timeout */
6365 else
6366 return;
6367 }
6368
6369 if (wpa_s->p2p_in_provisioning) {
6370 /*
6371 * Use the normal group formation timeout during the
6372 * provisioning phase to avoid terminating this process too
6373 * early due to group idle timeout.
6374 */
6375 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6376 "during provisioning");
6377 return;
6378 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306379
Dmitry Shmidt04949592012-07-19 12:16:46 -07006380 if (wpa_s->show_group_started) {
6381 /*
6382 * Use the normal group formation timeout between the end of
6383 * the provisioning phase and completion of 4-way handshake to
6384 * avoid terminating this process too early due to group idle
6385 * timeout.
6386 */
6387 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6388 "while waiting for initial 4-way handshake to "
6389 "complete");
6390 return;
6391 }
6392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006393 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006394 timeout);
6395 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6396 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006397}
6398
6399
Jouni Malinen2b89da82012-08-31 22:04:41 +03006400/* Returns 1 if the interface was removed */
6401int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6402 u16 reason_code, const u8 *ie, size_t ie_len,
6403 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006404{
6405 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006406 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006407
Dmitry Shmidt04949592012-07-19 12:16:46 -07006408 if (!locally_generated)
6409 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6410 ie_len);
6411
6412 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6413 wpa_s->current_ssid &&
6414 wpa_s->current_ssid->p2p_group &&
6415 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6416 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6417 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006418 if (wpas_p2p_group_delete(wpa_s,
6419 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6420 > 0)
6421 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006422 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006423
6424 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006425}
6426
6427
6428void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006429 u16 reason_code, const u8 *ie, size_t ie_len,
6430 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006431{
6432 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6433 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006434
Dmitry Shmidt04949592012-07-19 12:16:46 -07006435 if (!locally_generated)
6436 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6437 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006438}
6439
6440
6441void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6442{
6443 struct p2p_data *p2p = wpa_s->global->p2p;
6444
6445 if (p2p == NULL)
6446 return;
6447
6448 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6449 return;
6450
6451 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6452 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6453
6454 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6455 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6456
6457 if (wpa_s->wps &&
6458 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6459 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6460
6461 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6462 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6463
6464 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6465 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6466 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6467 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6468 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6469 }
6470
6471 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6472 p2p_set_sec_dev_types(p2p,
6473 (void *) wpa_s->conf->sec_device_type,
6474 wpa_s->conf->num_sec_device_types);
6475
6476 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6477 int i;
6478 p2p_remove_wps_vendor_extensions(p2p);
6479 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6480 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6481 continue;
6482 p2p_add_wps_vendor_extension(
6483 p2p, wpa_s->conf->wps_vendor_ext[i]);
6484 }
6485 }
6486
6487 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6488 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6489 char country[3];
6490 country[0] = wpa_s->conf->country[0];
6491 country[1] = wpa_s->conf->country[1];
6492 country[2] = 0x04;
6493 p2p_set_country(p2p, country);
6494 }
6495
6496 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6497 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6498 wpa_s->conf->p2p_ssid_postfix ?
6499 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6500 0);
6501 }
6502
6503 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6504 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006505
6506 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6507 u8 reg_class, channel;
6508 int ret;
6509 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006510 u8 channel_forced;
6511
Jouni Malinen75ecf522011-06-27 15:19:46 -07006512 if (wpa_s->conf->p2p_listen_reg_class &&
6513 wpa_s->conf->p2p_listen_channel) {
6514 reg_class = wpa_s->conf->p2p_listen_reg_class;
6515 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006516 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006517 } else {
6518 reg_class = 81;
6519 /*
6520 * Pick one of the social channels randomly as the
6521 * listen channel.
6522 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006523 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6524 channel = 1;
6525 else
6526 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006527 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006528 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006529 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6530 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006531 if (ret)
6532 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6533 "failed: %d", ret);
6534 }
6535 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6536 u8 op_reg_class, op_channel, cfg_op_channel;
6537 int ret = 0;
6538 unsigned int r;
6539 if (wpa_s->conf->p2p_oper_reg_class &&
6540 wpa_s->conf->p2p_oper_channel) {
6541 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6542 op_channel = wpa_s->conf->p2p_oper_channel;
6543 cfg_op_channel = 1;
6544 } else {
6545 op_reg_class = 81;
6546 /*
6547 * Use random operation channel from (1, 6, 11)
6548 *if no other preference is indicated.
6549 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006550 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6551 op_channel = 1;
6552 else
6553 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006554 cfg_op_channel = 0;
6555 }
6556 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6557 cfg_op_channel);
6558 if (ret)
6559 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6560 "failed: %d", ret);
6561 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006562
6563 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6564 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6565 wpa_s->conf->p2p_pref_chan) < 0) {
6566 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6567 "update failed");
6568 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006569
6570 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6571 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6572 "update failed");
6573 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006574 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006575
6576 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6577 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006578}
6579
6580
6581int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6582 int duration)
6583{
6584 if (!wpa_s->ap_iface)
6585 return -1;
6586 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6587 duration);
6588}
6589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590
6591int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6592{
6593 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6594 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006595
6596 wpa_s->global->cross_connection = enabled;
6597 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6598
6599 if (!enabled) {
6600 struct wpa_supplicant *iface;
6601
6602 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6603 {
6604 if (iface->cross_connect_enabled == 0)
6605 continue;
6606
6607 iface->cross_connect_enabled = 0;
6608 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006609 wpa_msg_global(iface->parent, MSG_INFO,
6610 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6611 iface->ifname,
6612 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006613 }
6614 }
6615
6616 return 0;
6617}
6618
6619
6620static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6621{
6622 struct wpa_supplicant *iface;
6623
6624 if (!uplink->global->cross_connection)
6625 return;
6626
6627 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6628 if (!iface->cross_connect_enabled)
6629 continue;
6630 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6631 0)
6632 continue;
6633 if (iface->ap_iface == NULL)
6634 continue;
6635 if (iface->cross_connect_in_use)
6636 continue;
6637
6638 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006639 wpa_msg_global(iface->parent, MSG_INFO,
6640 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6641 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006642 }
6643}
6644
6645
6646static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6647{
6648 struct wpa_supplicant *iface;
6649
6650 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6651 if (!iface->cross_connect_enabled)
6652 continue;
6653 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6654 0)
6655 continue;
6656 if (!iface->cross_connect_in_use)
6657 continue;
6658
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006659 wpa_msg_global(iface->parent, MSG_INFO,
6660 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6661 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006662 iface->cross_connect_in_use = 0;
6663 }
6664}
6665
6666
6667void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6668{
6669 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6670 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6671 wpa_s->cross_connect_disallowed)
6672 wpas_p2p_disable_cross_connect(wpa_s);
6673 else
6674 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006675 if (!wpa_s->ap_iface &&
6676 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6677 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006678}
6679
6680
6681void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6682{
6683 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006684 if (!wpa_s->ap_iface &&
6685 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6686 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006687 wpas_p2p_set_group_idle_timeout(wpa_s);
6688}
6689
6690
6691static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6692{
6693 struct wpa_supplicant *iface;
6694
6695 if (!wpa_s->global->cross_connection)
6696 return;
6697
6698 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6699 if (iface == wpa_s)
6700 continue;
6701 if (iface->drv_flags &
6702 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6703 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006704 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6705 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006706 continue;
6707
6708 wpa_s->cross_connect_enabled = 1;
6709 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6710 sizeof(wpa_s->cross_connect_uplink));
6711 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6712 "%s to %s whenever uplink is available",
6713 wpa_s->ifname, wpa_s->cross_connect_uplink);
6714
6715 if (iface->ap_iface || iface->current_ssid == NULL ||
6716 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6717 iface->cross_connect_disallowed ||
6718 iface->wpa_state != WPA_COMPLETED)
6719 break;
6720
6721 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006722 wpa_msg_global(wpa_s->parent, MSG_INFO,
6723 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6724 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725 break;
6726 }
6727}
6728
6729
6730int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6731{
6732 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6733 !wpa_s->p2p_in_provisioning)
6734 return 0; /* not P2P client operation */
6735
6736 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6737 "session overlap");
6738 if (wpa_s != wpa_s->parent)
6739 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006740 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006741 return 1;
6742}
6743
6744
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006745void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6746{
6747 struct wpa_supplicant *wpa_s = eloop_ctx;
6748 wpas_p2p_notif_pbc_overlap(wpa_s);
6749}
6750
6751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006752void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6753{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006754 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006755 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756
6757 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6758 return;
6759
6760 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006761 os_memset(&cli_chan, 0, sizeof(cli_chan));
6762 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006763 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6764 "channel list");
6765 return;
6766 }
6767
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006768 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006769
6770 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6771 int freq;
6772 if (!ifs->current_ssid ||
6773 !ifs->current_ssid->p2p_group ||
6774 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6775 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6776 continue;
6777 freq = ifs->current_ssid->frequency;
6778 if (freq_included(&chan, freq)) {
6779 wpa_dbg(ifs, MSG_DEBUG,
6780 "P2P GO operating frequency %d MHz in valid range",
6781 freq);
6782 continue;
6783 }
6784
6785 wpa_dbg(ifs, MSG_DEBUG,
6786 "P2P GO operating in invalid frequency %d MHz", freq);
6787 /* TODO: Consider using CSA or removing the group within
6788 * wpa_supplicant */
6789 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6790 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006791}
6792
6793
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006794static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6795 struct wpa_scan_results *scan_res)
6796{
6797 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6798}
6799
6800
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006801int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6802{
6803 struct wpa_global *global = wpa_s->global;
6804 int found = 0;
6805 const u8 *peer;
6806
6807 if (global->p2p == NULL)
6808 return -1;
6809
6810 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6811
6812 if (wpa_s->pending_interface_name[0] &&
6813 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6814 found = 1;
6815
6816 peer = p2p_get_go_neg_peer(global->p2p);
6817 if (peer) {
6818 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6819 MACSTR, MAC2STR(peer));
6820 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006821 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006822 }
6823
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006824 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6825 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6826 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6827 found = 1;
6828 }
6829
6830 if (wpa_s->pending_pd_before_join) {
6831 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6832 wpa_s->pending_pd_before_join = 0;
6833 found = 1;
6834 }
6835
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006836 wpas_p2p_stop_find(wpa_s);
6837
6838 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6839 if (wpa_s == global->p2p_group_formation &&
6840 (wpa_s->p2p_in_provisioning ||
6841 wpa_s->parent->pending_interface_type ==
6842 WPA_IF_P2P_CLIENT)) {
6843 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6844 "formation found - cancelling",
6845 wpa_s->ifname);
6846 found = 1;
6847 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6848 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006849 if (wpa_s->p2p_in_provisioning) {
6850 wpas_group_formation_completed(wpa_s, 0);
6851 break;
6852 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006853 wpas_p2p_group_delete(wpa_s,
6854 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006856 } else if (wpa_s->p2p_in_invitation) {
6857 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6858 wpa_s->ifname);
6859 found = 1;
6860 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006861 }
6862 }
6863
6864 if (!found) {
6865 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6866 return -1;
6867 }
6868
6869 return 0;
6870}
6871
6872
6873void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6874{
6875 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6876 return;
6877
6878 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6879 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006880 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006881}
6882
6883
6884void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6885 int freq_24, int freq_5, int freq_overall)
6886{
6887 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006888 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006889 return;
6890 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6891}
6892
6893
6894int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6895{
6896 u8 peer[ETH_ALEN];
6897 struct p2p_data *p2p = wpa_s->global->p2p;
6898
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006899 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006900 return -1;
6901
6902 if (hwaddr_aton(addr, peer))
6903 return -1;
6904
6905 return p2p_unauthorize(p2p, peer);
6906}
6907
6908
6909/**
6910 * wpas_p2p_disconnect - Disconnect from a P2P Group
6911 * @wpa_s: Pointer to wpa_supplicant data
6912 * Returns: 0 on success, -1 on failure
6913 *
6914 * This can be used to disconnect from a group in which the local end is a P2P
6915 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6916 * virtual network interface was created for this group, that interface will be
6917 * removed. Otherwise, only the configured P2P group network will be removed
6918 * from the interface.
6919 */
6920int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6921{
6922
6923 if (wpa_s == NULL)
6924 return -1;
6925
Jouni Malinen2b89da82012-08-31 22:04:41 +03006926 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6927 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006928}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006929
6930
6931int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6932{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006933 int ret;
6934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6936 return 0;
6937
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006938 ret = p2p_in_progress(wpa_s->global->p2p);
6939 if (ret == 0) {
6940 /*
6941 * Check whether there is an ongoing WPS provisioning step (or
6942 * other parts of group formation) on another interface since
6943 * p2p_in_progress() does not report this to avoid issues for
6944 * scans during such provisioning step.
6945 */
6946 if (wpa_s->global->p2p_group_formation &&
6947 wpa_s->global->p2p_group_formation != wpa_s) {
6948 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6949 "in group formation",
6950 wpa_s->global->p2p_group_formation->ifname);
6951 ret = 1;
6952 }
6953 }
6954
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006955 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006956 struct os_reltime now;
6957 os_get_reltime(&now);
6958 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6959 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006960 /* Wait for the first client has expired */
6961 wpa_s->global->p2p_go_wait_client.sec = 0;
6962 } else {
6963 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6964 ret = 1;
6965 }
6966 }
6967
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006968 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006969}
6970
6971
6972void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6973 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006974{
6975 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6976 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6977 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006978 /**
6979 * Remove the network by scheduling the group formation
6980 * timeout to happen immediately. The teardown code
6981 * needs to be scheduled to run asynch later so that we
6982 * don't delete data from under ourselves unexpectedly.
6983 * Calling wpas_p2p_group_formation_timeout directly
6984 * causes a series of crashes in WPS failure scenarios.
6985 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006986 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6987 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006988 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6989 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006990 }
6991}
6992
6993
6994struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006995 const u8 *addr, const u8 *ssid,
6996 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006997{
6998 struct wpa_ssid *s;
6999 size_t i;
7000
7001 for (s = wpa_s->conf->ssid; s; s = s->next) {
7002 if (s->disabled != 2)
7003 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007004 if (ssid &&
7005 (ssid_len != s->ssid_len ||
7006 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7007 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007008 if (addr == NULL) {
7009 if (s->mode == WPAS_MODE_P2P_GO)
7010 return s;
7011 continue;
7012 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007013 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7014 return s; /* peer is GO in the persistent group */
7015 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7016 continue;
7017 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007018 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007019 addr, ETH_ALEN) == 0)
7020 return s; /* peer is P2P client in persistent
7021 * group */
7022 }
7023 }
7024
7025 return NULL;
7026}
7027
7028
7029void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7030 const u8 *addr)
7031{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007032 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7033 wpa_s->parent, NULL) > 0) {
7034 /*
7035 * This can happen if WPS provisioning step is not terminated
7036 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7037 * peer was able to connect, there is no need to time out group
7038 * formation after this, though. In addition, this is used with
7039 * the initial connection wait on the GO as a separate formation
7040 * timeout and as such, expected to be hit after the initial WPS
7041 * provisioning step.
7042 */
7043 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007044
7045 if (!wpa_s->p2p_go_group_formation_completed &&
7046 !wpa_s->group_formation_reported) {
7047 /*
7048 * GO has not yet notified group formation success since
7049 * the WPS step was not completed cleanly. Do that
7050 * notification now since the P2P Client was able to
7051 * connect and as such, must have received the
7052 * credential from the WPS step.
7053 */
7054 if (wpa_s->global->p2p)
7055 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7056 wpas_group_formation_completed(wpa_s, 1);
7057 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007058 }
7059 if (!wpa_s->p2p_go_group_formation_completed) {
7060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7061 wpa_s->p2p_go_group_formation_completed = 1;
7062 wpa_s->global->p2p_group_formation = NULL;
7063 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007064 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007065 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007066 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007067 if (addr == NULL)
7068 return;
7069 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7070}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007071
Dmitry Shmidt04949592012-07-19 12:16:46 -07007072
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007073static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7074 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007075{
7076 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007077 int ret = 0;
7078
Dmitry Shmidt04949592012-07-19 12:16:46 -07007079 if (wpa_s->global->p2p_group_formation)
7080 group = wpa_s->global->p2p_group_formation;
7081 wpa_s = wpa_s->parent;
7082 offchannel_send_action_done(wpa_s);
7083 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007084 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007085 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7086 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7087 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7088 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7089 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007090 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007091 wpa_s->p2p_go_ht40,
7092 wpa_s->p2p_go_vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007093 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007094}
7095
7096
7097int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7098{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007099 int res;
7100
Dmitry Shmidt04949592012-07-19 12:16:46 -07007101 if (!wpa_s->p2p_fallback_to_go_neg ||
7102 wpa_s->p2p_in_provisioning <= 5)
7103 return 0;
7104
7105 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7106 return 0; /* peer operating as a GO */
7107
7108 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7109 "fallback to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007110 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7111 "reason=GO-not-found");
7112 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007113
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007114 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007115}
7116
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007117
7118unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7119{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007120 struct wpa_supplicant *ifs;
7121
7122 if (wpa_s->wpa_state > WPA_SCANNING) {
7123 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7124 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007125 wpa_s->conf->p2p_search_delay);
7126 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007127 }
7128
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007129 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7130 radio_list) {
7131 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007132 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7133 "delay due to concurrent operation on "
7134 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007135 wpa_s->conf->p2p_search_delay,
7136 ifs->ifname);
7137 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007138 }
7139 }
7140
7141 return 0;
7142}
7143
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007144
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007145static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7146 struct wpa_ssid *s, const u8 *addr,
7147 int iface_addr)
7148{
7149 struct psk_list_entry *psk, *tmp;
7150 int changed = 0;
7151
7152 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7153 list) {
7154 if ((iface_addr && !psk->p2p &&
7155 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7156 (!iface_addr && psk->p2p &&
7157 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7158 wpa_dbg(wpa_s, MSG_DEBUG,
7159 "P2P: Remove persistent group PSK list entry for "
7160 MACSTR " p2p=%u",
7161 MAC2STR(psk->addr), psk->p2p);
7162 dl_list_del(&psk->list);
7163 os_free(psk);
7164 changed++;
7165 }
7166 }
7167
7168 return changed;
7169}
7170
7171
7172void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7173 const u8 *p2p_dev_addr,
7174 const u8 *psk, size_t psk_len)
7175{
7176 struct wpa_ssid *ssid = wpa_s->current_ssid;
7177 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007178 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007179
7180 if (psk_len != sizeof(p->psk))
7181 return;
7182
7183 if (p2p_dev_addr) {
7184 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7185 " p2p_dev_addr=" MACSTR,
7186 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7187 if (is_zero_ether_addr(p2p_dev_addr))
7188 p2p_dev_addr = NULL;
7189 } else {
7190 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7191 MAC2STR(mac_addr));
7192 }
7193
7194 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7195 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7196 /* To be added to persistent group once created */
7197 if (wpa_s->global->add_psk == NULL) {
7198 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7199 if (wpa_s->global->add_psk == NULL)
7200 return;
7201 }
7202 p = wpa_s->global->add_psk;
7203 if (p2p_dev_addr) {
7204 p->p2p = 1;
7205 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7206 } else {
7207 p->p2p = 0;
7208 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7209 }
7210 os_memcpy(p->psk, psk, psk_len);
7211 return;
7212 }
7213
7214 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7216 return;
7217 }
7218
7219 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7220 ssid->ssid_len);
7221 if (!persistent) {
7222 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7223 return;
7224 }
7225
7226 p = os_zalloc(sizeof(*p));
7227 if (p == NULL)
7228 return;
7229 if (p2p_dev_addr) {
7230 p->p2p = 1;
7231 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7232 } else {
7233 p->p2p = 0;
7234 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7235 }
7236 os_memcpy(p->psk, psk, psk_len);
7237
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007238 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7239 (last = dl_list_last(&persistent->psk_list,
7240 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7242 MACSTR " (p2p=%u) to make room for a new one",
7243 MAC2STR(last->addr), last->p2p);
7244 dl_list_del(&last->list);
7245 os_free(last);
7246 }
7247
7248 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7249 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7250 p2p_dev_addr == NULL);
7251 if (p2p_dev_addr) {
7252 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7253 MACSTR, MAC2STR(p2p_dev_addr));
7254 } else {
7255 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7256 MAC2STR(mac_addr));
7257 }
7258 dl_list_add(&persistent->psk_list, &p->list);
7259
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007260 if (wpa_s->parent->conf->update_config &&
7261 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7262 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007263}
7264
7265
7266static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7267 struct wpa_ssid *s, const u8 *addr,
7268 int iface_addr)
7269{
7270 int res;
7271
7272 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007273 if (res > 0 && wpa_s->conf->update_config &&
7274 wpa_config_write(wpa_s->confname, wpa_s->conf))
7275 wpa_dbg(wpa_s, MSG_DEBUG,
7276 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007277}
7278
7279
7280static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7281 const u8 *peer, int iface_addr)
7282{
7283 struct hostapd_data *hapd;
7284 struct hostapd_wpa_psk *psk, *prev, *rem;
7285 struct sta_info *sta;
7286
7287 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7288 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7289 return;
7290
7291 /* Remove per-station PSK entry */
7292 hapd = wpa_s->ap_iface->bss[0];
7293 prev = NULL;
7294 psk = hapd->conf->ssid.wpa_psk;
7295 while (psk) {
7296 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7297 (!iface_addr &&
7298 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7299 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7300 MACSTR " iface_addr=%d",
7301 MAC2STR(peer), iface_addr);
7302 if (prev)
7303 prev->next = psk->next;
7304 else
7305 hapd->conf->ssid.wpa_psk = psk->next;
7306 rem = psk;
7307 psk = psk->next;
7308 os_free(rem);
7309 } else {
7310 prev = psk;
7311 psk = psk->next;
7312 }
7313 }
7314
7315 /* Disconnect from group */
7316 if (iface_addr)
7317 sta = ap_get_sta(hapd, peer);
7318 else
7319 sta = ap_get_sta_p2p(hapd, peer);
7320 if (sta) {
7321 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7322 " (iface_addr=%d) from group",
7323 MAC2STR(peer), iface_addr);
7324 hostapd_drv_sta_deauth(hapd, sta->addr,
7325 WLAN_REASON_DEAUTH_LEAVING);
7326 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7327 }
7328}
7329
7330
7331void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7332 int iface_addr)
7333{
7334 struct wpa_ssid *s;
7335 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007336 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007337
7338 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7339
7340 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007341 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007342 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7343 continue;
7344 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007345 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7346 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007347 }
7348
7349 /* Remove from any operating group */
7350 for (w = wpa_s->global->ifaces; w; w = w->next)
7351 wpas_p2p_remove_client_go(w, peer, iface_addr);
7352}
7353
7354
7355static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7356{
7357 struct wpa_supplicant *wpa_s = eloop_ctx;
7358 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7359}
7360
7361
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007362static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7363{
7364 struct wpa_supplicant *wpa_s = eloop_ctx;
7365
7366 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7367 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7368}
7369
7370
7371int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7372 struct wpa_ssid *ssid)
7373{
7374 struct wpa_supplicant *iface;
7375
7376 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7377 if (!iface->current_ssid ||
7378 iface->current_ssid->frequency == freq ||
7379 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7380 !iface->current_ssid->p2p_group))
7381 continue;
7382
7383 /* Remove the connection with least priority */
7384 if (!wpas_is_p2p_prioritized(iface)) {
7385 /* STA connection has priority over existing
7386 * P2P connection, so remove the interface. */
7387 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7388 eloop_register_timeout(0, 0,
7389 wpas_p2p_group_freq_conflict,
7390 iface, NULL);
7391 /* If connection in progress is P2P connection, do not
7392 * proceed for the connection. */
7393 if (wpa_s == iface)
7394 return -1;
7395 else
7396 return 0;
7397 } else {
7398 /* P2P connection has priority, disable the STA network
7399 */
7400 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7401 ssid);
7402 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7403 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7404 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7405 ETH_ALEN);
7406 /* If P2P connection is in progress, continue
7407 * connecting...*/
7408 if (wpa_s == iface)
7409 return 0;
7410 else
7411 return -1;
7412 }
7413 }
7414
7415 return 0;
7416}
7417
7418
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007419int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7420{
7421 struct wpa_ssid *ssid = wpa_s->current_ssid;
7422
7423 if (ssid == NULL || !ssid->p2p_group)
7424 return 0;
7425
7426 if (wpa_s->p2p_last_4way_hs_fail &&
7427 wpa_s->p2p_last_4way_hs_fail == ssid) {
7428 u8 go_dev_addr[ETH_ALEN];
7429 struct wpa_ssid *persistent;
7430
7431 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7432 ssid->ssid,
7433 ssid->ssid_len) <= 0) {
7434 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7435 goto disconnect;
7436 }
7437
7438 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7439 MACSTR, MAC2STR(go_dev_addr));
7440 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7441 ssid->ssid,
7442 ssid->ssid_len);
7443 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7444 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7445 goto disconnect;
7446 }
7447 wpa_msg_global(wpa_s->parent, MSG_INFO,
7448 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7449 persistent->id);
7450 disconnect:
7451 wpa_s->p2p_last_4way_hs_fail = NULL;
7452 /*
7453 * Remove the group from a timeout to avoid issues with caller
7454 * continuing to use the interface if this is on a P2P group
7455 * interface.
7456 */
7457 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7458 wpa_s, NULL);
7459 return 1;
7460 }
7461
7462 wpa_s->p2p_last_4way_hs_fail = ssid;
7463 return 0;
7464}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007465
7466
7467#ifdef CONFIG_WPS_NFC
7468
7469static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7470 struct wpabuf *p2p)
7471{
7472 struct wpabuf *ret;
7473 size_t wsc_len;
7474
7475 if (p2p == NULL) {
7476 wpabuf_free(wsc);
7477 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7478 return NULL;
7479 }
7480
7481 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7482 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7483 if (ret == NULL) {
7484 wpabuf_free(wsc);
7485 wpabuf_free(p2p);
7486 return NULL;
7487 }
7488
7489 wpabuf_put_be16(ret, wsc_len);
7490 if (wsc)
7491 wpabuf_put_buf(ret, wsc);
7492 wpabuf_put_be16(ret, wpabuf_len(p2p));
7493 wpabuf_put_buf(ret, p2p);
7494
7495 wpabuf_free(wsc);
7496 wpabuf_free(p2p);
7497 wpa_hexdump_buf(MSG_DEBUG,
7498 "P2P: Generated NFC connection handover message", ret);
7499
7500 if (ndef && ret) {
7501 struct wpabuf *tmp;
7502 tmp = ndef_build_p2p(ret);
7503 wpabuf_free(ret);
7504 if (tmp == NULL) {
7505 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7506 return NULL;
7507 }
7508 ret = tmp;
7509 }
7510
7511 return ret;
7512}
7513
7514
7515static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7516 struct wpa_ssid **ssid, u8 *go_dev_addr)
7517{
7518 struct wpa_supplicant *iface;
7519
7520 if (go_dev_addr)
7521 os_memset(go_dev_addr, 0, ETH_ALEN);
7522 if (ssid)
7523 *ssid = NULL;
7524 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7525 if (iface->wpa_state < WPA_ASSOCIATING ||
7526 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7527 !iface->current_ssid->p2p_group ||
7528 iface->current_ssid->mode != WPAS_MODE_INFRA)
7529 continue;
7530 if (ssid)
7531 *ssid = iface->current_ssid;
7532 if (go_dev_addr)
7533 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7534 return iface->assoc_freq;
7535 }
7536 return 0;
7537}
7538
7539
7540struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7541 int ndef)
7542{
7543 struct wpabuf *wsc, *p2p;
7544 struct wpa_ssid *ssid;
7545 u8 go_dev_addr[ETH_ALEN];
7546 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7547
7548 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7549 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7550 return NULL;
7551 }
7552
7553 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7554 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7555 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7556 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7557 return NULL;
7558 }
7559
7560 if (cli_freq == 0) {
7561 wsc = wps_build_nfc_handover_req_p2p(
7562 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7563 } else
7564 wsc = NULL;
7565 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7566 go_dev_addr, ssid ? ssid->ssid : NULL,
7567 ssid ? ssid->ssid_len : 0);
7568
7569 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7570}
7571
7572
7573struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7574 int ndef, int tag)
7575{
7576 struct wpabuf *wsc, *p2p;
7577 struct wpa_ssid *ssid;
7578 u8 go_dev_addr[ETH_ALEN];
7579 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7580
7581 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7582 return NULL;
7583
7584 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7585 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7586 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7587 return NULL;
7588
7589 if (cli_freq == 0) {
7590 wsc = wps_build_nfc_handover_sel_p2p(
7591 wpa_s->parent->wps,
7592 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7593 DEV_PW_NFC_CONNECTION_HANDOVER,
7594 wpa_s->conf->wps_nfc_dh_pubkey,
7595 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7596 } else
7597 wsc = NULL;
7598 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7599 go_dev_addr, ssid ? ssid->ssid : NULL,
7600 ssid ? ssid->ssid_len : 0);
7601
7602 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7603}
7604
7605
7606static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7607 struct p2p_nfc_params *params)
7608{
7609 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7610 "connection handover (freq=%d)",
7611 params->go_freq);
7612
7613 if (params->go_freq && params->go_ssid_len) {
7614 wpa_s->p2p_wps_method = WPS_NFC;
7615 wpa_s->pending_join_wps_method = WPS_NFC;
7616 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7617 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7618 ETH_ALEN);
7619 return wpas_p2p_join_start(wpa_s, params->go_freq,
7620 params->go_ssid,
7621 params->go_ssid_len);
7622 }
7623
7624 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7625 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7626 params->go_freq, -1, 0, 1, 1);
7627}
7628
7629
7630static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7631 struct p2p_nfc_params *params, int tag)
7632{
7633 int res, persistent;
7634 struct wpa_ssid *ssid;
7635
7636 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7637 "connection handover");
7638 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7639 ssid = wpa_s->current_ssid;
7640 if (ssid == NULL)
7641 continue;
7642 if (ssid->mode != WPAS_MODE_P2P_GO)
7643 continue;
7644 if (wpa_s->ap_iface == NULL)
7645 continue;
7646 break;
7647 }
7648 if (wpa_s == NULL) {
7649 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7650 return -1;
7651 }
7652
7653 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7654 DEV_PW_NFC_CONNECTION_HANDOVER &&
7655 !wpa_s->parent->p2p_oob_dev_pw) {
7656 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7657 return -1;
7658 }
7659 res = wpas_ap_wps_add_nfc_pw(
7660 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7661 wpa_s->parent->p2p_oob_dev_pw,
7662 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7663 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7664 if (res)
7665 return res;
7666
7667 if (!tag) {
7668 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7669 return 0;
7670 }
7671
7672 if (!params->peer ||
7673 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7674 return 0;
7675
7676 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7677 " to join", MAC2STR(params->peer->p2p_device_addr));
7678
7679 wpa_s->global->p2p_invite_group = wpa_s;
7680 persistent = ssid->p2p_persistent_group &&
7681 wpas_p2p_get_persistent(wpa_s->parent,
7682 params->peer->p2p_device_addr,
7683 ssid->ssid, ssid->ssid_len);
7684 wpa_s->parent->pending_invite_ssid_id = -1;
7685
7686 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7687 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7688 ssid->ssid, ssid->ssid_len, ssid->frequency,
7689 wpa_s->global->p2p_dev_addr, persistent, 0,
7690 wpa_s->parent->p2p_oob_dev_pw_id);
7691}
7692
7693
7694static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7695 struct p2p_nfc_params *params,
7696 int forced_freq)
7697{
7698 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7699 "connection handover");
7700 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7701 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7702 forced_freq, -1, 0, 1, 1);
7703}
7704
7705
7706static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7707 struct p2p_nfc_params *params,
7708 int forced_freq)
7709{
7710 int res;
7711
7712 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7713 "connection handover");
7714 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7715 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7716 forced_freq, -1, 0, 1, 1);
7717 if (res)
7718 return res;
7719
7720 res = wpas_p2p_listen(wpa_s, 60);
7721 if (res) {
7722 p2p_unauthorize(wpa_s->global->p2p,
7723 params->peer->p2p_device_addr);
7724 }
7725
7726 return res;
7727}
7728
7729
7730static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7731 const struct wpabuf *data,
7732 int sel, int tag, int forced_freq)
7733{
7734 const u8 *pos, *end;
7735 u16 len, id;
7736 struct p2p_nfc_params params;
7737 int res;
7738
7739 os_memset(&params, 0, sizeof(params));
7740 params.sel = sel;
7741
7742 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7743
7744 pos = wpabuf_head(data);
7745 end = pos + wpabuf_len(data);
7746
7747 if (end - pos < 2) {
7748 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7749 "attributes");
7750 return -1;
7751 }
7752 len = WPA_GET_BE16(pos);
7753 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007754 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007755 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7756 "attributes");
7757 return -1;
7758 }
7759 params.wsc_attr = pos;
7760 params.wsc_len = len;
7761 pos += len;
7762
7763 if (end - pos < 2) {
7764 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7765 "attributes");
7766 return -1;
7767 }
7768 len = WPA_GET_BE16(pos);
7769 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007770 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007771 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7772 "attributes");
7773 return -1;
7774 }
7775 params.p2p_attr = pos;
7776 params.p2p_len = len;
7777 pos += len;
7778
7779 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7780 params.wsc_attr, params.wsc_len);
7781 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7782 params.p2p_attr, params.p2p_len);
7783 if (pos < end) {
7784 wpa_hexdump(MSG_DEBUG,
7785 "P2P: Ignored extra data after P2P attributes",
7786 pos, end - pos);
7787 }
7788
7789 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7790 if (res)
7791 return res;
7792
7793 if (params.next_step == NO_ACTION)
7794 return 0;
7795
7796 if (params.next_step == BOTH_GO) {
7797 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7798 MAC2STR(params.peer->p2p_device_addr));
7799 return 0;
7800 }
7801
7802 if (params.next_step == PEER_CLIENT) {
7803 if (!is_zero_ether_addr(params.go_dev_addr)) {
7804 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7805 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7806 " ssid=\"%s\"",
7807 MAC2STR(params.peer->p2p_device_addr),
7808 params.go_freq,
7809 MAC2STR(params.go_dev_addr),
7810 wpa_ssid_txt(params.go_ssid,
7811 params.go_ssid_len));
7812 } else {
7813 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7814 "peer=" MACSTR " freq=%d",
7815 MAC2STR(params.peer->p2p_device_addr),
7816 params.go_freq);
7817 }
7818 return 0;
7819 }
7820
7821 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7822 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7823 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7824 return 0;
7825 }
7826
7827 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7828 wpa_s->p2p_oob_dev_pw = NULL;
7829
7830 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7831 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7832 "received");
7833 return -1;
7834 }
7835
7836 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7837 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7838 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7839 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7840 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7841 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7842 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7843
7844 if (tag) {
7845 if (id < 0x10) {
7846 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7847 "peer OOB Device Password Id %u", id);
7848 return -1;
7849 }
7850 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7851 "Device Password Id %u", id);
7852 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7853 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7854 params.oob_dev_pw_len -
7855 WPS_OOB_PUBKEY_HASH_LEN - 2);
7856 wpa_s->p2p_oob_dev_pw_id = id;
7857 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7858 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7859 params.oob_dev_pw_len -
7860 WPS_OOB_PUBKEY_HASH_LEN - 2);
7861 if (wpa_s->p2p_oob_dev_pw == NULL)
7862 return -1;
7863
7864 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7865 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7866 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7867 return -1;
7868 } else {
7869 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7870 "without Device Password");
7871 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7872 }
7873
7874 switch (params.next_step) {
7875 case NO_ACTION:
7876 case BOTH_GO:
7877 case PEER_CLIENT:
7878 /* already covered above */
7879 return 0;
7880 case JOIN_GROUP:
7881 return wpas_p2p_nfc_join_group(wpa_s, &params);
7882 case AUTH_JOIN:
7883 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7884 case INIT_GO_NEG:
7885 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7886 case RESP_GO_NEG:
7887 /* TODO: use own OOB Dev Pw */
7888 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7889 }
7890
7891 return -1;
7892}
7893
7894
7895int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7896 const struct wpabuf *data, int forced_freq)
7897{
7898 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7899 return -1;
7900
7901 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7902}
7903
7904
7905int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7906 const struct wpabuf *req,
7907 const struct wpabuf *sel, int forced_freq)
7908{
7909 struct wpabuf *tmp;
7910 int ret;
7911
7912 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7913 return -1;
7914
7915 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7916
7917 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7918 wpabuf_head(req), wpabuf_len(req));
7919 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7920 wpabuf_head(sel), wpabuf_len(sel));
7921 if (forced_freq)
7922 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7923 tmp = ndef_parse_p2p(init ? sel : req);
7924 if (tmp == NULL) {
7925 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7926 return -1;
7927 }
7928
7929 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7930 forced_freq);
7931 wpabuf_free(tmp);
7932
7933 return ret;
7934}
7935
7936
7937int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7938{
7939 const u8 *if_addr;
7940 int go_intent = wpa_s->conf->p2p_go_intent;
7941 struct wpa_supplicant *iface;
7942
7943 if (wpa_s->global->p2p == NULL)
7944 return -1;
7945
7946 if (!enabled) {
7947 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7948 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7949 {
7950 if (!iface->ap_iface)
7951 continue;
7952 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7953 }
7954 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7955 0, NULL);
7956 if (wpa_s->p2p_nfc_tag_enabled)
7957 wpas_p2p_remove_pending_group_interface(wpa_s);
7958 wpa_s->p2p_nfc_tag_enabled = 0;
7959 return 0;
7960 }
7961
7962 if (wpa_s->global->p2p_disabled)
7963 return -1;
7964
7965 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7966 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7967 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7968 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7969 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7970 "to allow static handover cases");
7971 return -1;
7972 }
7973
7974 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7975
7976 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7977 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7978 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7979 if (wpa_s->p2p_oob_dev_pw == NULL)
7980 return -1;
7981 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7982
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007983 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7984 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7985 /*
7986 * P2P Group Interface present and the command came on group
7987 * interface, so enable the token for the current interface.
7988 */
7989 wpa_s->create_p2p_iface = 0;
7990 } else {
7991 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7992 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007993
7994 if (wpa_s->create_p2p_iface) {
7995 enum wpa_driver_if_type iftype;
7996 /* Prepare to add a new interface for the group */
7997 iftype = WPA_IF_P2P_GROUP;
7998 if (go_intent == 15)
7999 iftype = WPA_IF_P2P_GO;
8000 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8001 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8002 "interface for the group");
8003 return -1;
8004 }
8005
8006 if_addr = wpa_s->pending_interface_addr;
8007 } else
8008 if_addr = wpa_s->own_addr;
8009
8010 wpa_s->p2p_nfc_tag_enabled = enabled;
8011
8012 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8013 struct hostapd_data *hapd;
8014 if (iface->ap_iface == NULL)
8015 continue;
8016 hapd = iface->ap_iface->bss[0];
8017 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8018 hapd->conf->wps_nfc_dh_pubkey =
8019 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8020 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8021 hapd->conf->wps_nfc_dh_privkey =
8022 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8023 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8024 hapd->conf->wps_nfc_dev_pw =
8025 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8026 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8027
8028 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8029 wpa_dbg(iface, MSG_DEBUG,
8030 "P2P: Failed to enable NFC Tag for GO");
8031 }
8032 }
8033 p2p_set_authorized_oob_dev_pw_id(
8034 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8035 if_addr);
8036
8037 return 0;
8038}
8039
8040#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008041
8042
8043static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8044 struct wpa_used_freq_data *freqs,
8045 unsigned int num)
8046{
8047 u8 curr_chan, cand, chan;
8048 unsigned int i;
8049
8050 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8051 for (i = 0, cand = 0; i < num; i++) {
8052 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8053 if (curr_chan == chan) {
8054 cand = 0;
8055 break;
8056 }
8057
8058 if (chan == 1 || chan == 6 || chan == 11)
8059 cand = chan;
8060 }
8061
8062 if (cand) {
8063 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008064 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008065 cand);
8066 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8067 }
8068}
8069
8070
8071void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8072{
8073 struct wpa_used_freq_data *freqs;
8074 unsigned int num = wpa_s->num_multichan_concurrent;
8075
8076 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8077 return;
8078
8079 /*
8080 * If possible, optimize the Listen channel to be a channel that is
8081 * already used by one of the other interfaces.
8082 */
8083 if (!wpa_s->conf->p2p_optimize_listen_chan)
8084 return;
8085
8086 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8087 return;
8088
8089 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8090 if (!freqs)
8091 return;
8092
8093 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8094
8095 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8096 os_free(freqs);
8097}
8098
8099
8100void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8101{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008102 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8103 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8104 "the management interface is being removed");
8105 wpas_p2p_deinit_global(wpa_s->global);
8106 }
8107}
8108
8109
8110void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8111{
8112 if (wpa_s->ap_iface->bss)
8113 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8114 wpas_p2p_group_deinit(wpa_s);
8115}