blob: b9ebd380f2d32430a544ca91ffcbafbf651fd9f8 [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 Shmidta3dc3092015-06-23 11:21:28 -0700129static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
130 int already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131
132
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700133/*
134 * Get the number of concurrent channels that the HW can operate, but that are
135 * currently not in use by any of the wpa_supplicant interfaces.
136 */
137static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
138{
139 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800140 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700141
142 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
143 if (!freqs)
144 return -1;
145
146 num = get_shared_radio_freqs(wpa_s, freqs,
147 wpa_s->num_multichan_concurrent);
148 os_free(freqs);
149
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800150 unused = wpa_s->num_multichan_concurrent - num;
151 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
152 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700153}
154
155
156/*
157 * Get the frequencies that are currently in use by one or more of the virtual
158 * interfaces, and that are also valid for P2P operation.
159 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700160static unsigned int
161wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
162 struct wpa_used_freq_data *p2p_freqs,
163 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700164{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700165 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700166 unsigned int num, i, j;
167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700168 freqs = os_calloc(wpa_s->num_multichan_concurrent,
169 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700171 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700172
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700173 num = get_shared_radio_freqs_data(wpa_s, freqs,
174 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700176 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700177
178 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700179 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700180 p2p_freqs[j++] = freqs[i];
181 }
182
183 os_free(freqs);
184
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700185 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800186
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700187 return j;
188}
189
190
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700191static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
192 int freq)
193{
194 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
195 return;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -0700196
197 /* Use the wpa_s used to control the P2P Device operation */
198 wpa_s = wpa_s->global->p2p_init_wpa_s;
199
200 if (wpa_s->conf->p2p_ignore_shared_freq &&
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800201 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
202 wpas_p2p_num_unused_channels(wpa_s) > 0) {
203 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
204 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700205 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800206 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700207 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
208}
209
210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
212 struct wpa_scan_results *scan_res)
213{
214 size_t i;
215
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800216 if (wpa_s->p2p_scan_work) {
217 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
218 wpa_s->p2p_scan_work = NULL;
219 radio_work_done(work);
220 }
221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
223 return;
224
225 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
226 (int) scan_res->num);
227
228 for (i = 0; i < scan_res->num; i++) {
229 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800230 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700231 const u8 *ies;
232 size_t ies_len;
233
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800234 time_tmp_age.sec = bss->age / 1000;
235 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800236 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700237
238 ies = (const u8 *) (bss + 1);
239 ies_len = bss->ie_len;
240 if (bss->beacon_ie_len > 0 &&
241 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
242 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
243 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
244 MACSTR, MAC2STR(bss->bssid));
245 ies = ies + ies_len;
246 ies_len = bss->beacon_ie_len;
247 }
248
249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800251 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700252 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700253 break;
254 }
255
256 p2p_scan_res_handled(wpa_s->global->p2p);
257}
258
259
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800260static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
261{
262 struct wpa_supplicant *wpa_s = work->wpa_s;
263 struct wpa_driver_scan_params *params = work->ctx;
264 int ret;
265
266 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800267 if (!work->started) {
268 wpa_scan_free_params(params);
269 return;
270 }
271
272 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800273 return;
274 }
275
276 ret = wpa_drv_scan(wpa_s, params);
277 wpa_scan_free_params(params);
278 work->ctx = NULL;
279 if (ret) {
280 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800281 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800282 return;
283 }
284
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800285 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 os_get_reltime(&wpa_s->scan_trigger_time);
287 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
288 wpa_s->own_scan_requested = 1;
289 wpa_s->p2p_scan_work = work;
290}
291
292
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800293static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
294 int freq)
295{
296 if (wpa_s->global->p2p_24ghz_social_channels &&
297 (freq == 2412 || freq == 2437 || freq == 2462)) {
298 /*
299 * Search all social channels regardless of whether these have
300 * been disabled for P2P operating channel use to avoid missing
301 * peers.
302 */
303 return 1;
304 }
305 return p2p_supported_freq(wpa_s->global->p2p, freq);
306}
307
308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
310 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700311 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312{
313 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800314 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700316 unsigned int num_channels = 0;
317 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700319 u8 *n, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320
321 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
322 return -1;
323
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800324 if (wpa_s->p2p_scan_work) {
325 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
326 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700327 }
328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800329 params = os_zalloc(sizeof(*params));
330 if (params == NULL)
331 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332
333 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800334 params->num_ssids = 1;
335 n = os_malloc(P2P_WILDCARD_SSID_LEN);
336 if (n == NULL)
337 goto fail;
338 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
339 params->ssids[0].ssid = n;
340 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341
342 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700343 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
344 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345 num_req_dev_types, req_dev_types);
346 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800347 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800349 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
350 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 if (ies == NULL) {
352 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800353 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 }
355 wpabuf_put_buf(ies, wps_ie);
356 wpabuf_free(wps_ie);
357
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800358 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800360 params->p2p_probe = 1;
361 n = os_malloc(wpabuf_len(ies));
362 if (n == NULL) {
363 wpabuf_free(ies);
364 goto fail;
365 }
366 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
367 params->extra_ies = n;
368 params->extra_ies_len = wpabuf_len(ies);
369 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370
371 switch (type) {
372 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700373 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
374 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800375 if (params->freqs == NULL)
376 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700377 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800378 if (wpas_p2p_search_social_channel(
379 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700380 params->freqs[num_channels++] =
381 social_channels_freq[i];
382 }
383 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384 break;
385 case P2P_SCAN_FULL:
386 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800387 case P2P_SCAN_SPECIFIC:
388 params->freqs = os_calloc(2, sizeof(int));
389 if (params->freqs == NULL)
390 goto fail;
391 params->freqs[0] = freq;
392 params->freqs[1] = 0;
393 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700395 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
396 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800397 if (params->freqs == NULL)
398 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700399 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800400 if (wpas_p2p_search_social_channel(
401 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700402 params->freqs[num_channels++] =
403 social_channels_freq[i];
404 }
405 if (p2p_supported_freq(wpa_s->global->p2p, freq))
406 params->freqs[num_channels++] = freq;
407 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 break;
409 }
410
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800411 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800412 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
413 params) < 0)
414 goto fail;
415 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800417fail:
418 wpa_scan_free_params(params);
419 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420}
421
422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
424{
425 switch (p2p_group_interface) {
426 case P2P_GROUP_INTERFACE_PENDING:
427 return WPA_IF_P2P_GROUP;
428 case P2P_GROUP_INTERFACE_GO:
429 return WPA_IF_P2P_GO;
430 case P2P_GROUP_INTERFACE_CLIENT:
431 return WPA_IF_P2P_CLIENT;
432 }
433
434 return WPA_IF_P2P_GROUP;
435}
436
437
438static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
439 const u8 *ssid,
440 size_t ssid_len, int *go)
441{
442 struct wpa_ssid *s;
443
444 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
445 for (s = wpa_s->conf->ssid; s; s = s->next) {
446 if (s->disabled != 0 || !s->p2p_group ||
447 s->ssid_len != ssid_len ||
448 os_memcmp(ssid, s->ssid, ssid_len) != 0)
449 continue;
450 if (s->mode == WPAS_MODE_P2P_GO &&
451 s != wpa_s->current_ssid)
452 continue;
453 if (go)
454 *go = s->mode == WPAS_MODE_P2P_GO;
455 return wpa_s;
456 }
457 }
458
459 return NULL;
460}
461
462
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800463static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
464{
465 struct wpa_supplicant *wpa_s = eloop_ctx;
466 wpa_printf(MSG_DEBUG,
467 "P2P: Complete previously requested removal of %s",
468 wpa_s->ifname);
469 wpas_p2p_disconnect(wpa_s);
470}
471
472
473static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
474 struct wpa_supplicant *calling_wpa_s)
475{
476 if (calling_wpa_s == wpa_s && wpa_s &&
477 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
478 /*
479 * The calling wpa_s instance is going to be removed. Do that
480 * from an eloop callback to keep the instance available until
481 * the caller has returned. This my be needed, e.g., to provide
482 * control interface responses on the per-interface socket.
483 */
484 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
485 wpa_s, NULL) < 0)
486 return -1;
487 return 0;
488 }
489
490 return wpas_p2p_disconnect(wpa_s);
491}
492
493
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800494/* Determine total number of clients in active groups where we are the GO */
495static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
496{
497 unsigned int count = 0;
498 struct wpa_ssid *s;
499
500 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
501 for (s = wpa_s->conf->ssid; s; s = s->next) {
502 wpa_printf(MSG_DEBUG,
503 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
504 wpa_s, s, s->disabled, s->p2p_group,
505 s->mode);
506 if (!s->disabled && s->p2p_group &&
507 s->mode == WPAS_MODE_P2P_GO) {
508 count += p2p_get_group_num_members(
509 wpa_s->p2p_group);
510 }
511 }
512 }
513
514 return count;
515}
516
517
518/* Find an interface for a P2P group where we are the GO */
519static struct wpa_supplicant *
520wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
521{
522 struct wpa_supplicant *save = NULL;
523 struct wpa_ssid *s;
524
525 if (!wpa_s)
526 return NULL;
527
528 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
529 for (s = wpa_s->conf->ssid; s; s = s->next) {
530 if (s->disabled || !s->p2p_group ||
531 s->mode != WPAS_MODE_P2P_GO)
532 continue;
533
534 /* Prefer a group with connected clients */
535 if (p2p_get_group_num_members(wpa_s->p2p_group))
536 return wpa_s;
537 save = wpa_s;
538 }
539 }
540
541 /* No group with connected clients, so pick the one without (if any) */
542 return save;
543}
544
545
546/* Find an active P2P group where we are the GO */
547static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
548 u8 *bssid)
549{
550 struct wpa_ssid *s, *empty = NULL;
551
552 if (!wpa_s)
553 return 0;
554
555 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
556 for (s = wpa_s->conf->ssid; s; s = s->next) {
557 if (s->disabled || !s->p2p_group ||
558 s->mode != WPAS_MODE_P2P_GO)
559 continue;
560
561 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
562 if (p2p_get_group_num_members(wpa_s->p2p_group))
563 return s;
564 empty = s;
565 }
566 }
567
568 return empty;
569}
570
571
572/* Find a persistent group where we are the GO */
573static struct wpa_ssid *
574wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
575{
576 struct wpa_ssid *s;
577
578 for (s = wpa_s->conf->ssid; s; s = s->next) {
579 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
580 return s;
581 }
582
583 return NULL;
584}
585
586
587static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
588{
589 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
590 struct wpa_ssid *s;
591 u8 conncap = P2PS_SETUP_NONE;
592 unsigned int owned_members = 0;
593 unsigned int owner = 0;
594 unsigned int client = 0;
595 struct wpa_supplicant *go_wpa_s;
596 struct wpa_ssid *persistent_go;
597 int p2p_no_group_iface;
598
599 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
600
601 /*
602 * For non-concurrent capable devices:
603 * If persistent_go, then no new.
604 * If GO, then no client.
605 * If client, then no GO.
606 */
607 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
608 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
609 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
610
611 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
612 go_wpa_s, persistent_go);
613
614 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
615 tmp_wpa_s = tmp_wpa_s->next) {
616 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
617 wpa_printf(MSG_DEBUG,
618 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
619 tmp_wpa_s, s, s->disabled,
620 s->p2p_group, s->mode);
621 if (!s->disabled && s->p2p_group) {
622 if (s->mode == WPAS_MODE_P2P_GO) {
623 owned_members +=
624 p2p_get_group_num_members(
625 tmp_wpa_s->p2p_group);
626 owner++;
627 } else
628 client++;
629 }
630 }
631 }
632
633 /* If not concurrent, restrict our choices */
634 if (p2p_no_group_iface) {
635 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
636
637 if (client)
638 return P2PS_SETUP_NONE;
639
640 if (go_wpa_s) {
641 if (role == P2PS_SETUP_CLIENT ||
642 incoming == P2PS_SETUP_GROUP_OWNER ||
643 p2p_client_limit_reached(go_wpa_s->p2p_group))
644 return P2PS_SETUP_NONE;
645
646 return P2PS_SETUP_GROUP_OWNER;
647 }
648
649 if (persistent_go) {
650 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
651 if (!incoming)
652 return P2PS_SETUP_GROUP_OWNER |
653 P2PS_SETUP_CLIENT;
654 if (incoming == P2PS_SETUP_NEW) {
655 u8 r;
656
657 if (os_get_random(&r, sizeof(r)) < 0 ||
658 (r & 1))
659 return P2PS_SETUP_CLIENT;
660 return P2PS_SETUP_GROUP_OWNER;
661 }
662 }
663 }
664 }
665
666 /* If a required role has been specified, handle it here */
667 if (role && role != P2PS_SETUP_NEW) {
668 switch (incoming) {
669 case P2PS_SETUP_NONE:
670 case P2PS_SETUP_NEW:
671 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
672 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
673 conncap = role;
674 goto grp_owner;
675
676 case P2PS_SETUP_GROUP_OWNER:
677 /*
678 * Must be a complimentary role - cannot be a client to
679 * more than one peer.
680 */
681 if (incoming == role || client)
682 return P2PS_SETUP_NONE;
683
684 return P2PS_SETUP_CLIENT;
685
686 case P2PS_SETUP_CLIENT:
687 /* Must be a complimentary role */
688 if (incoming != role) {
689 conncap = P2PS_SETUP_GROUP_OWNER;
690 goto grp_owner;
691 }
692
693 default:
694 return P2PS_SETUP_NONE;
695 }
696 }
697
698 /*
699 * For now, we only will support ownership of one group, and being a
700 * client of one group. Therefore, if we have either an existing GO
701 * group, or an existing client group, we will not do a new GO
702 * negotiation, but rather try to re-use the existing groups.
703 */
704 switch (incoming) {
705 case P2PS_SETUP_NONE:
706 case P2PS_SETUP_NEW:
707 if (client)
708 conncap = P2PS_SETUP_GROUP_OWNER;
709 else if (!owned_members)
710 conncap = P2PS_SETUP_NEW;
711 else if (incoming == P2PS_SETUP_NONE)
712 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
713 else
714 conncap = P2PS_SETUP_CLIENT;
715 break;
716
717 case P2PS_SETUP_CLIENT:
718 conncap = P2PS_SETUP_GROUP_OWNER;
719 break;
720
721 case P2PS_SETUP_GROUP_OWNER:
722 if (!client)
723 conncap = P2PS_SETUP_CLIENT;
724 break;
725
726 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
727 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
728 if (client)
729 conncap = P2PS_SETUP_GROUP_OWNER;
730 else {
731 u8 r;
732
733 if (os_get_random(&r, sizeof(r)) < 0 ||
734 (r & 1))
735 conncap = P2PS_SETUP_CLIENT;
736 else
737 conncap = P2PS_SETUP_GROUP_OWNER;
738 }
739 break;
740
741 default:
742 return P2PS_SETUP_NONE;
743 }
744
745grp_owner:
746 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
747 (!incoming && (conncap & P2PS_SETUP_NEW))) {
748 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
749 conncap &= ~P2PS_SETUP_GROUP_OWNER;
750 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
751 owner, owned_members, conncap);
752
753 s = wpas_p2p_get_persistent_go(wpa_s);
754
755 if (!s && !owner && p2p_no_group_iface) {
756 p2p_set_intended_addr(wpa_s->global->p2p,
757 wpa_s->own_addr);
758 } else if (!s && !owner) {
759 if (wpas_p2p_add_group_interface(wpa_s,
760 WPA_IF_P2P_GO) < 0) {
761 wpa_printf(MSG_ERROR,
762 "P2P: Failed to allocate a new interface for the group");
763 return P2PS_SETUP_NONE;
764 }
765 wpa_s->global->pending_group_iface_for_p2ps = 1;
766 p2p_set_intended_addr(wpa_s->global->p2p,
767 wpa_s->pending_interface_addr);
768 }
769 }
770
771 return conncap;
772}
773
774
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700775static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
776 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777{
778 struct wpa_ssid *ssid;
779 char *gtype;
780 const char *reason;
781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 ssid = wpa_s->current_ssid;
783 if (ssid == NULL) {
784 /*
785 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700786 * pending P2P group interface waiting for provisioning or a
787 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 */
789 ssid = wpa_s->conf->ssid;
790 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700791 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 break;
793 ssid = ssid->next;
794 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300795 if (ssid == NULL &&
796 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
797 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700798 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
799 "not found");
800 return -1;
801 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 }
803 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
804 gtype = "GO";
805 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
806 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
807 wpa_s->reassociate = 0;
808 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 gtype = "client";
810 } else
811 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700812
813 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
814 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
815
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800816 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700817 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800818 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
819 wpa_s, NULL)) {
820 wpa_printf(MSG_DEBUG,
821 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
822 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
823 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
824 wpa_s, NULL);
825 }
826 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 if (wpa_s->cross_connect_in_use) {
829 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 wpa_msg_global(wpa_s->parent, MSG_INFO,
831 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
832 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700834 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 case P2P_GROUP_REMOVAL_REQUESTED:
836 reason = " reason=REQUESTED";
837 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700838 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
839 reason = " reason=FORMATION_FAILED";
840 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
842 reason = " reason=IDLE";
843 break;
844 case P2P_GROUP_REMOVAL_UNAVAILABLE:
845 reason = " reason=UNAVAILABLE";
846 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700847 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
848 reason = " reason=GO_ENDING_SESSION";
849 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700850 case P2P_GROUP_REMOVAL_PSK_FAILURE:
851 reason = " reason=PSK_FAILURE";
852 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800853 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
854 reason = " reason=FREQ_CONFLICT";
855 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 default:
857 reason = "";
858 break;
859 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700860 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700861 wpa_msg_global(wpa_s->parent, MSG_INFO,
862 P2P_EVENT_GROUP_REMOVED "%s %s%s",
863 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700864 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800866 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
867 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700868 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
869 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800870 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700871 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800872 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
873 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700874 wpa_s->p2p_in_provisioning = 0;
875 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700876
Dmitry Shmidt15907092014-03-25 10:42:57 -0700877 wpa_s->p2p_in_invitation = 0;
878
Dmitry Shmidt96571392013-10-14 12:54:46 -0700879 /*
880 * Make sure wait for the first client does not remain active after the
881 * group has been removed.
882 */
883 wpa_s->global->p2p_go_wait_client.sec = 0;
884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
886 struct wpa_global *global;
887 char *ifname;
888 enum wpa_driver_if_type type;
889 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
890 wpa_s->ifname);
891 global = wpa_s->global;
892 ifname = os_strdup(wpa_s->ifname);
893 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800894 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700895 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 wpa_s = global->ifaces;
897 if (wpa_s && ifname)
898 wpa_drv_if_remove(wpa_s, type, ifname);
899 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300900 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 }
902
Dmitry Shmidt96571392013-10-14 12:54:46 -0700903 if (!wpa_s->p2p_go_group_formation_completed) {
904 wpa_s->global->p2p_group_formation = NULL;
905 wpa_s->p2p_in_provisioning = 0;
906 }
907
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800908 wpa_s->show_group_started = 0;
909 os_free(wpa_s->go_params);
910 wpa_s->go_params = NULL;
911
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800912 os_free(wpa_s->p2p_group_common_freqs);
913 wpa_s->p2p_group_common_freqs = NULL;
914 wpa_s->p2p_group_common_freqs_num = 0;
915
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800916 wpa_s->waiting_presence_resp = 0;
917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
919 if (ssid && (ssid->p2p_group ||
920 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
921 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
922 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700923 if (ssid == wpa_s->current_ssid) {
924 wpa_sm_set_config(wpa_s->wpa, NULL);
925 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700927 }
928 /*
929 * Networks objects created during any P2P activities are not
930 * exposed out as they might/will confuse certain non-P2P aware
931 * applications since these network objects won't behave like
932 * regular ones.
933 *
934 * Likewise, we don't send out network removed signals for such
935 * network objects.
936 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 wpa_config_remove_network(wpa_s->conf, id);
938 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800939 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 } else {
941 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
942 "found");
943 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700944 if (wpa_s->ap_iface)
945 wpa_supplicant_ap_deinit(wpa_s);
946 else
947 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700948
949 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950}
951
952
953static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
954 u8 *go_dev_addr,
955 const u8 *ssid, size_t ssid_len)
956{
957 struct wpa_bss *bss;
958 const u8 *bssid;
959 struct wpabuf *p2p;
960 u8 group_capab;
961 const u8 *addr;
962
963 if (wpa_s->go_params)
964 bssid = wpa_s->go_params->peer_interface_addr;
965 else
966 bssid = wpa_s->bssid;
967
968 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800969 if (bss == NULL && wpa_s->go_params &&
970 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
971 bss = wpa_bss_get_p2p_dev_addr(
972 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 if (bss == NULL) {
974 u8 iface_addr[ETH_ALEN];
975 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
976 iface_addr) == 0)
977 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
978 }
979 if (bss == NULL) {
980 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
981 "group is persistent - BSS " MACSTR " not found",
982 MAC2STR(bssid));
983 return 0;
984 }
985
986 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700987 if (p2p == NULL)
988 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
989 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 if (p2p == NULL) {
991 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
992 "group is persistent - BSS " MACSTR
993 " did not include P2P IE", MAC2STR(bssid));
994 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
995 (u8 *) (bss + 1), bss->ie_len);
996 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
997 ((u8 *) bss + 1) + bss->ie_len,
998 bss->beacon_ie_len);
999 return 0;
1000 }
1001
1002 group_capab = p2p_get_group_capab(p2p);
1003 addr = p2p_get_go_dev_addr(p2p);
1004 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
1005 "group_capab=0x%x", group_capab);
1006 if (addr) {
1007 os_memcpy(go_dev_addr, addr, ETH_ALEN);
1008 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
1009 MAC2STR(addr));
1010 } else
1011 os_memset(go_dev_addr, 0, ETH_ALEN);
1012 wpabuf_free(p2p);
1013
1014 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1015 "go_dev_addr=" MACSTR,
1016 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1017
1018 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1019}
1020
1021
Jouni Malinen75ecf522011-06-27 15:19:46 -07001022static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1023 struct wpa_ssid *ssid,
1024 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025{
1026 struct wpa_ssid *s;
1027 int changed = 0;
1028
1029 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1030 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1031 for (s = wpa_s->conf->ssid; s; s = s->next) {
1032 if (s->disabled == 2 &&
1033 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1034 s->ssid_len == ssid->ssid_len &&
1035 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1036 break;
1037 }
1038
1039 if (s) {
1040 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1041 "entry");
1042 if (ssid->passphrase && !s->passphrase)
1043 changed = 1;
1044 else if (ssid->passphrase && s->passphrase &&
1045 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1046 changed = 1;
1047 } else {
1048 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1049 "entry");
1050 changed = 1;
1051 s = wpa_config_add_network(wpa_s->conf);
1052 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001053 return -1;
1054
1055 /*
1056 * Instead of network_added we emit persistent_group_added
1057 * notification. Also to keep the defense checks in
1058 * persistent_group obj registration method, we set the
1059 * relevant flags in s to designate it as a persistent group.
1060 */
1061 s->p2p_group = 1;
1062 s->p2p_persistent_group = 1;
1063 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 wpa_config_set_network_defaults(s);
1065 }
1066
1067 s->p2p_group = 1;
1068 s->p2p_persistent_group = 1;
1069 s->disabled = 2;
1070 s->bssid_set = 1;
1071 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1072 s->mode = ssid->mode;
1073 s->auth_alg = WPA_AUTH_ALG_OPEN;
1074 s->key_mgmt = WPA_KEY_MGMT_PSK;
1075 s->proto = WPA_PROTO_RSN;
1076 s->pairwise_cipher = WPA_CIPHER_CCMP;
1077 s->export_keys = 1;
1078 if (ssid->passphrase) {
1079 os_free(s->passphrase);
1080 s->passphrase = os_strdup(ssid->passphrase);
1081 }
1082 if (ssid->psk_set) {
1083 s->psk_set = 1;
1084 os_memcpy(s->psk, ssid->psk, 32);
1085 }
1086 if (s->passphrase && !s->psk_set)
1087 wpa_config_update_psk(s);
1088 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1089 os_free(s->ssid);
1090 s->ssid = os_malloc(ssid->ssid_len);
1091 }
1092 if (s->ssid) {
1093 s->ssid_len = ssid->ssid_len;
1094 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1095 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001096 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1097 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1098 wpa_s->global->add_psk = NULL;
1099 changed = 1;
1100 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 if (changed && wpa_s->conf->update_config &&
1103 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1104 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1105 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001106
1107 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108}
1109
1110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001111static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1112 const u8 *addr)
1113{
1114 struct wpa_ssid *ssid, *s;
1115 u8 *n;
1116 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001117 int found = 0;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001118 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001119
1120 ssid = wpa_s->current_ssid;
1121 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1122 !ssid->p2p_persistent_group)
1123 return;
1124
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001125 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001126 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1127 continue;
1128
1129 if (s->ssid_len == ssid->ssid_len &&
1130 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1131 break;
1132 }
1133
1134 if (s == NULL)
1135 return;
1136
1137 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001138 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001139 ETH_ALEN) != 0)
1140 continue;
1141
1142 if (i == s->num_p2p_clients - 1)
1143 return; /* already the most recent entry */
1144
1145 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001146 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1147 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1148 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001149 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001150 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1151 ETH_ALEN);
1152 os_memset(s->p2p_client_list +
1153 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1154 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001155 found = 1;
1156 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157 }
1158
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001159 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1160 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001161 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001162 if (n == NULL)
1163 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001164 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1165 ETH_ALEN);
1166 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1167 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001168 s->p2p_client_list = n;
1169 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001170 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001171 /* Not enough room for an additional entry - drop the oldest
1172 * entry */
1173 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001174 s->p2p_client_list + 2 * ETH_ALEN,
1175 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001176 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001177 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001178 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001179 os_memset(s->p2p_client_list +
1180 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1181 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001182 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001183
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07001184 if (p2p_wpa_s->conf->update_config &&
1185 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001186 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001187}
1188
1189
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001190static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1191 int go, struct wpa_ssid *ssid, int freq,
1192 const u8 *psk, const char *passphrase,
1193 const u8 *go_dev_addr, int persistent,
1194 const char *extra)
1195{
1196 const char *ssid_txt;
1197 char psk_txt[65];
1198
1199 if (psk)
1200 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1201 else
1202 psk_txt[0] = '\0';
1203
1204 if (ssid)
1205 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1206 else
1207 ssid_txt = "";
1208
1209 if (passphrase && passphrase[0] == '\0')
1210 passphrase = NULL;
1211
1212 /*
1213 * Include PSK/passphrase only in the control interface message and
1214 * leave it out from the debug log entry.
1215 */
1216 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1217 P2P_EVENT_GROUP_STARTED
1218 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1219 MACSTR "%s%s",
1220 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1221 psk ? " psk=" : "", psk_txt,
1222 passphrase ? " passphrase=\"" : "",
1223 passphrase ? passphrase : "",
1224 passphrase ? "\"" : "",
1225 MAC2STR(go_dev_addr),
1226 persistent ? " [PERSISTENT]" : "", extra);
1227 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1228 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1229 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1230 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1231 extra);
1232}
1233
1234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001236 int success, int already_deleted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237{
1238 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 int client;
1240 int persistent;
1241 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07001242 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243
1244 /*
1245 * This callback is likely called for the main interface. Update wpa_s
1246 * to use the group interface if a new interface was created for the
1247 * group.
1248 */
1249 if (wpa_s->global->p2p_group_formation)
1250 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001251 if (wpa_s->p2p_go_group_formation_completed) {
1252 wpa_s->global->p2p_group_formation = NULL;
1253 wpa_s->p2p_in_provisioning = 0;
1254 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001255 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001256 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257
1258 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001259 wpa_msg_global(wpa_s->parent, MSG_INFO,
1260 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001261 if (already_deleted)
1262 return;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001263 wpas_p2p_group_delete(wpa_s,
1264 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 return;
1266 }
1267
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001268 wpa_msg_global(wpa_s->parent, MSG_INFO,
1269 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270
1271 ssid = wpa_s->current_ssid;
1272 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1273 ssid->mode = WPAS_MODE_P2P_GO;
1274 p2p_group_notif_formation_done(wpa_s->p2p_group);
1275 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1276 }
1277
1278 persistent = 0;
1279 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 client = ssid->mode == WPAS_MODE_INFRA;
1281 if (ssid->mode == WPAS_MODE_P2P_GO) {
1282 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001283 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1284 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 } else
1286 persistent = wpas_p2p_persistent_group(wpa_s,
1287 go_dev_addr,
1288 ssid->ssid,
1289 ssid->ssid_len);
1290 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 client = wpa_s->p2p_group_interface ==
1292 P2P_GROUP_INTERFACE_CLIENT;
1293 os_memset(go_dev_addr, 0, ETH_ALEN);
1294 }
1295
1296 wpa_s->show_group_started = 0;
1297 if (client) {
1298 /*
1299 * Indicate event only after successfully completed 4-way
1300 * handshake, i.e., when the interface is ready for data
1301 * packets.
1302 */
1303 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001305 wpas_p2p_group_started(wpa_s, 1, ssid,
1306 ssid ? ssid->frequency : 0,
1307 ssid && ssid->passphrase == NULL &&
1308 ssid->psk_set ? ssid->psk : NULL,
1309 ssid ? ssid->passphrase : NULL,
1310 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 wpas_p2p_cross_connect_setup(wpa_s);
1312 wpas_p2p_set_group_idle_timeout(wpa_s);
1313 }
1314
1315 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001316 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1317 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001318 else {
1319 os_free(wpa_s->global->add_psk);
1320 wpa_s->global->add_psk = NULL;
1321 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001323 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001324 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001325 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001326 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001327 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328}
1329
1330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001331struct send_action_work {
1332 unsigned int freq;
1333 u8 dst[ETH_ALEN];
1334 u8 src[ETH_ALEN];
1335 u8 bssid[ETH_ALEN];
1336 size_t len;
1337 unsigned int wait_time;
1338 u8 buf[0];
1339};
1340
1341
1342static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1343 void *timeout_ctx)
1344{
1345 struct wpa_supplicant *wpa_s = eloop_ctx;
1346
1347 if (!wpa_s->p2p_send_action_work)
1348 return;
1349
1350 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1351 os_free(wpa_s->p2p_send_action_work->ctx);
1352 radio_work_done(wpa_s->p2p_send_action_work);
1353 wpa_s->p2p_send_action_work = NULL;
1354}
1355
1356
Dmitry Shmidt03658832014-08-13 11:03:49 -07001357static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001359 if (wpa_s->p2p_send_action_work) {
1360 struct send_action_work *awork;
1361 awork = wpa_s->p2p_send_action_work->ctx;
1362 if (awork->wait_time == 0) {
1363 os_free(awork);
1364 radio_work_done(wpa_s->p2p_send_action_work);
1365 wpa_s->p2p_send_action_work = NULL;
1366 } else {
1367 /*
1368 * In theory, this should not be needed, but number of
1369 * places in the P2P code is still using non-zero wait
1370 * time for the last Action frame in the sequence and
1371 * some of these do not call send_action_done().
1372 */
1373 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1374 wpa_s, NULL);
1375 eloop_register_timeout(
1376 0, awork->wait_time * 1000,
1377 wpas_p2p_send_action_work_timeout,
1378 wpa_s, NULL);
1379 }
1380 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001381}
1382
1383
1384static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1385 unsigned int freq,
1386 const u8 *dst, const u8 *src,
1387 const u8 *bssid,
1388 const u8 *data, size_t data_len,
1389 enum offchannel_send_action_result
1390 result)
1391{
1392 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1393
1394 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1397 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001399 switch (result) {
1400 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1401 res = P2P_SEND_ACTION_SUCCESS;
1402 break;
1403 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1404 res = P2P_SEND_ACTION_NO_ACK;
1405 break;
1406 case OFFCHANNEL_SEND_ACTION_FAILED:
1407 res = P2P_SEND_ACTION_FAILED;
1408 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 }
1410
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001411 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001413 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1414 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001415 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001416 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1417 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001419 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1420 "during p2p_connect-auto");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001421 wpa_msg_global(wpa_s->parent, MSG_INFO,
1422 P2P_EVENT_FALLBACK_TO_GO_NEG
1423 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001424 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1425 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 }
1427}
1428
1429
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001430static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1431{
1432 struct wpa_supplicant *wpa_s = work->wpa_s;
1433 struct send_action_work *awork = work->ctx;
1434
1435 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001436 if (work->started) {
1437 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1438 wpa_s, NULL);
1439 wpa_s->p2p_send_action_work = NULL;
1440 offchannel_send_action_done(wpa_s);
1441 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001442 os_free(awork);
1443 return;
1444 }
1445
1446 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1447 awork->bssid, awork->buf, awork->len,
1448 awork->wait_time,
1449 wpas_p2p_send_action_tx_status, 1) < 0) {
1450 os_free(awork);
1451 radio_work_done(work);
1452 return;
1453 }
1454 wpa_s->p2p_send_action_work = work;
1455}
1456
1457
1458static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1459 unsigned int freq, const u8 *dst,
1460 const u8 *src, const u8 *bssid, const u8 *buf,
1461 size_t len, unsigned int wait_time)
1462{
1463 struct send_action_work *awork;
1464
1465 if (wpa_s->p2p_send_action_work) {
1466 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1467 return -1;
1468 }
1469
1470 awork = os_zalloc(sizeof(*awork) + len);
1471 if (awork == NULL)
1472 return -1;
1473
1474 awork->freq = freq;
1475 os_memcpy(awork->dst, dst, ETH_ALEN);
1476 os_memcpy(awork->src, src, ETH_ALEN);
1477 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1478 awork->len = len;
1479 awork->wait_time = wait_time;
1480 os_memcpy(awork->buf, buf, len);
1481
1482 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1483 wpas_send_action_cb, awork) < 0) {
1484 os_free(awork);
1485 return -1;
1486 }
1487
1488 return 0;
1489}
1490
1491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1493 const u8 *src, const u8 *bssid, const u8 *buf,
1494 size_t len, unsigned int wait_time)
1495{
1496 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001497 int listen_freq = -1, send_freq = -1;
1498
1499 if (wpa_s->p2p_listen_work)
1500 listen_freq = wpa_s->p2p_listen_work->freq;
1501 if (wpa_s->p2p_send_action_work)
1502 send_freq = wpa_s->p2p_send_action_work->freq;
1503 if (listen_freq != (int) freq && send_freq != (int) freq) {
1504 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1505 listen_freq, send_freq);
1506 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1507 len, wait_time);
1508 }
1509
1510 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001511 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1512 wait_time,
1513 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514}
1515
1516
1517static void wpas_send_action_done(void *ctx)
1518{
1519 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001520
1521 if (wpa_s->p2p_send_action_work) {
1522 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1523 wpa_s, NULL);
1524 os_free(wpa_s->p2p_send_action_work->ctx);
1525 radio_work_done(wpa_s->p2p_send_action_work);
1526 wpa_s->p2p_send_action_work = NULL;
1527 }
1528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001529 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530}
1531
1532
1533static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1534 struct p2p_go_neg_results *params)
1535{
1536 if (wpa_s->go_params == NULL) {
1537 wpa_s->go_params = os_malloc(sizeof(*params));
1538 if (wpa_s->go_params == NULL)
1539 return -1;
1540 }
1541 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1542 return 0;
1543}
1544
1545
1546static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1547 struct p2p_go_neg_results *res)
1548{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001549 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001550 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1551 " dev_addr " MACSTR " wps_method %d",
1552 MAC2STR(res->peer_interface_addr),
1553 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1555 res->ssid, res->ssid_len);
1556 wpa_supplicant_ap_deinit(wpa_s);
1557 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001558 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001560#ifdef CONFIG_WPS_NFC
1561 } else if (res->wps_method == WPS_NFC) {
1562 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1563 res->peer_interface_addr,
1564 wpa_s->parent->p2p_oob_dev_pw,
1565 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1566 wpa_s->parent->p2p_oob_dev_pw_id ==
1567 DEV_PW_NFC_CONNECTION_HANDOVER ?
1568 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1569 NULL,
1570 NULL, 0, 0);
1571#endif /* CONFIG_WPS_NFC */
1572 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001574 if (wpa_s->p2p_wps_method == WPS_P2PS)
1575 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1577 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1578 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1579 wpa_s->p2p_pin, 1, dev_pw_id);
1580 }
1581}
1582
1583
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001584static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1585 struct wpa_ssid *ssid)
1586{
1587 struct wpa_ssid *persistent;
1588 struct psk_list_entry *psk;
1589 struct hostapd_data *hapd;
1590
1591 if (!wpa_s->ap_iface)
1592 return;
1593
1594 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1595 ssid->ssid_len);
1596 if (persistent == NULL)
1597 return;
1598
1599 hapd = wpa_s->ap_iface->bss[0];
1600
1601 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1602 list) {
1603 struct hostapd_wpa_psk *hpsk;
1604
1605 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1606 MACSTR " psk=%d",
1607 MAC2STR(psk->addr), psk->p2p);
1608 hpsk = os_zalloc(sizeof(*hpsk));
1609 if (hpsk == NULL)
1610 break;
1611 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1612 if (psk->p2p)
1613 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1614 else
1615 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1616 hpsk->next = hapd->conf->ssid.wpa_psk;
1617 hapd->conf->ssid.wpa_psk = hpsk;
1618 }
1619}
1620
1621
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001622static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1623{
1624 unsigned int i;
1625
1626 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1627 wpa_s->p2p_group_common_freqs_num);
1628
1629 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1630 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1631 i, wpa_s->p2p_group_common_freqs[i]);
1632}
1633
1634
1635static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1636 struct p2p_go_neg_results *params)
1637{
1638 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1639
1640 wpa_s->p2p_group_common_freqs_num = 0;
1641 os_free(wpa_s->p2p_group_common_freqs);
1642 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1643 if (!wpa_s->p2p_group_common_freqs)
1644 return;
1645
1646 for (i = 0; i < len; i++) {
1647 if (!wpa_s->go_params->freq_list[i])
1648 break;
1649 wpa_s->p2p_group_common_freqs[i] =
1650 wpa_s->go_params->freq_list[i];
1651 }
1652 wpa_s->p2p_group_common_freqs_num = i;
1653}
1654
1655
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001656static void p2p_config_write(struct wpa_supplicant *wpa_s)
1657{
1658#ifndef CONFIG_NO_CONFIG_WRITE
1659 if (wpa_s->parent->conf->update_config &&
1660 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1661 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1662#endif /* CONFIG_NO_CONFIG_WRITE */
1663}
1664
1665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666static void p2p_go_configured(void *ctx, void *data)
1667{
1668 struct wpa_supplicant *wpa_s = ctx;
1669 struct p2p_go_neg_results *params = data;
1670 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001671 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001673 p2p_go_save_group_common_freqs(wpa_s, params);
1674 p2p_go_dump_common_freqs(wpa_s);
1675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 ssid = wpa_s->current_ssid;
1677 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1678 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1679 if (wpa_s->global->p2p_group_formation == wpa_s)
1680 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001681 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1682 params->passphrase[0] == '\0' ?
1683 params->psk : NULL,
1684 params->passphrase,
1685 wpa_s->global->p2p_dev_addr,
1686 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001687 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001688
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001689 if (wpa_s->parent->p2ps_join_addr_valid) {
1690 wpa_dbg(wpa_s, MSG_DEBUG,
1691 "P2PS: Setting default PIN for " MACSTR,
1692 MAC2STR(wpa_s->parent->p2ps_join_addr));
1693 wpa_supplicant_ap_wps_pin(wpa_s,
1694 wpa_s->parent->p2ps_join_addr,
1695 "12345670", NULL, 0, 0);
1696 wpa_s->parent->p2ps_join_addr_valid = 0;
1697 }
1698
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001699 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001700 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001701 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001703 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001704 wpas_p2p_add_psk_list(wpa_s, ssid);
1705 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001706 if (network_id < 0)
1707 network_id = ssid->id;
1708 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 wpas_p2p_cross_connect_setup(wpa_s);
1710 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001711
1712 if (wpa_s->p2p_first_connection_timeout) {
1713 wpa_dbg(wpa_s, MSG_DEBUG,
1714 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1715 wpa_s->p2p_first_connection_timeout);
1716 wpa_s->p2p_go_group_formation_completed = 0;
1717 wpa_s->global->p2p_group_formation = wpa_s;
1718 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1719 wpa_s->parent, NULL);
1720 eloop_register_timeout(
1721 wpa_s->p2p_first_connection_timeout, 0,
1722 wpas_p2p_group_formation_timeout,
1723 wpa_s->parent, NULL);
1724 }
1725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 return;
1727 }
1728
1729 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1730 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1731 params->peer_interface_addr)) {
1732 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1733 "filtering");
1734 return;
1735 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001736 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001738 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001739#ifdef CONFIG_WPS_NFC
1740 } else if (params->wps_method == WPS_NFC) {
1741 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1742 DEV_PW_NFC_CONNECTION_HANDOVER &&
1743 !wpa_s->parent->p2p_oob_dev_pw) {
1744 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1745 return;
1746 }
1747 wpas_ap_wps_add_nfc_pw(
1748 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1749 wpa_s->parent->p2p_oob_dev_pw,
1750 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1751 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1752#endif /* CONFIG_WPS_NFC */
1753 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001755 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 os_free(wpa_s->go_params);
1757 wpa_s->go_params = NULL;
1758}
1759
1760
1761static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1762 struct p2p_go_neg_results *params,
1763 int group_formation)
1764{
1765 struct wpa_ssid *ssid;
1766
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001767 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1768 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1769 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1770 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
1774 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001775 if (ssid == NULL) {
1776 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001778 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001780 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001781 wpa_s->p2p_go_group_formation_completed = 0;
1782 wpa_s->group_formation_reported = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 wpa_config_set_network_defaults(ssid);
1785 ssid->temporary = 1;
1786 ssid->p2p_group = 1;
1787 ssid->p2p_persistent_group = params->persistent_group;
1788 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1789 WPAS_MODE_P2P_GO;
1790 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001791 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001792 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001793 ssid->ssid = os_zalloc(params->ssid_len + 1);
1794 if (ssid->ssid) {
1795 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1796 ssid->ssid_len = params->ssid_len;
1797 }
1798 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1799 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1800 ssid->proto = WPA_PROTO_RSN;
1801 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001802 ssid->group_cipher = WPA_CIPHER_CCMP;
1803 if (params->freq > 56160) {
1804 /*
1805 * Enable GCMP instead of CCMP as pairwise_cipher and
1806 * group_cipher in 60 GHz.
1807 */
1808 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1809 ssid->group_cipher = WPA_CIPHER_GCMP;
1810 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001811 if (os_strlen(params->passphrase) > 0) {
1812 ssid->passphrase = os_strdup(params->passphrase);
1813 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001814 wpa_msg_global(wpa_s, MSG_ERROR,
1815 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001816 wpa_config_remove_network(wpa_s->conf, ssid->id);
1817 return;
1818 }
1819 } else
1820 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001821 ssid->psk_set = params->psk_set;
1822 if (ssid->psk_set)
1823 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001824 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001825 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001826 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827
1828 wpa_s->ap_configured_cb = p2p_go_configured;
1829 wpa_s->ap_configured_cb_ctx = wpa_s;
1830 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001831 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001832 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 wpa_s->reassociate = 1;
1834 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001835 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1836 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 wpa_supplicant_req_scan(wpa_s, 0, 0);
1838}
1839
1840
1841static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1842 const struct wpa_supplicant *src)
1843{
1844 struct wpa_config *d;
1845 const struct wpa_config *s;
1846
1847 d = dst->conf;
1848 s = src->conf;
1849
1850#define C(n) if (s->n) d->n = os_strdup(s->n)
1851 C(device_name);
1852 C(manufacturer);
1853 C(model_name);
1854 C(model_number);
1855 C(serial_number);
1856 C(config_methods);
1857#undef C
1858
1859 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1860 os_memcpy(d->sec_device_type, s->sec_device_type,
1861 sizeof(d->sec_device_type));
1862 d->num_sec_device_types = s->num_sec_device_types;
1863
1864 d->p2p_group_idle = s->p2p_group_idle;
1865 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001867 d->max_num_sta = s->max_num_sta;
1868 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001869 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001870 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001871 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001872 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001873 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001874 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001875 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001876
1877 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1878 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1879 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1880 }
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07001881 d->p2p_cli_probe = s->p2p_cli_probe;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882}
1883
1884
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001885static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1886 char *ifname, size_t len)
1887{
1888 char *ifname_ptr = wpa_s->ifname;
1889
1890 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1891 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1892 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1893 }
1894
1895 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1896 if (os_strlen(ifname) >= IFNAMSIZ &&
1897 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001898 int res;
1899
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001900 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001901 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1902 if (os_snprintf_error(len, res) && len)
1903 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001904 }
1905}
1906
1907
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1909 enum wpa_driver_if_type type)
1910{
1911 char ifname[120], force_ifname[120];
1912
1913 if (wpa_s->pending_interface_name[0]) {
1914 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1915 "- skip creation of a new one");
1916 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1917 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1918 "unknown?! ifname='%s'",
1919 wpa_s->pending_interface_name);
1920 return -1;
1921 }
1922 return 0;
1923 }
1924
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001925 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926 force_ifname[0] = '\0';
1927
1928 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1929 ifname);
1930 wpa_s->p2p_group_idx++;
1931
1932 wpa_s->pending_interface_type = type;
1933 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1934 wpa_s->pending_interface_addr, NULL) < 0) {
1935 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1936 "interface");
1937 return -1;
1938 }
1939
1940 if (force_ifname[0]) {
1941 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1942 force_ifname);
1943 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1944 sizeof(wpa_s->pending_interface_name));
1945 } else
1946 os_strlcpy(wpa_s->pending_interface_name, ifname,
1947 sizeof(wpa_s->pending_interface_name));
1948 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1949 MACSTR, wpa_s->pending_interface_name,
1950 MAC2STR(wpa_s->pending_interface_addr));
1951
1952 return 0;
1953}
1954
1955
1956static void wpas_p2p_remove_pending_group_interface(
1957 struct wpa_supplicant *wpa_s)
1958{
1959 if (!wpa_s->pending_interface_name[0] ||
1960 is_zero_ether_addr(wpa_s->pending_interface_addr))
1961 return; /* No pending virtual interface */
1962
1963 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1964 wpa_s->pending_interface_name);
1965 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1966 wpa_s->pending_interface_name);
1967 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1968 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001969 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001970}
1971
1972
1973static struct wpa_supplicant *
1974wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1975{
1976 struct wpa_interface iface;
1977 struct wpa_supplicant *group_wpa_s;
1978
1979 if (!wpa_s->pending_interface_name[0]) {
1980 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1981 if (!wpas_p2p_create_iface(wpa_s))
1982 return NULL;
1983 /*
1984 * Something has forced us to remove the pending interface; try
1985 * to create a new one and hope for the best that we will get
1986 * the same local address.
1987 */
1988 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1989 WPA_IF_P2P_CLIENT) < 0)
1990 return NULL;
1991 }
1992
1993 os_memset(&iface, 0, sizeof(iface));
1994 iface.ifname = wpa_s->pending_interface_name;
1995 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001996 if (wpa_s->conf->ctrl_interface == NULL &&
1997 wpa_s->parent != wpa_s &&
1998 wpa_s->p2p_mgmt &&
1999 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
2000 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
2001 else
2002 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08002004 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 if (group_wpa_s == NULL) {
2006 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
2007 "wpa_supplicant interface");
2008 return NULL;
2009 }
2010 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002011 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
2012 P2P_GROUP_INTERFACE_CLIENT;
2013 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002014 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015
2016 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2017
2018 return group_wpa_s;
2019}
2020
2021
2022static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2023 void *timeout_ctx)
2024{
2025 struct wpa_supplicant *wpa_s = eloop_ctx;
2026 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002027 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002028}
2029
2030
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002031static void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s,
2032 int already_deleted)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002033{
2034 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2035 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 if (wpa_s->global->p2p)
2037 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002038 wpas_group_formation_completed(wpa_s, 0, already_deleted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039}
2040
2041
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002042static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2043{
2044 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2045 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2046 wpa_s->parent, NULL);
2047 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2048 wpa_s->parent, NULL);
2049 wpa_s->global->p2p_fail_on_wps_complete = 0;
2050}
2051
2052
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002053void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2054{
2055 if (wpa_s->global->p2p_group_formation != wpa_s)
2056 return;
2057 /* Speed up group formation timeout since this cannot succeed */
2058 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2059 wpa_s->parent, NULL);
2060 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2061 wpa_s->parent, NULL);
2062}
2063
2064
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002065static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066{
2067 struct wpa_supplicant *wpa_s = ctx;
2068
2069 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2070 wpa_drv_cancel_remain_on_channel(wpa_s);
2071 wpa_s->off_channel_freq = 0;
2072 wpa_s->roc_waiting_drv_freq = 0;
2073 }
2074
2075 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002076 wpa_msg_global(wpa_s, MSG_INFO,
2077 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2078 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002079 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 wpas_p2p_remove_pending_group_interface(wpa_s);
2081 return;
2082 }
2083
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002084 if (wpa_s->p2p_go_ht40)
2085 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002086 if (wpa_s->p2p_go_vht)
2087 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002088
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002089 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2090 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2091 " wps_method=%s",
2092 res->role_go ? "GO" : "client", res->freq, res->ht40,
2093 MAC2STR(res->peer_device_addr),
2094 MAC2STR(res->peer_interface_addr),
2095 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002096 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002097
Dmitry Shmidt04949592012-07-19 12:16:46 -07002098 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2099 struct wpa_ssid *ssid;
2100 ssid = wpa_config_get_network(wpa_s->conf,
2101 wpa_s->p2p_persistent_id);
2102 if (ssid && ssid->disabled == 2 &&
2103 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2104 size_t len = os_strlen(ssid->passphrase);
2105 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2106 "on requested persistent group");
2107 os_memcpy(res->passphrase, ssid->passphrase, len);
2108 res->passphrase[len] = '\0';
2109 }
2110 }
2111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 if (wpa_s->create_p2p_iface) {
2113 struct wpa_supplicant *group_wpa_s =
2114 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2115 if (group_wpa_s == NULL) {
2116 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002117 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2118 wpa_s, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002119 wpas_p2p_group_formation_failed(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 return;
2121 }
2122 if (group_wpa_s != wpa_s) {
2123 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2124 sizeof(group_wpa_s->p2p_pin));
2125 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2126 }
2127 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2128 wpa_s->pending_interface_name[0] = '\0';
2129 group_wpa_s->p2p_in_provisioning = 1;
2130
2131 if (res->role_go)
2132 wpas_start_wps_go(group_wpa_s, res, 1);
2133 else
2134 wpas_start_wps_enrollee(group_wpa_s, res);
2135 } else {
2136 wpa_s->p2p_in_provisioning = 1;
2137 wpa_s->global->p2p_group_formation = wpa_s;
2138
2139 if (res->role_go)
2140 wpas_start_wps_go(wpa_s, res, 1);
2141 else
2142 wpas_start_wps_enrollee(ctx, res);
2143 }
2144
2145 wpa_s->p2p_long_listen = 0;
2146 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2147
2148 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2149 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2150 (res->peer_config_timeout % 100) * 10000,
2151 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2152}
2153
2154
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002155static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id,
2156 u8 go_intent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157{
2158 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002159 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002160 " dev_passwd_id=%u go_intent=%u", MAC2STR(src),
2161 dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162
Dmitry Shmidt7a53dbb2015-06-11 13:13:53 -07002163 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id, go_intent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164}
2165
2166
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002167static void wpas_dev_found(void *ctx, const u8 *addr,
2168 const struct p2p_peer_info *info,
2169 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002171#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 struct wpa_supplicant *wpa_s = ctx;
2173 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002174 char *wfd_dev_info_hex = NULL;
2175
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002176#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002177 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2178 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002179#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002180
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002181 if (info->p2ps_instance) {
2182 char str[256];
2183 const u8 *buf = wpabuf_head(info->p2ps_instance);
2184 size_t len = wpabuf_len(info->p2ps_instance);
2185
2186 while (len) {
2187 u32 id;
2188 u16 methods;
2189 u8 str_len;
2190
2191 if (len < 4 + 2 + 1)
2192 break;
2193 id = WPA_GET_LE32(buf);
2194 buf += sizeof(u32);
2195 methods = WPA_GET_BE16(buf);
2196 buf += sizeof(u16);
2197 str_len = *buf++;
2198 if (str_len > len - 4 - 2 - 1)
2199 break;
2200 os_memcpy(str, buf, str_len);
2201 str[str_len] = '\0';
2202 buf += str_len;
2203 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2204
2205 wpa_msg_global(wpa_s, MSG_INFO,
2206 P2P_EVENT_DEVICE_FOUND MACSTR
2207 " p2p_dev_addr=" MACSTR
2208 " pri_dev_type=%s name='%s'"
2209 " config_methods=0x%x"
2210 " dev_capab=0x%x"
2211 " group_capab=0x%x"
2212 " adv_id=%x asp_svc=%s%s",
2213 MAC2STR(addr),
2214 MAC2STR(info->p2p_device_addr),
2215 wps_dev_type_bin2str(
2216 info->pri_dev_type,
2217 devtype, sizeof(devtype)),
2218 info->device_name, methods,
2219 info->dev_capab, info->group_capab,
2220 id, str,
2221 info->vendor_elems ?
2222 " vendor_elems=1" : "");
2223 }
2224 goto done;
2225 }
2226
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002227 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2228 " p2p_dev_addr=" MACSTR
2229 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002230 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002231 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2232 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2233 sizeof(devtype)),
2234 info->device_name, info->config_methods,
2235 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002236 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002237 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002238 info->vendor_elems ? " vendor_elems=1" : "",
2239 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002241done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002242 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002243#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2246}
2247
2248
2249static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2250{
2251 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002252
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002253 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2254 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002255
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2257}
2258
2259
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002260static void wpas_find_stopped(void *ctx)
2261{
2262 struct wpa_supplicant *wpa_s = ctx;
2263 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002264 wpas_notify_p2p_find_stopped(wpa_s);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002265}
2266
2267
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002268struct wpas_p2p_listen_work {
2269 unsigned int freq;
2270 unsigned int duration;
2271 struct wpabuf *probe_resp_ie;
2272};
2273
2274
2275static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2276{
2277 if (lwork == NULL)
2278 return;
2279 wpabuf_free(lwork->probe_resp_ie);
2280 os_free(lwork);
2281}
2282
2283
2284static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2285{
2286 struct wpas_p2p_listen_work *lwork;
2287
2288 if (!wpa_s->p2p_listen_work)
2289 return;
2290
2291 lwork = wpa_s->p2p_listen_work->ctx;
2292 wpas_p2p_listen_work_free(lwork);
2293 radio_work_done(wpa_s->p2p_listen_work);
2294 wpa_s->p2p_listen_work = NULL;
2295}
2296
2297
2298static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2299{
2300 struct wpa_supplicant *wpa_s = work->wpa_s;
2301 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002302 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002303
2304 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002305 if (work->started) {
2306 wpa_s->p2p_listen_work = NULL;
2307 wpas_stop_listen(wpa_s);
2308 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002309 wpas_p2p_listen_work_free(lwork);
2310 return;
2311 }
2312
2313 wpa_s->p2p_listen_work = work;
2314
2315 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2316
2317 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2318 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2319 "report received Probe Request frames");
2320 wpas_p2p_listen_work_done(wpa_s);
2321 return;
2322 }
2323
2324 wpa_s->pending_listen_freq = lwork->freq;
2325 wpa_s->pending_listen_duration = lwork->duration;
2326
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002327 duration = lwork->duration;
2328#ifdef CONFIG_TESTING_OPTIONS
2329 if (wpa_s->extra_roc_dur) {
2330 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2331 duration, duration + wpa_s->extra_roc_dur);
2332 duration += wpa_s->extra_roc_dur;
2333 }
2334#endif /* CONFIG_TESTING_OPTIONS */
2335
2336 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002337 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2338 "to remain on channel (%u MHz) for Listen "
2339 "state", lwork->freq);
2340 wpas_p2p_listen_work_done(wpa_s);
2341 wpa_s->pending_listen_freq = 0;
2342 return;
2343 }
2344 wpa_s->off_channel_freq = 0;
2345 wpa_s->roc_waiting_drv_freq = lwork->freq;
2346}
2347
2348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002349static int wpas_start_listen(void *ctx, unsigned int freq,
2350 unsigned int duration,
2351 const struct wpabuf *probe_resp_ie)
2352{
2353 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002354 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002355
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002356 if (wpa_s->p2p_listen_work) {
2357 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 return -1;
2359 }
2360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002361 lwork = os_zalloc(sizeof(*lwork));
2362 if (lwork == NULL)
2363 return -1;
2364 lwork->freq = freq;
2365 lwork->duration = duration;
2366 if (probe_resp_ie) {
2367 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2368 if (lwork->probe_resp_ie == NULL) {
2369 wpas_p2p_listen_work_free(lwork);
2370 return -1;
2371 }
2372 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002374 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2375 lwork) < 0) {
2376 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 return -1;
2378 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379
2380 return 0;
2381}
2382
2383
2384static void wpas_stop_listen(void *ctx)
2385{
2386 struct wpa_supplicant *wpa_s = ctx;
2387 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2388 wpa_drv_cancel_remain_on_channel(wpa_s);
2389 wpa_s->off_channel_freq = 0;
2390 wpa_s->roc_waiting_drv_freq = 0;
2391 }
2392 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002393
2394 /*
2395 * Don't cancel Probe Request RX reporting for a connected P2P Client
2396 * handling Probe Request frames.
2397 */
2398 if (!wpa_s->p2p_cli_probe)
2399 wpa_drv_probe_req_report(wpa_s, 0);
2400
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002401 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002402}
2403
2404
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002405static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf,
2406 unsigned int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407{
2408 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07002409 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
2410 freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411}
2412
2413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2415 const u8 *peer, const char *params,
2416 unsigned int generated_pin)
2417{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002418 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2419 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002420}
2421
2422
2423static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2424 const u8 *peer, const char *params)
2425{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002426 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2427 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428}
2429
2430
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002431static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2432 const u8 *dev_addr, const u8 *pri_dev_type,
2433 const char *dev_name, u16 supp_config_methods,
2434 u8 dev_capab, u8 group_capab, const u8 *group_id,
2435 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002436{
2437 struct wpa_supplicant *wpa_s = ctx;
2438 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002439 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 u8 empty_dev_type[8];
2441 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002442 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002443 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002444
2445 if (group_id) {
2446 for (group = wpa_s->global->ifaces; group; group = group->next)
2447 {
2448 struct wpa_ssid *s = group->current_ssid;
2449 if (s != NULL &&
2450 s->mode == WPAS_MODE_P2P_GO &&
2451 group_id_len - ETH_ALEN == s->ssid_len &&
2452 os_memcmp(group_id + ETH_ALEN, s->ssid,
2453 s->ssid_len) == 0)
2454 break;
2455 }
2456 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457
2458 if (pri_dev_type == NULL) {
2459 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2460 pri_dev_type = empty_dev_type;
2461 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002462 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2463 " pri_dev_type=%s name='%s' config_methods=0x%x "
2464 "dev_capab=0x%x group_capab=0x%x%s%s",
2465 MAC2STR(dev_addr),
2466 wps_dev_type_bin2str(pri_dev_type, devtype,
2467 sizeof(devtype)),
2468 dev_name, supp_config_methods, dev_capab, group_capab,
2469 group ? " group=" : "",
2470 group ? group->ifname : "");
2471 if (os_snprintf_error(sizeof(params), res))
2472 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 if (config_methods & WPS_CONFIG_DISPLAY) {
2476 generated_pin = wps_generate_pin();
2477 wpas_prov_disc_local_display(wpa_s, peer, params,
2478 generated_pin);
2479 } else if (config_methods & WPS_CONFIG_KEYPAD)
2480 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2481 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002482 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2483 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002484
2485 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2486 P2P_PROV_DISC_SUCCESS,
2487 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488}
2489
2490
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002491static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002492{
2493 struct wpa_supplicant *wpa_s = ctx;
2494 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002495 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002497 if (wpa_s->pending_pd_before_join &&
2498 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2499 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2500 wpa_s->pending_pd_before_join = 0;
2501 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2502 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002503 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002504 return;
2505 }
2506
Dmitry Shmidt04949592012-07-19 12:16:46 -07002507 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002508 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
2509 int res;
2510
2511 res = os_snprintf(params, sizeof(params), " peer_go=%d",
2512 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2513 if (os_snprintf_error(sizeof(params), res))
2514 params[sizeof(params) - 1] = '\0';
2515 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07002516 params[0] = '\0';
2517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002518 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002519 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002520 else if (config_methods & WPS_CONFIG_KEYPAD) {
2521 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002522 wpas_prov_disc_local_display(wpa_s, peer, params,
2523 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002524 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002525 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2526 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527
Jouni Malinen75ecf522011-06-27 15:19:46 -07002528 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2529 P2P_PROV_DISC_SUCCESS,
2530 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531}
2532
2533
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002534static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002535 enum p2p_prov_disc_status status,
2536 u32 adv_id, const u8 *adv_mac,
2537 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002538{
2539 struct wpa_supplicant *wpa_s = ctx;
2540
Dmitry Shmidt04949592012-07-19 12:16:46 -07002541 if (wpa_s->p2p_fallback_to_go_neg) {
2542 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2543 "failed - fall back to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08002544 wpa_msg_global(wpa_s->parent, MSG_INFO,
2545 P2P_EVENT_FALLBACK_TO_GO_NEG
2546 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002547 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2548 return;
2549 }
2550
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002551 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002552 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002553 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2554 "join-existing-group operation (no ACK for PD "
2555 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002556 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002557 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002558 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002559
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002560 if (adv_id && adv_mac && deferred_session_resp) {
2561 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2562 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
2563 " deferred_session_resp='%s'",
2564 MAC2STR(peer), status, adv_id,
2565 deferred_session_resp);
2566 } else if (adv_id && adv_mac) {
2567 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2568 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
2569 MAC2STR(peer), status, adv_id);
2570 } else {
2571 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2572 " p2p_dev_addr=" MACSTR " status=%d",
2573 MAC2STR(peer), status);
2574 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07002575
Jouni Malinen75ecf522011-06-27 15:19:46 -07002576 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2577 status, 0, 0);
2578}
2579
2580
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002581static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2582{
2583 if (channels == NULL)
2584 return 1; /* Assume no restrictions */
2585 return p2p_channels_includes_freq(channels, freq);
2586
2587}
2588
2589
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002590/**
2591 * Pick the best frequency to use from all the currently used frequencies.
2592 */
2593static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2594 struct wpa_used_freq_data *freqs,
2595 unsigned int num)
2596{
2597 unsigned int i, c;
2598
2599 /* find a candidate freq that is supported by P2P */
2600 for (c = 0; c < num; c++)
2601 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2602 break;
2603
2604 if (c == num)
2605 return 0;
2606
2607 /* once we have a candidate, try to find a 'better' one */
2608 for (i = c + 1; i < num; i++) {
2609 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2610 continue;
2611
2612 /*
2613 * 1. Infrastructure station interfaces have higher preference.
2614 * 2. P2P Clients have higher preference.
2615 * 3. All others.
2616 */
2617 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2618 c = i;
2619 break;
2620 }
2621
2622 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2623 c = i;
2624 }
2625 return freqs[c].freq;
2626}
2627
2628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2630 const u8 *go_dev_addr, const u8 *ssid,
2631 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002632 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002633 const struct p2p_channels *channels,
2634 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635{
2636 struct wpa_supplicant *wpa_s = ctx;
2637 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002638 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002640 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641
2642 if (!persistent_group) {
2643 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002644 " to join an active group (SSID: %s)",
2645 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2647 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2648 == 0 ||
2649 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2650 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2651 "authorized invitation");
2652 goto accept_inv;
2653 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002654
2655#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002656 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
2657 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002658 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002659 wpa_s->p2p_wps_method = WPS_NFC;
2660 wpa_s->pending_join_wps_method = WPS_NFC;
2661 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002662 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08002663 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002664 bssid, ETH_ALEN);
2665 goto accept_inv;
2666 }
2667#endif /* CONFIG_WPS_NFC */
2668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002669 /*
2670 * Do not accept the invitation automatically; notify user and
2671 * request approval.
2672 */
2673 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2674 }
2675
2676 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2677 if (grp) {
2678 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2679 "running persistent group");
2680 if (*go)
2681 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2682 goto accept_inv;
2683 }
2684
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002685 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2686 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2687 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2688 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07002689 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002690 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2692
2693 for (s = wpa_s->conf->ssid; s; s = s->next) {
2694 if (s->disabled == 2 &&
2695 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2696 s->ssid_len == ssid_len &&
2697 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2698 break;
2699 }
2700
2701 if (!s) {
2702 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2703 " requested reinvocation of an unknown group",
2704 MAC2STR(sa));
2705 return P2P_SC_FAIL_UNKNOWN_GROUP;
2706 }
2707
2708 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2709 *go = 1;
2710 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2711 wpa_printf(MSG_DEBUG, "P2P: The only available "
2712 "interface is already in use - reject "
2713 "invitation");
2714 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2715 }
2716 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2717 } else if (s->mode == WPAS_MODE_P2P_GO) {
2718 *go = 1;
2719 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2720 {
2721 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2722 "interface address for the group");
2723 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2724 }
2725 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2726 ETH_ALEN);
2727 }
2728
2729accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002730 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2731
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002732 best_freq = 0;
2733 freqs = os_calloc(wpa_s->num_multichan_concurrent,
2734 sizeof(struct wpa_used_freq_data));
2735 if (freqs) {
2736 int num_channels = wpa_s->num_multichan_concurrent;
2737 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
2738 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
2739 os_free(freqs);
2740 }
2741
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002742 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002743 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002744 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002745 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002746
2747 if (wpa_s->num_multichan_concurrent < 2 ||
2748 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2749 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 -07002750 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002751 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 }
2753
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002754 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2755 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002756 if (*go == 0) {
2757 /* We are the client */
2758 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2759 "running a GO but we are capable of MCC, "
2760 "figure out the best channel to use");
2761 *force_freq = 0;
2762 } else if (!freq_included(channels, *force_freq)) {
2763 /* We are the GO, and *force_freq is not in the
2764 * intersection */
2765 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2766 "in intersection but we are capable of MCC, "
2767 "figure out the best channel to use",
2768 *force_freq);
2769 *force_freq = 0;
2770 }
2771 }
2772
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 return P2P_SC_SUCCESS;
2774}
2775
2776
2777static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2778 const u8 *ssid, size_t ssid_len,
2779 const u8 *go_dev_addr, u8 status,
2780 int op_freq)
2781{
2782 struct wpa_supplicant *wpa_s = ctx;
2783 struct wpa_ssid *s;
2784
2785 for (s = wpa_s->conf->ssid; s; s = s->next) {
2786 if (s->disabled == 2 &&
2787 s->ssid_len == ssid_len &&
2788 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2789 break;
2790 }
2791
2792 if (status == P2P_SC_SUCCESS) {
2793 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002794 " was accepted; op_freq=%d MHz, SSID=%s",
2795 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002796 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002797 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07002799 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002800 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002802 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002804 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002805 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 }
2807 return;
2808 }
2809
2810 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2811 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2812 " was rejected (status %u)", MAC2STR(sa), status);
2813 return;
2814 }
2815
2816 if (!s) {
2817 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002818 wpa_msg_global(wpa_s, MSG_INFO,
2819 P2P_EVENT_INVITATION_RECEIVED
2820 "sa=" MACSTR " go_dev_addr=" MACSTR
2821 " bssid=" MACSTR " unknown-network",
2822 MAC2STR(sa), MAC2STR(go_dev_addr),
2823 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002825 wpa_msg_global(wpa_s, MSG_INFO,
2826 P2P_EVENT_INVITATION_RECEIVED
2827 "sa=" MACSTR " go_dev_addr=" MACSTR
2828 " unknown-network",
2829 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 }
2831 return;
2832 }
2833
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002834 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002835 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2836 "sa=" MACSTR " persistent=%d freq=%d",
2837 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002838 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002839 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2840 "sa=" MACSTR " persistent=%d",
2841 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002842 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843}
2844
2845
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002846static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2847 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002848 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002849{
2850 size_t i;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002851 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002852
2853 if (ssid == NULL)
2854 return;
2855
2856 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002857 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002858 ETH_ALEN) == 0)
2859 break;
2860 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002861 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002862 if (ssid->mode != WPAS_MODE_P2P_GO &&
2863 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2864 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2865 "due to invitation result", ssid->id);
2866 wpas_notify_network_removed(wpa_s, ssid);
2867 wpa_config_remove_network(wpa_s->conf, ssid->id);
2868 return;
2869 }
2870 return; /* Peer not found in client list */
2871 }
2872
2873 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002874 "group %d client list%s",
2875 MAC2STR(peer), ssid->id,
2876 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08002877 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
2878 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
2879 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002880 ssid->num_p2p_clients--;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07002881 if (p2p_wpa_s->conf->update_config &&
2882 wpa_config_write(p2p_wpa_s->confname, p2p_wpa_s->conf))
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002883 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002884}
2885
2886
2887static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2888 const u8 *peer)
2889{
2890 struct wpa_ssid *ssid;
2891
2892 wpa_s = wpa_s->global->p2p_invite_group;
2893 if (wpa_s == NULL)
2894 return; /* No known invitation group */
2895 ssid = wpa_s->current_ssid;
2896 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2897 !ssid->p2p_persistent_group)
2898 return; /* Not operating as a GO in persistent group */
2899 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2900 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002901 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002902}
2903
2904
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002905static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002906 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002907 const u8 *peer, int neg_freq,
2908 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002909{
2910 struct wpa_supplicant *wpa_s = ctx;
2911 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07002912 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913
2914 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002915 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2916 "status=%d " MACSTR,
2917 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002919 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2920 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 }
2922 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2923
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002924 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2925 status, MAC2STR(peer));
2926 if (wpa_s->pending_invite_ssid_id == -1) {
2927 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2928 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002930 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002932 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2933 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2934 "invitation exchange to indicate readiness for "
2935 "re-invocation");
2936 }
2937
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002938 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002939 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2940 ssid = wpa_config_get_network(
2941 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002942 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944 wpas_p2p_remove_pending_group_interface(wpa_s);
2945 return;
2946 }
2947
2948 ssid = wpa_config_get_network(wpa_s->conf,
2949 wpa_s->pending_invite_ssid_id);
2950 if (ssid == NULL) {
2951 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2952 "data matching with invitation");
2953 return;
2954 }
2955
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002956 /*
2957 * The peer could have missed our ctrl::ack frame for Invitation
2958 * Response and continue retransmitting the frame. To reduce the
2959 * likelihood of the peer not getting successful TX status for the
2960 * Invitation Response frame, wait a short time here before starting
2961 * the persistent group so that we will remain on the current channel to
2962 * acknowledge any possible retransmission from the peer.
2963 */
2964 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2965 "starting persistent group");
2966 os_sleep(0, 50000);
2967
Dmitry Shmidt15907092014-03-25 10:42:57 -07002968 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2969 freq_included(channels, neg_freq))
2970 freq = neg_freq;
2971 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
2972 freq_included(channels, peer_oper_freq))
2973 freq = peer_oper_freq;
2974 else
2975 freq = 0;
2976
2977 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
2978 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002980 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08002981 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07002982 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002983 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2984 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002985 ssid->mode == WPAS_MODE_P2P_GO ?
2986 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2987 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988}
2989
2990
Dmitry Shmidt04949592012-07-19 12:16:46 -07002991static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2992 unsigned int freq)
2993{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002994 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
2995 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002996 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002997}
2998
2999
3000static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3001{
3002 reg->channel[reg->channels] = chan;
3003 reg->channels++;
3004}
3005
3006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003008 struct p2p_channels *chan,
3009 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010{
3011 int i, cla = 0;
3012
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003013 wpa_s->global->p2p_24ghz_social_channels = 1;
3014
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003015 os_memset(cli_chan, 0, sizeof(*cli_chan));
3016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3018 "band");
3019
3020 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3021 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003022 chan->reg_class[cla].channels = 0;
3023 for (i = 0; i < 11; i++) {
3024 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3025 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3026 }
3027 if (chan->reg_class[cla].channels)
3028 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029
3030 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3031 "band");
3032
3033 /* Operating class 115 - 5 GHz, channels 36-48 */
3034 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003035 chan->reg_class[cla].channels = 0;
3036 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3037 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3038 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3039 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3040 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3041 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3042 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3043 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3044 if (chan->reg_class[cla].channels)
3045 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046
3047 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3048 "band");
3049
3050 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3051 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003052 chan->reg_class[cla].channels = 0;
3053 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3054 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3055 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3056 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3057 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3058 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3059 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3060 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3061 if (chan->reg_class[cla].channels)
3062 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003063
3064 chan->reg_classes = cla;
3065 return 0;
3066}
3067
3068
3069static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3070 u16 num_modes,
3071 enum hostapd_hw_mode mode)
3072{
3073 u16 i;
3074
3075 for (i = 0; i < num_modes; i++) {
3076 if (modes[i].mode == mode)
3077 return &modes[i];
3078 }
3079
3080 return NULL;
3081}
3082
3083
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003084enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003085 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003086};
3087
Dmitry Shmidt04949592012-07-19 12:16:46 -07003088static int has_channel(struct wpa_global *global,
3089 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090{
3091 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003092 unsigned int freq;
3093
3094 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3095 chan * 5;
3096 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003097 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098
3099 for (i = 0; i < mode->num_channels; i++) {
3100 if (mode->channels[i].chan == chan) {
3101 if (flags)
3102 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003103 if (mode->channels[i].flag &
3104 (HOSTAPD_CHAN_DISABLED |
3105 HOSTAPD_CHAN_RADAR))
3106 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003107 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3108 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003109 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110 }
3111 }
3112
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003113 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003114}
3115
3116
3117struct p2p_oper_class_map {
3118 enum hostapd_hw_mode mode;
3119 u8 op_class;
3120 u8 min_chan;
3121 u8 max_chan;
3122 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003123 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124};
3125
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003126static const struct p2p_oper_class_map op_class[] = {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003127 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003128#if 0 /* Do not enable HT40 on 2 GHz for now */
3129 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3130 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3131#endif
3132 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3133 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003134 { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003135 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3136 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3137 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3138 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003139
3140 /*
3141 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3142 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3143 * 80 MHz, but currently use the following definition for simplicity
3144 * (these center frequencies are not actual channels, which makes
3145 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3146 * removing invalid channels.
3147 */
3148 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003149 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003150 { -1, 0, 0, 0, 0, BW20 }
3151};
3152
3153
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003154static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3155 struct hostapd_hw_modes *mode,
3156 u8 channel)
3157{
3158 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3159 unsigned int i;
3160
3161 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3162 return 0;
3163
3164 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3165 /*
3166 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3167 * so the center channel is 6 channels away from the start/end.
3168 */
3169 if (channel >= center_channels[i] - 6 &&
3170 channel <= center_channels[i] + 6)
3171 return center_channels[i];
3172
3173 return 0;
3174}
3175
3176
3177static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3178 struct hostapd_hw_modes *mode,
3179 u8 channel, u8 bw)
3180{
3181 u8 center_chan;
3182 int i, flags;
3183 enum chan_allowed res, ret = ALLOWED;
3184
3185 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3186 if (!center_chan)
3187 return NOT_ALLOWED;
3188 if (center_chan >= 58 && center_chan <= 138)
3189 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3190
3191 /* check all the channels are available */
3192 for (i = 0; i < 4; i++) {
3193 int adj_chan = center_chan - 6 + i * 4;
3194
3195 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3196 if (res == NOT_ALLOWED)
3197 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003198 if (res == NO_IR)
3199 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003200
3201 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3202 return NOT_ALLOWED;
3203 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3204 return NOT_ALLOWED;
3205 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3206 return NOT_ALLOWED;
3207 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3208 return NOT_ALLOWED;
3209 }
3210
3211 return ret;
3212}
3213
3214
3215static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3216 struct hostapd_hw_modes *mode,
3217 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003218{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003219 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003220 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003221
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003222 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3223 if (bw == BW40MINUS) {
3224 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3225 return NOT_ALLOWED;
3226 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3227 } else if (bw == BW40PLUS) {
3228 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3229 return NOT_ALLOWED;
3230 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3231 } else if (bw == BW80) {
3232 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3233 }
3234
3235 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3236 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003237 if (res == NO_IR || res2 == NO_IR)
3238 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003239 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003240}
3241
3242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003244 struct p2p_channels *chan,
3245 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003247 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003248 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003250 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3252 "of all supported channels; assume dualband "
3253 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003254 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003255 }
3256
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003257 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258
3259 for (op = 0; op_class[op].op_class; op++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003260 const struct p2p_oper_class_map *o = &op_class[op];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003262 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003264 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265 if (mode == NULL)
3266 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003267 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3268 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003270 enum chan_allowed res;
3271 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3272 if (res == ALLOWED) {
3273 if (reg == NULL) {
3274 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3275 o->op_class);
3276 reg = &chan->reg_class[cla];
3277 cla++;
3278 reg->reg_class = o->op_class;
3279 }
3280 reg->channel[reg->channels] = ch;
3281 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003282 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003283 wpa_s->conf->p2p_add_cli_chan) {
3284 if (cli_reg == NULL) {
3285 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3286 o->op_class);
3287 cli_reg = &cli_chan->reg_class[cli_cla];
3288 cli_cla++;
3289 cli_reg->reg_class = o->op_class;
3290 }
3291 cli_reg->channel[cli_reg->channels] = ch;
3292 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003294 }
3295 if (reg) {
3296 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3297 reg->channel, reg->channels);
3298 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003299 if (cli_reg) {
3300 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3301 cli_reg->channel, cli_reg->channels);
3302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 }
3304
3305 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003306 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308 return 0;
3309}
3310
3311
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003312int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3313 struct hostapd_hw_modes *mode, u8 channel)
3314{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003315 int op;
3316 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003317
3318 for (op = 0; op_class[op].op_class; op++) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003319 const struct p2p_oper_class_map *o = &op_class[op];
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003320 u8 ch;
3321
3322 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3323 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07003324 (o->bw != BW40PLUS && o->bw != BW40MINUS) ||
3325 ch != channel)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003326 continue;
3327 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003328 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003329 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003330 }
3331 }
3332 return 0;
3333}
3334
3335
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003336int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3337 struct hostapd_hw_modes *mode, u8 channel)
3338{
3339 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3340 return 0;
3341
3342 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3343}
3344
3345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003346static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3347 size_t buf_len)
3348{
3349 struct wpa_supplicant *wpa_s = ctx;
3350
3351 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3352 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3353 break;
3354 }
3355 if (wpa_s == NULL)
3356 return -1;
3357
3358 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3359}
3360
3361
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003362struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3363 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003364{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003365 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3366 struct wpa_ssid *s = wpa_s->current_ssid;
3367 if (s == NULL)
3368 continue;
3369 if (s->mode != WPAS_MODE_P2P_GO &&
3370 s->mode != WPAS_MODE_AP &&
3371 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3372 continue;
3373 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003374 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003375 continue;
3376 return wpa_s;
3377 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003378
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003379 return NULL;
3380
3381}
3382
3383
3384struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3385 const u8 *peer_dev_addr)
3386{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003387 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3388 struct wpa_ssid *ssid = wpa_s->current_ssid;
3389 if (ssid == NULL)
3390 continue;
3391 if (ssid->mode != WPAS_MODE_INFRA)
3392 continue;
3393 if (wpa_s->wpa_state != WPA_COMPLETED &&
3394 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3395 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003396 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3397 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003398 }
3399
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003400 return NULL;
3401}
3402
3403
3404static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3405{
3406 struct wpa_supplicant *wpa_s = ctx;
3407
3408 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003409}
3410
3411
Dmitry Shmidt18463232014-01-24 12:29:41 -08003412static int wpas_is_concurrent_session_active(void *ctx)
3413{
3414 struct wpa_supplicant *wpa_s = ctx;
3415 struct wpa_supplicant *ifs;
3416
3417 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3418 if (ifs == wpa_s)
3419 continue;
3420 if (ifs->wpa_state > WPA_ASSOCIATED)
3421 return 1;
3422 }
3423 return 0;
3424}
3425
3426
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003427static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3428{
3429 struct wpa_supplicant *wpa_s = ctx;
3430 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3431}
3432
3433
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003434int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3435 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003436{
3437 struct wpa_interface iface;
3438 struct wpa_supplicant *p2pdev_wpa_s;
3439 char ifname[100];
3440 char force_name[100];
3441 int ret;
3442
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003443 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3444 wpa_s->ifname);
3445 if (os_snprintf_error(sizeof(ifname), ret))
3446 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003447 force_name[0] = '\0';
3448 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3449 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3450 force_name, wpa_s->pending_interface_addr, NULL);
3451 if (ret < 0) {
3452 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3453 return ret;
3454 }
3455 os_strlcpy(wpa_s->pending_interface_name, ifname,
3456 sizeof(wpa_s->pending_interface_name));
3457
3458 os_memset(&iface, 0, sizeof(iface));
3459 iface.p2p_mgmt = 1;
3460 iface.ifname = wpa_s->pending_interface_name;
3461 iface.driver = wpa_s->driver->name;
3462 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003463
3464 /*
3465 * If a P2P Device configuration file was given, use it as the interface
3466 * configuration file (instead of using parent's configuration file.
3467 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003468 if (conf_p2p_dev) {
3469 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003470 iface.ctrl_interface = NULL;
3471 } else {
3472 iface.confname = wpa_s->confname;
3473 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3474 }
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003475
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08003476 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface, wpa_s);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003477 if (!p2pdev_wpa_s) {
3478 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3479 return -1;
3480 }
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003481
3482 wpa_s->pending_interface_name[0] = '\0';
3483 return 0;
3484}
3485
3486
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003487static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3488 const u8 *noa, size_t noa_len)
3489{
3490 struct wpa_supplicant *wpa_s, *intf = ctx;
3491 char hex[100];
3492
3493 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3494 if (wpa_s->waiting_presence_resp)
3495 break;
3496 }
3497 if (!wpa_s) {
3498 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3499 return;
3500 }
3501 wpa_s->waiting_presence_resp = 0;
3502
3503 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3504 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3505 " status=%u noa=%s", MAC2STR(src), status, hex);
3506}
3507
3508
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003509static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
3510 size_t ssid_len, u8 *go_dev_addr,
3511 u8 *ret_ssid, size_t *ret_ssid_len)
3512{
3513 struct wpa_supplicant *wpa_s = ctx;
3514 struct wpa_ssid *s;
3515
3516 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
3517 if (s) {
3518 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
3519 *ret_ssid_len = s->ssid_len;
3520 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
3521 return 1;
3522 }
3523
3524 return 0;
3525}
3526
3527
3528static int wpas_get_go_info(void *ctx, u8 *intended_addr,
3529 u8 *ssid, size_t *ssid_len, int *group_iface)
3530{
3531 struct wpa_supplicant *wpa_s = ctx;
3532 struct wpa_ssid *s;
3533 u8 bssid[ETH_ALEN];
3534
3535 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
3536 if (!s) {
3537 s = wpas_p2p_get_persistent_go(wpa_s);
3538 if (s)
3539 os_memcpy(bssid, s->bssid, ETH_ALEN);
3540 }
3541
3542 *group_iface = wpas_p2p_create_iface(wpa_s);
3543 if (!s)
3544 return 0;
3545
3546 os_memcpy(intended_addr, bssid, ETH_ALEN);
3547 os_memcpy(ssid, s->ssid, s->ssid_len);
3548 *ssid_len = s->ssid_len;
3549
3550 return 1;
3551}
3552
3553
3554static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
3555 const u8 *ssid, size_t ssid_len)
3556{
3557 struct wpa_supplicant *wpa_s = ctx;
3558 struct wpa_ssid *s;
3559 int save_config = 0;
3560 size_t i;
3561
3562 /* Start with our first choice of Persistent Groups */
3563 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
3564 if (go && ssid && ssid_len &&
3565 s->ssid_len == ssid_len &&
3566 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
3567 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3568 break;
3569
3570 /* Remove stale persistent group */
3571 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
3572 wpa_config_remove_network(wpa_s->conf, s->id);
3573 save_config = 1;
3574 continue;
3575 }
3576
3577 for (i = 0; i < s->num_p2p_clients; i++) {
3578 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
3579 peer, ETH_ALEN) != 0)
3580 continue;
3581
3582 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
3583 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3584 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3585 break;
3586 }
3587 s->num_p2p_clients--;
3588 save_config = 1;
3589 }
3590
3591 if (save_config)
3592 p2p_config_write(wpa_s);
3593
3594 /* Return TRUE if valid SSID remains */
3595 return s != NULL;
3596}
3597
3598
3599static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
3600 const u8 *adv_mac, const u8 *ses_mac,
3601 const u8 *grp_mac, u32 adv_id, u32 ses_id,
3602 u8 conncap, int passwd_id,
3603 const u8 *persist_ssid,
3604 size_t persist_ssid_size, int response_done,
3605 int prov_start, const char *session_info)
3606{
3607 struct wpa_supplicant *wpa_s = ctx;
3608 u8 mac[ETH_ALEN];
3609 struct wpa_ssid *persistent_go, *stale, *s;
3610 int save_config = 0;
3611 struct wpa_supplicant *go_wpa_s;
3612
3613 if (!dev)
3614 return;
3615
3616 os_memset(mac, 0, ETH_ALEN);
3617 if (!adv_mac)
3618 adv_mac = mac;
3619 if (!ses_mac)
3620 ses_mac = mac;
3621 if (!grp_mac)
3622 grp_mac = mac;
3623
3624 if (prov_start) {
3625 if (session_info == NULL) {
3626 wpa_msg_global(wpa_s, MSG_INFO,
3627 P2P_EVENT_P2PS_PROVISION_START MACSTR
3628 " adv_id=%x conncap=%x"
3629 " adv_mac=" MACSTR
3630 " session=%x mac=" MACSTR
3631 " dev_passwd_id=%d",
3632 MAC2STR(dev), adv_id, conncap,
3633 MAC2STR(adv_mac),
3634 ses_id, MAC2STR(ses_mac),
3635 passwd_id);
3636 } else {
3637 wpa_msg_global(wpa_s, MSG_INFO,
3638 P2P_EVENT_P2PS_PROVISION_START MACSTR
3639 " adv_id=%x conncap=%x"
3640 " adv_mac=" MACSTR
3641 " session=%x mac=" MACSTR
3642 " dev_passwd_id=%d info='%s'",
3643 MAC2STR(dev), adv_id, conncap,
3644 MAC2STR(adv_mac),
3645 ses_id, MAC2STR(ses_mac),
3646 passwd_id, session_info);
3647 }
3648 return;
3649 }
3650
3651 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
3652 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3653
3654 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
3655 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3656 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3657
3658 if (persistent_go && !persistent_go->num_p2p_clients) {
3659 /* remove empty persistent GO */
3660 wpa_config_remove_network(wpa_s->conf,
3661 persistent_go->id);
3662 }
3663
3664 wpa_msg_global(wpa_s, MSG_INFO,
3665 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3666 " status=%d"
3667 " adv_id=%x adv_mac=" MACSTR
3668 " session=%x mac=" MACSTR,
3669 MAC2STR(dev), status,
3670 adv_id, MAC2STR(adv_mac),
3671 ses_id, MAC2STR(ses_mac));
3672 return;
3673 }
3674
3675 /* Clean up stale persistent groups with this device */
3676 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
3677 persist_ssid_size);
3678 for (;;) {
3679 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
3680 if (!stale)
3681 break;
3682
3683 if (s && s->ssid_len == stale->ssid_len &&
3684 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
3685 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
3686 break;
3687
3688 /* Remove stale persistent group */
3689 if (stale->mode != WPAS_MODE_P2P_GO ||
3690 stale->num_p2p_clients <= 1) {
3691 wpa_config_remove_network(wpa_s->conf, stale->id);
3692 } else {
3693 size_t i;
3694
3695 for (i = 0; i < stale->num_p2p_clients; i++) {
3696 if (os_memcmp(stale->p2p_client_list +
3697 i * ETH_ALEN,
3698 dev, ETH_ALEN) == 0) {
3699 os_memmove(stale->p2p_client_list +
3700 i * ETH_ALEN,
3701 stale->p2p_client_list +
3702 (i + 1) * ETH_ALEN,
3703 (stale->num_p2p_clients -
3704 i - 1) * ETH_ALEN);
3705 break;
3706 }
3707 }
3708 stale->num_p2p_clients--;
3709 }
3710 save_config = 1;
3711 }
3712
3713 if (save_config)
3714 p2p_config_write(wpa_s);
3715
3716 if (s) {
3717 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3718 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3719
3720 if (persistent_go && s != persistent_go &&
3721 !persistent_go->num_p2p_clients) {
3722 /* remove empty persistent GO */
3723 wpa_config_remove_network(wpa_s->conf,
3724 persistent_go->id);
3725 /* Save config */
3726 }
3727
3728 wpa_msg_global(wpa_s, MSG_INFO,
3729 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3730 " status=%d"
3731 " adv_id=%x adv_mac=" MACSTR
3732 " session=%x mac=" MACSTR
3733 " persist=%d",
3734 MAC2STR(dev), status,
3735 adv_id, MAC2STR(adv_mac),
3736 ses_id, MAC2STR(ses_mac), s->id);
3737 return;
3738 }
3739
3740 if (conncap == P2PS_SETUP_GROUP_OWNER) {
3741 const char *go_ifname = NULL;
3742 if (!go_wpa_s) {
3743 wpa_s->global->pending_p2ps_group = 1;
3744
3745 if (wpa_s->conf->p2p_no_group_iface)
3746 go_ifname = wpa_s->ifname;
3747 else if (wpa_s->pending_interface_name[0])
3748 go_ifname = wpa_s->pending_interface_name;
3749
3750 if (!go_ifname) {
3751 wpas_p2ps_prov_complete(
3752 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
3753 dev, adv_mac, ses_mac,
3754 NULL, adv_id, ses_id, 0, 0,
3755 NULL, 0, 0, 0, NULL);
3756 return;
3757 }
3758
3759 /* If PD Resp complete, start up the GO */
3760 if (response_done && persistent_go) {
3761 wpas_p2p_group_add_persistent(
3762 wpa_s, persistent_go,
3763 0, 0, 0, 0, 0, NULL,
3764 persistent_go->mode ==
3765 WPAS_MODE_P2P_GO ?
3766 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3767 0);
3768 } else if (response_done) {
3769 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3770 }
3771
3772 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3773 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3774 wpa_s->p2ps_join_addr_valid = 1;
3775 wpa_dbg(wpa_s, MSG_DEBUG,
3776 "P2PS: Saving PIN for " MACSTR,
3777 MAC2STR(dev));
3778 }
3779 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
3780 go_ifname = go_wpa_s->ifname;
3781
3782 wpa_dbg(go_wpa_s, MSG_DEBUG,
3783 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
3784 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
3785 NULL, 0, 0);
3786
3787 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
3788 wpa_s->p2ps_join_addr_valid = 1;
3789 wpa_dbg(wpa_s, MSG_DEBUG,
3790 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
3791 }
3792
3793 wpa_msg_global(wpa_s, MSG_INFO,
3794 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3795 " status=%d conncap=%x"
3796 " adv_id=%x adv_mac=" MACSTR
3797 " session=%x mac=" MACSTR
3798 " dev_passwd_id=%d go=%s",
3799 MAC2STR(dev), status, conncap,
3800 adv_id, MAC2STR(adv_mac),
3801 ses_id, MAC2STR(ses_mac),
3802 passwd_id, go_ifname);
3803 return;
3804 }
3805
3806 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
3807 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
3808
3809 if (persistent_go && !persistent_go->num_p2p_clients) {
3810 /* remove empty persistent GO */
3811 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
3812 }
3813
3814 if (conncap == P2PS_SETUP_CLIENT) {
3815 wpa_msg_global(wpa_s, MSG_INFO,
3816 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3817 " status=%d conncap=%x"
3818 " adv_id=%x adv_mac=" MACSTR
3819 " session=%x mac=" MACSTR
3820 " dev_passwd_id=%d join=" MACSTR,
3821 MAC2STR(dev), status, conncap,
3822 adv_id, MAC2STR(adv_mac),
3823 ses_id, MAC2STR(ses_mac),
3824 passwd_id, MAC2STR(grp_mac));
3825 } else {
3826 wpa_msg_global(wpa_s, MSG_INFO,
3827 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
3828 " status=%d conncap=%x"
3829 " adv_id=%x adv_mac=" MACSTR
3830 " session=%x mac=" MACSTR
3831 " dev_passwd_id=%d",
3832 MAC2STR(dev), status, conncap,
3833 adv_id, MAC2STR(adv_mac),
3834 ses_id, MAC2STR(ses_mac),
3835 passwd_id);
3836 }
3837}
3838
3839
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003840static int _wpas_p2p_in_progress(void *ctx)
3841{
3842 struct wpa_supplicant *wpa_s = ctx;
3843 return wpas_p2p_in_progress(wpa_s);
3844}
3845
3846
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003847static int wpas_prov_disc_resp_cb(void *ctx)
3848{
3849 struct wpa_supplicant *wpa_s = ctx;
3850 struct wpa_ssid *persistent_go;
3851
3852 if (!wpa_s->global->pending_p2ps_group)
3853 return 0;
3854
3855 wpa_s->global->pending_p2ps_group = 0;
3856
3857 if (wpas_p2p_get_go_group(wpa_s))
3858 return 0;
3859 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
3860
3861 if (persistent_go) {
3862 wpas_p2p_group_add_persistent(
3863 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
3864 persistent_go->mode == WPAS_MODE_P2P_GO ?
3865 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3866 } else {
3867 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
3868 }
3869
3870 return 1;
3871}
3872
3873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874/**
3875 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3876 * @global: Pointer to global data from wpa_supplicant_init()
3877 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3878 * Returns: 0 on success, -1 on failure
3879 */
3880int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3881{
3882 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003883 int i;
3884
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003885 if (wpa_s->conf->p2p_disabled)
3886 return 0;
3887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3889 return 0;
3890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891 if (global->p2p)
3892 return 0;
3893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003895 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003896 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003897 p2p.p2p_scan = wpas_p2p_scan;
3898 p2p.send_action = wpas_send_action;
3899 p2p.send_action_done = wpas_send_action_done;
3900 p2p.go_neg_completed = wpas_go_neg_completed;
3901 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3902 p2p.dev_found = wpas_dev_found;
3903 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003904 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 p2p.start_listen = wpas_start_listen;
3906 p2p.stop_listen = wpas_stop_listen;
3907 p2p.send_probe_resp = wpas_send_probe_resp;
3908 p2p.sd_request = wpas_sd_request;
3909 p2p.sd_response = wpas_sd_response;
3910 p2p.prov_disc_req = wpas_prov_disc_req;
3911 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003912 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 p2p.invitation_process = wpas_invitation_process;
3914 p2p.invitation_received = wpas_invitation_received;
3915 p2p.invitation_result = wpas_invitation_result;
3916 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003917 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003918 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003919 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003920 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003921 p2p.get_persistent_group = wpas_get_persistent_group;
3922 p2p.get_go_info = wpas_get_go_info;
3923 p2p.remove_stale_groups = wpas_remove_stale_groups;
3924 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
3925 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
3926 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927
3928 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003929 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930 p2p.dev_name = wpa_s->conf->device_name;
3931 p2p.manufacturer = wpa_s->conf->manufacturer;
3932 p2p.model_name = wpa_s->conf->model_name;
3933 p2p.model_number = wpa_s->conf->model_number;
3934 p2p.serial_number = wpa_s->conf->serial_number;
3935 if (wpa_s->wps) {
3936 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3937 p2p.config_methods = wpa_s->wps->config_methods;
3938 }
3939
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003940 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3941 wpa_printf(MSG_ERROR,
3942 "P2P: Failed to configure supported channel list");
3943 return -1;
3944 }
3945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 if (wpa_s->conf->p2p_listen_reg_class &&
3947 wpa_s->conf->p2p_listen_channel) {
3948 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3949 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003950 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003951 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 /*
3953 * Pick one of the social channels randomly as the listen
3954 * channel.
3955 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003956 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
3957 &p2p.channel) != 0) {
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003958 wpa_printf(MSG_INFO,
3959 "P2P: No social channels supported by the driver - do not enable P2P");
3960 return 0;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003961 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003962 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003963 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003964 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
3965 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966
3967 if (wpa_s->conf->p2p_oper_reg_class &&
3968 wpa_s->conf->p2p_oper_channel) {
3969 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3970 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3971 p2p.cfg_op_channel = 1;
3972 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3973 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3974
3975 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003977 * Use random operation channel from 2.4 GHz band social
3978 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
3979 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003980 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003981 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
3982 &p2p.op_channel) != 0) {
3983 wpa_printf(MSG_ERROR,
3984 "P2P: Failed to select random social channel as operation channel");
3985 return -1;
3986 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 p2p.cfg_op_channel = 0;
3988 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3989 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3990 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003991
3992 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3993 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3994 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3995 }
3996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3998 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3999 p2p.country[2] = 0x04;
4000 } else
4001 os_memcpy(p2p.country, "XX\x04", 3);
4002
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4004 WPS_DEV_TYPE_LEN);
4005
4006 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4007 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4008 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4009
4010 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4011 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4012
4013 p2p.max_peers = 100;
4014
4015 if (wpa_s->conf->p2p_ssid_postfix) {
4016 p2p.ssid_postfix_len =
4017 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4018 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4019 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4020 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4021 p2p.ssid_postfix_len);
4022 }
4023
4024 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4025
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004026 p2p.max_listen = wpa_s->max_remain_on_chan;
4027
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004028 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4029 wpa_s->conf->p2p_passphrase_len <= 63)
4030 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4031 else
4032 p2p.passphrase_len = 8;
4033
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 global->p2p = p2p_init(&p2p);
4035 if (global->p2p == NULL)
4036 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004037 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004038
4039 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4040 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4041 continue;
4042 p2p_add_wps_vendor_extension(
4043 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4044 }
4045
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004046 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004048 return 0;
4049}
4050
4051
4052/**
4053 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4054 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4055 *
4056 * This function deinitialize per-interface P2P data.
4057 */
4058void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4059{
4060 if (wpa_s->driver && wpa_s->drv_priv)
4061 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004062
4063 if (wpa_s->go_params) {
4064 /* Clear any stored provisioning info */
4065 p2p_clear_provisioning_info(
4066 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004067 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004068 }
4069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004070 os_free(wpa_s->go_params);
4071 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004072 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004073 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4074 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4075 wpa_s->p2p_long_listen = 0;
4076 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4077 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4078 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004079 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004080 wpas_p2p_listen_work_done(wpa_s);
4081 if (wpa_s->p2p_send_action_work) {
4082 os_free(wpa_s->p2p_send_action_work->ctx);
4083 radio_work_done(wpa_s->p2p_send_action_work);
4084 wpa_s->p2p_send_action_work = NULL;
4085 }
4086 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004087
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004088 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4089 wpa_s->p2p_oob_dev_pw = NULL;
4090
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004091 os_free(wpa_s->p2p_group_common_freqs);
4092 wpa_s->p2p_group_common_freqs = NULL;
4093 wpa_s->p2p_group_common_freqs_num = 0;
4094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004095 /* TODO: remove group interface from the driver if this wpa_s instance
4096 * is on top of a P2P group interface */
4097}
4098
4099
4100/**
4101 * wpas_p2p_deinit_global - Deinitialize global P2P module
4102 * @global: Pointer to global data from wpa_supplicant_init()
4103 *
4104 * This function deinitializes the global (per device) P2P module.
4105 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004106static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004107{
4108 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004109
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004110 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004111
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004112 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004113
4114 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4116 wpa_s = wpa_s->next;
4117 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 tmp = global->ifaces;
4119 while (tmp &&
4120 (tmp == wpa_s ||
4121 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4122 tmp = tmp->next;
4123 }
4124 if (tmp == NULL)
4125 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 /* Disconnect from the P2P group and deinit the interface */
4127 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 }
4129
4130 /*
4131 * Deinit GO data on any possibly remaining interface (if main
4132 * interface is used as GO).
4133 */
4134 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4135 if (wpa_s->ap_iface)
4136 wpas_p2p_group_deinit(wpa_s);
4137 }
4138
4139 p2p_deinit(global->p2p);
4140 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004141 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142}
4143
4144
4145static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4146{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004147 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4148 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004149 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004150 if (wpa_s->drv_flags &
4151 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4152 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4153 return 1; /* P2P group requires a new interface in every case
4154 */
4155 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4156 return 0; /* driver does not support concurrent operations */
4157 if (wpa_s->global->ifaces->next)
4158 return 1; /* more that one interface already in use */
4159 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4160 return 1; /* this interface is already in use */
4161 return 0;
4162}
4163
4164
4165static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4166 const u8 *peer_addr,
4167 enum p2p_wps_method wps_method,
4168 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004169 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004170 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004171{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004172 if (persistent_group && wpa_s->conf->persistent_reconnect)
4173 persistent_group = 2;
4174
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004175 /*
4176 * Increase GO config timeout if HT40 is used since it takes some time
4177 * to scan channels for coex purposes before the BSS can be started.
4178 */
4179 p2p_set_config_timeout(wpa_s->global->p2p,
4180 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004182 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4183 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004184 persistent_group, ssid ? ssid->ssid : NULL,
4185 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004186 wpa_s->p2p_pd_before_go_neg, pref_freq,
4187 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4188 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189}
4190
4191
4192static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4193 const u8 *peer_addr,
4194 enum p2p_wps_method wps_method,
4195 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004196 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004197 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004199 if (persistent_group && wpa_s->conf->persistent_reconnect)
4200 persistent_group = 2;
4201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004202 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4203 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004204 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004205 ssid ? ssid->ssid_len : 0, pref_freq,
4206 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4207 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208}
4209
4210
4211static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4212{
4213 wpa_s->p2p_join_scan_count++;
4214 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4215 wpa_s->p2p_join_scan_count);
4216 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4217 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4218 " for join operationg - stop join attempt",
4219 MAC2STR(wpa_s->pending_join_iface_addr));
4220 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004221 if (wpa_s->p2p_auto_pd) {
4222 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004223 wpa_msg_global(wpa_s, MSG_INFO,
4224 P2P_EVENT_PROV_DISC_FAILURE
4225 " p2p_dev_addr=" MACSTR " status=N/A",
4226 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004227 return;
4228 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004229 wpa_msg_global(wpa_s->parent, MSG_INFO,
4230 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231 }
4232}
4233
4234
Dmitry Shmidt04949592012-07-19 12:16:46 -07004235static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4236{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004237 int res;
4238 unsigned int num, i;
4239 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004240
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004241 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4242 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004243 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004244 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004245
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004246 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4247 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004248 if (!freqs)
4249 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004250
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004251 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4252 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004253
4254 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004255 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004256 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4257 freq);
4258 res = 0;
4259 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004260 }
4261 }
4262
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004263 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004264 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004265
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004266exit_free:
4267 os_free(freqs);
4268 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004269}
4270
4271
4272static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4273 const u8 *peer_dev_addr)
4274{
4275 struct wpa_bss *bss;
4276 int updated;
4277
4278 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4279 if (bss == NULL)
4280 return -1;
4281 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4282 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4283 "last scan");
4284 return 0;
4285 }
4286
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004287 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4288 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004289 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4290 "%ld.%06ld (%supdated in last scan)",
4291 bss->last_update.sec, bss->last_update.usec,
4292 updated ? "": "not ");
4293
4294 return updated;
4295}
4296
4297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004298static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4299 struct wpa_scan_results *scan_res)
4300{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004301 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302 int freq;
4303 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004304
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004305 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4306
4307 if (wpa_s->global->p2p_disabled)
4308 return;
4309
Dmitry Shmidt04949592012-07-19 12:16:46 -07004310 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4311 scan_res ? (int) scan_res->num : -1,
4312 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313
4314 if (scan_res)
4315 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4316
Dmitry Shmidt04949592012-07-19 12:16:46 -07004317 if (wpa_s->p2p_auto_pd) {
4318 int join = wpas_p2p_peer_go(wpa_s,
4319 wpa_s->pending_join_dev_addr);
4320 if (join == 0 &&
4321 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4322 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004323 bss = wpa_bss_get_bssid_latest(
4324 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004325 if (bss) {
4326 freq = bss->freq;
4327 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4328 "the peer " MACSTR " at %d MHz",
4329 wpa_s->auto_pd_scan_retry,
4330 MAC2STR(wpa_s->
4331 pending_join_dev_addr),
4332 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004333 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004334 return;
4335 }
4336 }
4337
4338 if (join < 0)
4339 join = 0;
4340
4341 wpa_s->p2p_auto_pd = 0;
4342 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4343 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4344 MAC2STR(wpa_s->pending_join_dev_addr), join);
4345 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004346 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004347 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004348 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004349 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004350 wpa_msg_global(wpa_s, MSG_INFO,
4351 P2P_EVENT_PROV_DISC_FAILURE
4352 " p2p_dev_addr=" MACSTR " status=N/A",
4353 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004354 }
4355 return;
4356 }
4357
4358 if (wpa_s->p2p_auto_join) {
4359 int join = wpas_p2p_peer_go(wpa_s,
4360 wpa_s->pending_join_dev_addr);
4361 if (join < 0) {
4362 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4363 "running a GO -> use GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004364 wpa_msg_global(wpa_s->parent, MSG_INFO,
4365 P2P_EVENT_FALLBACK_TO_GO_NEG
4366 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004367 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4368 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4369 wpa_s->p2p_persistent_group, 0, 0, 0,
4370 wpa_s->p2p_go_intent,
4371 wpa_s->p2p_connect_freq,
4372 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004373 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004374 wpa_s->p2p_go_ht40,
4375 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376 return;
4377 }
4378
4379 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4380 "try to join the group", join ? "" :
4381 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004382 if (!join) {
4383 wpa_msg_global(wpa_s->parent, MSG_INFO,
4384 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004385 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004386 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004387 }
4388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004389 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4390 wpa_s->pending_join_iface_addr);
4391 if (freq < 0 &&
4392 p2p_get_interface_addr(wpa_s->global->p2p,
4393 wpa_s->pending_join_dev_addr,
4394 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004395 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4396 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4398 "address for join from " MACSTR " to " MACSTR
4399 " based on newly discovered P2P peer entry",
4400 MAC2STR(wpa_s->pending_join_iface_addr),
4401 MAC2STR(iface_addr));
4402 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4403 ETH_ALEN);
4404
4405 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4406 wpa_s->pending_join_iface_addr);
4407 }
4408 if (freq >= 0) {
4409 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4410 "from P2P peer table: %d MHz", freq);
4411 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004412 if (wpa_s->p2p_join_ssid_len) {
4413 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4414 MACSTR " and SSID %s",
4415 MAC2STR(wpa_s->pending_join_iface_addr),
4416 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4417 wpa_s->p2p_join_ssid_len));
4418 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4419 wpa_s->p2p_join_ssid,
4420 wpa_s->p2p_join_ssid_len);
4421 }
4422 if (!bss) {
4423 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4424 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4425 bss = wpa_bss_get_bssid_latest(wpa_s,
4426 wpa_s->pending_join_iface_addr);
4427 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004428 if (bss) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004429 u8 dev_addr[ETH_ALEN];
4430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004431 freq = bss->freq;
4432 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004433 "from BSS table: %d MHz (SSID %s)", freq,
4434 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07004435 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
4436 dev_addr) == 0 &&
4437 os_memcmp(wpa_s->pending_join_dev_addr,
4438 wpa_s->pending_join_iface_addr, ETH_ALEN) == 0 &&
4439 os_memcmp(dev_addr, wpa_s->pending_join_dev_addr,
4440 ETH_ALEN) != 0) {
4441 wpa_printf(MSG_DEBUG,
4442 "P2P: Update target GO device address based on BSS entry: " MACSTR " (was " MACSTR ")",
4443 MAC2STR(dev_addr),
4444 MAC2STR(wpa_s->pending_join_dev_addr));
4445 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr,
4446 ETH_ALEN);
4447 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004448 }
4449 if (freq > 0) {
4450 u16 method;
4451
Dmitry Shmidt04949592012-07-19 12:16:46 -07004452 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004453 wpa_msg_global(wpa_s->parent, MSG_INFO,
4454 P2P_EVENT_GROUP_FORMATION_FAILURE
4455 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004456 return;
4457 }
4458
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4460 "prior to joining an existing group (GO " MACSTR
4461 " freq=%u MHz)",
4462 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4463 wpa_s->pending_pd_before_join = 1;
4464
4465 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004466 case WPS_PIN_DISPLAY:
4467 method = WPS_CONFIG_KEYPAD;
4468 break;
4469 case WPS_PIN_KEYPAD:
4470 method = WPS_CONFIG_DISPLAY;
4471 break;
4472 case WPS_PBC:
4473 method = WPS_CONFIG_PUSHBUTTON;
4474 break;
4475 default:
4476 method = 0;
4477 break;
4478 }
4479
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004480 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4481 wpa_s->pending_join_dev_addr) ==
4482 method)) {
4483 /*
4484 * We have already performed provision discovery for
4485 * joining the group. Proceed directly to join
4486 * operation without duplicated provision discovery. */
4487 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4488 "with " MACSTR " already done - proceed to "
4489 "join",
4490 MAC2STR(wpa_s->pending_join_dev_addr));
4491 wpa_s->pending_pd_before_join = 0;
4492 goto start;
4493 }
4494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004495 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004496 wpa_s->pending_join_dev_addr,
4497 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004498 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004499 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4500 "Discovery Request before joining an "
4501 "existing group");
4502 wpa_s->pending_pd_before_join = 0;
4503 goto start;
4504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 return;
4506 }
4507
4508 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4509 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4510 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4511 wpas_p2p_check_join_scan_limit(wpa_s);
4512 return;
4513
4514start:
4515 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004516 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004517}
4518
4519
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004520static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4521 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523 int ret;
4524 struct wpa_driver_scan_params params;
4525 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004526 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004527 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528
4529 os_memset(&params, 0, sizeof(params));
4530
4531 /* P2P Wildcard SSID */
4532 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004533 if (ssid && ssid_len) {
4534 params.ssids[0].ssid = ssid;
4535 params.ssids[0].ssid_len = ssid_len;
4536 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4537 wpa_s->p2p_join_ssid_len = ssid_len;
4538 } else {
4539 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4540 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4541 wpa_s->p2p_join_ssid_len = 0;
4542 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543
4544 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004545 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4546 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4547 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004548 if (wps_ie == NULL) {
4549 wpas_p2p_scan_res_join(wpa_s, NULL);
4550 return;
4551 }
4552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004553 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4554 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004555 if (ies == NULL) {
4556 wpabuf_free(wps_ie);
4557 wpas_p2p_scan_res_join(wpa_s, NULL);
4558 return;
4559 }
4560 wpabuf_put_buf(ies, wps_ie);
4561 wpabuf_free(wps_ie);
4562
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004563 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004564
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004565 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566 params.extra_ies = wpabuf_head(ies);
4567 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004568
4569 if (!freq) {
4570 int oper_freq;
4571 /*
4572 * If freq is not provided, check the operating freq of the GO
4573 * and use a single channel scan on if possible.
4574 */
4575 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4576 wpa_s->pending_join_iface_addr);
4577 if (oper_freq > 0)
4578 freq = oper_freq;
4579 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004580 if (freq > 0) {
4581 freqs[0] = freq;
4582 params.freqs = freqs;
4583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584
4585 /*
4586 * Run a scan to update BSS table and start Provision Discovery once
4587 * the new scan results become available.
4588 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004589 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004590 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004591 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004592 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004593 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004594 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004595
4596 wpabuf_free(ies);
4597
4598 if (ret) {
4599 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4600 "try again later");
4601 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4602 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4603 wpas_p2p_check_join_scan_limit(wpa_s);
4604 }
4605}
4606
4607
Dmitry Shmidt04949592012-07-19 12:16:46 -07004608static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4609{
4610 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004611 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004612}
4613
4614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004616 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004617 int auto_join, int op_freq,
4618 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004619{
4620 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004621 MACSTR " dev " MACSTR " op_freq=%d)%s",
4622 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004623 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004624 if (ssid && ssid_len) {
4625 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4626 wpa_ssid_txt(ssid, ssid_len));
4627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004628
Dmitry Shmidt04949592012-07-19 12:16:46 -07004629 wpa_s->p2p_auto_pd = 0;
4630 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4632 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4633 wpa_s->pending_join_wps_method = wps_method;
4634
4635 /* Make sure we are not running find during connection establishment */
4636 wpas_p2p_stop_find(wpa_s);
4637
4638 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004639 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004640 return 0;
4641}
4642
4643
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004644static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4645 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646{
4647 struct wpa_supplicant *group;
4648 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004649 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650
4651 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4652 if (group == NULL)
4653 return -1;
4654 if (group != wpa_s) {
4655 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4656 sizeof(group->p2p_pin));
4657 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004658 } else {
4659 /*
4660 * Need to mark the current interface for p2p_group_formation
4661 * when a separate group interface is not used. This is needed
4662 * to allow p2p_cancel stop a pending p2p_connect-join.
4663 * wpas_p2p_init_group_interface() addresses this for the case
4664 * where a separate group interface is used.
4665 */
4666 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 }
4668
4669 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004670 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004671
4672 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004673 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4675 ETH_ALEN);
4676 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004677 if (freq && ssid && ssid_len) {
4678 res.freq = freq;
4679 res.ssid_len = ssid_len;
4680 os_memcpy(res.ssid, ssid, ssid_len);
4681 } else {
4682 bss = wpa_bss_get_bssid_latest(wpa_s,
4683 wpa_s->pending_join_iface_addr);
4684 if (bss) {
4685 res.freq = bss->freq;
4686 res.ssid_len = bss->ssid_len;
4687 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4688 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4689 bss->freq,
4690 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4691 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004692 }
4693
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004694 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4695 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4696 "starting client");
4697 wpa_drv_cancel_remain_on_channel(wpa_s);
4698 wpa_s->off_channel_freq = 0;
4699 wpa_s->roc_waiting_drv_freq = 0;
4700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701 wpas_start_wps_enrollee(group, &res);
4702
4703 /*
4704 * Allow a longer timeout for join-a-running-group than normal 15
4705 * second group formation timeout since the GO may not have authorized
4706 * our connection yet.
4707 */
4708 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4709 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4710 wpa_s, NULL);
4711
4712 return 0;
4713}
4714
4715
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004716static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004717 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004718{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004719 struct wpa_used_freq_data *freqs;
4720 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004721 unsigned int freq_in_use = 0, num, i;
4722
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004723 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4724 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004725 if (!freqs)
4726 return -1;
4727
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004728 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4729 wpa_s->num_multichan_concurrent);
4730
4731 /*
4732 * It is possible that the total number of used frequencies is bigger
4733 * than the number of frequencies used for P2P, so get the system wide
4734 * number of unused frequencies.
4735 */
4736 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4737
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004738 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004739 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4740 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004741
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004742 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004743 int ret;
4744 if (go)
4745 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4746 else
4747 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4748 if (!ret) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08004749 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
4750 ieee80211_is_dfs(freq)) {
4751 /*
4752 * If freq is a DFS channel and DFS is offloaded
4753 * to the driver, allow P2P GO to use it.
4754 */
4755 wpa_printf(MSG_DEBUG,
4756 "P2P: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to the driver",
4757 freq);
4758 } else {
4759 wpa_printf(MSG_DEBUG,
4760 "P2P: The forced channel (%u MHz) is not supported for P2P uses",
4761 freq);
4762 res = -3;
4763 goto exit_free;
4764 }
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004765 }
4766
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004767 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004768 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004769 freq_in_use = 1;
4770 }
4771
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004772 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004773 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4774 freq);
4775 res = -2;
4776 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004777 }
4778 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4779 "requested channel (%u MHz)", freq);
4780 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004781 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004782 }
4783
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004784 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004785
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004786 /* We have a candidate frequency to use */
4787 if (best_freq > 0) {
4788 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004789 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004790 best_freq);
4791 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004792 } else {
4793 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 -07004794 best_freq);
4795 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004796 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004797 } else if (num_unused > 0) {
4798 wpa_printf(MSG_DEBUG,
4799 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4800 *force_freq = 0;
4801 } else {
4802 wpa_printf(MSG_DEBUG,
4803 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4804 res = -2;
4805 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004806 }
4807
4808exit_ok:
4809 res = 0;
4810exit_free:
4811 os_free(freqs);
4812 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004813}
4814
4815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816/**
4817 * wpas_p2p_connect - Request P2P Group Formation to be started
4818 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4819 * @peer_addr: Address of the peer P2P Device
4820 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4821 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004822 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004823 * @join: Whether to join an existing group (as a client) instead of starting
4824 * Group Owner negotiation; @peer_addr is BSSID in that case
4825 * @auth: Whether to only authorize the connection instead of doing that and
4826 * initiating Group Owner negotiation
4827 * @go_intent: GO Intent or -1 to use default
4828 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004829 * @persistent_id: Persistent group credentials to use for forcing GO
4830 * parameters or -1 to generate new values (SSID/passphrase)
4831 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4832 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004833 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004834 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4836 * failure, -2 on failure due to channel not currently available,
4837 * -3 if forced channel is not supported
4838 */
4839int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4840 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004841 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004842 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004843 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004845 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004846 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004847 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004848 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004849 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
4851 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4852 return -1;
4853
Dmitry Shmidt04949592012-07-19 12:16:46 -07004854 if (persistent_id >= 0) {
4855 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4856 if (ssid == NULL || ssid->disabled != 2 ||
4857 ssid->mode != WPAS_MODE_P2P_GO)
4858 return -1;
4859 }
4860
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004861 os_free(wpa_s->global->add_psk);
4862 wpa_s->global->add_psk = NULL;
4863
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004864 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004865 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004867 if (go_intent < 0)
4868 go_intent = wpa_s->conf->p2p_go_intent;
4869
4870 if (!auth)
4871 wpa_s->p2p_long_listen = 0;
4872
4873 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004874 wpa_s->p2p_persistent_group = !!persistent_group;
4875 wpa_s->p2p_persistent_id = persistent_id;
4876 wpa_s->p2p_go_intent = go_intent;
4877 wpa_s->p2p_connect_freq = freq;
4878 wpa_s->p2p_fallback_to_go_neg = 0;
4879 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004880 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004881 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004882
4883 if (pin)
4884 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4885 else if (wps_method == WPS_PIN_DISPLAY) {
4886 ret = wps_generate_pin();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004887 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4888 "%08d", ret);
4889 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
4890 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4892 wpa_s->p2p_pin);
4893 } else
4894 wpa_s->p2p_pin[0] = '\0';
4895
Dmitry Shmidt04949592012-07-19 12:16:46 -07004896 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004897 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4898 if (auth) {
4899 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4900 "connect a running group from " MACSTR,
4901 MAC2STR(peer_addr));
4902 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4903 return ret;
4904 }
4905 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4906 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4907 iface_addr) < 0) {
4908 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4909 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4910 dev_addr);
4911 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004912 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004913 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004914 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4915 "%ld.%06ld",
4916 wpa_s->p2p_auto_started.sec,
4917 wpa_s->p2p_auto_started.usec);
4918 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004919 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004920 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004921 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 return -1;
4923 return ret;
4924 }
4925
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004926 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4927 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004928 if (res)
4929 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004930 wpas_p2p_set_own_freq_preference(wpa_s,
4931 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004932
4933 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4934
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004935 if (wpa_s->create_p2p_iface) {
4936 /* Prepare to add a new interface for the group */
4937 iftype = WPA_IF_P2P_GROUP;
4938 if (go_intent == 15)
4939 iftype = WPA_IF_P2P_GO;
4940 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4941 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4942 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004944 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004945
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004946 if_addr = wpa_s->pending_interface_addr;
4947 } else
4948 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004949
4950 if (auth) {
4951 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004953 force_freq, persistent_group, ssid,
4954 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 return -1;
4956 return ret;
4957 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004958
4959 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4960 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004961 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004962 if (wpa_s->create_p2p_iface)
4963 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004964 return -1;
4965 }
4966 return ret;
4967}
4968
4969
4970/**
4971 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4972 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4973 * @freq: Frequency of the channel in MHz
4974 * @duration: Duration of the stay on the channel in milliseconds
4975 *
4976 * This callback is called when the driver indicates that it has started the
4977 * requested remain-on-channel duration.
4978 */
4979void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4980 unsigned int freq, unsigned int duration)
4981{
4982 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4983 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004984 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)",
4985 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4986 wpa_s->roc_waiting_drv_freq, freq, duration);
4987 if (wpa_s->off_channel_freq &&
4988 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4990 wpa_s->pending_listen_duration);
4991 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004992 } else {
4993 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4994 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4995 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004996 }
4997}
4998
4999
Jithu Jance57cea1a2014-08-20 10:22:04 -07005000int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005001{
5002 /* Limit maximum Listen state time based on driver limitation. */
5003 if (timeout > wpa_s->max_remain_on_chan)
5004 timeout = wpa_s->max_remain_on_chan;
5005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 return p2p_listen(wpa_s->global->p2p, timeout);
5007}
5008
5009
5010/**
5011 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5012 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5013 * @freq: Frequency of the channel in MHz
5014 *
5015 * This callback is called when the driver indicates that a remain-on-channel
5016 * operation has been completed, i.e., the duration on the requested channel
5017 * has timed out.
5018 */
5019void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5020 unsigned int freq)
5021{
5022 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5023 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005024 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005025 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5027 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07005028 if (wpa_s->p2p_long_listen > 0)
5029 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5031 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005032 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 if (wpa_s->p2p_long_listen > 0) {
5035 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5036 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005037 } else {
5038 /*
5039 * When listen duration is over, stop listen & update p2p_state
5040 * to IDLE.
5041 */
5042 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 }
5044}
5045
5046
5047/**
5048 * wpas_p2p_group_remove - Remove a P2P group
5049 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5050 * @ifname: Network interface name of the group interface or "*" to remove all
5051 * groups
5052 * Returns: 0 on success, -1 on failure
5053 *
5054 * This function is used to remove a P2P group. This can be used to disconnect
5055 * from a group in which the local end is a P2P Client or to end a P2P Group in
5056 * case the local end is the Group Owner. If a virtual network interface was
5057 * created for this group, that interface will be removed. Otherwise, only the
5058 * configured P2P group network will be removed from the interface.
5059 */
5060int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5061{
5062 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005063 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064
5065 if (os_strcmp(ifname, "*") == 0) {
5066 struct wpa_supplicant *prev;
5067 wpa_s = global->ifaces;
5068 while (wpa_s) {
5069 prev = wpa_s;
5070 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005071 if (prev->p2p_group_interface !=
5072 NOT_P2P_GROUP_INTERFACE ||
5073 (prev->current_ssid &&
5074 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005075 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005076 }
5077 return 0;
5078 }
5079
5080 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5081 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5082 break;
5083 }
5084
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005085 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086}
5087
5088
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005089static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5090{
5091 unsigned int r;
5092
5093 if (freq == 2) {
5094 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5095 "band");
5096 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005097 p2p_supported_freq_go(wpa_s->global->p2p,
5098 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005099 freq = wpa_s->best_24_freq;
5100 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5101 "channel: %d MHz", freq);
5102 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005103 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5104 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005105 freq = 2412 + (r % 3) * 25;
5106 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5107 "channel: %d MHz", freq);
5108 }
5109 }
5110
5111 if (freq == 5) {
5112 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5113 "band");
5114 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005115 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005116 wpa_s->best_5_freq)) {
5117 freq = wpa_s->best_5_freq;
5118 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5119 "channel: %d MHz", freq);
5120 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005121 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5122 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005123 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005124 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005125 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5126 "5 GHz channel for P2P group");
5127 return -1;
5128 }
5129 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5130 "channel: %d MHz", freq);
5131 }
5132 }
5133
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005134 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005135 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5136 ieee80211_is_dfs(freq)) {
5137 /*
5138 * If freq is a DFS channel and DFS is offloaded to the
5139 * driver, allow P2P GO to use it.
5140 */
5141 wpa_printf(MSG_DEBUG, "P2P: "
5142 "%s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded",
5143 __func__, freq);
5144 return freq;
5145 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005146 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5147 "(%u MHz) is not supported for P2P uses",
5148 freq);
5149 return -1;
5150 }
5151
5152 return freq;
5153}
5154
5155
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005156static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5157 struct p2p_go_neg_results *params,
5158 const struct p2p_channels *channels)
5159{
5160 unsigned int i, r;
5161
5162 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005163 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5164 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005165
5166 for (i = 0; i < 3; i++) {
5167 params->freq = 2412 + ((r + i) % 3) * 25;
5168 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005169 freq_included(channels, params->freq) &&
5170 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005171 goto out;
5172 }
5173
5174 /* try all channels in reg. class 81 */
5175 for (i = 0; i < 11; i++) {
5176 params->freq = 2412 + i * 5;
5177 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005178 freq_included(channels, params->freq) &&
5179 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005180 goto out;
5181 }
5182
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005183 /* try all channels in operating class 115 */
5184 for (i = 0; i < 4; i++) {
5185 params->freq = 5180 + i * 20;
5186 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5187 freq_included(channels, params->freq) &&
5188 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5189 goto out;
5190 }
5191
5192 /* try all channels in operating class 124 */
5193 for (i = 0; i < 4; i++) {
5194 params->freq = 5745 + i * 20;
5195 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5196 freq_included(channels, params->freq) &&
5197 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5198 goto out;
5199 }
5200
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005201 /* try social channel class 180 channel 2 */
5202 params->freq = 58320 + 1 * 2160;
5203 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5204 freq_included(channels, params->freq) &&
5205 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5206 goto out;
5207
5208 /* try all channels in reg. class 180 */
5209 for (i = 0; i < 4; i++) {
5210 params->freq = 58320 + i * 2160;
5211 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5212 freq_included(channels, params->freq) &&
5213 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5214 goto out;
5215 }
5216
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005217 wpa_printf(MSG_DEBUG, "P2P: No 2.4, 5, or 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005218 return -1;
5219out:
5220 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5221 params->freq);
5222 return 0;
5223}
5224
5225
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005226static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5227 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005228 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005229 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005231 struct wpa_used_freq_data *freqs;
5232 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005233 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005234
5235 os_memset(params, 0, sizeof(*params));
5236 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005237 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005238 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005240 if (!freq_included(channels, freq)) {
5241 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5242 "accepted", freq);
5243 return -1;
5244 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5246 "frequency %d MHz", freq);
5247 params->freq = freq;
5248 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5249 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005250 wpa_s->conf->p2p_oper_channel <= 11 &&
5251 freq_included(channels,
5252 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005253 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5254 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5255 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005256 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5257 wpa_s->conf->p2p_oper_reg_class == 116 ||
5258 wpa_s->conf->p2p_oper_reg_class == 117 ||
5259 wpa_s->conf->p2p_oper_reg_class == 124 ||
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005260 wpa_s->conf->p2p_oper_reg_class == 125 ||
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005261 wpa_s->conf->p2p_oper_reg_class == 126 ||
5262 wpa_s->conf->p2p_oper_reg_class == 127) &&
5263 freq_included(channels,
5264 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005265 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5266 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5267 "frequency %d MHz", params->freq);
5268 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5269 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005270 p2p_supported_freq_go(wpa_s->global->p2p,
5271 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005272 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005273 params->freq = wpa_s->best_overall_freq;
5274 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5275 "channel %d MHz", params->freq);
5276 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5277 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005278 p2p_supported_freq_go(wpa_s->global->p2p,
5279 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005280 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281 params->freq = wpa_s->best_24_freq;
5282 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5283 "channel %d MHz", params->freq);
5284 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5285 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005286 p2p_supported_freq_go(wpa_s->global->p2p,
5287 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005288 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005289 params->freq = wpa_s->best_5_freq;
5290 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5291 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005292 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5293 channels))) {
5294 params->freq = pref_freq;
5295 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5296 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005297 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005298 /* no preference, select some channel */
5299 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005300 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005301 }
5302
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005303 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5304 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005305 if (!freqs)
5306 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005308 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005309 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005310
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005311 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5312
5313 /* First try the best used frequency if possible */
5314 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5315 params->freq = cand_freq;
5316 } else if (!freq) {
5317 /* Try any of the used frequencies */
5318 for (i = 0; i < num; i++) {
5319 if (freq_included(channels, freqs[i].freq)) {
5320 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5321 freqs[i].freq);
5322 params->freq = freqs[i].freq;
5323 break;
5324 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005325 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005326
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005327 if (i == num) {
5328 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005329 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005330 os_free(freqs);
5331 return -1;
5332 } else {
5333 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5334 }
5335 }
5336 } else {
5337 for (i = 0; i < num; i++) {
5338 if (freqs[i].freq == freq)
5339 break;
5340 }
5341
5342 if (i == num) {
5343 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5344 if (freq)
5345 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5346 os_free(freqs);
5347 return -1;
5348 } else {
5349 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5350 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005351 }
5352 }
5353
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005354 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005355 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356}
5357
5358
5359static struct wpa_supplicant *
5360wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5361 int go)
5362{
5363 struct wpa_supplicant *group_wpa_s;
5364
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005365 if (!wpas_p2p_create_iface(wpa_s)) {
5366 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5367 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005368 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005369 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005370 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371
5372 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005373 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005374 wpa_msg_global(wpa_s, MSG_ERROR,
5375 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005376 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005378 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5379 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005380 wpa_msg_global(wpa_s, MSG_ERROR,
5381 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382 wpas_p2p_remove_pending_group_interface(wpa_s);
5383 return NULL;
5384 }
5385
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005386 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5387 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005388 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 return group_wpa_s;
5390}
5391
5392
5393/**
5394 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5395 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5396 * @persistent_group: Whether to create a persistent group
5397 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005398 * @ht40: Start GO with 40 MHz channel width
5399 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005400 * Returns: 0 on success, -1 on failure
5401 *
5402 * This function creates a new P2P group with the local end as the Group Owner,
5403 * i.e., without using Group Owner Negotiation.
5404 */
5405int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005406 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005407{
5408 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409
5410 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5411 return -1;
5412
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005413 os_free(wpa_s->global->add_psk);
5414 wpa_s->global->add_psk = NULL;
5415
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005416 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005417 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005418 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005419
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005420 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5421 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005423
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005424 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005425 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005426 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005427 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005428 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) &&
5429 ieee80211_is_dfs(params.freq)) {
5430 /*
5431 * If freq is a DFS channel and DFS is offloaded to the
5432 * driver, allow P2P GO to use it.
5433 */
5434 wpa_printf(MSG_DEBUG,
5435 "P2P: %s: The forced channel for GO (%u MHz) is DFS, and DFS is offloaded to driver",
5436 __func__, params.freq);
5437 } else {
5438 wpa_printf(MSG_DEBUG,
5439 "P2P: The selected channel for GO (%u MHz) is not supported for P2P uses",
5440 params.freq);
5441 return -1;
5442 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005444 p2p_go_params(wpa_s->global->p2p, &params);
5445 params.persistent_group = persistent_group;
5446
5447 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5448 if (wpa_s == NULL)
5449 return -1;
5450 wpas_start_wps_go(wpa_s, &params, 0);
5451
5452 return 0;
5453}
5454
5455
5456static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005457 struct wpa_ssid *params, int addr_allocated,
5458 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459{
5460 struct wpa_ssid *ssid;
5461
5462 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5463 if (wpa_s == NULL)
5464 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005465 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466
5467 wpa_supplicant_ap_deinit(wpa_s);
5468
5469 ssid = wpa_config_add_network(wpa_s->conf);
5470 if (ssid == NULL)
5471 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 wpa_config_set_network_defaults(ssid);
5473 ssid->temporary = 1;
5474 ssid->proto = WPA_PROTO_RSN;
5475 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5476 ssid->group_cipher = WPA_CIPHER_CCMP;
5477 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5478 ssid->ssid = os_malloc(params->ssid_len);
5479 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005480 wpa_config_remove_network(wpa_s->conf, ssid->id);
5481 return -1;
5482 }
5483 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5484 ssid->ssid_len = params->ssid_len;
5485 ssid->p2p_group = 1;
5486 ssid->export_keys = 1;
5487 if (params->psk_set) {
5488 os_memcpy(ssid->psk, params->psk, 32);
5489 ssid->psk_set = 1;
5490 }
5491 if (params->passphrase)
5492 ssid->passphrase = os_strdup(params->passphrase);
5493
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005494 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005495 wpa_s->p2p_in_invitation = 1;
5496 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005497
Dmitry Shmidt15907092014-03-25 10:42:57 -07005498 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5499 NULL);
5500 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5501 wpas_p2p_group_formation_timeout,
5502 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005503 wpa_supplicant_select_network(wpa_s, ssid);
5504
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005505 return 0;
5506}
5507
5508
5509int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5510 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005511 int force_freq, int neg_freq, int ht40,
5512 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005513 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005514{
5515 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005516 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005517
5518 if (ssid->disabled != 2 || ssid->ssid == NULL)
5519 return -1;
5520
5521 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5522 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5523 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5524 "already running");
Dmitry Shmidtb1e52102015-05-29 12:36:29 -07005525 if (go == 0 &&
5526 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5527 wpa_s->parent, NULL)) {
5528 /*
5529 * This can happen if Invitation Response frame was lost
5530 * and the peer (GO of a persistent group) tries to
5531 * invite us again. Reschedule the timeout to avoid
5532 * terminating the wait for the connection too early
5533 * since we now know that the peer is still trying to
5534 * invite us instead of having already started the GO.
5535 */
5536 wpa_printf(MSG_DEBUG,
5537 "P2P: Reschedule group formation timeout since peer is still trying to invite us");
5538 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5539 wpas_p2p_group_formation_timeout,
5540 wpa_s->parent, NULL);
5541 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005542 return 0;
5543 }
5544
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005545 os_free(wpa_s->global->add_psk);
5546 wpa_s->global->add_psk = NULL;
5547
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005549 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550
Dmitry Shmidt04949592012-07-19 12:16:46 -07005551 wpa_s->p2p_fallback_to_go_neg = 0;
5552
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005553 if (ssid->mode == WPAS_MODE_P2P_GO) {
5554 if (force_freq > 0) {
5555 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5556 if (freq < 0)
5557 return -1;
5558 } else {
5559 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5560 if (freq < 0 ||
5561 (freq > 0 && !freq_included(channels, freq)))
5562 freq = 0;
5563 }
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005564 } else if (ssid->mode == WPAS_MODE_INFRA) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005565 freq = neg_freq;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005566 if (freq <= 0 || !freq_included(channels, freq)) {
5567 struct os_reltime now;
5568 struct wpa_bss *bss =
5569 wpa_bss_get_p2p_dev_addr(wpa_s, ssid->bssid);
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005570
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005571 os_get_reltime(&now);
5572 if (bss &&
5573 !os_reltime_expired(&now, &bss->last_update, 5) &&
5574 freq_included(channels, bss->freq))
5575 freq = bss->freq;
5576 else
5577 freq = 0;
5578 }
5579
Dmitry Shmidt15907092014-03-25 10:42:57 -07005580 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005581 } else {
Dmitry Shmidt15907092014-03-25 10:42:57 -07005582 return -1;
Dmitry Shmidt912c6ec2015-03-30 13:16:51 -07005583 }
Dmitry Shmidt15907092014-03-25 10:42:57 -07005584
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005585 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005586 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587
5588 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005589 params.psk_set = ssid->psk_set;
5590 if (params.psk_set)
5591 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005592 if (ssid->passphrase) {
5593 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5594 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5595 "persistent group");
5596 return -1;
5597 }
5598 os_strlcpy(params.passphrase, ssid->passphrase,
5599 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005600 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5602 params.ssid_len = ssid->ssid_len;
5603 params.persistent_group = 1;
5604
5605 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5606 if (wpa_s == NULL)
5607 return -1;
5608
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005609 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5610
Dmitry Shmidt56052862013-10-04 10:23:25 -07005611 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005612 wpas_start_wps_go(wpa_s, &params, 0);
5613
5614 return 0;
5615}
5616
5617
5618static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5619 struct wpabuf *proberesp_ies)
5620{
5621 struct wpa_supplicant *wpa_s = ctx;
5622 if (wpa_s->ap_iface) {
5623 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5625 wpabuf_free(beacon_ies);
5626 wpabuf_free(proberesp_ies);
5627 return;
5628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005629 if (beacon_ies) {
5630 wpabuf_free(hapd->p2p_beacon_ie);
5631 hapd->p2p_beacon_ie = beacon_ies;
5632 }
5633 wpabuf_free(hapd->p2p_probe_resp_ie);
5634 hapd->p2p_probe_resp_ie = proberesp_ies;
5635 } else {
5636 wpabuf_free(beacon_ies);
5637 wpabuf_free(proberesp_ies);
5638 }
5639 wpa_supplicant_ap_update_beacon(wpa_s);
5640}
5641
5642
5643static void wpas_p2p_idle_update(void *ctx, int idle)
5644{
5645 struct wpa_supplicant *wpa_s = ctx;
5646 if (!wpa_s->ap_iface)
5647 return;
5648 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005649 if (idle) {
5650 if (wpa_s->global->p2p_fail_on_wps_complete &&
5651 wpa_s->p2p_in_provisioning) {
5652 wpas_p2p_grpform_fail_after_wps(wpa_s);
5653 return;
5654 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005655 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005656 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005657 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5658}
5659
5660
5661struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005662 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663{
5664 struct p2p_group *group;
5665 struct p2p_group_config *cfg;
5666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5668 return NULL;
5669
5670 cfg = os_zalloc(sizeof(*cfg));
5671 if (cfg == NULL)
5672 return NULL;
5673
Dmitry Shmidt04949592012-07-19 12:16:46 -07005674 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005675 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005676 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005677 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005678 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5679 if (wpa_s->max_stations &&
5680 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5681 cfg->max_clients = wpa_s->max_stations;
5682 else
5683 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005684 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5685 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005686 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005687 cfg->cb_ctx = wpa_s;
5688 cfg->ie_update = wpas_p2p_ie_update;
5689 cfg->idle_update = wpas_p2p_idle_update;
5690
5691 group = p2p_group_init(wpa_s->global->p2p, cfg);
5692 if (group == NULL)
5693 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005694 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005695 p2p_group_notif_formation_done(group);
5696 wpa_s->p2p_group = group;
5697 return group;
5698}
5699
5700
5701void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5702 int registrar)
5703{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005704 struct wpa_ssid *ssid = wpa_s->current_ssid;
5705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 if (!wpa_s->p2p_in_provisioning) {
5707 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5708 "provisioning not in progress");
5709 return;
5710 }
5711
Dmitry Shmidt04949592012-07-19 12:16:46 -07005712 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5713 u8 go_dev_addr[ETH_ALEN];
5714 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5715 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5716 ssid->ssid_len);
5717 /* Clear any stored provisioning info */
5718 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5719 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005720
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005721 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5722 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005723 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005724 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5725 /*
5726 * Use a separate timeout for initial data connection to
5727 * complete to allow the group to be removed automatically if
5728 * something goes wrong in this step before the P2P group idle
5729 * timeout mechanism is taken into use.
5730 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005731 wpa_dbg(wpa_s, MSG_DEBUG,
5732 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5733 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005734 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5735 wpas_p2p_group_formation_timeout,
5736 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005737 } else if (ssid) {
5738 /*
5739 * Use a separate timeout for initial data connection to
5740 * complete to allow the group to be removed automatically if
5741 * the client does not complete data connection successfully.
5742 */
5743 wpa_dbg(wpa_s, MSG_DEBUG,
5744 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5745 P2P_MAX_INITIAL_CONN_WAIT_GO);
5746 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5747 wpas_p2p_group_formation_timeout,
5748 wpa_s->parent, NULL);
5749 /*
5750 * Complete group formation on first successful data connection
5751 */
5752 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005753 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005754 if (wpa_s->global->p2p)
5755 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07005756 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757}
5758
5759
Jouni Malinen75ecf522011-06-27 15:19:46 -07005760void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5761 struct wps_event_fail *fail)
5762{
5763 if (!wpa_s->p2p_in_provisioning) {
5764 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5765 "provisioning not in progress");
5766 return;
5767 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005768
5769 if (wpa_s->go_params) {
5770 p2p_clear_provisioning_info(
5771 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005772 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005773 }
5774
Jouni Malinen75ecf522011-06-27 15:19:46 -07005775 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005776
5777 if (wpa_s == wpa_s->global->p2p_group_formation) {
5778 /*
5779 * Allow some time for the failed WPS negotiation exchange to
5780 * complete, but remove the group since group formation cannot
5781 * succeed after provisioning failure.
5782 */
5783 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5784 wpa_s->global->p2p_fail_on_wps_complete = 1;
5785 eloop_deplete_timeout(0, 50000,
5786 wpas_p2p_group_formation_timeout,
5787 wpa_s->parent, NULL);
5788 }
5789}
5790
5791
5792int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5793{
5794 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5795 !wpa_s->p2p_in_provisioning)
5796 return 0;
5797
5798 wpas_p2p_grpform_fail_after_wps(wpa_s);
5799
5800 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005801}
5802
5803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005805 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005806 enum wpas_p2p_prov_disc_use use,
5807 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808{
5809 u16 config_methods;
5810
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005811 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005812 wpa_s->p2p_fallback_to_go_neg = 0;
5813 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005814 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
5815 p2ps_prov->conncap = p2ps_group_capability(
5816 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
5817 wpa_printf(MSG_DEBUG,
5818 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
5819 __func__, p2ps_prov->conncap,
5820 p2ps_prov->adv_id, p2ps_prov->conncap,
5821 p2ps_prov->status, p2ps_prov->info);
5822
5823 config_methods = 0;
5824 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005826 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005828 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5829 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005831 else {
5832 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005833 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005836
Dmitry Shmidt04949592012-07-19 12:16:46 -07005837 if (use == WPAS_P2P_PD_AUTO) {
5838 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5839 wpa_s->pending_pd_config_methods = config_methods;
5840 wpa_s->p2p_auto_pd = 1;
5841 wpa_s->p2p_auto_join = 0;
5842 wpa_s->pending_pd_before_join = 0;
5843 wpa_s->auto_pd_scan_retry = 0;
5844 wpas_p2p_stop_find(wpa_s);
5845 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005846 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005847 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5848 wpa_s->p2p_auto_started.sec,
5849 wpa_s->p2p_auto_started.usec);
5850 wpas_p2p_join_scan(wpa_s, NULL);
5851 return 0;
5852 }
5853
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005854 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
5855 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005857 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005858
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005859 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005860 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005861 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005862}
5863
5864
5865int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5866 char *end)
5867{
5868 return p2p_scan_result_text(ies, ies_len, buf, end);
5869}
5870
5871
5872static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5873{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005874 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005875 return;
5876
Dmitry Shmidt03658832014-08-13 11:03:49 -07005877 wpas_p2p_action_tx_clear(wpa_s);
5878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5880 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005881 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882}
5883
5884
5885int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5886 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005887 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005888 const u8 *dev_id, unsigned int search_delay,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005889 u8 seek_cnt, const char **seek_string, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890{
5891 wpas_p2p_clear_pending_action_tx(wpa_s);
5892 wpa_s->p2p_long_listen = 0;
5893
Dmitry Shmidt04949592012-07-19 12:16:46 -07005894 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5895 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005896 return -1;
5897
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005898 wpa_supplicant_cancel_sched_scan(wpa_s);
5899
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005900 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005901 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08005902 search_delay, seek_cnt, seek_string, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903}
5904
5905
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005906static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5907 struct wpa_scan_results *scan_res)
5908{
5909 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5910
5911 if (wpa_s->p2p_scan_work) {
5912 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5913 wpa_s->p2p_scan_work = NULL;
5914 radio_work_done(work);
5915 }
5916
5917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5918 return;
5919
5920 /*
5921 * Indicate that results have been processed so that the P2P module can
5922 * continue pending tasks.
5923 */
5924 p2p_scan_res_handled(wpa_s->global->p2p);
5925}
5926
5927
5928static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929{
5930 wpas_p2p_clear_pending_action_tx(wpa_s);
5931 wpa_s->p2p_long_listen = 0;
5932 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5933 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005934
5935 if (wpa_s->global->p2p)
5936 p2p_stop_find(wpa_s->global->p2p);
5937
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005938 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5939 wpa_printf(MSG_DEBUG,
5940 "P2P: Do not consider the scan results after stop_find");
5941 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5942 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005943}
5944
5945
5946void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5947{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005948 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005949 if (!wpa_s->global->pending_group_iface_for_p2ps)
5950 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005951}
5952
5953
5954static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5955{
5956 struct wpa_supplicant *wpa_s = eloop_ctx;
5957 wpa_s->p2p_long_listen = 0;
5958}
5959
5960
5961int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5962{
5963 int res;
5964
5965 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5966 return -1;
5967
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005968 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005969 wpas_p2p_clear_pending_action_tx(wpa_s);
5970
5971 if (timeout == 0) {
5972 /*
5973 * This is a request for unlimited Listen state. However, at
5974 * least for now, this is mapped to a Listen state for one
5975 * hour.
5976 */
5977 timeout = 3600;
5978 }
5979 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5980 wpa_s->p2p_long_listen = 0;
5981
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005982 /*
5983 * Stop previous find/listen operation to avoid trying to request a new
5984 * remain-on-channel operation while the driver is still running the
5985 * previous one.
5986 */
5987 if (wpa_s->global->p2p)
5988 p2p_stop_find(wpa_s->global->p2p);
5989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5991 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5992 wpa_s->p2p_long_listen = timeout * 1000;
5993 eloop_register_timeout(timeout, 0,
5994 wpas_p2p_long_listen_timeout,
5995 wpa_s, NULL);
5996 }
5997
5998 return res;
5999}
6000
6001
6002int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6003 u8 *buf, size_t len, int p2p_group)
6004{
6005 struct wpabuf *p2p_ie;
6006 int ret;
6007
6008 if (wpa_s->global->p2p_disabled)
6009 return -1;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07006010 /*
6011 * Advertize mandatory cross connection capability even on
6012 * p2p_disabled=1 interface when associating with a P2P Manager WLAN AP.
6013 */
6014 if (wpa_s->conf->p2p_disabled && p2p_group)
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006015 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006016 if (wpa_s->global->p2p == NULL)
6017 return -1;
6018 if (bss == NULL)
6019 return -1;
6020
6021 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6022 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6023 p2p_group, p2p_ie);
6024 wpabuf_free(p2p_ie);
6025
6026 return ret;
6027}
6028
6029
6030int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006031 const u8 *dst, const u8 *bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006032 const u8 *ie, size_t ie_len,
6033 unsigned int rx_freq, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006034{
6035 if (wpa_s->global->p2p_disabled)
6036 return 0;
6037 if (wpa_s->global->p2p == NULL)
6038 return 0;
6039
Dmitry Shmidt04949592012-07-19 12:16:46 -07006040 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006041 ie, ie_len, rx_freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006042 case P2P_PREQ_NOT_P2P:
6043 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6044 ssi_signal);
6045 /* fall through */
6046 case P2P_PREQ_MALFORMED:
6047 case P2P_PREQ_NOT_LISTEN:
6048 case P2P_PREQ_NOT_PROCESSED:
6049 default: /* make gcc happy */
6050 return 0;
6051 case P2P_PREQ_PROCESSED:
6052 return 1;
6053 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006054}
6055
6056
6057void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6058 const u8 *sa, const u8 *bssid,
6059 u8 category, const u8 *data, size_t len, int freq)
6060{
6061 if (wpa_s->global->p2p_disabled)
6062 return;
6063 if (wpa_s->global->p2p == NULL)
6064 return;
6065
6066 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6067 freq);
6068}
6069
6070
6071void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6072{
6073 if (wpa_s->global->p2p_disabled)
6074 return;
6075 if (wpa_s->global->p2p == NULL)
6076 return;
6077
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006078 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006079}
6080
6081
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006082static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006083{
6084 p2p_group_deinit(wpa_s->p2p_group);
6085 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006086
6087 wpa_s->ap_configured_cb = NULL;
6088 wpa_s->ap_configured_cb_ctx = NULL;
6089 wpa_s->ap_configured_cb_data = NULL;
6090 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006091}
6092
6093
6094int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6095{
6096 wpa_s->p2p_long_listen = 0;
6097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6099 return -1;
6100
6101 return p2p_reject(wpa_s->global->p2p, addr);
6102}
6103
6104
6105/* Invite to reinvoke a persistent group */
6106int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03006107 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006108 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109{
6110 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006111 u8 *bssid = NULL;
6112 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006113 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006114 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006115
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006116 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006117 if (peer_addr)
6118 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6119 else
6120 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006121
Jouni Malinen31be0a42012-08-31 21:20:51 +03006122 wpa_s->p2p_persistent_go_freq = freq;
6123 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006124 if (ssid->mode == WPAS_MODE_P2P_GO) {
6125 role = P2P_INVITE_ROLE_GO;
6126 if (peer_addr == NULL) {
6127 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6128 "address in invitation command");
6129 return -1;
6130 }
6131 if (wpas_p2p_create_iface(wpa_s)) {
6132 if (wpas_p2p_add_group_interface(wpa_s,
6133 WPA_IF_P2P_GO) < 0) {
6134 wpa_printf(MSG_ERROR, "P2P: Failed to "
6135 "allocate a new interface for the "
6136 "group");
6137 return -1;
6138 }
6139 bssid = wpa_s->pending_interface_addr;
6140 } else
6141 bssid = wpa_s->own_addr;
6142 } else {
6143 role = P2P_INVITE_ROLE_CLIENT;
6144 peer_addr = ssid->bssid;
6145 }
6146 wpa_s->pending_invite_ssid_id = ssid->id;
6147
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006148 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6149 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006150 if (res)
6151 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07006152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6154 return -1;
6155
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006156 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6157 no_pref_freq_given && pref_freq > 0 &&
6158 wpa_s->num_multichan_concurrent > 1 &&
6159 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6160 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6161 pref_freq);
6162 pref_freq = 0;
6163 }
6164
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08006165 /*
6166 * Stop any find/listen operations before invitation and possibly
6167 * connection establishment.
6168 */
6169 wpas_p2p_stop_find_oper(wpa_s);
6170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006171 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006172 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006173 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006174}
6175
6176
6177/* Invite to join an active group */
6178int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6179 const u8 *peer_addr, const u8 *go_dev_addr)
6180{
6181 struct wpa_global *global = wpa_s->global;
6182 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006183 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006184 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006185 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006186 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006187 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006188
Jouni Malinen31be0a42012-08-31 21:20:51 +03006189 wpa_s->p2p_persistent_go_freq = 0;
6190 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006191 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006193 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6194 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6195 break;
6196 }
6197 if (wpa_s == NULL) {
6198 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6199 return -1;
6200 }
6201
6202 ssid = wpa_s->current_ssid;
6203 if (ssid == NULL) {
6204 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6205 "invitation");
6206 return -1;
6207 }
6208
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006209 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006210 persistent = ssid->p2p_persistent_group &&
6211 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6212 ssid->ssid, ssid->ssid_len);
6213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214 if (ssid->mode == WPAS_MODE_P2P_GO) {
6215 role = P2P_INVITE_ROLE_ACTIVE_GO;
6216 bssid = wpa_s->own_addr;
6217 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006218 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006219 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006220 } else {
6221 role = P2P_INVITE_ROLE_CLIENT;
6222 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6223 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6224 "invite to current group");
6225 return -1;
6226 }
6227 bssid = wpa_s->bssid;
6228 if (go_dev_addr == NULL &&
6229 !is_zero_ether_addr(wpa_s->go_dev_addr))
6230 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006231 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6232 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006233 }
6234 wpa_s->parent->pending_invite_ssid_id = -1;
6235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006236 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6237 return -1;
6238
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006239 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6240 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006241 if (res)
6242 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006243 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006246 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006247 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006248}
6249
6250
6251void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6252{
6253 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006254 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006255 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006256 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006257 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006258 u8 ip[3 * 4];
6259 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006260
Dmitry Shmidt04949592012-07-19 12:16:46 -07006261 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6262 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6263 wpa_s->parent, NULL);
6264 }
6265
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006266 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006267 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268
6269 wpa_s->show_group_started = 0;
6270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006271 os_memset(go_dev_addr, 0, ETH_ALEN);
6272 if (ssid->bssid_set)
6273 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6274 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6275 ssid->ssid_len);
6276 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6277
6278 if (wpa_s->global->p2p_group_formation == wpa_s)
6279 wpa_s->global->p2p_group_formation = NULL;
6280
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006281 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6282 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006283
6284 ip_addr[0] = '\0';
6285 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006286 int res;
6287
6288 res = os_snprintf(ip_addr, sizeof(ip_addr),
6289 " ip_addr=%u.%u.%u.%u "
6290 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6291 ip[0], ip[1], ip[2], ip[3],
6292 ip[4], ip[5], ip[6], ip[7],
6293 ip[8], ip[9], ip[10], ip[11]);
6294 if (os_snprintf_error(sizeof(ip_addr), res))
6295 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006296 }
6297
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006298 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6299 ssid->passphrase == NULL && ssid->psk_set ?
6300 ssid->psk : NULL,
6301 ssid->passphrase, go_dev_addr, persistent,
6302 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303
6304 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006305 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6306 ssid, go_dev_addr);
6307 if (network_id < 0)
6308 network_id = ssid->id;
6309 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006310}
6311
6312
6313int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6314 u32 interval1, u32 duration2, u32 interval2)
6315{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006316 int ret;
6317
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006318 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6319 return -1;
6320
6321 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6322 wpa_s->current_ssid == NULL ||
6323 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6324 return -1;
6325
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006326 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6327 wpa_s->own_addr, wpa_s->assoc_freq,
6328 duration1, interval1, duration2, interval2);
6329 if (ret == 0)
6330 wpa_s->waiting_presence_resp = 1;
6331
6332 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006333}
6334
6335
6336int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6337 unsigned int interval)
6338{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006339 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6340 return -1;
6341
6342 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6343}
6344
6345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006346static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6347{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006348 if (wpa_s->current_ssid == NULL) {
6349 /*
6350 * current_ssid can be cleared when P2P client interface gets
6351 * disconnected, so assume this interface was used as P2P
6352 * client.
6353 */
6354 return 1;
6355 }
6356 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006357 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6358}
6359
6360
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006361static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6362{
6363 struct wpa_supplicant *wpa_s = eloop_ctx;
6364
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006365 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006366 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6367 "disabled");
6368 return;
6369 }
6370
Dmitry Shmidt04949592012-07-19 12:16:46 -07006371 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6372 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006373 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006374}
6375
6376
6377static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6378{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006379 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006380
Dmitry Shmidt04949592012-07-19 12:16:46 -07006381 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6382 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006384 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6385 return;
6386
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006387 timeout = wpa_s->conf->p2p_group_idle;
6388 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6389 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6390 timeout = P2P_MAX_CLIENT_IDLE;
6391
6392 if (timeout == 0)
6393 return;
6394
Dmitry Shmidt04949592012-07-19 12:16:46 -07006395 if (timeout < 0) {
6396 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6397 timeout = 0; /* special client mode no-timeout */
6398 else
6399 return;
6400 }
6401
6402 if (wpa_s->p2p_in_provisioning) {
6403 /*
6404 * Use the normal group formation timeout during the
6405 * provisioning phase to avoid terminating this process too
6406 * early due to group idle timeout.
6407 */
6408 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6409 "during provisioning");
6410 return;
6411 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306412
Dmitry Shmidt04949592012-07-19 12:16:46 -07006413 if (wpa_s->show_group_started) {
6414 /*
6415 * Use the normal group formation timeout between the end of
6416 * the provisioning phase and completion of 4-way handshake to
6417 * avoid terminating this process too early due to group idle
6418 * timeout.
6419 */
6420 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6421 "while waiting for initial 4-way handshake to "
6422 "complete");
6423 return;
6424 }
6425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006426 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006427 timeout);
6428 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6429 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006430}
6431
6432
Jouni Malinen2b89da82012-08-31 22:04:41 +03006433/* Returns 1 if the interface was removed */
6434int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6435 u16 reason_code, const u8 *ie, size_t ie_len,
6436 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437{
6438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006439 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006440
Dmitry Shmidt04949592012-07-19 12:16:46 -07006441 if (!locally_generated)
6442 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6443 ie_len);
6444
6445 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6446 wpa_s->current_ssid &&
6447 wpa_s->current_ssid->p2p_group &&
6448 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6449 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6450 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006451 if (wpas_p2p_group_delete(wpa_s,
6452 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6453 > 0)
6454 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006455 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006456
6457 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006458}
6459
6460
6461void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006462 u16 reason_code, const u8 *ie, size_t ie_len,
6463 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464{
6465 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6466 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006467
Dmitry Shmidt04949592012-07-19 12:16:46 -07006468 if (!locally_generated)
6469 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6470 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006471}
6472
6473
6474void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6475{
6476 struct p2p_data *p2p = wpa_s->global->p2p;
6477
6478 if (p2p == NULL)
6479 return;
6480
6481 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6482 return;
6483
6484 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6485 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6486
6487 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6488 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6489
6490 if (wpa_s->wps &&
6491 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6492 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6493
6494 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6495 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6496
6497 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6498 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6499 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6500 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6501 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6502 }
6503
6504 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6505 p2p_set_sec_dev_types(p2p,
6506 (void *) wpa_s->conf->sec_device_type,
6507 wpa_s->conf->num_sec_device_types);
6508
6509 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6510 int i;
6511 p2p_remove_wps_vendor_extensions(p2p);
6512 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6513 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6514 continue;
6515 p2p_add_wps_vendor_extension(
6516 p2p, wpa_s->conf->wps_vendor_ext[i]);
6517 }
6518 }
6519
6520 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6521 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6522 char country[3];
6523 country[0] = wpa_s->conf->country[0];
6524 country[1] = wpa_s->conf->country[1];
6525 country[2] = 0x04;
6526 p2p_set_country(p2p, country);
6527 }
6528
6529 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6530 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6531 wpa_s->conf->p2p_ssid_postfix ?
6532 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6533 0);
6534 }
6535
6536 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6537 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006538
6539 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6540 u8 reg_class, channel;
6541 int ret;
6542 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006543 u8 channel_forced;
6544
Jouni Malinen75ecf522011-06-27 15:19:46 -07006545 if (wpa_s->conf->p2p_listen_reg_class &&
6546 wpa_s->conf->p2p_listen_channel) {
6547 reg_class = wpa_s->conf->p2p_listen_reg_class;
6548 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006549 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006550 } else {
6551 reg_class = 81;
6552 /*
6553 * Pick one of the social channels randomly as the
6554 * listen channel.
6555 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006556 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6557 channel = 1;
6558 else
6559 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006560 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006561 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006562 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6563 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006564 if (ret)
6565 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6566 "failed: %d", ret);
6567 }
6568 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6569 u8 op_reg_class, op_channel, cfg_op_channel;
6570 int ret = 0;
6571 unsigned int r;
6572 if (wpa_s->conf->p2p_oper_reg_class &&
6573 wpa_s->conf->p2p_oper_channel) {
6574 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6575 op_channel = wpa_s->conf->p2p_oper_channel;
6576 cfg_op_channel = 1;
6577 } else {
6578 op_reg_class = 81;
6579 /*
6580 * Use random operation channel from (1, 6, 11)
6581 *if no other preference is indicated.
6582 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006583 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6584 op_channel = 1;
6585 else
6586 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006587 cfg_op_channel = 0;
6588 }
6589 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6590 cfg_op_channel);
6591 if (ret)
6592 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6593 "failed: %d", ret);
6594 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006595
6596 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6597 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6598 wpa_s->conf->p2p_pref_chan) < 0) {
6599 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6600 "update failed");
6601 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006602
6603 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6604 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6605 "update failed");
6606 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006607 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006608
6609 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6610 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006611}
6612
6613
6614int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6615 int duration)
6616{
6617 if (!wpa_s->ap_iface)
6618 return -1;
6619 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6620 duration);
6621}
6622
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006623
6624int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6625{
6626 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6627 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006628
6629 wpa_s->global->cross_connection = enabled;
6630 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6631
6632 if (!enabled) {
6633 struct wpa_supplicant *iface;
6634
6635 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6636 {
6637 if (iface->cross_connect_enabled == 0)
6638 continue;
6639
6640 iface->cross_connect_enabled = 0;
6641 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006642 wpa_msg_global(iface->parent, MSG_INFO,
6643 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6644 iface->ifname,
6645 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006646 }
6647 }
6648
6649 return 0;
6650}
6651
6652
6653static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6654{
6655 struct wpa_supplicant *iface;
6656
6657 if (!uplink->global->cross_connection)
6658 return;
6659
6660 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6661 if (!iface->cross_connect_enabled)
6662 continue;
6663 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6664 0)
6665 continue;
6666 if (iface->ap_iface == NULL)
6667 continue;
6668 if (iface->cross_connect_in_use)
6669 continue;
6670
6671 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006672 wpa_msg_global(iface->parent, MSG_INFO,
6673 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6674 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006675 }
6676}
6677
6678
6679static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6680{
6681 struct wpa_supplicant *iface;
6682
6683 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6684 if (!iface->cross_connect_enabled)
6685 continue;
6686 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6687 0)
6688 continue;
6689 if (!iface->cross_connect_in_use)
6690 continue;
6691
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006692 wpa_msg_global(iface->parent, MSG_INFO,
6693 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6694 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006695 iface->cross_connect_in_use = 0;
6696 }
6697}
6698
6699
6700void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6701{
6702 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6703 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6704 wpa_s->cross_connect_disallowed)
6705 wpas_p2p_disable_cross_connect(wpa_s);
6706 else
6707 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006708 if (!wpa_s->ap_iface &&
6709 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6710 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711}
6712
6713
6714void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6715{
6716 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006717 if (!wpa_s->ap_iface &&
6718 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6719 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006720 wpas_p2p_set_group_idle_timeout(wpa_s);
6721}
6722
6723
6724static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6725{
6726 struct wpa_supplicant *iface;
6727
6728 if (!wpa_s->global->cross_connection)
6729 return;
6730
6731 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6732 if (iface == wpa_s)
6733 continue;
6734 if (iface->drv_flags &
6735 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6736 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006737 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6738 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006739 continue;
6740
6741 wpa_s->cross_connect_enabled = 1;
6742 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6743 sizeof(wpa_s->cross_connect_uplink));
6744 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6745 "%s to %s whenever uplink is available",
6746 wpa_s->ifname, wpa_s->cross_connect_uplink);
6747
6748 if (iface->ap_iface || iface->current_ssid == NULL ||
6749 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6750 iface->cross_connect_disallowed ||
6751 iface->wpa_state != WPA_COMPLETED)
6752 break;
6753
6754 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006755 wpa_msg_global(wpa_s->parent, MSG_INFO,
6756 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6757 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006758 break;
6759 }
6760}
6761
6762
6763int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6764{
6765 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6766 !wpa_s->p2p_in_provisioning)
6767 return 0; /* not P2P client operation */
6768
6769 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6770 "session overlap");
6771 if (wpa_s != wpa_s->parent)
6772 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006773 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006774 return 1;
6775}
6776
6777
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006778void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6779{
6780 struct wpa_supplicant *wpa_s = eloop_ctx;
6781 wpas_p2p_notif_pbc_overlap(wpa_s);
6782}
6783
6784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006785void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6786{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006787 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006788 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006789
6790 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6791 return;
6792
6793 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006794 os_memset(&cli_chan, 0, sizeof(cli_chan));
6795 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006796 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6797 "channel list");
6798 return;
6799 }
6800
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006801 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006802
6803 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6804 int freq;
6805 if (!ifs->current_ssid ||
6806 !ifs->current_ssid->p2p_group ||
6807 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6808 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6809 continue;
6810 freq = ifs->current_ssid->frequency;
6811 if (freq_included(&chan, freq)) {
6812 wpa_dbg(ifs, MSG_DEBUG,
6813 "P2P GO operating frequency %d MHz in valid range",
6814 freq);
6815 continue;
6816 }
6817
6818 wpa_dbg(ifs, MSG_DEBUG,
6819 "P2P GO operating in invalid frequency %d MHz", freq);
6820 /* TODO: Consider using CSA or removing the group within
6821 * wpa_supplicant */
6822 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006824}
6825
6826
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006827static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6828 struct wpa_scan_results *scan_res)
6829{
6830 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6831}
6832
6833
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006834int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6835{
6836 struct wpa_global *global = wpa_s->global;
6837 int found = 0;
6838 const u8 *peer;
6839
6840 if (global->p2p == NULL)
6841 return -1;
6842
6843 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6844
6845 if (wpa_s->pending_interface_name[0] &&
6846 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6847 found = 1;
6848
6849 peer = p2p_get_go_neg_peer(global->p2p);
6850 if (peer) {
6851 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6852 MACSTR, MAC2STR(peer));
6853 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006854 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 }
6856
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006857 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6858 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6859 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6860 found = 1;
6861 }
6862
6863 if (wpa_s->pending_pd_before_join) {
6864 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6865 wpa_s->pending_pd_before_join = 0;
6866 found = 1;
6867 }
6868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006869 wpas_p2p_stop_find(wpa_s);
6870
6871 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6872 if (wpa_s == global->p2p_group_formation &&
6873 (wpa_s->p2p_in_provisioning ||
6874 wpa_s->parent->pending_interface_type ==
6875 WPA_IF_P2P_CLIENT)) {
6876 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6877 "formation found - cancelling",
6878 wpa_s->ifname);
6879 found = 1;
6880 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6881 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006882 if (wpa_s->p2p_in_provisioning) {
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006883 wpas_group_formation_completed(wpa_s, 0, 0);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006884 break;
6885 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006886 wpas_p2p_group_delete(wpa_s,
6887 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006888 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006889 } else if (wpa_s->p2p_in_invitation) {
6890 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6891 wpa_s->ifname);
6892 found = 1;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07006893 wpas_p2p_group_formation_failed(wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006894 }
6895 }
6896
6897 if (!found) {
6898 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6899 return -1;
6900 }
6901
6902 return 0;
6903}
6904
6905
6906void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6907{
6908 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6909 return;
6910
6911 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6912 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006913 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006914}
6915
6916
6917void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6918 int freq_24, int freq_5, int freq_overall)
6919{
6920 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006921 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006922 return;
6923 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6924}
6925
6926
6927int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6928{
6929 u8 peer[ETH_ALEN];
6930 struct p2p_data *p2p = wpa_s->global->p2p;
6931
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006932 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933 return -1;
6934
6935 if (hwaddr_aton(addr, peer))
6936 return -1;
6937
6938 return p2p_unauthorize(p2p, peer);
6939}
6940
6941
6942/**
6943 * wpas_p2p_disconnect - Disconnect from a P2P Group
6944 * @wpa_s: Pointer to wpa_supplicant data
6945 * Returns: 0 on success, -1 on failure
6946 *
6947 * This can be used to disconnect from a group in which the local end is a P2P
6948 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6949 * virtual network interface was created for this group, that interface will be
6950 * removed. Otherwise, only the configured P2P group network will be removed
6951 * from the interface.
6952 */
6953int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6954{
6955
6956 if (wpa_s == NULL)
6957 return -1;
6958
Jouni Malinen2b89da82012-08-31 22:04:41 +03006959 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6960 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006962
6963
6964int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6965{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006966 int ret;
6967
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006968 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6969 return 0;
6970
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006971 ret = p2p_in_progress(wpa_s->global->p2p);
6972 if (ret == 0) {
6973 /*
6974 * Check whether there is an ongoing WPS provisioning step (or
6975 * other parts of group formation) on another interface since
6976 * p2p_in_progress() does not report this to avoid issues for
6977 * scans during such provisioning step.
6978 */
6979 if (wpa_s->global->p2p_group_formation &&
6980 wpa_s->global->p2p_group_formation != wpa_s) {
6981 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6982 "in group formation",
6983 wpa_s->global->p2p_group_formation->ifname);
6984 ret = 1;
6985 }
6986 }
6987
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006988 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006989 struct os_reltime now;
6990 os_get_reltime(&now);
6991 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6992 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006993 /* Wait for the first client has expired */
6994 wpa_s->global->p2p_go_wait_client.sec = 0;
6995 } else {
6996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6997 ret = 1;
6998 }
6999 }
7000
Dmitry Shmidtf8623282013-02-20 14:34:59 -08007001 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007002}
7003
7004
7005void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7006 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007007{
7008 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7009 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7010 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07007011 /**
7012 * Remove the network by scheduling the group formation
7013 * timeout to happen immediately. The teardown code
7014 * needs to be scheduled to run asynch later so that we
7015 * don't delete data from under ourselves unexpectedly.
7016 * Calling wpas_p2p_group_formation_timeout directly
7017 * causes a series of crashes in WPS failure scenarios.
7018 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007019 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7020 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07007021 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7022 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007023 }
7024}
7025
7026
7027struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007028 const u8 *addr, const u8 *ssid,
7029 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007030{
7031 struct wpa_ssid *s;
7032 size_t i;
7033
7034 for (s = wpa_s->conf->ssid; s; s = s->next) {
7035 if (s->disabled != 2)
7036 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007037 if (ssid &&
7038 (ssid_len != s->ssid_len ||
7039 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7040 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007041 if (addr == NULL) {
7042 if (s->mode == WPAS_MODE_P2P_GO)
7043 return s;
7044 continue;
7045 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007046 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7047 return s; /* peer is GO in the persistent group */
7048 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7049 continue;
7050 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08007051 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007052 addr, ETH_ALEN) == 0)
7053 return s; /* peer is P2P client in persistent
7054 * group */
7055 }
7056 }
7057
7058 return NULL;
7059}
7060
7061
7062void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7063 const u8 *addr)
7064{
Dmitry Shmidt56052862013-10-04 10:23:25 -07007065 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7066 wpa_s->parent, NULL) > 0) {
7067 /*
7068 * This can happen if WPS provisioning step is not terminated
7069 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7070 * peer was able to connect, there is no need to time out group
7071 * formation after this, though. In addition, this is used with
7072 * the initial connection wait on the GO as a separate formation
7073 * timeout and as such, expected to be hit after the initial WPS
7074 * provisioning step.
7075 */
7076 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007077
7078 if (!wpa_s->p2p_go_group_formation_completed &&
7079 !wpa_s->group_formation_reported) {
7080 /*
7081 * GO has not yet notified group formation success since
7082 * the WPS step was not completed cleanly. Do that
7083 * notification now since the P2P Client was able to
7084 * connect and as such, must have received the
7085 * credential from the WPS step.
7086 */
7087 if (wpa_s->global->p2p)
7088 p2p_wps_success_cb(wpa_s->global->p2p, addr);
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007089 wpas_group_formation_completed(wpa_s, 1, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007090 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07007091 }
7092 if (!wpa_s->p2p_go_group_formation_completed) {
7093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7094 wpa_s->p2p_go_group_formation_completed = 1;
7095 wpa_s->global->p2p_group_formation = NULL;
7096 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007097 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07007098 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07007099 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007100 if (addr == NULL)
7101 return;
7102 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7103}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007104
Dmitry Shmidt04949592012-07-19 12:16:46 -07007105
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007106static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7107 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07007108{
7109 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007110 int ret = 0;
7111
Dmitry Shmidt04949592012-07-19 12:16:46 -07007112 if (wpa_s->global->p2p_group_formation)
7113 group = wpa_s->global->p2p_group_formation;
Dmitry Shmidta3dc3092015-06-23 11:21:28 -07007114 wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007115 offchannel_send_action_done(wpa_s);
7116 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007117 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007118 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7119 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7120 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7121 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7122 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007123 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007124 wpa_s->p2p_go_ht40,
7125 wpa_s->p2p_go_vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007126 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007127}
7128
7129
7130int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7131{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007132 int res;
7133
Dmitry Shmidt04949592012-07-19 12:16:46 -07007134 if (!wpa_s->p2p_fallback_to_go_neg ||
7135 wpa_s->p2p_in_provisioning <= 5)
7136 return 0;
7137
7138 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7139 return 0; /* peer operating as a GO */
7140
7141 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7142 "fallback to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007143 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
7144 "reason=GO-not-found");
7145 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007146
Dmitry Shmidt7f656022015-02-25 14:36:37 -08007147 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007148}
7149
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007150
7151unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7152{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007153 struct wpa_supplicant *ifs;
7154
7155 if (wpa_s->wpa_state > WPA_SCANNING) {
7156 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7157 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007158 wpa_s->conf->p2p_search_delay);
7159 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007160 }
7161
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08007162 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7163 radio_list) {
7164 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007165 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7166 "delay due to concurrent operation on "
7167 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07007168 wpa_s->conf->p2p_search_delay,
7169 ifs->ifname);
7170 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007171 }
7172 }
7173
7174 return 0;
7175}
7176
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07007177
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007178static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7179 struct wpa_ssid *s, const u8 *addr,
7180 int iface_addr)
7181{
7182 struct psk_list_entry *psk, *tmp;
7183 int changed = 0;
7184
7185 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7186 list) {
7187 if ((iface_addr && !psk->p2p &&
7188 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7189 (!iface_addr && psk->p2p &&
7190 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7191 wpa_dbg(wpa_s, MSG_DEBUG,
7192 "P2P: Remove persistent group PSK list entry for "
7193 MACSTR " p2p=%u",
7194 MAC2STR(psk->addr), psk->p2p);
7195 dl_list_del(&psk->list);
7196 os_free(psk);
7197 changed++;
7198 }
7199 }
7200
7201 return changed;
7202}
7203
7204
7205void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7206 const u8 *p2p_dev_addr,
7207 const u8 *psk, size_t psk_len)
7208{
7209 struct wpa_ssid *ssid = wpa_s->current_ssid;
7210 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007211 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007212
7213 if (psk_len != sizeof(p->psk))
7214 return;
7215
7216 if (p2p_dev_addr) {
7217 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7218 " p2p_dev_addr=" MACSTR,
7219 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7220 if (is_zero_ether_addr(p2p_dev_addr))
7221 p2p_dev_addr = NULL;
7222 } else {
7223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7224 MAC2STR(mac_addr));
7225 }
7226
7227 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7228 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7229 /* To be added to persistent group once created */
7230 if (wpa_s->global->add_psk == NULL) {
7231 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7232 if (wpa_s->global->add_psk == NULL)
7233 return;
7234 }
7235 p = wpa_s->global->add_psk;
7236 if (p2p_dev_addr) {
7237 p->p2p = 1;
7238 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7239 } else {
7240 p->p2p = 0;
7241 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7242 }
7243 os_memcpy(p->psk, psk, psk_len);
7244 return;
7245 }
7246
7247 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7248 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7249 return;
7250 }
7251
7252 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7253 ssid->ssid_len);
7254 if (!persistent) {
7255 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7256 return;
7257 }
7258
7259 p = os_zalloc(sizeof(*p));
7260 if (p == NULL)
7261 return;
7262 if (p2p_dev_addr) {
7263 p->p2p = 1;
7264 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7265 } else {
7266 p->p2p = 0;
7267 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7268 }
7269 os_memcpy(p->psk, psk, psk_len);
7270
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007271 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7272 (last = dl_list_last(&persistent->psk_list,
7273 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007274 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7275 MACSTR " (p2p=%u) to make room for a new one",
7276 MAC2STR(last->addr), last->p2p);
7277 dl_list_del(&last->list);
7278 os_free(last);
7279 }
7280
7281 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7282 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7283 p2p_dev_addr == NULL);
7284 if (p2p_dev_addr) {
7285 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7286 MACSTR, MAC2STR(p2p_dev_addr));
7287 } else {
7288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7289 MAC2STR(mac_addr));
7290 }
7291 dl_list_add(&persistent->psk_list, &p->list);
7292
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007293 if (wpa_s->parent->conf->update_config &&
7294 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7295 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007296}
7297
7298
7299static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7300 struct wpa_ssid *s, const u8 *addr,
7301 int iface_addr)
7302{
7303 int res;
7304
7305 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007306 if (res > 0 && wpa_s->conf->update_config &&
7307 wpa_config_write(wpa_s->confname, wpa_s->conf))
7308 wpa_dbg(wpa_s, MSG_DEBUG,
7309 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007310}
7311
7312
7313static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7314 const u8 *peer, int iface_addr)
7315{
7316 struct hostapd_data *hapd;
7317 struct hostapd_wpa_psk *psk, *prev, *rem;
7318 struct sta_info *sta;
7319
7320 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7321 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7322 return;
7323
7324 /* Remove per-station PSK entry */
7325 hapd = wpa_s->ap_iface->bss[0];
7326 prev = NULL;
7327 psk = hapd->conf->ssid.wpa_psk;
7328 while (psk) {
7329 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7330 (!iface_addr &&
7331 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7332 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7333 MACSTR " iface_addr=%d",
7334 MAC2STR(peer), iface_addr);
7335 if (prev)
7336 prev->next = psk->next;
7337 else
7338 hapd->conf->ssid.wpa_psk = psk->next;
7339 rem = psk;
7340 psk = psk->next;
7341 os_free(rem);
7342 } else {
7343 prev = psk;
7344 psk = psk->next;
7345 }
7346 }
7347
7348 /* Disconnect from group */
7349 if (iface_addr)
7350 sta = ap_get_sta(hapd, peer);
7351 else
7352 sta = ap_get_sta_p2p(hapd, peer);
7353 if (sta) {
7354 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7355 " (iface_addr=%d) from group",
7356 MAC2STR(peer), iface_addr);
7357 hostapd_drv_sta_deauth(hapd, sta->addr,
7358 WLAN_REASON_DEAUTH_LEAVING);
7359 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7360 }
7361}
7362
7363
7364void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7365 int iface_addr)
7366{
7367 struct wpa_ssid *s;
7368 struct wpa_supplicant *w;
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007369 struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007370
7371 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7372
7373 /* Remove from any persistent group */
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007374 for (s = p2p_wpa_s->conf->ssid; s; s = s->next) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007375 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7376 continue;
7377 if (!iface_addr)
Dmitry Shmidt8bd70b72015-05-26 16:02:19 -07007378 wpas_remove_persistent_peer(p2p_wpa_s, s, peer, 0);
7379 wpas_p2p_remove_psk(p2p_wpa_s, s, peer, iface_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007380 }
7381
7382 /* Remove from any operating group */
7383 for (w = wpa_s->global->ifaces; w; w = w->next)
7384 wpas_p2p_remove_client_go(w, peer, iface_addr);
7385}
7386
7387
7388static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7389{
7390 struct wpa_supplicant *wpa_s = eloop_ctx;
7391 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7392}
7393
7394
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007395static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7396{
7397 struct wpa_supplicant *wpa_s = eloop_ctx;
7398
7399 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7400 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7401}
7402
7403
7404int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7405 struct wpa_ssid *ssid)
7406{
7407 struct wpa_supplicant *iface;
7408
7409 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7410 if (!iface->current_ssid ||
7411 iface->current_ssid->frequency == freq ||
7412 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7413 !iface->current_ssid->p2p_group))
7414 continue;
7415
7416 /* Remove the connection with least priority */
7417 if (!wpas_is_p2p_prioritized(iface)) {
7418 /* STA connection has priority over existing
7419 * P2P connection, so remove the interface. */
7420 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7421 eloop_register_timeout(0, 0,
7422 wpas_p2p_group_freq_conflict,
7423 iface, NULL);
7424 /* If connection in progress is P2P connection, do not
7425 * proceed for the connection. */
7426 if (wpa_s == iface)
7427 return -1;
7428 else
7429 return 0;
7430 } else {
7431 /* P2P connection has priority, disable the STA network
7432 */
7433 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7434 ssid);
7435 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7436 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7437 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7438 ETH_ALEN);
7439 /* If P2P connection is in progress, continue
7440 * connecting...*/
7441 if (wpa_s == iface)
7442 return 0;
7443 else
7444 return -1;
7445 }
7446 }
7447
7448 return 0;
7449}
7450
7451
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007452int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7453{
7454 struct wpa_ssid *ssid = wpa_s->current_ssid;
7455
7456 if (ssid == NULL || !ssid->p2p_group)
7457 return 0;
7458
7459 if (wpa_s->p2p_last_4way_hs_fail &&
7460 wpa_s->p2p_last_4way_hs_fail == ssid) {
7461 u8 go_dev_addr[ETH_ALEN];
7462 struct wpa_ssid *persistent;
7463
7464 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7465 ssid->ssid,
7466 ssid->ssid_len) <= 0) {
7467 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7468 goto disconnect;
7469 }
7470
7471 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7472 MACSTR, MAC2STR(go_dev_addr));
7473 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7474 ssid->ssid,
7475 ssid->ssid_len);
7476 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7477 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7478 goto disconnect;
7479 }
7480 wpa_msg_global(wpa_s->parent, MSG_INFO,
7481 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7482 persistent->id);
7483 disconnect:
7484 wpa_s->p2p_last_4way_hs_fail = NULL;
7485 /*
7486 * Remove the group from a timeout to avoid issues with caller
7487 * continuing to use the interface if this is on a P2P group
7488 * interface.
7489 */
7490 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7491 wpa_s, NULL);
7492 return 1;
7493 }
7494
7495 wpa_s->p2p_last_4way_hs_fail = ssid;
7496 return 0;
7497}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007498
7499
7500#ifdef CONFIG_WPS_NFC
7501
7502static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7503 struct wpabuf *p2p)
7504{
7505 struct wpabuf *ret;
7506 size_t wsc_len;
7507
7508 if (p2p == NULL) {
7509 wpabuf_free(wsc);
7510 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7511 return NULL;
7512 }
7513
7514 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7515 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7516 if (ret == NULL) {
7517 wpabuf_free(wsc);
7518 wpabuf_free(p2p);
7519 return NULL;
7520 }
7521
7522 wpabuf_put_be16(ret, wsc_len);
7523 if (wsc)
7524 wpabuf_put_buf(ret, wsc);
7525 wpabuf_put_be16(ret, wpabuf_len(p2p));
7526 wpabuf_put_buf(ret, p2p);
7527
7528 wpabuf_free(wsc);
7529 wpabuf_free(p2p);
7530 wpa_hexdump_buf(MSG_DEBUG,
7531 "P2P: Generated NFC connection handover message", ret);
7532
7533 if (ndef && ret) {
7534 struct wpabuf *tmp;
7535 tmp = ndef_build_p2p(ret);
7536 wpabuf_free(ret);
7537 if (tmp == NULL) {
7538 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7539 return NULL;
7540 }
7541 ret = tmp;
7542 }
7543
7544 return ret;
7545}
7546
7547
7548static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7549 struct wpa_ssid **ssid, u8 *go_dev_addr)
7550{
7551 struct wpa_supplicant *iface;
7552
7553 if (go_dev_addr)
7554 os_memset(go_dev_addr, 0, ETH_ALEN);
7555 if (ssid)
7556 *ssid = NULL;
7557 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7558 if (iface->wpa_state < WPA_ASSOCIATING ||
7559 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7560 !iface->current_ssid->p2p_group ||
7561 iface->current_ssid->mode != WPAS_MODE_INFRA)
7562 continue;
7563 if (ssid)
7564 *ssid = iface->current_ssid;
7565 if (go_dev_addr)
7566 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7567 return iface->assoc_freq;
7568 }
7569 return 0;
7570}
7571
7572
7573struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7574 int ndef)
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 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7583 return NULL;
7584 }
7585
7586 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7587 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7588 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7589 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7590 return NULL;
7591 }
7592
7593 if (cli_freq == 0) {
7594 wsc = wps_build_nfc_handover_req_p2p(
7595 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7596 } else
7597 wsc = NULL;
7598 p2p = p2p_build_nfc_handover_req(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
7606struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7607 int ndef, int tag)
7608{
7609 struct wpabuf *wsc, *p2p;
7610 struct wpa_ssid *ssid;
7611 u8 go_dev_addr[ETH_ALEN];
7612 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7613
7614 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7615 return NULL;
7616
7617 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7618 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7619 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7620 return NULL;
7621
7622 if (cli_freq == 0) {
7623 wsc = wps_build_nfc_handover_sel_p2p(
7624 wpa_s->parent->wps,
7625 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7626 DEV_PW_NFC_CONNECTION_HANDOVER,
7627 wpa_s->conf->wps_nfc_dh_pubkey,
7628 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7629 } else
7630 wsc = NULL;
7631 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7632 go_dev_addr, ssid ? ssid->ssid : NULL,
7633 ssid ? ssid->ssid_len : 0);
7634
7635 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7636}
7637
7638
7639static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7640 struct p2p_nfc_params *params)
7641{
7642 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7643 "connection handover (freq=%d)",
7644 params->go_freq);
7645
7646 if (params->go_freq && params->go_ssid_len) {
7647 wpa_s->p2p_wps_method = WPS_NFC;
7648 wpa_s->pending_join_wps_method = WPS_NFC;
7649 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7650 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7651 ETH_ALEN);
7652 return wpas_p2p_join_start(wpa_s, params->go_freq,
7653 params->go_ssid,
7654 params->go_ssid_len);
7655 }
7656
7657 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7658 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7659 params->go_freq, -1, 0, 1, 1);
7660}
7661
7662
7663static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7664 struct p2p_nfc_params *params, int tag)
7665{
7666 int res, persistent;
7667 struct wpa_ssid *ssid;
7668
7669 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7670 "connection handover");
7671 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7672 ssid = wpa_s->current_ssid;
7673 if (ssid == NULL)
7674 continue;
7675 if (ssid->mode != WPAS_MODE_P2P_GO)
7676 continue;
7677 if (wpa_s->ap_iface == NULL)
7678 continue;
7679 break;
7680 }
7681 if (wpa_s == NULL) {
7682 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7683 return -1;
7684 }
7685
7686 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7687 DEV_PW_NFC_CONNECTION_HANDOVER &&
7688 !wpa_s->parent->p2p_oob_dev_pw) {
7689 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7690 return -1;
7691 }
7692 res = wpas_ap_wps_add_nfc_pw(
7693 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7694 wpa_s->parent->p2p_oob_dev_pw,
7695 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7696 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7697 if (res)
7698 return res;
7699
7700 if (!tag) {
7701 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7702 return 0;
7703 }
7704
7705 if (!params->peer ||
7706 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7707 return 0;
7708
7709 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7710 " to join", MAC2STR(params->peer->p2p_device_addr));
7711
7712 wpa_s->global->p2p_invite_group = wpa_s;
7713 persistent = ssid->p2p_persistent_group &&
7714 wpas_p2p_get_persistent(wpa_s->parent,
7715 params->peer->p2p_device_addr,
7716 ssid->ssid, ssid->ssid_len);
7717 wpa_s->parent->pending_invite_ssid_id = -1;
7718
7719 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7720 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7721 ssid->ssid, ssid->ssid_len, ssid->frequency,
7722 wpa_s->global->p2p_dev_addr, persistent, 0,
7723 wpa_s->parent->p2p_oob_dev_pw_id);
7724}
7725
7726
7727static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7728 struct p2p_nfc_params *params,
7729 int forced_freq)
7730{
7731 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7732 "connection handover");
7733 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7734 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7735 forced_freq, -1, 0, 1, 1);
7736}
7737
7738
7739static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7740 struct p2p_nfc_params *params,
7741 int forced_freq)
7742{
7743 int res;
7744
7745 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7746 "connection handover");
7747 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7748 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7749 forced_freq, -1, 0, 1, 1);
7750 if (res)
7751 return res;
7752
7753 res = wpas_p2p_listen(wpa_s, 60);
7754 if (res) {
7755 p2p_unauthorize(wpa_s->global->p2p,
7756 params->peer->p2p_device_addr);
7757 }
7758
7759 return res;
7760}
7761
7762
7763static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7764 const struct wpabuf *data,
7765 int sel, int tag, int forced_freq)
7766{
7767 const u8 *pos, *end;
7768 u16 len, id;
7769 struct p2p_nfc_params params;
7770 int res;
7771
7772 os_memset(&params, 0, sizeof(params));
7773 params.sel = sel;
7774
7775 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7776
7777 pos = wpabuf_head(data);
7778 end = pos + wpabuf_len(data);
7779
7780 if (end - pos < 2) {
7781 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7782 "attributes");
7783 return -1;
7784 }
7785 len = WPA_GET_BE16(pos);
7786 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007787 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007788 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7789 "attributes");
7790 return -1;
7791 }
7792 params.wsc_attr = pos;
7793 params.wsc_len = len;
7794 pos += len;
7795
7796 if (end - pos < 2) {
7797 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7798 "attributes");
7799 return -1;
7800 }
7801 len = WPA_GET_BE16(pos);
7802 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007803 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007804 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7805 "attributes");
7806 return -1;
7807 }
7808 params.p2p_attr = pos;
7809 params.p2p_len = len;
7810 pos += len;
7811
7812 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7813 params.wsc_attr, params.wsc_len);
7814 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7815 params.p2p_attr, params.p2p_len);
7816 if (pos < end) {
7817 wpa_hexdump(MSG_DEBUG,
7818 "P2P: Ignored extra data after P2P attributes",
7819 pos, end - pos);
7820 }
7821
7822 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7823 if (res)
7824 return res;
7825
7826 if (params.next_step == NO_ACTION)
7827 return 0;
7828
7829 if (params.next_step == BOTH_GO) {
7830 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7831 MAC2STR(params.peer->p2p_device_addr));
7832 return 0;
7833 }
7834
7835 if (params.next_step == PEER_CLIENT) {
7836 if (!is_zero_ether_addr(params.go_dev_addr)) {
7837 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7838 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7839 " ssid=\"%s\"",
7840 MAC2STR(params.peer->p2p_device_addr),
7841 params.go_freq,
7842 MAC2STR(params.go_dev_addr),
7843 wpa_ssid_txt(params.go_ssid,
7844 params.go_ssid_len));
7845 } else {
7846 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7847 "peer=" MACSTR " freq=%d",
7848 MAC2STR(params.peer->p2p_device_addr),
7849 params.go_freq);
7850 }
7851 return 0;
7852 }
7853
7854 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7855 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7856 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7857 return 0;
7858 }
7859
7860 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7861 wpa_s->p2p_oob_dev_pw = NULL;
7862
7863 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7864 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7865 "received");
7866 return -1;
7867 }
7868
7869 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7870 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7871 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7872 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7873 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7874 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7875 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7876
7877 if (tag) {
7878 if (id < 0x10) {
7879 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7880 "peer OOB Device Password Id %u", id);
7881 return -1;
7882 }
7883 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7884 "Device Password Id %u", id);
7885 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7886 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7887 params.oob_dev_pw_len -
7888 WPS_OOB_PUBKEY_HASH_LEN - 2);
7889 wpa_s->p2p_oob_dev_pw_id = id;
7890 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7891 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7892 params.oob_dev_pw_len -
7893 WPS_OOB_PUBKEY_HASH_LEN - 2);
7894 if (wpa_s->p2p_oob_dev_pw == NULL)
7895 return -1;
7896
7897 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7898 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7899 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7900 return -1;
7901 } else {
7902 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7903 "without Device Password");
7904 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7905 }
7906
7907 switch (params.next_step) {
7908 case NO_ACTION:
7909 case BOTH_GO:
7910 case PEER_CLIENT:
7911 /* already covered above */
7912 return 0;
7913 case JOIN_GROUP:
7914 return wpas_p2p_nfc_join_group(wpa_s, &params);
7915 case AUTH_JOIN:
7916 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7917 case INIT_GO_NEG:
7918 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7919 case RESP_GO_NEG:
7920 /* TODO: use own OOB Dev Pw */
7921 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7922 }
7923
7924 return -1;
7925}
7926
7927
7928int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7929 const struct wpabuf *data, int forced_freq)
7930{
7931 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7932 return -1;
7933
7934 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7935}
7936
7937
7938int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7939 const struct wpabuf *req,
7940 const struct wpabuf *sel, int forced_freq)
7941{
7942 struct wpabuf *tmp;
7943 int ret;
7944
7945 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7946 return -1;
7947
7948 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7949
7950 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7951 wpabuf_head(req), wpabuf_len(req));
7952 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7953 wpabuf_head(sel), wpabuf_len(sel));
7954 if (forced_freq)
7955 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7956 tmp = ndef_parse_p2p(init ? sel : req);
7957 if (tmp == NULL) {
7958 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7959 return -1;
7960 }
7961
7962 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7963 forced_freq);
7964 wpabuf_free(tmp);
7965
7966 return ret;
7967}
7968
7969
7970int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7971{
7972 const u8 *if_addr;
7973 int go_intent = wpa_s->conf->p2p_go_intent;
7974 struct wpa_supplicant *iface;
7975
7976 if (wpa_s->global->p2p == NULL)
7977 return -1;
7978
7979 if (!enabled) {
7980 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7981 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7982 {
7983 if (!iface->ap_iface)
7984 continue;
7985 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7986 }
7987 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7988 0, NULL);
7989 if (wpa_s->p2p_nfc_tag_enabled)
7990 wpas_p2p_remove_pending_group_interface(wpa_s);
7991 wpa_s->p2p_nfc_tag_enabled = 0;
7992 return 0;
7993 }
7994
7995 if (wpa_s->global->p2p_disabled)
7996 return -1;
7997
7998 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7999 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8000 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8001 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8002 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8003 "to allow static handover cases");
8004 return -1;
8005 }
8006
8007 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8008
8009 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8010 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8011 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8012 if (wpa_s->p2p_oob_dev_pw == NULL)
8013 return -1;
8014 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8015
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07008016 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8017 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8018 /*
8019 * P2P Group Interface present and the command came on group
8020 * interface, so enable the token for the current interface.
8021 */
8022 wpa_s->create_p2p_iface = 0;
8023 } else {
8024 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8025 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008026
8027 if (wpa_s->create_p2p_iface) {
8028 enum wpa_driver_if_type iftype;
8029 /* Prepare to add a new interface for the group */
8030 iftype = WPA_IF_P2P_GROUP;
8031 if (go_intent == 15)
8032 iftype = WPA_IF_P2P_GO;
8033 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8034 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8035 "interface for the group");
8036 return -1;
8037 }
8038
8039 if_addr = wpa_s->pending_interface_addr;
8040 } else
8041 if_addr = wpa_s->own_addr;
8042
8043 wpa_s->p2p_nfc_tag_enabled = enabled;
8044
8045 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8046 struct hostapd_data *hapd;
8047 if (iface->ap_iface == NULL)
8048 continue;
8049 hapd = iface->ap_iface->bss[0];
8050 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8051 hapd->conf->wps_nfc_dh_pubkey =
8052 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8053 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8054 hapd->conf->wps_nfc_dh_privkey =
8055 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8056 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8057 hapd->conf->wps_nfc_dev_pw =
8058 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8059 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8060
8061 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8062 wpa_dbg(iface, MSG_DEBUG,
8063 "P2P: Failed to enable NFC Tag for GO");
8064 }
8065 }
8066 p2p_set_authorized_oob_dev_pw_id(
8067 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8068 if_addr);
8069
8070 return 0;
8071}
8072
8073#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008074
8075
8076static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8077 struct wpa_used_freq_data *freqs,
8078 unsigned int num)
8079{
8080 u8 curr_chan, cand, chan;
8081 unsigned int i;
8082
8083 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8084 for (i = 0, cand = 0; i < num; i++) {
8085 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8086 if (curr_chan == chan) {
8087 cand = 0;
8088 break;
8089 }
8090
8091 if (chan == 1 || chan == 6 || chan == 11)
8092 cand = chan;
8093 }
8094
8095 if (cand) {
8096 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08008097 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008098 cand);
8099 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8100 }
8101}
8102
8103
8104void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8105{
8106 struct wpa_used_freq_data *freqs;
8107 unsigned int num = wpa_s->num_multichan_concurrent;
8108
8109 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8110 return;
8111
8112 /*
8113 * If possible, optimize the Listen channel to be a channel that is
8114 * already used by one of the other interfaces.
8115 */
8116 if (!wpa_s->conf->p2p_optimize_listen_chan)
8117 return;
8118
8119 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8120 return;
8121
8122 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8123 if (!freqs)
8124 return;
8125
8126 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8127
8128 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8129 os_free(freqs);
8130}
8131
8132
8133void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8134{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07008135 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8136 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8137 "the management interface is being removed");
8138 wpas_p2p_deinit_global(wpa_s->global);
8139 }
8140}
8141
8142
8143void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8144{
8145 if (wpa_s->ap_iface->bss)
8146 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8147 wpas_p2p_group_deinit(wpa_s);
8148}