blob: 6b85efecfb9945817a4885652433030dbc538ae5 [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"
Jouni Malinen75ecf522011-06-27 15:19:46 -070025#include "eapol_supp/eapol_supp_sm.h"
26#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ap.h"
30#include "config_ssid.h"
31#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "notify.h"
33#include "scan.h"
34#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps_supplicant.h"
37#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080038#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
40
41/*
42 * How many times to try to scan to find the GO before giving up on join
43 * request.
44 */
45#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46
Dmitry Shmidt04949592012-07-19 12:16:46 -070047#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049#ifndef P2P_MAX_CLIENT_IDLE
50/*
51 * How many seconds to try to reconnect to the GO when connection in P2P client
52 * role has been lost.
53 */
54#define P2P_MAX_CLIENT_IDLE 10
55#endif /* P2P_MAX_CLIENT_IDLE */
56
Dmitry Shmidt04949592012-07-19 12:16:46 -070057#ifndef P2P_MAX_INITIAL_CONN_WAIT
58/*
59 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070060 * WPS provisioning step or after the re-invocation of a persistent group on a
61 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070062 */
63#define P2P_MAX_INITIAL_CONN_WAIT 10
64#endif /* P2P_MAX_INITIAL_CONN_WAIT */
65
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070066#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67/*
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
72 */
73#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75
Dmitry Shmidt56052862013-10-04 10:23:25 -070076#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77/*
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
81 */
82#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84
Dmitry Shmidt34af3062013-07-11 10:46:32 -070085#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080095 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097};
98
Jouni Malinendc7b7132012-09-14 12:53:47 -070099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101static struct wpa_supplicant *
102wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
103 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800104static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800106static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800117static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
118 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800119static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700120static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
121 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800122static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800123static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700124static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700125static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126
127
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700128/*
129 * Get the number of concurrent channels that the HW can operate, but that are
130 * currently not in use by any of the wpa_supplicant interfaces.
131 */
132static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
133{
134 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800135 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700136
137 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
138 if (!freqs)
139 return -1;
140
141 num = get_shared_radio_freqs(wpa_s, freqs,
142 wpa_s->num_multichan_concurrent);
143 os_free(freqs);
144
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800145 unused = wpa_s->num_multichan_concurrent - num;
146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
147 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700155static unsigned int
156wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 struct wpa_used_freq_data *p2p_freqs,
158 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700159{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700160 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700161 unsigned int num, i, j;
162
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700163 freqs = os_calloc(wpa_s->num_multichan_concurrent,
164 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700165 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700166 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700168 num = get_shared_radio_freqs_data(wpa_s, freqs,
169 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700171 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700172
173 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700174 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175 p2p_freqs[j++] = freqs[i];
176 }
177
178 os_free(freqs);
179
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700180 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800181
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700182 return j;
183}
184
185
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700186static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
187 int freq)
188{
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
190 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800191 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
192 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
193 wpas_p2p_num_unused_channels(wpa_s) > 0) {
194 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
195 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700196 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800197 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700198 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
199}
200
201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
203 struct wpa_scan_results *scan_res)
204{
205 size_t i;
206
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800207 if (wpa_s->p2p_scan_work) {
208 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
209 wpa_s->p2p_scan_work = NULL;
210 radio_work_done(work);
211 }
212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
214 return;
215
216 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
217 (int) scan_res->num);
218
219 for (i = 0; i < scan_res->num; i++) {
220 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800221 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700222 const u8 *ies;
223 size_t ies_len;
224
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800225 time_tmp_age.sec = bss->age / 1000;
226 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800227 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700228
229 ies = (const u8 *) (bss + 1);
230 ies_len = bss->ie_len;
231 if (bss->beacon_ie_len > 0 &&
232 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
233 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
234 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
235 MACSTR, MAC2STR(bss->bssid));
236 ies = ies + ies_len;
237 ies_len = bss->beacon_ie_len;
238 }
239
240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800242 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700243 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 break;
245 }
246
247 p2p_scan_res_handled(wpa_s->global->p2p);
248}
249
250
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
252{
253 struct wpa_supplicant *wpa_s = work->wpa_s;
254 struct wpa_driver_scan_params *params = work->ctx;
255 int ret;
256
257 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800258 if (!work->started) {
259 wpa_scan_free_params(params);
260 return;
261 }
262
263 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 return;
265 }
266
267 ret = wpa_drv_scan(wpa_s, params);
268 wpa_scan_free_params(params);
269 work->ctx = NULL;
270 if (ret) {
271 radio_work_done(work);
272 return;
273 }
274
275 os_get_reltime(&wpa_s->scan_trigger_time);
276 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
277 wpa_s->own_scan_requested = 1;
278 wpa_s->p2p_scan_work = work;
279}
280
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
283 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285{
286 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800287 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800290 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
293 return -1;
294
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800295 if (wpa_s->p2p_scan_work) {
296 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
297 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700298 }
299
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800300 params = os_zalloc(sizeof(*params));
301 if (params == NULL)
302 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
304 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800305 params->num_ssids = 1;
306 n = os_malloc(P2P_WILDCARD_SSID_LEN);
307 if (n == NULL)
308 goto fail;
309 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
310 params->ssids[0].ssid = n;
311 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312
313 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700314 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
315 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 num_req_dev_types, req_dev_types);
317 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800318 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
321 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 if (ies == NULL) {
323 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800324 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 }
326 wpabuf_put_buf(ies, wps_ie);
327 wpabuf_free(wps_ie);
328
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800329 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 params->p2p_probe = 1;
332 n = os_malloc(wpabuf_len(ies));
333 if (n == NULL) {
334 wpabuf_free(ies);
335 goto fail;
336 }
337 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
338 params->extra_ies = n;
339 params->extra_ies_len = wpabuf_len(ies);
340 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341
342 switch (type) {
343 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 params->freqs = os_malloc(4 * sizeof(int));
345 if (params->freqs == NULL)
346 goto fail;
347 params->freqs[0] = 2412;
348 params->freqs[1] = 2437;
349 params->freqs[2] = 2462;
350 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 break;
352 case P2P_SCAN_FULL:
353 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 params->freqs = os_malloc(5 * sizeof(int));
356 if (params->freqs == NULL)
357 goto fail;
358 params->freqs[0] = 2412;
359 params->freqs[1] = 2437;
360 params->freqs[2] = 2462;
361 params->freqs[3] = freq;
362 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363 break;
364 }
365
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800366 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800367 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
368 params) < 0)
369 goto fail;
370 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800372fail:
373 wpa_scan_free_params(params);
374 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375}
376
377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
379{
380 switch (p2p_group_interface) {
381 case P2P_GROUP_INTERFACE_PENDING:
382 return WPA_IF_P2P_GROUP;
383 case P2P_GROUP_INTERFACE_GO:
384 return WPA_IF_P2P_GO;
385 case P2P_GROUP_INTERFACE_CLIENT:
386 return WPA_IF_P2P_CLIENT;
387 }
388
389 return WPA_IF_P2P_GROUP;
390}
391
392
393static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
394 const u8 *ssid,
395 size_t ssid_len, int *go)
396{
397 struct wpa_ssid *s;
398
399 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
400 for (s = wpa_s->conf->ssid; s; s = s->next) {
401 if (s->disabled != 0 || !s->p2p_group ||
402 s->ssid_len != ssid_len ||
403 os_memcmp(ssid, s->ssid, ssid_len) != 0)
404 continue;
405 if (s->mode == WPAS_MODE_P2P_GO &&
406 s != wpa_s->current_ssid)
407 continue;
408 if (go)
409 *go = s->mode == WPAS_MODE_P2P_GO;
410 return wpa_s;
411 }
412 }
413
414 return NULL;
415}
416
417
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700418static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
419 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420{
421 struct wpa_ssid *ssid;
422 char *gtype;
423 const char *reason;
424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 ssid = wpa_s->current_ssid;
426 if (ssid == NULL) {
427 /*
428 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700429 * pending P2P group interface waiting for provisioning or a
430 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431 */
432 ssid = wpa_s->conf->ssid;
433 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700434 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 break;
436 ssid = ssid->next;
437 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300438 if (ssid == NULL &&
439 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
440 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
442 "not found");
443 return -1;
444 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 }
446 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
447 gtype = "GO";
448 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
449 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
450 wpa_s->reassociate = 0;
451 wpa_s->disconnected = 1;
452 wpa_supplicant_deauthenticate(wpa_s,
453 WLAN_REASON_DEAUTH_LEAVING);
454 gtype = "client";
455 } else
456 gtype = "GO";
457 if (wpa_s->cross_connect_in_use) {
458 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700459 wpa_msg_global(wpa_s->parent, MSG_INFO,
460 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
461 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700463 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 case P2P_GROUP_REMOVAL_REQUESTED:
465 reason = " reason=REQUESTED";
466 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700467 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
468 reason = " reason=FORMATION_FAILED";
469 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
471 reason = " reason=IDLE";
472 break;
473 case P2P_GROUP_REMOVAL_UNAVAILABLE:
474 reason = " reason=UNAVAILABLE";
475 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700476 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
477 reason = " reason=GO_ENDING_SESSION";
478 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700479 case P2P_GROUP_REMOVAL_PSK_FAILURE:
480 reason = " reason=PSK_FAILURE";
481 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800482 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
483 reason = " reason=FREQ_CONFLICT";
484 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 default:
486 reason = "";
487 break;
488 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700489 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700490 wpa_msg_global(wpa_s->parent, MSG_INFO,
491 P2P_EVENT_GROUP_REMOVED "%s %s%s",
492 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800495 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700497 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
498 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800499 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700500 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800501 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
502 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700503 wpa_s->p2p_in_provisioning = 0;
504 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700505
Dmitry Shmidt15907092014-03-25 10:42:57 -0700506 wpa_s->p2p_in_invitation = 0;
507
Dmitry Shmidt96571392013-10-14 12:54:46 -0700508 /*
509 * Make sure wait for the first client does not remain active after the
510 * group has been removed.
511 */
512 wpa_s->global->p2p_go_wait_client.sec = 0;
513
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700514 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
516
517 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
518 struct wpa_global *global;
519 char *ifname;
520 enum wpa_driver_if_type type;
521 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
522 wpa_s->ifname);
523 global = wpa_s->global;
524 ifname = os_strdup(wpa_s->ifname);
525 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700526 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 wpa_s = global->ifaces;
528 if (wpa_s && ifname)
529 wpa_drv_if_remove(wpa_s, type, ifname);
530 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300531 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 }
533
Dmitry Shmidt96571392013-10-14 12:54:46 -0700534 if (!wpa_s->p2p_go_group_formation_completed) {
535 wpa_s->global->p2p_group_formation = NULL;
536 wpa_s->p2p_in_provisioning = 0;
537 }
538
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800539 wpa_s->show_group_started = 0;
540 os_free(wpa_s->go_params);
541 wpa_s->go_params = NULL;
542
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800543 wpa_s->waiting_presence_resp = 0;
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
546 if (ssid && (ssid->p2p_group ||
547 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
548 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
549 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700550 if (ssid == wpa_s->current_ssid) {
551 wpa_sm_set_config(wpa_s->wpa, NULL);
552 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700554 }
555 /*
556 * Networks objects created during any P2P activities are not
557 * exposed out as they might/will confuse certain non-P2P aware
558 * applications since these network objects won't behave like
559 * regular ones.
560 *
561 * Likewise, we don't send out network removed signals for such
562 * network objects.
563 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 wpa_config_remove_network(wpa_s->conf, id);
565 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800566 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 } else {
568 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
569 "found");
570 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700571 if (wpa_s->ap_iface)
572 wpa_supplicant_ap_deinit(wpa_s);
573 else
574 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700575
576 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577}
578
579
580static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
581 u8 *go_dev_addr,
582 const u8 *ssid, size_t ssid_len)
583{
584 struct wpa_bss *bss;
585 const u8 *bssid;
586 struct wpabuf *p2p;
587 u8 group_capab;
588 const u8 *addr;
589
590 if (wpa_s->go_params)
591 bssid = wpa_s->go_params->peer_interface_addr;
592 else
593 bssid = wpa_s->bssid;
594
595 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800596 if (bss == NULL && wpa_s->go_params &&
597 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
598 bss = wpa_bss_get_p2p_dev_addr(
599 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 if (bss == NULL) {
601 u8 iface_addr[ETH_ALEN];
602 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
603 iface_addr) == 0)
604 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
605 }
606 if (bss == NULL) {
607 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
608 "group is persistent - BSS " MACSTR " not found",
609 MAC2STR(bssid));
610 return 0;
611 }
612
613 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700614 if (p2p == NULL)
615 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
616 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 if (p2p == NULL) {
618 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
619 "group is persistent - BSS " MACSTR
620 " did not include P2P IE", MAC2STR(bssid));
621 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
622 (u8 *) (bss + 1), bss->ie_len);
623 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
624 ((u8 *) bss + 1) + bss->ie_len,
625 bss->beacon_ie_len);
626 return 0;
627 }
628
629 group_capab = p2p_get_group_capab(p2p);
630 addr = p2p_get_go_dev_addr(p2p);
631 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
632 "group_capab=0x%x", group_capab);
633 if (addr) {
634 os_memcpy(go_dev_addr, addr, ETH_ALEN);
635 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
636 MAC2STR(addr));
637 } else
638 os_memset(go_dev_addr, 0, ETH_ALEN);
639 wpabuf_free(p2p);
640
641 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
642 "go_dev_addr=" MACSTR,
643 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
644
645 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
646}
647
648
Jouni Malinen75ecf522011-06-27 15:19:46 -0700649static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
650 struct wpa_ssid *ssid,
651 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652{
653 struct wpa_ssid *s;
654 int changed = 0;
655
656 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
657 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
658 for (s = wpa_s->conf->ssid; s; s = s->next) {
659 if (s->disabled == 2 &&
660 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
661 s->ssid_len == ssid->ssid_len &&
662 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
663 break;
664 }
665
666 if (s) {
667 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
668 "entry");
669 if (ssid->passphrase && !s->passphrase)
670 changed = 1;
671 else if (ssid->passphrase && s->passphrase &&
672 os_strcmp(ssid->passphrase, s->passphrase) != 0)
673 changed = 1;
674 } else {
675 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
676 "entry");
677 changed = 1;
678 s = wpa_config_add_network(wpa_s->conf);
679 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700680 return -1;
681
682 /*
683 * Instead of network_added we emit persistent_group_added
684 * notification. Also to keep the defense checks in
685 * persistent_group obj registration method, we set the
686 * relevant flags in s to designate it as a persistent group.
687 */
688 s->p2p_group = 1;
689 s->p2p_persistent_group = 1;
690 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 wpa_config_set_network_defaults(s);
692 }
693
694 s->p2p_group = 1;
695 s->p2p_persistent_group = 1;
696 s->disabled = 2;
697 s->bssid_set = 1;
698 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
699 s->mode = ssid->mode;
700 s->auth_alg = WPA_AUTH_ALG_OPEN;
701 s->key_mgmt = WPA_KEY_MGMT_PSK;
702 s->proto = WPA_PROTO_RSN;
703 s->pairwise_cipher = WPA_CIPHER_CCMP;
704 s->export_keys = 1;
705 if (ssid->passphrase) {
706 os_free(s->passphrase);
707 s->passphrase = os_strdup(ssid->passphrase);
708 }
709 if (ssid->psk_set) {
710 s->psk_set = 1;
711 os_memcpy(s->psk, ssid->psk, 32);
712 }
713 if (s->passphrase && !s->psk_set)
714 wpa_config_update_psk(s);
715 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
716 os_free(s->ssid);
717 s->ssid = os_malloc(ssid->ssid_len);
718 }
719 if (s->ssid) {
720 s->ssid_len = ssid->ssid_len;
721 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
722 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700723 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
724 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
725 wpa_s->global->add_psk = NULL;
726 changed = 1;
727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 if (changed && wpa_s->conf->update_config &&
730 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
731 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
732 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700733
734 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735}
736
737
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800738static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
739 const u8 *addr)
740{
741 struct wpa_ssid *ssid, *s;
742 u8 *n;
743 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700744 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800745
746 ssid = wpa_s->current_ssid;
747 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
748 !ssid->p2p_persistent_group)
749 return;
750
751 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
752 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
753 continue;
754
755 if (s->ssid_len == ssid->ssid_len &&
756 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
757 break;
758 }
759
760 if (s == NULL)
761 return;
762
763 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
764 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700765 ETH_ALEN) != 0)
766 continue;
767
768 if (i == s->num_p2p_clients - 1)
769 return; /* already the most recent entry */
770
771 /* move the entry to mark it most recent */
772 os_memmove(s->p2p_client_list + i * ETH_ALEN,
773 s->p2p_client_list + (i + 1) * ETH_ALEN,
774 (s->num_p2p_clients - i - 1) * ETH_ALEN);
775 os_memcpy(s->p2p_client_list +
776 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
777 found = 1;
778 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779 }
780
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
782 n = os_realloc_array(s->p2p_client_list,
783 s->num_p2p_clients + 1, ETH_ALEN);
784 if (n == NULL)
785 return;
786 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
787 s->p2p_client_list = n;
788 s->num_p2p_clients++;
789 } else if (!found) {
790 /* Not enough room for an additional entry - drop the oldest
791 * entry */
792 os_memmove(s->p2p_client_list,
793 s->p2p_client_list + ETH_ALEN,
794 (s->num_p2p_clients - 1) * ETH_ALEN);
795 os_memcpy(s->p2p_client_list +
796 (s->num_p2p_clients - 1) * ETH_ALEN,
797 addr, ETH_ALEN);
798 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800800 if (wpa_s->parent->conf->update_config &&
801 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
802 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803}
804
805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
807 int success)
808{
809 struct wpa_ssid *ssid;
810 const char *ssid_txt;
811 int client;
812 int persistent;
813 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700814 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815
816 /*
817 * This callback is likely called for the main interface. Update wpa_s
818 * to use the group interface if a new interface was created for the
819 * group.
820 */
821 if (wpa_s->global->p2p_group_formation)
822 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700823 if (wpa_s->p2p_go_group_formation_completed) {
824 wpa_s->global->p2p_group_formation = NULL;
825 wpa_s->p2p_in_provisioning = 0;
826 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700827 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828
829 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 wpa_msg_global(wpa_s->parent, MSG_INFO,
831 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700832 wpas_p2p_group_delete(wpa_s,
833 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 return;
835 }
836
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700837 wpa_msg_global(wpa_s->parent, MSG_INFO,
838 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839
840 ssid = wpa_s->current_ssid;
841 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
842 ssid->mode = WPAS_MODE_P2P_GO;
843 p2p_group_notif_formation_done(wpa_s->p2p_group);
844 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
845 }
846
847 persistent = 0;
848 if (ssid) {
849 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
850 client = ssid->mode == WPAS_MODE_INFRA;
851 if (ssid->mode == WPAS_MODE_P2P_GO) {
852 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700853 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
854 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 } else
856 persistent = wpas_p2p_persistent_group(wpa_s,
857 go_dev_addr,
858 ssid->ssid,
859 ssid->ssid_len);
860 } else {
861 ssid_txt = "";
862 client = wpa_s->p2p_group_interface ==
863 P2P_GROUP_INTERFACE_CLIENT;
864 os_memset(go_dev_addr, 0, ETH_ALEN);
865 }
866
867 wpa_s->show_group_started = 0;
868 if (client) {
869 /*
870 * Indicate event only after successfully completed 4-way
871 * handshake, i.e., when the interface is ready for data
872 * packets.
873 */
874 wpa_s->show_group_started = 1;
875 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
876 char psk[65];
877 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700878 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
879 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
880 MACSTR "%s",
881 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
882 MAC2STR(go_dev_addr),
883 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 wpas_p2p_cross_connect_setup(wpa_s);
885 wpas_p2p_set_group_idle_timeout(wpa_s);
886 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700887 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
888 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
889 "go_dev_addr=" MACSTR "%s",
890 wpa_s->ifname, ssid_txt,
891 ssid ? ssid->frequency : 0,
892 ssid && ssid->passphrase ? ssid->passphrase : "",
893 MAC2STR(go_dev_addr),
894 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 wpas_p2p_cross_connect_setup(wpa_s);
896 wpas_p2p_set_group_idle_timeout(wpa_s);
897 }
898
899 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700900 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
901 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700902 else {
903 os_free(wpa_s->global->add_psk);
904 wpa_s->global->add_psk = NULL;
905 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800906 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700907 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700908 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700909 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800910 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912}
913
914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800915struct send_action_work {
916 unsigned int freq;
917 u8 dst[ETH_ALEN];
918 u8 src[ETH_ALEN];
919 u8 bssid[ETH_ALEN];
920 size_t len;
921 unsigned int wait_time;
922 u8 buf[0];
923};
924
925
926static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
927 void *timeout_ctx)
928{
929 struct wpa_supplicant *wpa_s = eloop_ctx;
930
931 if (!wpa_s->p2p_send_action_work)
932 return;
933
934 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
935 os_free(wpa_s->p2p_send_action_work->ctx);
936 radio_work_done(wpa_s->p2p_send_action_work);
937 wpa_s->p2p_send_action_work = NULL;
938}
939
940
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800941static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
942 unsigned int freq,
943 const u8 *dst, const u8 *src,
944 const u8 *bssid,
945 const u8 *data, size_t data_len,
946 enum offchannel_send_action_result
947 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800949 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800951 if (wpa_s->p2p_send_action_work) {
952 struct send_action_work *awork;
953 awork = wpa_s->p2p_send_action_work->ctx;
954 if (awork->wait_time == 0) {
955 os_free(awork);
956 radio_work_done(wpa_s->p2p_send_action_work);
957 wpa_s->p2p_send_action_work = NULL;
958 } else {
959 /*
960 * In theory, this should not be needed, but number of
961 * places in the P2P code is still using non-zero wait
962 * time for the last Action frame in the sequence and
963 * some of these do not call send_action_done().
964 */
965 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
966 wpa_s, NULL);
967 eloop_register_timeout(
968 0, awork->wait_time * 1000,
969 wpas_p2p_send_action_work_timeout,
970 wpa_s, NULL);
971 }
972 }
973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
975 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 switch (result) {
978 case OFFCHANNEL_SEND_ACTION_SUCCESS:
979 res = P2P_SEND_ACTION_SUCCESS;
980 break;
981 case OFFCHANNEL_SEND_ACTION_NO_ACK:
982 res = P2P_SEND_ACTION_NO_ACK;
983 break;
984 case OFFCHANNEL_SEND_ACTION_FAILED:
985 res = P2P_SEND_ACTION_FAILED;
986 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 }
988
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800989 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800991 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
992 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800994 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
995 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800997 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
998 "during p2p_connect-auto");
999 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1000 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 }
1002}
1003
1004
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001005static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1006{
1007 struct wpa_supplicant *wpa_s = work->wpa_s;
1008 struct send_action_work *awork = work->ctx;
1009
1010 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001011 if (work->started) {
1012 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1013 wpa_s, NULL);
1014 wpa_s->p2p_send_action_work = NULL;
1015 offchannel_send_action_done(wpa_s);
1016 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001017 os_free(awork);
1018 return;
1019 }
1020
1021 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1022 awork->bssid, awork->buf, awork->len,
1023 awork->wait_time,
1024 wpas_p2p_send_action_tx_status, 1) < 0) {
1025 os_free(awork);
1026 radio_work_done(work);
1027 return;
1028 }
1029 wpa_s->p2p_send_action_work = work;
1030}
1031
1032
1033static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1034 unsigned int freq, const u8 *dst,
1035 const u8 *src, const u8 *bssid, const u8 *buf,
1036 size_t len, unsigned int wait_time)
1037{
1038 struct send_action_work *awork;
1039
1040 if (wpa_s->p2p_send_action_work) {
1041 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1042 return -1;
1043 }
1044
1045 awork = os_zalloc(sizeof(*awork) + len);
1046 if (awork == NULL)
1047 return -1;
1048
1049 awork->freq = freq;
1050 os_memcpy(awork->dst, dst, ETH_ALEN);
1051 os_memcpy(awork->src, src, ETH_ALEN);
1052 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1053 awork->len = len;
1054 awork->wait_time = wait_time;
1055 os_memcpy(awork->buf, buf, len);
1056
1057 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1058 wpas_send_action_cb, awork) < 0) {
1059 os_free(awork);
1060 return -1;
1061 }
1062
1063 return 0;
1064}
1065
1066
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1068 const u8 *src, const u8 *bssid, const u8 *buf,
1069 size_t len, unsigned int wait_time)
1070{
1071 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001072 int listen_freq = -1, send_freq = -1;
1073
1074 if (wpa_s->p2p_listen_work)
1075 listen_freq = wpa_s->p2p_listen_work->freq;
1076 if (wpa_s->p2p_send_action_work)
1077 send_freq = wpa_s->p2p_send_action_work->freq;
1078 if (listen_freq != (int) freq && send_freq != (int) freq) {
1079 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1080 listen_freq, send_freq);
1081 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1082 len, wait_time);
1083 }
1084
1085 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1087 wait_time,
1088 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089}
1090
1091
1092static void wpas_send_action_done(void *ctx)
1093{
1094 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001095
1096 if (wpa_s->p2p_send_action_work) {
1097 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1098 wpa_s, NULL);
1099 os_free(wpa_s->p2p_send_action_work->ctx);
1100 radio_work_done(wpa_s->p2p_send_action_work);
1101 wpa_s->p2p_send_action_work = NULL;
1102 }
1103
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001104 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105}
1106
1107
1108static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1109 struct p2p_go_neg_results *params)
1110{
1111 if (wpa_s->go_params == NULL) {
1112 wpa_s->go_params = os_malloc(sizeof(*params));
1113 if (wpa_s->go_params == NULL)
1114 return -1;
1115 }
1116 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1117 return 0;
1118}
1119
1120
1121static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1122 struct p2p_go_neg_results *res)
1123{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001124 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1125 " dev_addr " MACSTR " wps_method %d",
1126 MAC2STR(res->peer_interface_addr),
1127 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1129 res->ssid, res->ssid_len);
1130 wpa_supplicant_ap_deinit(wpa_s);
1131 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001132 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001134#ifdef CONFIG_WPS_NFC
1135 } else if (res->wps_method == WPS_NFC) {
1136 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1137 res->peer_interface_addr,
1138 wpa_s->parent->p2p_oob_dev_pw,
1139 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1140 wpa_s->parent->p2p_oob_dev_pw_id ==
1141 DEV_PW_NFC_CONNECTION_HANDOVER ?
1142 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1143 NULL,
1144 NULL, 0, 0);
1145#endif /* CONFIG_WPS_NFC */
1146 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 u16 dev_pw_id = DEV_PW_DEFAULT;
1148 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1149 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1150 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1151 wpa_s->p2p_pin, 1, dev_pw_id);
1152 }
1153}
1154
1155
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001156static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1157 struct wpa_ssid *ssid)
1158{
1159 struct wpa_ssid *persistent;
1160 struct psk_list_entry *psk;
1161 struct hostapd_data *hapd;
1162
1163 if (!wpa_s->ap_iface)
1164 return;
1165
1166 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1167 ssid->ssid_len);
1168 if (persistent == NULL)
1169 return;
1170
1171 hapd = wpa_s->ap_iface->bss[0];
1172
1173 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1174 list) {
1175 struct hostapd_wpa_psk *hpsk;
1176
1177 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1178 MACSTR " psk=%d",
1179 MAC2STR(psk->addr), psk->p2p);
1180 hpsk = os_zalloc(sizeof(*hpsk));
1181 if (hpsk == NULL)
1182 break;
1183 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1184 if (psk->p2p)
1185 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1186 else
1187 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1188 hpsk->next = hapd->conf->ssid.wpa_psk;
1189 hapd->conf->ssid.wpa_psk = hpsk;
1190 }
1191}
1192
1193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194static void p2p_go_configured(void *ctx, void *data)
1195{
1196 struct wpa_supplicant *wpa_s = ctx;
1197 struct p2p_go_neg_results *params = data;
1198 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001199 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
1201 ssid = wpa_s->current_ssid;
1202 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1203 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1204 if (wpa_s->global->p2p_group_formation == wpa_s)
1205 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001206 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001207 wpa_msg_global(wpa_s->parent, MSG_INFO,
1208 P2P_EVENT_GROUP_STARTED
1209 "%s GO ssid=\"%s\" freq=%d "
1210 "passphrase=\"%s\" go_dev_addr=" MACSTR
1211 "%s", wpa_s->ifname,
1212 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1213 ssid->frequency, params->passphrase,
1214 MAC2STR(wpa_s->global->p2p_dev_addr),
1215 params->persistent_group ?
1216 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001217 } else {
1218 char psk[65];
1219 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1220 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001221 wpa_msg_global(wpa_s->parent, MSG_INFO,
1222 P2P_EVENT_GROUP_STARTED
1223 "%s GO ssid=\"%s\" freq=%d psk=%s "
1224 "go_dev_addr=" MACSTR "%s",
1225 wpa_s->ifname,
1226 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1227 ssid->frequency, psk,
1228 MAC2STR(wpa_s->global->p2p_dev_addr),
1229 params->persistent_group ?
1230 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001231 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001232
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001233 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001234 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001235 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001236 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001237 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001238 wpas_p2p_add_psk_list(wpa_s, ssid);
1239 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001240 if (network_id < 0)
1241 network_id = ssid->id;
1242 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 wpas_p2p_cross_connect_setup(wpa_s);
1244 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001245
1246 if (wpa_s->p2p_first_connection_timeout) {
1247 wpa_dbg(wpa_s, MSG_DEBUG,
1248 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1249 wpa_s->p2p_first_connection_timeout);
1250 wpa_s->p2p_go_group_formation_completed = 0;
1251 wpa_s->global->p2p_group_formation = wpa_s;
1252 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1253 wpa_s->parent, NULL);
1254 eloop_register_timeout(
1255 wpa_s->p2p_first_connection_timeout, 0,
1256 wpas_p2p_group_formation_timeout,
1257 wpa_s->parent, NULL);
1258 }
1259
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001260 return;
1261 }
1262
1263 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1264 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1265 params->peer_interface_addr)) {
1266 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1267 "filtering");
1268 return;
1269 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001270 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001272 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001273#ifdef CONFIG_WPS_NFC
1274 } else if (params->wps_method == WPS_NFC) {
1275 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1276 DEV_PW_NFC_CONNECTION_HANDOVER &&
1277 !wpa_s->parent->p2p_oob_dev_pw) {
1278 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1279 return;
1280 }
1281 wpas_ap_wps_add_nfc_pw(
1282 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1283 wpa_s->parent->p2p_oob_dev_pw,
1284 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1285 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1286#endif /* CONFIG_WPS_NFC */
1287 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001289 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001290 os_free(wpa_s->go_params);
1291 wpa_s->go_params = NULL;
1292}
1293
1294
1295static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1296 struct p2p_go_neg_results *params,
1297 int group_formation)
1298{
1299 struct wpa_ssid *ssid;
1300
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001301 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1302 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1303 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1304 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307
1308 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001309 if (ssid == NULL) {
1310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001314 wpa_s->show_group_started = 0;
1315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 wpa_config_set_network_defaults(ssid);
1317 ssid->temporary = 1;
1318 ssid->p2p_group = 1;
1319 ssid->p2p_persistent_group = params->persistent_group;
1320 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1321 WPAS_MODE_P2P_GO;
1322 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001323 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001324 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 ssid->ssid = os_zalloc(params->ssid_len + 1);
1326 if (ssid->ssid) {
1327 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1328 ssid->ssid_len = params->ssid_len;
1329 }
1330 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1331 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1332 ssid->proto = WPA_PROTO_RSN;
1333 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001334 if (os_strlen(params->passphrase) > 0) {
1335 ssid->passphrase = os_strdup(params->passphrase);
1336 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001337 wpa_msg_global(wpa_s, MSG_ERROR,
1338 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001339 wpa_config_remove_network(wpa_s->conf, ssid->id);
1340 return;
1341 }
1342 } else
1343 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001344 ssid->psk_set = params->psk_set;
1345 if (ssid->psk_set)
1346 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001347 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001348 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001349 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350
1351 wpa_s->ap_configured_cb = p2p_go_configured;
1352 wpa_s->ap_configured_cb_ctx = wpa_s;
1353 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001354 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001355 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001356 wpa_s->reassociate = 1;
1357 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001358 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1359 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 wpa_supplicant_req_scan(wpa_s, 0, 0);
1361}
1362
1363
1364static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1365 const struct wpa_supplicant *src)
1366{
1367 struct wpa_config *d;
1368 const struct wpa_config *s;
1369
1370 d = dst->conf;
1371 s = src->conf;
1372
1373#define C(n) if (s->n) d->n = os_strdup(s->n)
1374 C(device_name);
1375 C(manufacturer);
1376 C(model_name);
1377 C(model_number);
1378 C(serial_number);
1379 C(config_methods);
1380#undef C
1381
1382 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1383 os_memcpy(d->sec_device_type, s->sec_device_type,
1384 sizeof(d->sec_device_type));
1385 d->num_sec_device_types = s->num_sec_device_types;
1386
1387 d->p2p_group_idle = s->p2p_group_idle;
1388 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001389 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001390 d->max_num_sta = s->max_num_sta;
1391 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001392 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001393 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001394 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001395 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001396 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001397
1398 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1399 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1400 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1401 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402}
1403
1404
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001405static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1406 char *ifname, size_t len)
1407{
1408 char *ifname_ptr = wpa_s->ifname;
1409
1410 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1411 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1412 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1413 }
1414
1415 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1416 if (os_strlen(ifname) >= IFNAMSIZ &&
1417 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1418 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001419 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001420 }
1421}
1422
1423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001424static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1425 enum wpa_driver_if_type type)
1426{
1427 char ifname[120], force_ifname[120];
1428
1429 if (wpa_s->pending_interface_name[0]) {
1430 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1431 "- skip creation of a new one");
1432 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1433 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1434 "unknown?! ifname='%s'",
1435 wpa_s->pending_interface_name);
1436 return -1;
1437 }
1438 return 0;
1439 }
1440
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001441 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 force_ifname[0] = '\0';
1443
1444 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1445 ifname);
1446 wpa_s->p2p_group_idx++;
1447
1448 wpa_s->pending_interface_type = type;
1449 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1450 wpa_s->pending_interface_addr, NULL) < 0) {
1451 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1452 "interface");
1453 return -1;
1454 }
1455
1456 if (force_ifname[0]) {
1457 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1458 force_ifname);
1459 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1460 sizeof(wpa_s->pending_interface_name));
1461 } else
1462 os_strlcpy(wpa_s->pending_interface_name, ifname,
1463 sizeof(wpa_s->pending_interface_name));
1464 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1465 MACSTR, wpa_s->pending_interface_name,
1466 MAC2STR(wpa_s->pending_interface_addr));
1467
1468 return 0;
1469}
1470
1471
1472static void wpas_p2p_remove_pending_group_interface(
1473 struct wpa_supplicant *wpa_s)
1474{
1475 if (!wpa_s->pending_interface_name[0] ||
1476 is_zero_ether_addr(wpa_s->pending_interface_addr))
1477 return; /* No pending virtual interface */
1478
1479 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1480 wpa_s->pending_interface_name);
1481 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1482 wpa_s->pending_interface_name);
1483 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1484 wpa_s->pending_interface_name[0] = '\0';
1485}
1486
1487
1488static struct wpa_supplicant *
1489wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1490{
1491 struct wpa_interface iface;
1492 struct wpa_supplicant *group_wpa_s;
1493
1494 if (!wpa_s->pending_interface_name[0]) {
1495 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1496 if (!wpas_p2p_create_iface(wpa_s))
1497 return NULL;
1498 /*
1499 * Something has forced us to remove the pending interface; try
1500 * to create a new one and hope for the best that we will get
1501 * the same local address.
1502 */
1503 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1504 WPA_IF_P2P_CLIENT) < 0)
1505 return NULL;
1506 }
1507
1508 os_memset(&iface, 0, sizeof(iface));
1509 iface.ifname = wpa_s->pending_interface_name;
1510 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001511 if (wpa_s->conf->ctrl_interface == NULL &&
1512 wpa_s->parent != wpa_s &&
1513 wpa_s->p2p_mgmt &&
1514 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1515 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1516 else
1517 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001518 iface.driver_param = wpa_s->conf->driver_param;
1519 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1520 if (group_wpa_s == NULL) {
1521 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1522 "wpa_supplicant interface");
1523 return NULL;
1524 }
1525 wpa_s->pending_interface_name[0] = '\0';
1526 group_wpa_s->parent = wpa_s;
1527 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1528 P2P_GROUP_INTERFACE_CLIENT;
1529 wpa_s->global->p2p_group_formation = group_wpa_s;
1530
1531 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1532
1533 return group_wpa_s;
1534}
1535
1536
1537static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1538 void *timeout_ctx)
1539{
1540 struct wpa_supplicant *wpa_s = eloop_ctx;
1541 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001542 wpas_p2p_group_formation_failed(wpa_s);
1543}
1544
1545
1546void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1547{
1548 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1549 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 if (wpa_s->global->p2p)
1551 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001552 wpas_group_formation_completed(wpa_s, 0);
1553}
1554
1555
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001556static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1557{
1558 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1559 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1560 wpa_s->parent, NULL);
1561 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1562 wpa_s->parent, NULL);
1563 wpa_s->global->p2p_fail_on_wps_complete = 0;
1564}
1565
1566
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001567void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1568{
1569 if (wpa_s->global->p2p_group_formation != wpa_s)
1570 return;
1571 /* Speed up group formation timeout since this cannot succeed */
1572 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1573 wpa_s->parent, NULL);
1574 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1575 wpa_s->parent, NULL);
1576}
1577
1578
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001579static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580{
1581 struct wpa_supplicant *wpa_s = ctx;
1582
1583 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1584 wpa_drv_cancel_remain_on_channel(wpa_s);
1585 wpa_s->off_channel_freq = 0;
1586 wpa_s->roc_waiting_drv_freq = 0;
1587 }
1588
1589 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001590 wpa_msg_global(wpa_s, MSG_INFO,
1591 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1592 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 wpas_p2p_remove_pending_group_interface(wpa_s);
1595 return;
1596 }
1597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001598 if (wpa_s->p2p_go_ht40)
1599 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001600 if (wpa_s->p2p_go_vht)
1601 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001602
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001603 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1604 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1605 " wps_method=%s",
1606 res->role_go ? "GO" : "client", res->freq, res->ht40,
1607 MAC2STR(res->peer_device_addr),
1608 MAC2STR(res->peer_interface_addr),
1609 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001610 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611
Dmitry Shmidt04949592012-07-19 12:16:46 -07001612 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1613 struct wpa_ssid *ssid;
1614 ssid = wpa_config_get_network(wpa_s->conf,
1615 wpa_s->p2p_persistent_id);
1616 if (ssid && ssid->disabled == 2 &&
1617 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1618 size_t len = os_strlen(ssid->passphrase);
1619 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1620 "on requested persistent group");
1621 os_memcpy(res->passphrase, ssid->passphrase, len);
1622 res->passphrase[len] = '\0';
1623 }
1624 }
1625
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 if (wpa_s->create_p2p_iface) {
1627 struct wpa_supplicant *group_wpa_s =
1628 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1629 if (group_wpa_s == NULL) {
1630 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001631 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1632 wpa_s, NULL);
1633 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 return;
1635 }
1636 if (group_wpa_s != wpa_s) {
1637 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1638 sizeof(group_wpa_s->p2p_pin));
1639 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1640 }
1641 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1642 wpa_s->pending_interface_name[0] = '\0';
1643 group_wpa_s->p2p_in_provisioning = 1;
1644
1645 if (res->role_go)
1646 wpas_start_wps_go(group_wpa_s, res, 1);
1647 else
1648 wpas_start_wps_enrollee(group_wpa_s, res);
1649 } else {
1650 wpa_s->p2p_in_provisioning = 1;
1651 wpa_s->global->p2p_group_formation = wpa_s;
1652
1653 if (res->role_go)
1654 wpas_start_wps_go(wpa_s, res, 1);
1655 else
1656 wpas_start_wps_enrollee(ctx, res);
1657 }
1658
1659 wpa_s->p2p_long_listen = 0;
1660 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1661
1662 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1663 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1664 (res->peer_config_timeout % 100) * 10000,
1665 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1666}
1667
1668
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001669static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670{
1671 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001672 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1673 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674
1675 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1676}
1677
1678
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001679static void wpas_dev_found(void *ctx, const u8 *addr,
1680 const struct p2p_peer_info *info,
1681 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001683#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 struct wpa_supplicant *wpa_s = ctx;
1685 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001686 char *wfd_dev_info_hex = NULL;
1687
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001688#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001689 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1690 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001691#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001692
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001693 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1694 " p2p_dev_addr=" MACSTR
1695 " pri_dev_type=%s name='%s' config_methods=0x%x "
1696 "dev_capab=0x%x group_capab=0x%x%s%s",
1697 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1698 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1699 sizeof(devtype)),
1700 info->device_name, info->config_methods,
1701 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001702 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1703 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001705 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001706#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1709}
1710
1711
1712static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1713{
1714 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001715
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001716 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1717 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1720}
1721
1722
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001723static void wpas_find_stopped(void *ctx)
1724{
1725 struct wpa_supplicant *wpa_s = ctx;
1726 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1727}
1728
1729
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001730struct wpas_p2p_listen_work {
1731 unsigned int freq;
1732 unsigned int duration;
1733 struct wpabuf *probe_resp_ie;
1734};
1735
1736
1737static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1738{
1739 if (lwork == NULL)
1740 return;
1741 wpabuf_free(lwork->probe_resp_ie);
1742 os_free(lwork);
1743}
1744
1745
1746static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1747{
1748 struct wpas_p2p_listen_work *lwork;
1749
1750 if (!wpa_s->p2p_listen_work)
1751 return;
1752
1753 lwork = wpa_s->p2p_listen_work->ctx;
1754 wpas_p2p_listen_work_free(lwork);
1755 radio_work_done(wpa_s->p2p_listen_work);
1756 wpa_s->p2p_listen_work = NULL;
1757}
1758
1759
1760static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1761{
1762 struct wpa_supplicant *wpa_s = work->wpa_s;
1763 struct wpas_p2p_listen_work *lwork = work->ctx;
1764
1765 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001766 if (work->started) {
1767 wpa_s->p2p_listen_work = NULL;
1768 wpas_stop_listen(wpa_s);
1769 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001770 wpas_p2p_listen_work_free(lwork);
1771 return;
1772 }
1773
1774 wpa_s->p2p_listen_work = work;
1775
1776 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1777
1778 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1779 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1780 "report received Probe Request frames");
1781 wpas_p2p_listen_work_done(wpa_s);
1782 return;
1783 }
1784
1785 wpa_s->pending_listen_freq = lwork->freq;
1786 wpa_s->pending_listen_duration = lwork->duration;
1787
1788 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1789 {
1790 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1791 "to remain on channel (%u MHz) for Listen "
1792 "state", lwork->freq);
1793 wpas_p2p_listen_work_done(wpa_s);
1794 wpa_s->pending_listen_freq = 0;
1795 return;
1796 }
1797 wpa_s->off_channel_freq = 0;
1798 wpa_s->roc_waiting_drv_freq = lwork->freq;
1799}
1800
1801
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802static int wpas_start_listen(void *ctx, unsigned int freq,
1803 unsigned int duration,
1804 const struct wpabuf *probe_resp_ie)
1805{
1806 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001807 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001809 if (wpa_s->p2p_listen_work) {
1810 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 return -1;
1812 }
1813
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001814 lwork = os_zalloc(sizeof(*lwork));
1815 if (lwork == NULL)
1816 return -1;
1817 lwork->freq = freq;
1818 lwork->duration = duration;
1819 if (probe_resp_ie) {
1820 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1821 if (lwork->probe_resp_ie == NULL) {
1822 wpas_p2p_listen_work_free(lwork);
1823 return -1;
1824 }
1825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001827 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1828 lwork) < 0) {
1829 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 return -1;
1831 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832
1833 return 0;
1834}
1835
1836
1837static void wpas_stop_listen(void *ctx)
1838{
1839 struct wpa_supplicant *wpa_s = ctx;
1840 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1841 wpa_drv_cancel_remain_on_channel(wpa_s);
1842 wpa_s->off_channel_freq = 0;
1843 wpa_s->roc_waiting_drv_freq = 0;
1844 }
1845 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1846 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001847 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848}
1849
1850
1851static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1852{
1853 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001854 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855}
1856
1857
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001858/*
1859 * DNS Header section is used only to calculate compression pointers, so the
1860 * contents of this data does not matter, but the length needs to be reserved
1861 * in the virtual packet.
1862 */
1863#define DNS_HEADER_LEN 12
1864
1865/*
1866 * 27-octet in-memory packet from P2P specification containing two implied
1867 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1868 */
1869#define P2P_SD_IN_MEMORY_LEN 27
1870
1871static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1872 u8 **spos, const u8 *end)
1873{
1874 while (*spos < end) {
1875 u8 val = ((*spos)[0] & 0xc0) >> 6;
1876 int len;
1877
1878 if (val == 1 || val == 2) {
1879 /* These are reserved values in RFC 1035 */
1880 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1881 "sequence starting with 0x%x", val);
1882 return -1;
1883 }
1884
1885 if (val == 3) {
1886 u16 offset;
1887 u8 *spos_tmp;
1888
1889 /* Offset */
1890 if (*spos + 2 > end) {
1891 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1892 "DNS offset field");
1893 return -1;
1894 }
1895
1896 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1897 if (offset >= *spos - start) {
1898 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1899 "pointer offset %u", offset);
1900 return -1;
1901 }
1902
1903 (*spos) += 2;
1904 spos_tmp = start + offset;
1905 return p2p_sd_dns_uncompress_label(upos, uend, start,
1906 &spos_tmp,
1907 *spos - 2);
1908 }
1909
1910 /* Label */
1911 len = (*spos)[0] & 0x3f;
1912 if (len == 0)
1913 return 0;
1914
1915 (*spos)++;
1916 if (*spos + len > end) {
1917 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1918 "sequence - no room for label with length "
1919 "%u", len);
1920 return -1;
1921 }
1922
1923 if (*upos + len + 2 > uend)
1924 return -2;
1925
1926 os_memcpy(*upos, *spos, len);
1927 *spos += len;
1928 *upos += len;
1929 (*upos)[0] = '.';
1930 (*upos)++;
1931 (*upos)[0] = '\0';
1932 }
1933
1934 return 0;
1935}
1936
1937
1938/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1939 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1940 * not large enough */
1941static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1942 size_t msg_len, size_t offset)
1943{
1944 /* 27-octet in-memory packet from P2P specification */
1945 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1946 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1947 u8 *tmp, *end, *spos;
1948 char *upos, *uend;
1949 int ret = 0;
1950
1951 if (buf_len < 2)
1952 return -1;
1953 if (offset > msg_len)
1954 return -1;
1955
1956 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1957 if (tmp == NULL)
1958 return -1;
1959 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1960 end = spos + msg_len;
1961 spos += offset;
1962
1963 os_memset(tmp, 0, DNS_HEADER_LEN);
1964 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1965 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1966
1967 upos = buf;
1968 uend = buf + buf_len;
1969
1970 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1971 if (ret) {
1972 os_free(tmp);
1973 return ret;
1974 }
1975
1976 if (upos == buf) {
1977 upos[0] = '.';
1978 upos[1] = '\0';
1979 } else if (upos[-1] == '.')
1980 upos[-1] = '\0';
1981
1982 os_free(tmp);
1983 return 0;
1984}
1985
1986
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987static struct p2p_srv_bonjour *
1988wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1989 const struct wpabuf *query)
1990{
1991 struct p2p_srv_bonjour *bsrv;
1992 size_t len;
1993
1994 len = wpabuf_len(query);
1995 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1996 struct p2p_srv_bonjour, list) {
1997 if (len == wpabuf_len(bsrv->query) &&
1998 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1999 len) == 0)
2000 return bsrv;
2001 }
2002 return NULL;
2003}
2004
2005
2006static struct p2p_srv_upnp *
2007wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2008 const char *service)
2009{
2010 struct p2p_srv_upnp *usrv;
2011
2012 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2013 struct p2p_srv_upnp, list) {
2014 if (version == usrv->version &&
2015 os_strcmp(service, usrv->service) == 0)
2016 return usrv;
2017 }
2018 return NULL;
2019}
2020
2021
2022static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2023 u8 srv_trans_id)
2024{
2025 u8 *len_pos;
2026
2027 if (wpabuf_tailroom(resp) < 5)
2028 return;
2029
2030 /* Length (to be filled) */
2031 len_pos = wpabuf_put(resp, 2);
2032 wpabuf_put_u8(resp, srv_proto);
2033 wpabuf_put_u8(resp, srv_trans_id);
2034 /* Status Code */
2035 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2036 /* Response Data: empty */
2037 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2038}
2039
2040
2041static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2042 struct wpabuf *resp, u8 srv_trans_id)
2043{
2044 struct p2p_srv_bonjour *bsrv;
2045 u8 *len_pos;
2046
2047 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2048
2049 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2050 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2051 return;
2052 }
2053
2054 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2055 struct p2p_srv_bonjour, list) {
2056 if (wpabuf_tailroom(resp) <
2057 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2058 return;
2059 /* Length (to be filled) */
2060 len_pos = wpabuf_put(resp, 2);
2061 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2062 wpabuf_put_u8(resp, srv_trans_id);
2063 /* Status Code */
2064 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2065 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2066 wpabuf_head(bsrv->resp),
2067 wpabuf_len(bsrv->resp));
2068 /* Response Data */
2069 wpabuf_put_buf(resp, bsrv->query); /* Key */
2070 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2071 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2072 2);
2073 }
2074}
2075
2076
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002077static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2078 size_t query_len)
2079{
2080 char str_rx[256], str_srv[256];
2081
2082 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2083 return 0; /* Too short to include DNS Type and Version */
2084 if (os_memcmp(query + query_len - 3,
2085 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2086 3) != 0)
2087 return 0; /* Mismatch in DNS Type or Version */
2088 if (query_len == wpabuf_len(bsrv->query) &&
2089 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2090 return 1; /* Binary match */
2091
2092 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2093 0))
2094 return 0; /* Failed to uncompress query */
2095 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2096 wpabuf_head(bsrv->query),
2097 wpabuf_len(bsrv->query) - 3, 0))
2098 return 0; /* Failed to uncompress service */
2099
2100 return os_strcmp(str_rx, str_srv) == 0;
2101}
2102
2103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2105 struct wpabuf *resp, u8 srv_trans_id,
2106 const u8 *query, size_t query_len)
2107{
2108 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002110 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111
2112 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2113 query, query_len);
2114 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2115 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2116 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2117 srv_trans_id);
2118 return;
2119 }
2120
2121 if (query_len == 0) {
2122 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2123 return;
2124 }
2125
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002126 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2127 struct p2p_srv_bonjour, list) {
2128 if (!match_bonjour_query(bsrv, query, query_len))
2129 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002131 if (wpabuf_tailroom(resp) <
2132 5 + query_len + wpabuf_len(bsrv->resp))
2133 return;
2134
2135 matches++;
2136
2137 /* Length (to be filled) */
2138 len_pos = wpabuf_put(resp, 2);
2139 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2140 wpabuf_put_u8(resp, srv_trans_id);
2141
2142 /* Status Code */
2143 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2144 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2145 wpabuf_head(bsrv->resp),
2146 wpabuf_len(bsrv->resp));
2147
2148 /* Response Data */
2149 wpabuf_put_data(resp, query, query_len); /* Key */
2150 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2151
2152 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2153 }
2154
2155 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2157 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002158 if (wpabuf_tailroom(resp) < 5)
2159 return;
2160
2161 /* Length (to be filled) */
2162 len_pos = wpabuf_put(resp, 2);
2163 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2164 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165
2166 /* Status Code */
2167 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2168 /* Response Data: empty */
2169 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2170 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172}
2173
2174
2175static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2176 struct wpabuf *resp, u8 srv_trans_id)
2177{
2178 struct p2p_srv_upnp *usrv;
2179 u8 *len_pos;
2180
2181 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2182
2183 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2184 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2185 return;
2186 }
2187
2188 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2189 struct p2p_srv_upnp, list) {
2190 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2191 return;
2192
2193 /* Length (to be filled) */
2194 len_pos = wpabuf_put(resp, 2);
2195 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2196 wpabuf_put_u8(resp, srv_trans_id);
2197
2198 /* Status Code */
2199 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2200 /* Response Data */
2201 wpabuf_put_u8(resp, usrv->version);
2202 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2203 usrv->service);
2204 wpabuf_put_str(resp, usrv->service);
2205 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2206 2);
2207 }
2208}
2209
2210
2211static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2212 struct wpabuf *resp, u8 srv_trans_id,
2213 const u8 *query, size_t query_len)
2214{
2215 struct p2p_srv_upnp *usrv;
2216 u8 *len_pos;
2217 u8 version;
2218 char *str;
2219 int count = 0;
2220
2221 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2222 query, query_len);
2223
2224 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2225 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2226 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2227 srv_trans_id);
2228 return;
2229 }
2230
2231 if (query_len == 0) {
2232 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2233 return;
2234 }
2235
2236 if (wpabuf_tailroom(resp) < 5)
2237 return;
2238
2239 /* Length (to be filled) */
2240 len_pos = wpabuf_put(resp, 2);
2241 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2242 wpabuf_put_u8(resp, srv_trans_id);
2243
2244 version = query[0];
2245 str = os_malloc(query_len);
2246 if (str == NULL)
2247 return;
2248 os_memcpy(str, query + 1, query_len - 1);
2249 str[query_len - 1] = '\0';
2250
2251 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2252 struct p2p_srv_upnp, list) {
2253 if (version != usrv->version)
2254 continue;
2255
2256 if (os_strcmp(str, "ssdp:all") != 0 &&
2257 os_strstr(usrv->service, str) == NULL)
2258 continue;
2259
2260 if (wpabuf_tailroom(resp) < 2)
2261 break;
2262 if (count == 0) {
2263 /* Status Code */
2264 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2265 /* Response Data */
2266 wpabuf_put_u8(resp, version);
2267 } else
2268 wpabuf_put_u8(resp, ',');
2269
2270 count++;
2271
2272 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2273 usrv->service);
2274 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2275 break;
2276 wpabuf_put_str(resp, usrv->service);
2277 }
2278 os_free(str);
2279
2280 if (count == 0) {
2281 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2282 "available");
2283 /* Status Code */
2284 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2285 /* Response Data: empty */
2286 }
2287
2288 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2289}
2290
2291
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002292#ifdef CONFIG_WIFI_DISPLAY
2293static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2294 struct wpabuf *resp, u8 srv_trans_id,
2295 const u8 *query, size_t query_len)
2296{
2297 const u8 *pos;
2298 u8 role;
2299 u8 *len_pos;
2300
2301 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2302
2303 if (!wpa_s->global->wifi_display) {
2304 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2305 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2306 srv_trans_id);
2307 return;
2308 }
2309
2310 if (query_len < 1) {
2311 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2312 "Role");
2313 return;
2314 }
2315
2316 if (wpabuf_tailroom(resp) < 5)
2317 return;
2318
2319 pos = query;
2320 role = *pos++;
2321 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2322
2323 /* TODO: role specific handling */
2324
2325 /* Length (to be filled) */
2326 len_pos = wpabuf_put(resp, 2);
2327 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2328 wpabuf_put_u8(resp, srv_trans_id);
2329 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2330
2331 while (pos < query + query_len) {
2332 if (*pos < MAX_WFD_SUBELEMS &&
2333 wpa_s->global->wfd_subelem[*pos] &&
2334 wpabuf_tailroom(resp) >=
2335 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2336 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2337 "subelement %u", *pos);
2338 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2339 }
2340 pos++;
2341 }
2342
2343 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2344}
2345#endif /* CONFIG_WIFI_DISPLAY */
2346
2347
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002348static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2349 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350{
2351 struct wpa_supplicant *wpa_s = ctx;
2352 const u8 *pos = tlvs;
2353 const u8 *end = tlvs + tlvs_len;
2354 const u8 *tlv_end;
2355 u16 slen;
2356 struct wpabuf *resp;
2357 u8 srv_proto, srv_trans_id;
2358 size_t buf_len;
2359 char *buf;
2360
2361 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2362 tlvs, tlvs_len);
2363 buf_len = 2 * tlvs_len + 1;
2364 buf = os_malloc(buf_len);
2365 if (buf) {
2366 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2367 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2368 MACSTR " %u %u %s",
2369 freq, MAC2STR(sa), dialog_token, update_indic,
2370 buf);
2371 os_free(buf);
2372 }
2373
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002374 if (wpa_s->p2p_sd_over_ctrl_iface) {
2375 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2376 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002378 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379
2380 resp = wpabuf_alloc(10000);
2381 if (resp == NULL)
2382 return;
2383
2384 while (pos + 1 < end) {
2385 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2386 slen = WPA_GET_LE16(pos);
2387 pos += 2;
2388 if (pos + slen > end || slen < 2) {
2389 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2390 "length");
2391 wpabuf_free(resp);
2392 return;
2393 }
2394 tlv_end = pos + slen;
2395
2396 srv_proto = *pos++;
2397 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2398 srv_proto);
2399 srv_trans_id = *pos++;
2400 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2401 srv_trans_id);
2402
2403 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2404 pos, tlv_end - pos);
2405
2406
2407 if (wpa_s->force_long_sd) {
2408 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2409 "response");
2410 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2411 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2412 goto done;
2413 }
2414
2415 switch (srv_proto) {
2416 case P2P_SERV_ALL_SERVICES:
2417 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2418 "for all services");
2419 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2420 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2421 wpa_printf(MSG_DEBUG, "P2P: No service "
2422 "discovery protocols available");
2423 wpas_sd_add_proto_not_avail(
2424 resp, P2P_SERV_ALL_SERVICES,
2425 srv_trans_id);
2426 break;
2427 }
2428 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2429 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2430 break;
2431 case P2P_SERV_BONJOUR:
2432 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2433 pos, tlv_end - pos);
2434 break;
2435 case P2P_SERV_UPNP:
2436 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2437 pos, tlv_end - pos);
2438 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002439#ifdef CONFIG_WIFI_DISPLAY
2440 case P2P_SERV_WIFI_DISPLAY:
2441 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2442 pos, tlv_end - pos);
2443 break;
2444#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002445 default:
2446 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2447 "protocol %u", srv_proto);
2448 wpas_sd_add_proto_not_avail(resp, srv_proto,
2449 srv_trans_id);
2450 break;
2451 }
2452
2453 pos = tlv_end;
2454 }
2455
2456done:
2457 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2458 update_indic, tlvs, tlvs_len);
2459
2460 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2461
2462 wpabuf_free(resp);
2463}
2464
2465
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002466static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2467 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468{
2469 struct wpa_supplicant *wpa_s = ctx;
2470 const u8 *pos = tlvs;
2471 const u8 *end = tlvs + tlvs_len;
2472 const u8 *tlv_end;
2473 u16 slen;
2474 size_t buf_len;
2475 char *buf;
2476
2477 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2478 tlvs, tlvs_len);
2479 if (tlvs_len > 1500) {
2480 /* TODO: better way for handling this */
2481 wpa_msg_ctrl(wpa_s, MSG_INFO,
2482 P2P_EVENT_SERV_DISC_RESP MACSTR
2483 " %u <long response: %u bytes>",
2484 MAC2STR(sa), update_indic,
2485 (unsigned int) tlvs_len);
2486 } else {
2487 buf_len = 2 * tlvs_len + 1;
2488 buf = os_malloc(buf_len);
2489 if (buf) {
2490 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2491 wpa_msg_ctrl(wpa_s, MSG_INFO,
2492 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2493 MAC2STR(sa), update_indic, buf);
2494 os_free(buf);
2495 }
2496 }
2497
2498 while (pos < end) {
2499 u8 srv_proto, srv_trans_id, status;
2500
2501 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2502 slen = WPA_GET_LE16(pos);
2503 pos += 2;
2504 if (pos + slen > end || slen < 3) {
2505 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2506 "length");
2507 return;
2508 }
2509 tlv_end = pos + slen;
2510
2511 srv_proto = *pos++;
2512 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2513 srv_proto);
2514 srv_trans_id = *pos++;
2515 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2516 srv_trans_id);
2517 status = *pos++;
2518 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2519 status);
2520
2521 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2522 pos, tlv_end - pos);
2523
2524 pos = tlv_end;
2525 }
2526
2527 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2528}
2529
2530
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002531u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2532 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002535 return 0;
2536 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537}
2538
2539
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002540u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2541 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542{
2543 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002544 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545
2546 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2547 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002548 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2550 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2551 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2552 wpabuf_put_u8(tlvs, version);
2553 wpabuf_put_str(tlvs, query);
2554 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2555 wpabuf_free(tlvs);
2556 return ret;
2557}
2558
2559
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002560#ifdef CONFIG_WIFI_DISPLAY
2561
2562static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2563 const struct wpabuf *tlvs)
2564{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002565 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2566 return 0;
2567 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2568}
2569
2570
2571#define MAX_WFD_SD_SUBELEMS 20
2572
2573static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2574 const char *subelems)
2575{
2576 u8 *len;
2577 const char *pos;
2578 int val;
2579 int count = 0;
2580
2581 len = wpabuf_put(tlvs, 2);
2582 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2583 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2584
2585 wpabuf_put_u8(tlvs, role);
2586
2587 pos = subelems;
2588 while (*pos) {
2589 val = atoi(pos);
2590 if (val >= 0 && val < 256) {
2591 wpabuf_put_u8(tlvs, val);
2592 count++;
2593 if (count == MAX_WFD_SD_SUBELEMS)
2594 break;
2595 }
2596 pos = os_strchr(pos + 1, ',');
2597 if (pos == NULL)
2598 break;
2599 pos++;
2600 }
2601
2602 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2603}
2604
2605
2606u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2607 const u8 *dst, const char *role)
2608{
2609 struct wpabuf *tlvs;
2610 u64 ret;
2611 const char *subelems;
2612 u8 id = 1;
2613
2614 subelems = os_strchr(role, ' ');
2615 if (subelems == NULL)
2616 return 0;
2617 subelems++;
2618
2619 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2620 if (tlvs == NULL)
2621 return 0;
2622
2623 if (os_strstr(role, "[source]"))
2624 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2625 if (os_strstr(role, "[pri-sink]"))
2626 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2627 if (os_strstr(role, "[sec-sink]"))
2628 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2629 if (os_strstr(role, "[source+sink]"))
2630 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2631
2632 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2633 wpabuf_free(tlvs);
2634 return ret;
2635}
2636
2637#endif /* CONFIG_WIFI_DISPLAY */
2638
2639
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002640int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002642 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2643 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002644 return p2p_sd_cancel_request(wpa_s->global->p2p,
2645 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646}
2647
2648
2649void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2650 const u8 *dst, u8 dialog_token,
2651 const struct wpabuf *resp_tlvs)
2652{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2654 return;
2655 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2656 resp_tlvs);
2657}
2658
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2661{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 if (wpa_s->global->p2p)
2663 p2p_sd_service_update(wpa_s->global->p2p);
2664}
2665
2666
2667static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2668{
2669 dl_list_del(&bsrv->list);
2670 wpabuf_free(bsrv->query);
2671 wpabuf_free(bsrv->resp);
2672 os_free(bsrv);
2673}
2674
2675
2676static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2677{
2678 dl_list_del(&usrv->list);
2679 os_free(usrv->service);
2680 os_free(usrv);
2681}
2682
2683
2684void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2685{
2686 struct p2p_srv_bonjour *bsrv, *bn;
2687 struct p2p_srv_upnp *usrv, *un;
2688
2689 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2690 struct p2p_srv_bonjour, list)
2691 wpas_p2p_srv_bonjour_free(bsrv);
2692
2693 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2694 struct p2p_srv_upnp, list)
2695 wpas_p2p_srv_upnp_free(usrv);
2696
2697 wpas_p2p_sd_service_update(wpa_s);
2698}
2699
2700
2701int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2702 struct wpabuf *query, struct wpabuf *resp)
2703{
2704 struct p2p_srv_bonjour *bsrv;
2705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706 bsrv = os_zalloc(sizeof(*bsrv));
2707 if (bsrv == NULL)
2708 return -1;
2709 bsrv->query = query;
2710 bsrv->resp = resp;
2711 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2712
2713 wpas_p2p_sd_service_update(wpa_s);
2714 return 0;
2715}
2716
2717
2718int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2719 const struct wpabuf *query)
2720{
2721 struct p2p_srv_bonjour *bsrv;
2722
2723 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2724 if (bsrv == NULL)
2725 return -1;
2726 wpas_p2p_srv_bonjour_free(bsrv);
2727 wpas_p2p_sd_service_update(wpa_s);
2728 return 0;
2729}
2730
2731
2732int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2733 const char *service)
2734{
2735 struct p2p_srv_upnp *usrv;
2736
2737 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2738 return 0; /* Already listed */
2739 usrv = os_zalloc(sizeof(*usrv));
2740 if (usrv == NULL)
2741 return -1;
2742 usrv->version = version;
2743 usrv->service = os_strdup(service);
2744 if (usrv->service == NULL) {
2745 os_free(usrv);
2746 return -1;
2747 }
2748 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2749
2750 wpas_p2p_sd_service_update(wpa_s);
2751 return 0;
2752}
2753
2754
2755int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2756 const char *service)
2757{
2758 struct p2p_srv_upnp *usrv;
2759
2760 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2761 if (usrv == NULL)
2762 return -1;
2763 wpas_p2p_srv_upnp_free(usrv);
2764 wpas_p2p_sd_service_update(wpa_s);
2765 return 0;
2766}
2767
2768
2769static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2770 const u8 *peer, const char *params,
2771 unsigned int generated_pin)
2772{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002773 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2774 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775}
2776
2777
2778static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2779 const u8 *peer, const char *params)
2780{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002781 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2782 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783}
2784
2785
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002786static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2787 const u8 *dev_addr, const u8 *pri_dev_type,
2788 const char *dev_name, u16 supp_config_methods,
2789 u8 dev_capab, u8 group_capab, const u8 *group_id,
2790 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791{
2792 struct wpa_supplicant *wpa_s = ctx;
2793 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002794 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 u8 empty_dev_type[8];
2796 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002797 struct wpa_supplicant *group = NULL;
2798
2799 if (group_id) {
2800 for (group = wpa_s->global->ifaces; group; group = group->next)
2801 {
2802 struct wpa_ssid *s = group->current_ssid;
2803 if (s != NULL &&
2804 s->mode == WPAS_MODE_P2P_GO &&
2805 group_id_len - ETH_ALEN == s->ssid_len &&
2806 os_memcmp(group_id + ETH_ALEN, s->ssid,
2807 s->ssid_len) == 0)
2808 break;
2809 }
2810 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811
2812 if (pri_dev_type == NULL) {
2813 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2814 pri_dev_type = empty_dev_type;
2815 }
2816 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2817 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002818 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002819 MAC2STR(dev_addr),
2820 wps_dev_type_bin2str(pri_dev_type, devtype,
2821 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002822 dev_name, supp_config_methods, dev_capab, group_capab,
2823 group ? " group=" : "",
2824 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 if (config_methods & WPS_CONFIG_DISPLAY) {
2828 generated_pin = wps_generate_pin();
2829 wpas_prov_disc_local_display(wpa_s, peer, params,
2830 generated_pin);
2831 } else if (config_methods & WPS_CONFIG_KEYPAD)
2832 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2833 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002834 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2835 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002836
2837 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2838 P2P_PROV_DISC_SUCCESS,
2839 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840}
2841
2842
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002843static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002844{
2845 struct wpa_supplicant *wpa_s = ctx;
2846 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002847 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002849 if (wpa_s->pending_pd_before_join &&
2850 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2851 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2852 wpa_s->pending_pd_before_join = 0;
2853 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2854 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002855 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002856 return;
2857 }
2858
Dmitry Shmidt04949592012-07-19 12:16:46 -07002859 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2860 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2861 os_snprintf(params, sizeof(params), " peer_go=%d",
2862 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2863 else
2864 params[0] = '\0';
2865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002867 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 else if (config_methods & WPS_CONFIG_KEYPAD) {
2869 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002870 wpas_prov_disc_local_display(wpa_s, peer, params,
2871 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002873 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2874 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875
Jouni Malinen75ecf522011-06-27 15:19:46 -07002876 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2877 P2P_PROV_DISC_SUCCESS,
2878 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879}
2880
2881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002882static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2883 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002884{
2885 struct wpa_supplicant *wpa_s = ctx;
2886
Dmitry Shmidt04949592012-07-19 12:16:46 -07002887 if (wpa_s->p2p_fallback_to_go_neg) {
2888 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2889 "failed - fall back to GO Negotiation");
2890 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2891 return;
2892 }
2893
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002894 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002895 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002896 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2897 "join-existing-group operation (no ACK for PD "
2898 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002899 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002900 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002901 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002902
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002903 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2904 " p2p_dev_addr=" MACSTR " status=%d",
2905 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002906
Jouni Malinen75ecf522011-06-27 15:19:46 -07002907 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2908 status, 0, 0);
2909}
2910
2911
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002912static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2913{
2914 if (channels == NULL)
2915 return 1; /* Assume no restrictions */
2916 return p2p_channels_includes_freq(channels, freq);
2917
2918}
2919
2920
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002921/**
2922 * Pick the best frequency to use from all the currently used frequencies.
2923 */
2924static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2925 struct wpa_used_freq_data *freqs,
2926 unsigned int num)
2927{
2928 unsigned int i, c;
2929
2930 /* find a candidate freq that is supported by P2P */
2931 for (c = 0; c < num; c++)
2932 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2933 break;
2934
2935 if (c == num)
2936 return 0;
2937
2938 /* once we have a candidate, try to find a 'better' one */
2939 for (i = c + 1; i < num; i++) {
2940 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2941 continue;
2942
2943 /*
2944 * 1. Infrastructure station interfaces have higher preference.
2945 * 2. P2P Clients have higher preference.
2946 * 3. All others.
2947 */
2948 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2949 c = i;
2950 break;
2951 }
2952
2953 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2954 c = i;
2955 }
2956 return freqs[c].freq;
2957}
2958
2959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002960static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2961 const u8 *go_dev_addr, const u8 *ssid,
2962 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002963 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002964 const struct p2p_channels *channels,
2965 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966{
2967 struct wpa_supplicant *wpa_s = ctx;
2968 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002969 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002971 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002972
2973 if (!persistent_group) {
2974 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002975 " to join an active group (SSID: %s)",
2976 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2978 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2979 == 0 ||
2980 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2981 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2982 "authorized invitation");
2983 goto accept_inv;
2984 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002985
2986#ifdef CONFIG_WPS_NFC
2987 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2988 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2989 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2990 wpa_s->parent->p2p_wps_method = WPS_NFC;
2991 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2992 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2993 go_dev_addr, ETH_ALEN);
2994 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2995 bssid, ETH_ALEN);
2996 goto accept_inv;
2997 }
2998#endif /* CONFIG_WPS_NFC */
2999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 /*
3001 * Do not accept the invitation automatically; notify user and
3002 * request approval.
3003 */
3004 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3005 }
3006
3007 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3008 if (grp) {
3009 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3010 "running persistent group");
3011 if (*go)
3012 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3013 goto accept_inv;
3014 }
3015
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003016 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3017 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3018 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3019 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003020 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003021 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3023
3024 for (s = wpa_s->conf->ssid; s; s = s->next) {
3025 if (s->disabled == 2 &&
3026 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3027 s->ssid_len == ssid_len &&
3028 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3029 break;
3030 }
3031
3032 if (!s) {
3033 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3034 " requested reinvocation of an unknown group",
3035 MAC2STR(sa));
3036 return P2P_SC_FAIL_UNKNOWN_GROUP;
3037 }
3038
3039 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3040 *go = 1;
3041 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3042 wpa_printf(MSG_DEBUG, "P2P: The only available "
3043 "interface is already in use - reject "
3044 "invitation");
3045 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3046 }
3047 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3048 } else if (s->mode == WPAS_MODE_P2P_GO) {
3049 *go = 1;
3050 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3051 {
3052 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3053 "interface address for the group");
3054 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3055 }
3056 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3057 ETH_ALEN);
3058 }
3059
3060accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003061 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3062
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003063 best_freq = 0;
3064 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3065 sizeof(struct wpa_used_freq_data));
3066 if (freqs) {
3067 int num_channels = wpa_s->num_multichan_concurrent;
3068 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3069 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3070 os_free(freqs);
3071 }
3072
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003073 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003074 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003075 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003076 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003077
3078 if (wpa_s->num_multichan_concurrent < 2 ||
3079 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3080 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 -07003081 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003082 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 }
3084
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003085 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3086 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003087 if (*go == 0) {
3088 /* We are the client */
3089 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3090 "running a GO but we are capable of MCC, "
3091 "figure out the best channel to use");
3092 *force_freq = 0;
3093 } else if (!freq_included(channels, *force_freq)) {
3094 /* We are the GO, and *force_freq is not in the
3095 * intersection */
3096 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3097 "in intersection but we are capable of MCC, "
3098 "figure out the best channel to use",
3099 *force_freq);
3100 *force_freq = 0;
3101 }
3102 }
3103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104 return P2P_SC_SUCCESS;
3105}
3106
3107
3108static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3109 const u8 *ssid, size_t ssid_len,
3110 const u8 *go_dev_addr, u8 status,
3111 int op_freq)
3112{
3113 struct wpa_supplicant *wpa_s = ctx;
3114 struct wpa_ssid *s;
3115
3116 for (s = wpa_s->conf->ssid; s; s = s->next) {
3117 if (s->disabled == 2 &&
3118 s->ssid_len == ssid_len &&
3119 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3120 break;
3121 }
3122
3123 if (status == P2P_SC_SUCCESS) {
3124 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003125 " was accepted; op_freq=%d MHz, SSID=%s",
3126 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003127 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003128 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003129 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003130 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003131 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003133 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003135 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003136 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 }
3138 return;
3139 }
3140
3141 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3142 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3143 " was rejected (status %u)", MAC2STR(sa), status);
3144 return;
3145 }
3146
3147 if (!s) {
3148 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003149 wpa_msg_global(wpa_s, MSG_INFO,
3150 P2P_EVENT_INVITATION_RECEIVED
3151 "sa=" MACSTR " go_dev_addr=" MACSTR
3152 " bssid=" MACSTR " unknown-network",
3153 MAC2STR(sa), MAC2STR(go_dev_addr),
3154 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003156 wpa_msg_global(wpa_s, MSG_INFO,
3157 P2P_EVENT_INVITATION_RECEIVED
3158 "sa=" MACSTR " go_dev_addr=" MACSTR
3159 " unknown-network",
3160 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 }
3162 return;
3163 }
3164
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003165 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003166 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3167 "sa=" MACSTR " persistent=%d freq=%d",
3168 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003169 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003170 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3171 "sa=" MACSTR " persistent=%d",
3172 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003173 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174}
3175
3176
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003177static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3178 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003179 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003180{
3181 size_t i;
3182
3183 if (ssid == NULL)
3184 return;
3185
3186 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3187 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3188 ETH_ALEN) == 0)
3189 break;
3190 }
3191 if (i >= ssid->num_p2p_clients) {
3192 if (ssid->mode != WPAS_MODE_P2P_GO &&
3193 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3194 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3195 "due to invitation result", ssid->id);
3196 wpas_notify_network_removed(wpa_s, ssid);
3197 wpa_config_remove_network(wpa_s->conf, ssid->id);
3198 return;
3199 }
3200 return; /* Peer not found in client list */
3201 }
3202
3203 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003204 "group %d client list%s",
3205 MAC2STR(peer), ssid->id,
3206 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003207 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3208 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3209 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3210 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003211 if (wpa_s->parent->conf->update_config &&
3212 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3213 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003214}
3215
3216
3217static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3218 const u8 *peer)
3219{
3220 struct wpa_ssid *ssid;
3221
3222 wpa_s = wpa_s->global->p2p_invite_group;
3223 if (wpa_s == NULL)
3224 return; /* No known invitation group */
3225 ssid = wpa_s->current_ssid;
3226 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3227 !ssid->p2p_persistent_group)
3228 return; /* Not operating as a GO in persistent group */
3229 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3230 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003231 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003232}
3233
3234
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003235static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003236 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003237 const u8 *peer, int neg_freq,
3238 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003239{
3240 struct wpa_supplicant *wpa_s = ctx;
3241 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003242 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243
3244 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003245 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3246 "status=%d " MACSTR,
3247 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003249 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3250 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 }
3252 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3253
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003254 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3255 status, MAC2STR(peer));
3256 if (wpa_s->pending_invite_ssid_id == -1) {
3257 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3258 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003259 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003260 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003261
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003262 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3263 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3264 "invitation exchange to indicate readiness for "
3265 "re-invocation");
3266 }
3267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003269 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3270 ssid = wpa_config_get_network(
3271 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003272 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 wpas_p2p_remove_pending_group_interface(wpa_s);
3275 return;
3276 }
3277
3278 ssid = wpa_config_get_network(wpa_s->conf,
3279 wpa_s->pending_invite_ssid_id);
3280 if (ssid == NULL) {
3281 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3282 "data matching with invitation");
3283 return;
3284 }
3285
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003286 /*
3287 * The peer could have missed our ctrl::ack frame for Invitation
3288 * Response and continue retransmitting the frame. To reduce the
3289 * likelihood of the peer not getting successful TX status for the
3290 * Invitation Response frame, wait a short time here before starting
3291 * the persistent group so that we will remain on the current channel to
3292 * acknowledge any possible retransmission from the peer.
3293 */
3294 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3295 "starting persistent group");
3296 os_sleep(0, 50000);
3297
Dmitry Shmidt15907092014-03-25 10:42:57 -07003298 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3299 freq_included(channels, neg_freq))
3300 freq = neg_freq;
3301 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3302 freq_included(channels, peer_oper_freq))
3303 freq = peer_oper_freq;
3304 else
3305 freq = 0;
3306
3307 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3308 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003310 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003311 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003312 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003313 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3314 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003315 ssid->mode == WPAS_MODE_P2P_GO ?
3316 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3317 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003318}
3319
3320
Dmitry Shmidt04949592012-07-19 12:16:46 -07003321static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3322 unsigned int freq)
3323{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003324 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3325 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003326 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003327}
3328
3329
3330static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3331{
3332 reg->channel[reg->channels] = chan;
3333 reg->channels++;
3334}
3335
3336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003338 struct p2p_channels *chan,
3339 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340{
3341 int i, cla = 0;
3342
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003343 os_memset(cli_chan, 0, sizeof(*cli_chan));
3344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003345 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3346 "band");
3347
3348 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3349 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003350 chan->reg_class[cla].channels = 0;
3351 for (i = 0; i < 11; i++) {
3352 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3353 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3354 }
3355 if (chan->reg_class[cla].channels)
3356 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003357
3358 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3359 "band");
3360
3361 /* Operating class 115 - 5 GHz, channels 36-48 */
3362 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003363 chan->reg_class[cla].channels = 0;
3364 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3365 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3366 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3367 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3368 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3369 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3370 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3371 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3372 if (chan->reg_class[cla].channels)
3373 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374
3375 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3376 "band");
3377
3378 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3379 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003380 chan->reg_class[cla].channels = 0;
3381 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3382 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3383 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3384 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3385 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3386 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3387 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3388 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3389 if (chan->reg_class[cla].channels)
3390 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391
3392 chan->reg_classes = cla;
3393 return 0;
3394}
3395
3396
3397static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3398 u16 num_modes,
3399 enum hostapd_hw_mode mode)
3400{
3401 u16 i;
3402
3403 for (i = 0; i < num_modes; i++) {
3404 if (modes[i].mode == mode)
3405 return &modes[i];
3406 }
3407
3408 return NULL;
3409}
3410
3411
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003412enum chan_allowed {
3413 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3414};
3415
Dmitry Shmidt04949592012-07-19 12:16:46 -07003416static int has_channel(struct wpa_global *global,
3417 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003418{
3419 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003420 unsigned int freq;
3421
3422 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3423 chan * 5;
3424 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003425 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426
3427 for (i = 0; i < mode->num_channels; i++) {
3428 if (mode->channels[i].chan == chan) {
3429 if (flags)
3430 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003431 if (mode->channels[i].flag &
3432 (HOSTAPD_CHAN_DISABLED |
3433 HOSTAPD_CHAN_RADAR))
3434 return NOT_ALLOWED;
3435 if (mode->channels[i].flag &
3436 (HOSTAPD_CHAN_PASSIVE_SCAN |
3437 HOSTAPD_CHAN_NO_IBSS))
3438 return PASSIVE_ONLY;
3439 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440 }
3441 }
3442
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003443 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444}
3445
3446
3447struct p2p_oper_class_map {
3448 enum hostapd_hw_mode mode;
3449 u8 op_class;
3450 u8 min_chan;
3451 u8 max_chan;
3452 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003453 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454};
3455
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003456static struct p2p_oper_class_map op_class[] = {
3457 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003458#if 0 /* Do not enable HT40 on 2 GHz for now */
3459 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3460 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3461#endif
3462 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3463 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3464 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3465 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3466 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3467 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003468
3469 /*
3470 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3471 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3472 * 80 MHz, but currently use the following definition for simplicity
3473 * (these center frequencies are not actual channels, which makes
3474 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3475 * removing invalid channels.
3476 */
3477 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003478 { -1, 0, 0, 0, 0, BW20 }
3479};
3480
3481
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003482static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3483 struct hostapd_hw_modes *mode,
3484 u8 channel)
3485{
3486 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3487 unsigned int i;
3488
3489 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3490 return 0;
3491
3492 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3493 /*
3494 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3495 * so the center channel is 6 channels away from the start/end.
3496 */
3497 if (channel >= center_channels[i] - 6 &&
3498 channel <= center_channels[i] + 6)
3499 return center_channels[i];
3500
3501 return 0;
3502}
3503
3504
3505static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3506 struct hostapd_hw_modes *mode,
3507 u8 channel, u8 bw)
3508{
3509 u8 center_chan;
3510 int i, flags;
3511 enum chan_allowed res, ret = ALLOWED;
3512
3513 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3514 if (!center_chan)
3515 return NOT_ALLOWED;
3516 if (center_chan >= 58 && center_chan <= 138)
3517 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3518
3519 /* check all the channels are available */
3520 for (i = 0; i < 4; i++) {
3521 int adj_chan = center_chan - 6 + i * 4;
3522
3523 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3524 if (res == NOT_ALLOWED)
3525 return NOT_ALLOWED;
3526 if (res == PASSIVE_ONLY)
3527 ret = PASSIVE_ONLY;
3528
3529 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3530 return NOT_ALLOWED;
3531 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3532 return NOT_ALLOWED;
3533 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3534 return NOT_ALLOWED;
3535 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3536 return NOT_ALLOWED;
3537 }
3538
3539 return ret;
3540}
3541
3542
3543static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3544 struct hostapd_hw_modes *mode,
3545 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003546{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003547 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003548 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003549
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003550 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3551 if (bw == BW40MINUS) {
3552 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3553 return NOT_ALLOWED;
3554 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3555 } else if (bw == BW40PLUS) {
3556 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3557 return NOT_ALLOWED;
3558 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3559 } else if (bw == BW80) {
3560 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3561 }
3562
3563 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3564 return NOT_ALLOWED;
3565 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3566 return PASSIVE_ONLY;
3567 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003568}
3569
3570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003571static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003572 struct p2p_channels *chan,
3573 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003575 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003576 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003578 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3580 "of all supported channels; assume dualband "
3581 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003582 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 }
3584
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003585 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003586
3587 for (op = 0; op_class[op].op_class; op++) {
3588 struct p2p_oper_class_map *o = &op_class[op];
3589 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003590 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003591
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003592 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003593 if (mode == NULL)
3594 continue;
3595 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003596 enum chan_allowed res;
3597 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3598 if (res == ALLOWED) {
3599 if (reg == NULL) {
3600 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3601 o->op_class);
3602 reg = &chan->reg_class[cla];
3603 cla++;
3604 reg->reg_class = o->op_class;
3605 }
3606 reg->channel[reg->channels] = ch;
3607 reg->channels++;
3608 } else if (res == PASSIVE_ONLY &&
3609 wpa_s->conf->p2p_add_cli_chan) {
3610 if (cli_reg == NULL) {
3611 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3612 o->op_class);
3613 cli_reg = &cli_chan->reg_class[cli_cla];
3614 cli_cla++;
3615 cli_reg->reg_class = o->op_class;
3616 }
3617 cli_reg->channel[cli_reg->channels] = ch;
3618 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 }
3621 if (reg) {
3622 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3623 reg->channel, reg->channels);
3624 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003625 if (cli_reg) {
3626 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3627 cli_reg->channel, cli_reg->channels);
3628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629 }
3630
3631 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003632 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 return 0;
3635}
3636
3637
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003638int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3639 struct hostapd_hw_modes *mode, u8 channel)
3640{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003641 int op;
3642 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003643
3644 for (op = 0; op_class[op].op_class; op++) {
3645 struct p2p_oper_class_map *o = &op_class[op];
3646 u8 ch;
3647
3648 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3649 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3650 o->bw == BW20 || ch != channel)
3651 continue;
3652 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003653 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003654 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003655 }
3656 }
3657 return 0;
3658}
3659
3660
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003661int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3662 struct hostapd_hw_modes *mode, u8 channel)
3663{
3664 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3665 return 0;
3666
3667 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3668}
3669
3670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3672 size_t buf_len)
3673{
3674 struct wpa_supplicant *wpa_s = ctx;
3675
3676 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3677 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3678 break;
3679 }
3680 if (wpa_s == NULL)
3681 return -1;
3682
3683 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3684}
3685
3686
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003687static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3688{
3689 struct wpa_supplicant *wpa_s = ctx;
3690
3691 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3692 struct wpa_ssid *ssid = wpa_s->current_ssid;
3693 if (ssid == NULL)
3694 continue;
3695 if (ssid->mode != WPAS_MODE_INFRA)
3696 continue;
3697 if (wpa_s->wpa_state != WPA_COMPLETED &&
3698 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3699 continue;
3700 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3701 return 1;
3702 }
3703
3704 return 0;
3705}
3706
3707
Dmitry Shmidt18463232014-01-24 12:29:41 -08003708static int wpas_is_concurrent_session_active(void *ctx)
3709{
3710 struct wpa_supplicant *wpa_s = ctx;
3711 struct wpa_supplicant *ifs;
3712
3713 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3714 if (ifs == wpa_s)
3715 continue;
3716 if (ifs->wpa_state > WPA_ASSOCIATED)
3717 return 1;
3718 }
3719 return 0;
3720}
3721
3722
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003723static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3724{
3725 struct wpa_supplicant *wpa_s = ctx;
3726 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3727}
3728
3729
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003730int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3731{
3732 struct wpa_interface iface;
3733 struct wpa_supplicant *p2pdev_wpa_s;
3734 char ifname[100];
3735 char force_name[100];
3736 int ret;
3737
3738 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3739 wpa_s->ifname);
3740 force_name[0] = '\0';
3741 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3742 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3743 force_name, wpa_s->pending_interface_addr, NULL);
3744 if (ret < 0) {
3745 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3746 return ret;
3747 }
3748 os_strlcpy(wpa_s->pending_interface_name, ifname,
3749 sizeof(wpa_s->pending_interface_name));
3750
3751 os_memset(&iface, 0, sizeof(iface));
3752 iface.p2p_mgmt = 1;
3753 iface.ifname = wpa_s->pending_interface_name;
3754 iface.driver = wpa_s->driver->name;
3755 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003756
3757 /*
3758 * If a P2P Device configuration file was given, use it as the interface
3759 * configuration file (instead of using parent's configuration file.
3760 */
3761 if (wpa_s->conf_p2p_dev) {
3762 iface.confname = wpa_s->conf_p2p_dev;
3763 iface.ctrl_interface = NULL;
3764 } else {
3765 iface.confname = wpa_s->confname;
3766 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3767 }
3768 iface.conf_p2p_dev = NULL;
3769
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003770 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3771 if (!p2pdev_wpa_s) {
3772 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3773 return -1;
3774 }
3775 p2pdev_wpa_s->parent = wpa_s;
3776
3777 wpa_s->pending_interface_name[0] = '\0';
3778 return 0;
3779}
3780
3781
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003782static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3783 const u8 *noa, size_t noa_len)
3784{
3785 struct wpa_supplicant *wpa_s, *intf = ctx;
3786 char hex[100];
3787
3788 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3789 if (wpa_s->waiting_presence_resp)
3790 break;
3791 }
3792 if (!wpa_s) {
3793 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3794 return;
3795 }
3796 wpa_s->waiting_presence_resp = 0;
3797
3798 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3799 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3800 " status=%u noa=%s", MAC2STR(src), status, hex);
3801}
3802
3803
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003804static int _wpas_p2p_in_progress(void *ctx)
3805{
3806 struct wpa_supplicant *wpa_s = ctx;
3807 return wpas_p2p_in_progress(wpa_s);
3808}
3809
3810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811/**
3812 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3813 * @global: Pointer to global data from wpa_supplicant_init()
3814 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3815 * Returns: 0 on success, -1 on failure
3816 */
3817int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3818{
3819 struct p2p_config p2p;
3820 unsigned int r;
3821 int i;
3822
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003823 if (wpa_s->conf->p2p_disabled)
3824 return 0;
3825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3827 return 0;
3828
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003829 if (global->p2p)
3830 return 0;
3831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003833 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003834 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003835 p2p.p2p_scan = wpas_p2p_scan;
3836 p2p.send_action = wpas_send_action;
3837 p2p.send_action_done = wpas_send_action_done;
3838 p2p.go_neg_completed = wpas_go_neg_completed;
3839 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3840 p2p.dev_found = wpas_dev_found;
3841 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003842 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003843 p2p.start_listen = wpas_start_listen;
3844 p2p.stop_listen = wpas_stop_listen;
3845 p2p.send_probe_resp = wpas_send_probe_resp;
3846 p2p.sd_request = wpas_sd_request;
3847 p2p.sd_response = wpas_sd_response;
3848 p2p.prov_disc_req = wpas_prov_disc_req;
3849 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003850 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003851 p2p.invitation_process = wpas_invitation_process;
3852 p2p.invitation_received = wpas_invitation_received;
3853 p2p.invitation_result = wpas_invitation_result;
3854 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003855 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003856 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003857 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003858 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859
3860 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003861 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 p2p.dev_name = wpa_s->conf->device_name;
3863 p2p.manufacturer = wpa_s->conf->manufacturer;
3864 p2p.model_name = wpa_s->conf->model_name;
3865 p2p.model_number = wpa_s->conf->model_number;
3866 p2p.serial_number = wpa_s->conf->serial_number;
3867 if (wpa_s->wps) {
3868 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3869 p2p.config_methods = wpa_s->wps->config_methods;
3870 }
3871
3872 if (wpa_s->conf->p2p_listen_reg_class &&
3873 wpa_s->conf->p2p_listen_channel) {
3874 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3875 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003876 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003877 } else {
3878 p2p.reg_class = 81;
3879 /*
3880 * Pick one of the social channels randomly as the listen
3881 * channel.
3882 */
3883 os_get_random((u8 *) &r, sizeof(r));
3884 p2p.channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003885 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003886 }
3887 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3888
3889 if (wpa_s->conf->p2p_oper_reg_class &&
3890 wpa_s->conf->p2p_oper_channel) {
3891 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3892 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3893 p2p.cfg_op_channel = 1;
3894 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3895 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3896
3897 } else {
3898 p2p.op_reg_class = 81;
3899 /*
3900 * Use random operation channel from (1, 6, 11) if no other
3901 * preference is indicated.
3902 */
3903 os_get_random((u8 *) &r, sizeof(r));
3904 p2p.op_channel = 1 + (r % 3) * 5;
3905 p2p.cfg_op_channel = 0;
3906 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3907 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3908 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003909
3910 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3911 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3912 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3913 }
3914
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3916 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3917 p2p.country[2] = 0x04;
3918 } else
3919 os_memcpy(p2p.country, "XX\x04", 3);
3920
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003921 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003922 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3923 "channel list");
3924 return -1;
3925 }
3926
3927 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3928 WPS_DEV_TYPE_LEN);
3929
3930 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3931 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3932 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3933
3934 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3935 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3936
3937 p2p.max_peers = 100;
3938
3939 if (wpa_s->conf->p2p_ssid_postfix) {
3940 p2p.ssid_postfix_len =
3941 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3942 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3943 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3944 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3945 p2p.ssid_postfix_len);
3946 }
3947
3948 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3949
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003950 p2p.max_listen = wpa_s->max_remain_on_chan;
3951
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003952 global->p2p = p2p_init(&p2p);
3953 if (global->p2p == NULL)
3954 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003955 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003956
3957 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3958 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3959 continue;
3960 p2p_add_wps_vendor_extension(
3961 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3962 }
3963
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003964 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 return 0;
3967}
3968
3969
3970/**
3971 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3972 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3973 *
3974 * This function deinitialize per-interface P2P data.
3975 */
3976void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3977{
3978 if (wpa_s->driver && wpa_s->drv_priv)
3979 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003980
3981 if (wpa_s->go_params) {
3982 /* Clear any stored provisioning info */
3983 p2p_clear_provisioning_info(
3984 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003985 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003986 }
3987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 os_free(wpa_s->go_params);
3989 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07003990 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003991 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3992 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3993 wpa_s->p2p_long_listen = 0;
3994 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3995 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3996 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003997 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003998 wpas_p2p_listen_work_done(wpa_s);
3999 if (wpa_s->p2p_send_action_work) {
4000 os_free(wpa_s->p2p_send_action_work->ctx);
4001 radio_work_done(wpa_s->p2p_send_action_work);
4002 wpa_s->p2p_send_action_work = NULL;
4003 }
4004 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004005
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004006 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4007 wpa_s->p2p_oob_dev_pw = NULL;
4008
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004009 /* TODO: remove group interface from the driver if this wpa_s instance
4010 * is on top of a P2P group interface */
4011}
4012
4013
4014/**
4015 * wpas_p2p_deinit_global - Deinitialize global P2P module
4016 * @global: Pointer to global data from wpa_supplicant_init()
4017 *
4018 * This function deinitializes the global (per device) P2P module.
4019 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004020static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021{
4022 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004024 wpa_s = global->ifaces;
4025 if (wpa_s)
4026 wpas_p2p_service_flush(wpa_s);
4027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 if (global->p2p == NULL)
4029 return;
4030
4031 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004032 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4033 wpa_s = wpa_s->next;
4034 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 tmp = global->ifaces;
4036 while (tmp &&
4037 (tmp == wpa_s ||
4038 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4039 tmp = tmp->next;
4040 }
4041 if (tmp == NULL)
4042 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004043 /* Disconnect from the P2P group and deinit the interface */
4044 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004045 }
4046
4047 /*
4048 * Deinit GO data on any possibly remaining interface (if main
4049 * interface is used as GO).
4050 */
4051 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4052 if (wpa_s->ap_iface)
4053 wpas_p2p_group_deinit(wpa_s);
4054 }
4055
4056 p2p_deinit(global->p2p);
4057 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004058 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059}
4060
4061
4062static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4063{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004064 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4065 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004066 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067 if (wpa_s->drv_flags &
4068 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4069 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4070 return 1; /* P2P group requires a new interface in every case
4071 */
4072 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4073 return 0; /* driver does not support concurrent operations */
4074 if (wpa_s->global->ifaces->next)
4075 return 1; /* more that one interface already in use */
4076 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4077 return 1; /* this interface is already in use */
4078 return 0;
4079}
4080
4081
4082static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4083 const u8 *peer_addr,
4084 enum p2p_wps_method wps_method,
4085 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004086 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004087 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004089 if (persistent_group && wpa_s->conf->persistent_reconnect)
4090 persistent_group = 2;
4091
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004092 /*
4093 * Increase GO config timeout if HT40 is used since it takes some time
4094 * to scan channels for coex purposes before the BSS can be started.
4095 */
4096 p2p_set_config_timeout(wpa_s->global->p2p,
4097 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4100 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004101 persistent_group, ssid ? ssid->ssid : NULL,
4102 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004103 wpa_s->p2p_pd_before_go_neg, pref_freq,
4104 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4105 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004106}
4107
4108
4109static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4110 const u8 *peer_addr,
4111 enum p2p_wps_method wps_method,
4112 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004113 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004114 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004116 if (persistent_group && wpa_s->conf->persistent_reconnect)
4117 persistent_group = 2;
4118
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4120 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004121 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004122 ssid ? ssid->ssid_len : 0, pref_freq,
4123 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4124 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125}
4126
4127
4128static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4129{
4130 wpa_s->p2p_join_scan_count++;
4131 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4132 wpa_s->p2p_join_scan_count);
4133 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4134 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4135 " for join operationg - stop join attempt",
4136 MAC2STR(wpa_s->pending_join_iface_addr));
4137 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004138 if (wpa_s->p2p_auto_pd) {
4139 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004140 wpa_msg_global(wpa_s, MSG_INFO,
4141 P2P_EVENT_PROV_DISC_FAILURE
4142 " p2p_dev_addr=" MACSTR " status=N/A",
4143 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004144 return;
4145 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004146 wpa_msg_global(wpa_s->parent, MSG_INFO,
4147 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004148 }
4149}
4150
4151
Dmitry Shmidt04949592012-07-19 12:16:46 -07004152static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4153{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004154 int res;
4155 unsigned int num, i;
4156 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004157
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004158 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4159 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004160 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004161 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004162
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004163 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4164 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004165 if (!freqs)
4166 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004167
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004168 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4169 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004170
4171 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004172 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004173 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4174 freq);
4175 res = 0;
4176 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004177 }
4178 }
4179
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004180 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004181 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004182
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004183exit_free:
4184 os_free(freqs);
4185 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004186}
4187
4188
4189static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4190 const u8 *peer_dev_addr)
4191{
4192 struct wpa_bss *bss;
4193 int updated;
4194
4195 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4196 if (bss == NULL)
4197 return -1;
4198 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4199 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4200 "last scan");
4201 return 0;
4202 }
4203
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004204 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4205 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004206 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4207 "%ld.%06ld (%supdated in last scan)",
4208 bss->last_update.sec, bss->last_update.usec,
4209 updated ? "": "not ");
4210
4211 return updated;
4212}
4213
4214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004215static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4216 struct wpa_scan_results *scan_res)
4217{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004218 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219 int freq;
4220 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004221
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004222 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4223
4224 if (wpa_s->global->p2p_disabled)
4225 return;
4226
Dmitry Shmidt04949592012-07-19 12:16:46 -07004227 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4228 scan_res ? (int) scan_res->num : -1,
4229 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230
4231 if (scan_res)
4232 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4233
Dmitry Shmidt04949592012-07-19 12:16:46 -07004234 if (wpa_s->p2p_auto_pd) {
4235 int join = wpas_p2p_peer_go(wpa_s,
4236 wpa_s->pending_join_dev_addr);
4237 if (join == 0 &&
4238 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4239 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004240 bss = wpa_bss_get_bssid_latest(
4241 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004242 if (bss) {
4243 freq = bss->freq;
4244 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4245 "the peer " MACSTR " at %d MHz",
4246 wpa_s->auto_pd_scan_retry,
4247 MAC2STR(wpa_s->
4248 pending_join_dev_addr),
4249 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004250 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004251 return;
4252 }
4253 }
4254
4255 if (join < 0)
4256 join = 0;
4257
4258 wpa_s->p2p_auto_pd = 0;
4259 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4260 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4261 MAC2STR(wpa_s->pending_join_dev_addr), join);
4262 if (p2p_prov_disc_req(wpa_s->global->p2p,
4263 wpa_s->pending_join_dev_addr,
4264 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004265 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004266 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004267 wpa_msg_global(wpa_s, MSG_INFO,
4268 P2P_EVENT_PROV_DISC_FAILURE
4269 " p2p_dev_addr=" MACSTR " status=N/A",
4270 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004271 }
4272 return;
4273 }
4274
4275 if (wpa_s->p2p_auto_join) {
4276 int join = wpas_p2p_peer_go(wpa_s,
4277 wpa_s->pending_join_dev_addr);
4278 if (join < 0) {
4279 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4280 "running a GO -> use GO Negotiation");
4281 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4282 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4283 wpa_s->p2p_persistent_group, 0, 0, 0,
4284 wpa_s->p2p_go_intent,
4285 wpa_s->p2p_connect_freq,
4286 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004287 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004288 wpa_s->p2p_go_ht40,
4289 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004290 return;
4291 }
4292
4293 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4294 "try to join the group", join ? "" :
4295 " in older scan");
4296 if (!join)
4297 wpa_s->p2p_fallback_to_go_neg = 1;
4298 }
4299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4301 wpa_s->pending_join_iface_addr);
4302 if (freq < 0 &&
4303 p2p_get_interface_addr(wpa_s->global->p2p,
4304 wpa_s->pending_join_dev_addr,
4305 iface_addr) == 0 &&
4306 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4307 {
4308 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4309 "address for join from " MACSTR " to " MACSTR
4310 " based on newly discovered P2P peer entry",
4311 MAC2STR(wpa_s->pending_join_iface_addr),
4312 MAC2STR(iface_addr));
4313 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4314 ETH_ALEN);
4315
4316 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4317 wpa_s->pending_join_iface_addr);
4318 }
4319 if (freq >= 0) {
4320 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4321 "from P2P peer table: %d MHz", freq);
4322 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004323 if (wpa_s->p2p_join_ssid_len) {
4324 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4325 MACSTR " and SSID %s",
4326 MAC2STR(wpa_s->pending_join_iface_addr),
4327 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4328 wpa_s->p2p_join_ssid_len));
4329 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4330 wpa_s->p2p_join_ssid,
4331 wpa_s->p2p_join_ssid_len);
4332 }
4333 if (!bss) {
4334 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4335 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4336 bss = wpa_bss_get_bssid_latest(wpa_s,
4337 wpa_s->pending_join_iface_addr);
4338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 if (bss) {
4340 freq = bss->freq;
4341 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004342 "from BSS table: %d MHz (SSID %s)", freq,
4343 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344 }
4345 if (freq > 0) {
4346 u16 method;
4347
Dmitry Shmidt04949592012-07-19 12:16:46 -07004348 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004349 wpa_msg_global(wpa_s->parent, MSG_INFO,
4350 P2P_EVENT_GROUP_FORMATION_FAILURE
4351 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004352 return;
4353 }
4354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004355 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4356 "prior to joining an existing group (GO " MACSTR
4357 " freq=%u MHz)",
4358 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4359 wpa_s->pending_pd_before_join = 1;
4360
4361 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004362 case WPS_PIN_DISPLAY:
4363 method = WPS_CONFIG_KEYPAD;
4364 break;
4365 case WPS_PIN_KEYPAD:
4366 method = WPS_CONFIG_DISPLAY;
4367 break;
4368 case WPS_PBC:
4369 method = WPS_CONFIG_PUSHBUTTON;
4370 break;
4371 default:
4372 method = 0;
4373 break;
4374 }
4375
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4377 wpa_s->pending_join_dev_addr) ==
4378 method)) {
4379 /*
4380 * We have already performed provision discovery for
4381 * joining the group. Proceed directly to join
4382 * operation without duplicated provision discovery. */
4383 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4384 "with " MACSTR " already done - proceed to "
4385 "join",
4386 MAC2STR(wpa_s->pending_join_dev_addr));
4387 wpa_s->pending_pd_before_join = 0;
4388 goto start;
4389 }
4390
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004392 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004393 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004394 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4395 "Discovery Request before joining an "
4396 "existing group");
4397 wpa_s->pending_pd_before_join = 0;
4398 goto start;
4399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400 return;
4401 }
4402
4403 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4404 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4405 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4406 wpas_p2p_check_join_scan_limit(wpa_s);
4407 return;
4408
4409start:
4410 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004411 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004412}
4413
4414
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004415static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4416 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004417{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004418 int ret;
4419 struct wpa_driver_scan_params params;
4420 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004421 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004422 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423
4424 os_memset(&params, 0, sizeof(params));
4425
4426 /* P2P Wildcard SSID */
4427 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004428 if (ssid && ssid_len) {
4429 params.ssids[0].ssid = ssid;
4430 params.ssids[0].ssid_len = ssid_len;
4431 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4432 wpa_s->p2p_join_ssid_len = ssid_len;
4433 } else {
4434 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4435 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4436 wpa_s->p2p_join_ssid_len = 0;
4437 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438
4439 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004440 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4441 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4442 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004443 if (wps_ie == NULL) {
4444 wpas_p2p_scan_res_join(wpa_s, NULL);
4445 return;
4446 }
4447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004448 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4449 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450 if (ies == NULL) {
4451 wpabuf_free(wps_ie);
4452 wpas_p2p_scan_res_join(wpa_s, NULL);
4453 return;
4454 }
4455 wpabuf_put_buf(ies, wps_ie);
4456 wpabuf_free(wps_ie);
4457
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004458 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004460 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 params.extra_ies = wpabuf_head(ies);
4462 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004463
4464 if (!freq) {
4465 int oper_freq;
4466 /*
4467 * If freq is not provided, check the operating freq of the GO
4468 * and use a single channel scan on if possible.
4469 */
4470 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4471 wpa_s->pending_join_iface_addr);
4472 if (oper_freq > 0)
4473 freq = oper_freq;
4474 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004475 if (freq > 0) {
4476 freqs[0] = freq;
4477 params.freqs = freqs;
4478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004479
4480 /*
4481 * Run a scan to update BSS table and start Provision Discovery once
4482 * the new scan results become available.
4483 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004484 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004485 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004486 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004487 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004488 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004489 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004490
4491 wpabuf_free(ies);
4492
4493 if (ret) {
4494 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4495 "try again later");
4496 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4497 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4498 wpas_p2p_check_join_scan_limit(wpa_s);
4499 }
4500}
4501
4502
Dmitry Shmidt04949592012-07-19 12:16:46 -07004503static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4504{
4505 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004506 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004507}
4508
4509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004510static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004511 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004512 int auto_join, int op_freq,
4513 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004514{
4515 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004516 MACSTR " dev " MACSTR " op_freq=%d)%s",
4517 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004518 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004519 if (ssid && ssid_len) {
4520 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4521 wpa_ssid_txt(ssid, ssid_len));
4522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004523
Dmitry Shmidt04949592012-07-19 12:16:46 -07004524 wpa_s->p2p_auto_pd = 0;
4525 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004526 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4527 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4528 wpa_s->pending_join_wps_method = wps_method;
4529
4530 /* Make sure we are not running find during connection establishment */
4531 wpas_p2p_stop_find(wpa_s);
4532
4533 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004534 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004535 return 0;
4536}
4537
4538
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004539static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4540 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541{
4542 struct wpa_supplicant *group;
4543 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004544 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004545
4546 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4547 if (group == NULL)
4548 return -1;
4549 if (group != wpa_s) {
4550 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4551 sizeof(group->p2p_pin));
4552 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004553 } else {
4554 /*
4555 * Need to mark the current interface for p2p_group_formation
4556 * when a separate group interface is not used. This is needed
4557 * to allow p2p_cancel stop a pending p2p_connect-join.
4558 * wpas_p2p_init_group_interface() addresses this for the case
4559 * where a separate group interface is used.
4560 */
4561 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004562 }
4563
4564 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004565 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004566
4567 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004568 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004569 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4570 ETH_ALEN);
4571 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004572 if (freq && ssid && ssid_len) {
4573 res.freq = freq;
4574 res.ssid_len = ssid_len;
4575 os_memcpy(res.ssid, ssid, ssid_len);
4576 } else {
4577 bss = wpa_bss_get_bssid_latest(wpa_s,
4578 wpa_s->pending_join_iface_addr);
4579 if (bss) {
4580 res.freq = bss->freq;
4581 res.ssid_len = bss->ssid_len;
4582 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4583 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4584 bss->freq,
4585 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4586 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004587 }
4588
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004589 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4590 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4591 "starting client");
4592 wpa_drv_cancel_remain_on_channel(wpa_s);
4593 wpa_s->off_channel_freq = 0;
4594 wpa_s->roc_waiting_drv_freq = 0;
4595 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 wpas_start_wps_enrollee(group, &res);
4597
4598 /*
4599 * Allow a longer timeout for join-a-running-group than normal 15
4600 * second group formation timeout since the GO may not have authorized
4601 * our connection yet.
4602 */
4603 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4604 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4605 wpa_s, NULL);
4606
4607 return 0;
4608}
4609
4610
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004611static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004612 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004613{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004614 struct wpa_used_freq_data *freqs;
4615 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004616 unsigned int freq_in_use = 0, num, i;
4617
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004618 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4619 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004620 if (!freqs)
4621 return -1;
4622
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004623 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4624 wpa_s->num_multichan_concurrent);
4625
4626 /*
4627 * It is possible that the total number of used frequencies is bigger
4628 * than the number of frequencies used for P2P, so get the system wide
4629 * number of unused frequencies.
4630 */
4631 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4632
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004633 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004634 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4635 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004636
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004637 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004638 int ret;
4639 if (go)
4640 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4641 else
4642 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4643 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004644 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4645 "(%u MHz) is not supported for P2P uses",
4646 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004647 res = -3;
4648 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004649 }
4650
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004651 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004652 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004653 freq_in_use = 1;
4654 }
4655
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004656 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004657 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4658 freq);
4659 res = -2;
4660 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004661 }
4662 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4663 "requested channel (%u MHz)", freq);
4664 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004665 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004666 }
4667
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004668 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004669
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004670 /* We have a candidate frequency to use */
4671 if (best_freq > 0) {
4672 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004673 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004674 best_freq);
4675 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004676 } else {
4677 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 -07004678 best_freq);
4679 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004680 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004681 } else if (num_unused > 0) {
4682 wpa_printf(MSG_DEBUG,
4683 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4684 *force_freq = 0;
4685 } else {
4686 wpa_printf(MSG_DEBUG,
4687 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4688 res = -2;
4689 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004690 }
4691
4692exit_ok:
4693 res = 0;
4694exit_free:
4695 os_free(freqs);
4696 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004697}
4698
4699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004700/**
4701 * wpas_p2p_connect - Request P2P Group Formation to be started
4702 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4703 * @peer_addr: Address of the peer P2P Device
4704 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4705 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004706 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707 * @join: Whether to join an existing group (as a client) instead of starting
4708 * Group Owner negotiation; @peer_addr is BSSID in that case
4709 * @auth: Whether to only authorize the connection instead of doing that and
4710 * initiating Group Owner negotiation
4711 * @go_intent: GO Intent or -1 to use default
4712 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004713 * @persistent_id: Persistent group credentials to use for forcing GO
4714 * parameters or -1 to generate new values (SSID/passphrase)
4715 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4716 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004717 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004718 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4720 * failure, -2 on failure due to channel not currently available,
4721 * -3 if forced channel is not supported
4722 */
4723int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4724 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004725 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004726 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004727 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004728{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004729 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004730 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004732 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004733 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004734
4735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4736 return -1;
4737
Dmitry Shmidt04949592012-07-19 12:16:46 -07004738 if (persistent_id >= 0) {
4739 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4740 if (ssid == NULL || ssid->disabled != 2 ||
4741 ssid->mode != WPAS_MODE_P2P_GO)
4742 return -1;
4743 }
4744
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004745 os_free(wpa_s->global->add_psk);
4746 wpa_s->global->add_psk = NULL;
4747
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004748 wpa_s->global->p2p_fail_on_wps_complete = 0;
4749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 if (go_intent < 0)
4751 go_intent = wpa_s->conf->p2p_go_intent;
4752
4753 if (!auth)
4754 wpa_s->p2p_long_listen = 0;
4755
4756 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004757 wpa_s->p2p_persistent_group = !!persistent_group;
4758 wpa_s->p2p_persistent_id = persistent_id;
4759 wpa_s->p2p_go_intent = go_intent;
4760 wpa_s->p2p_connect_freq = freq;
4761 wpa_s->p2p_fallback_to_go_neg = 0;
4762 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004763 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004764 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004765
4766 if (pin)
4767 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4768 else if (wps_method == WPS_PIN_DISPLAY) {
4769 ret = wps_generate_pin();
4770 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4771 ret);
4772 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4773 wpa_s->p2p_pin);
4774 } else
4775 wpa_s->p2p_pin[0] = '\0';
4776
Dmitry Shmidt04949592012-07-19 12:16:46 -07004777 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4779 if (auth) {
4780 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4781 "connect a running group from " MACSTR,
4782 MAC2STR(peer_addr));
4783 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4784 return ret;
4785 }
4786 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4787 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4788 iface_addr) < 0) {
4789 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4790 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4791 dev_addr);
4792 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004793 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004794 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004795 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4796 "%ld.%06ld",
4797 wpa_s->p2p_auto_started.sec,
4798 wpa_s->p2p_auto_started.usec);
4799 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004800 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004801 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004802 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004803 return -1;
4804 return ret;
4805 }
4806
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004807 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4808 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004809 if (res)
4810 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004811 wpas_p2p_set_own_freq_preference(wpa_s,
4812 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004813
4814 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004816 if (wpa_s->create_p2p_iface) {
4817 /* Prepare to add a new interface for the group */
4818 iftype = WPA_IF_P2P_GROUP;
4819 if (go_intent == 15)
4820 iftype = WPA_IF_P2P_GO;
4821 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4822 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4823 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004825 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004827 if_addr = wpa_s->pending_interface_addr;
4828 } else
4829 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004830
4831 if (auth) {
4832 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004833 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004834 force_freq, persistent_group, ssid,
4835 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 return -1;
4837 return ret;
4838 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004839
4840 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4841 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004842 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004843 if (wpa_s->create_p2p_iface)
4844 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004845 return -1;
4846 }
4847 return ret;
4848}
4849
4850
4851/**
4852 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4853 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4854 * @freq: Frequency of the channel in MHz
4855 * @duration: Duration of the stay on the channel in milliseconds
4856 *
4857 * This callback is called when the driver indicates that it has started the
4858 * requested remain-on-channel duration.
4859 */
4860void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4861 unsigned int freq, unsigned int duration)
4862{
4863 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4864 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4866 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4867 wpa_s->pending_listen_duration);
4868 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004869 } else {
4870 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4871 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4872 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 }
4874}
4875
4876
4877static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4878 unsigned int timeout)
4879{
4880 /* Limit maximum Listen state time based on driver limitation. */
4881 if (timeout > wpa_s->max_remain_on_chan)
4882 timeout = wpa_s->max_remain_on_chan;
4883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 return p2p_listen(wpa_s->global->p2p, timeout);
4885}
4886
4887
4888/**
4889 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4890 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4891 * @freq: Frequency of the channel in MHz
4892 *
4893 * This callback is called when the driver indicates that a remain-on-channel
4894 * operation has been completed, i.e., the duration on the requested channel
4895 * has timed out.
4896 */
4897void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4898 unsigned int freq)
4899{
4900 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4901 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004902 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004903 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004904 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4905 return;
4906 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4907 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004908 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909 return;
4910 if (wpa_s->p2p_long_listen > 0)
4911 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4912 if (wpa_s->p2p_long_listen > 0) {
4913 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4914 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004915 } else {
4916 /*
4917 * When listen duration is over, stop listen & update p2p_state
4918 * to IDLE.
4919 */
4920 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004921 }
4922}
4923
4924
4925/**
4926 * wpas_p2p_group_remove - Remove a P2P group
4927 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4928 * @ifname: Network interface name of the group interface or "*" to remove all
4929 * groups
4930 * Returns: 0 on success, -1 on failure
4931 *
4932 * This function is used to remove a P2P group. This can be used to disconnect
4933 * from a group in which the local end is a P2P Client or to end a P2P Group in
4934 * case the local end is the Group Owner. If a virtual network interface was
4935 * created for this group, that interface will be removed. Otherwise, only the
4936 * configured P2P group network will be removed from the interface.
4937 */
4938int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4939{
4940 struct wpa_global *global = wpa_s->global;
4941
4942 if (os_strcmp(ifname, "*") == 0) {
4943 struct wpa_supplicant *prev;
4944 wpa_s = global->ifaces;
4945 while (wpa_s) {
4946 prev = wpa_s;
4947 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004948 if (prev->p2p_group_interface !=
4949 NOT_P2P_GROUP_INTERFACE ||
4950 (prev->current_ssid &&
4951 prev->current_ssid->p2p_group))
4952 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 }
4954 return 0;
4955 }
4956
4957 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4958 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4959 break;
4960 }
4961
4962 return wpas_p2p_disconnect(wpa_s);
4963}
4964
4965
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004966static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4967{
4968 unsigned int r;
4969
4970 if (freq == 2) {
4971 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4972 "band");
4973 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004974 p2p_supported_freq_go(wpa_s->global->p2p,
4975 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004976 freq = wpa_s->best_24_freq;
4977 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4978 "channel: %d MHz", freq);
4979 } else {
4980 os_get_random((u8 *) &r, sizeof(r));
4981 freq = 2412 + (r % 3) * 25;
4982 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4983 "channel: %d MHz", freq);
4984 }
4985 }
4986
4987 if (freq == 5) {
4988 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4989 "band");
4990 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004991 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004992 wpa_s->best_5_freq)) {
4993 freq = wpa_s->best_5_freq;
4994 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4995 "channel: %d MHz", freq);
4996 } else {
4997 os_get_random((u8 *) &r, sizeof(r));
4998 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004999 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005000 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5001 "5 GHz channel for P2P group");
5002 return -1;
5003 }
5004 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5005 "channel: %d MHz", freq);
5006 }
5007 }
5008
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005009 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005010 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5011 "(%u MHz) is not supported for P2P uses",
5012 freq);
5013 return -1;
5014 }
5015
5016 return freq;
5017}
5018
5019
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005020static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5021 struct p2p_go_neg_results *params,
5022 const struct p2p_channels *channels)
5023{
5024 unsigned int i, r;
5025
5026 /* first try some random selection of the social channels */
5027 os_get_random((u8 *) &r, sizeof(r));
5028
5029 for (i = 0; i < 3; i++) {
5030 params->freq = 2412 + ((r + i) % 3) * 25;
5031 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5032 freq_included(channels, params->freq))
5033 goto out;
5034 }
5035
5036 /* try all channels in reg. class 81 */
5037 for (i = 0; i < 11; i++) {
5038 params->freq = 2412 + i * 5;
5039 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5040 freq_included(channels, params->freq))
5041 goto out;
5042 }
5043
5044 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel allowed");
5045 return -1;
5046out:
5047 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5048 params->freq);
5049 return 0;
5050}
5051
5052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005053static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5054 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005055 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005056 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005058 struct wpa_used_freq_data *freqs;
5059 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005060 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061
5062 os_memset(params, 0, sizeof(*params));
5063 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005064 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005065 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005067 if (!freq_included(channels, freq)) {
5068 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5069 "accepted", freq);
5070 return -1;
5071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5073 "frequency %d MHz", freq);
5074 params->freq = freq;
5075 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5076 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005077 wpa_s->conf->p2p_oper_channel <= 11 &&
5078 freq_included(channels,
5079 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5081 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5082 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005083 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5084 wpa_s->conf->p2p_oper_reg_class == 116 ||
5085 wpa_s->conf->p2p_oper_reg_class == 117 ||
5086 wpa_s->conf->p2p_oper_reg_class == 124 ||
5087 wpa_s->conf->p2p_oper_reg_class == 126 ||
5088 wpa_s->conf->p2p_oper_reg_class == 127) &&
5089 freq_included(channels,
5090 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005091 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5092 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5093 "frequency %d MHz", params->freq);
5094 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5095 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005096 p2p_supported_freq_go(wpa_s->global->p2p,
5097 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005098 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005099 params->freq = wpa_s->best_overall_freq;
5100 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5101 "channel %d MHz", params->freq);
5102 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5103 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005104 p2p_supported_freq_go(wpa_s->global->p2p,
5105 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005106 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005107 params->freq = wpa_s->best_24_freq;
5108 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5109 "channel %d MHz", params->freq);
5110 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5111 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005112 p2p_supported_freq_go(wpa_s->global->p2p,
5113 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005114 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115 params->freq = wpa_s->best_5_freq;
5116 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5117 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005118 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5119 channels))) {
5120 params->freq = pref_freq;
5121 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5122 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005123 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005124 /* no preference, select some channel */
5125 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005126 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127 }
5128
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005129 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5130 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005131 if (!freqs)
5132 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005134 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005135 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005136
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005137 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5138
5139 /* First try the best used frequency if possible */
5140 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5141 params->freq = cand_freq;
5142 } else if (!freq) {
5143 /* Try any of the used frequencies */
5144 for (i = 0; i < num; i++) {
5145 if (freq_included(channels, freqs[i].freq)) {
5146 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5147 freqs[i].freq);
5148 params->freq = freqs[i].freq;
5149 break;
5150 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005151 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005152
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005153 if (i == num) {
5154 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005155 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005156 os_free(freqs);
5157 return -1;
5158 } else {
5159 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5160 }
5161 }
5162 } else {
5163 for (i = 0; i < num; i++) {
5164 if (freqs[i].freq == freq)
5165 break;
5166 }
5167
5168 if (i == num) {
5169 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5170 if (freq)
5171 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5172 os_free(freqs);
5173 return -1;
5174 } else {
5175 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5176 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005177 }
5178 }
5179
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005180 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005181 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005182}
5183
5184
5185static struct wpa_supplicant *
5186wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5187 int go)
5188{
5189 struct wpa_supplicant *group_wpa_s;
5190
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005191 if (!wpas_p2p_create_iface(wpa_s)) {
5192 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5193 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005194 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005196 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005197
5198 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005199 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005200 wpa_msg_global(wpa_s, MSG_ERROR,
5201 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005202 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005203 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5205 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005206 wpa_msg_global(wpa_s, MSG_ERROR,
5207 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005208 wpas_p2p_remove_pending_group_interface(wpa_s);
5209 return NULL;
5210 }
5211
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005212 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5213 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005214 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005215 return group_wpa_s;
5216}
5217
5218
5219/**
5220 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5221 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5222 * @persistent_group: Whether to create a persistent group
5223 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005224 * @ht40: Start GO with 40 MHz channel width
5225 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226 * Returns: 0 on success, -1 on failure
5227 *
5228 * This function creates a new P2P group with the local end as the Group Owner,
5229 * i.e., without using Group Owner Negotiation.
5230 */
5231int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005232 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005233{
5234 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005235
5236 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5237 return -1;
5238
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005239 os_free(wpa_s->global->add_psk);
5240 wpa_s->global->add_psk = NULL;
5241
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005242 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005243 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005244 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005245
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005246 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5247 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005248 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005249
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005250 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005251 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005252 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005253 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005254 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5255 "(%u MHz) is not supported for P2P uses",
5256 params.freq);
5257 return -1;
5258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 p2p_go_params(wpa_s->global->p2p, &params);
5260 params.persistent_group = persistent_group;
5261
5262 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5263 if (wpa_s == NULL)
5264 return -1;
5265 wpas_start_wps_go(wpa_s, &params, 0);
5266
5267 return 0;
5268}
5269
5270
5271static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005272 struct wpa_ssid *params, int addr_allocated,
5273 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005274{
5275 struct wpa_ssid *ssid;
5276
5277 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5278 if (wpa_s == NULL)
5279 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005280 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281
5282 wpa_supplicant_ap_deinit(wpa_s);
5283
5284 ssid = wpa_config_add_network(wpa_s->conf);
5285 if (ssid == NULL)
5286 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005287 wpa_config_set_network_defaults(ssid);
5288 ssid->temporary = 1;
5289 ssid->proto = WPA_PROTO_RSN;
5290 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5291 ssid->group_cipher = WPA_CIPHER_CCMP;
5292 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5293 ssid->ssid = os_malloc(params->ssid_len);
5294 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005295 wpa_config_remove_network(wpa_s->conf, ssid->id);
5296 return -1;
5297 }
5298 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5299 ssid->ssid_len = params->ssid_len;
5300 ssid->p2p_group = 1;
5301 ssid->export_keys = 1;
5302 if (params->psk_set) {
5303 os_memcpy(ssid->psk, params->psk, 32);
5304 ssid->psk_set = 1;
5305 }
5306 if (params->passphrase)
5307 ssid->passphrase = os_strdup(params->passphrase);
5308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005310 wpa_s->p2p_in_invitation = 1;
5311 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312
Dmitry Shmidt15907092014-03-25 10:42:57 -07005313 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5314 NULL);
5315 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5316 wpas_p2p_group_formation_timeout,
5317 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005318 wpa_supplicant_select_network(wpa_s, ssid);
5319
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320 return 0;
5321}
5322
5323
5324int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5325 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005326 int force_freq, int neg_freq, int ht40,
5327 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005328 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005329{
5330 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005331 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005332
5333 if (ssid->disabled != 2 || ssid->ssid == NULL)
5334 return -1;
5335
5336 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5337 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5338 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5339 "already running");
5340 return 0;
5341 }
5342
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005343 os_free(wpa_s->global->add_psk);
5344 wpa_s->global->add_psk = NULL;
5345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005347 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005348
Dmitry Shmidt04949592012-07-19 12:16:46 -07005349 wpa_s->p2p_fallback_to_go_neg = 0;
5350
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005351 if (force_freq > 0) {
5352 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5353 if (freq < 0)
5354 return -1;
5355 } else {
5356 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5357 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5358 freq = 0;
5359 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005360
Dmitry Shmidt15907092014-03-25 10:42:57 -07005361 if (ssid->mode == WPAS_MODE_INFRA)
5362 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5363
5364 if (ssid->mode != WPAS_MODE_P2P_GO)
5365 return -1;
5366
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005367 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005368 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005369
5370 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005371 params.psk_set = ssid->psk_set;
5372 if (params.psk_set)
5373 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005374 if (ssid->passphrase) {
5375 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5376 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5377 "persistent group");
5378 return -1;
5379 }
5380 os_strlcpy(params.passphrase, ssid->passphrase,
5381 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5384 params.ssid_len = ssid->ssid_len;
5385 params.persistent_group = 1;
5386
5387 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5388 if (wpa_s == NULL)
5389 return -1;
5390
Dmitry Shmidt56052862013-10-04 10:23:25 -07005391 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005392 wpas_start_wps_go(wpa_s, &params, 0);
5393
5394 return 0;
5395}
5396
5397
5398static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5399 struct wpabuf *proberesp_ies)
5400{
5401 struct wpa_supplicant *wpa_s = ctx;
5402 if (wpa_s->ap_iface) {
5403 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005404 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5405 wpabuf_free(beacon_ies);
5406 wpabuf_free(proberesp_ies);
5407 return;
5408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409 if (beacon_ies) {
5410 wpabuf_free(hapd->p2p_beacon_ie);
5411 hapd->p2p_beacon_ie = beacon_ies;
5412 }
5413 wpabuf_free(hapd->p2p_probe_resp_ie);
5414 hapd->p2p_probe_resp_ie = proberesp_ies;
5415 } else {
5416 wpabuf_free(beacon_ies);
5417 wpabuf_free(proberesp_ies);
5418 }
5419 wpa_supplicant_ap_update_beacon(wpa_s);
5420}
5421
5422
5423static void wpas_p2p_idle_update(void *ctx, int idle)
5424{
5425 struct wpa_supplicant *wpa_s = ctx;
5426 if (!wpa_s->ap_iface)
5427 return;
5428 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005429 if (idle) {
5430 if (wpa_s->global->p2p_fail_on_wps_complete &&
5431 wpa_s->p2p_in_provisioning) {
5432 wpas_p2p_grpform_fail_after_wps(wpa_s);
5433 return;
5434 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005435 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005436 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005437 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5438}
5439
5440
5441struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005442 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443{
5444 struct p2p_group *group;
5445 struct p2p_group_config *cfg;
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5448 return NULL;
5449
5450 cfg = os_zalloc(sizeof(*cfg));
5451 if (cfg == NULL)
5452 return NULL;
5453
Dmitry Shmidt04949592012-07-19 12:16:46 -07005454 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005455 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005456 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005457 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5459 if (wpa_s->max_stations &&
5460 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5461 cfg->max_clients = wpa_s->max_stations;
5462 else
5463 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005464 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5465 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005466 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 cfg->cb_ctx = wpa_s;
5468 cfg->ie_update = wpas_p2p_ie_update;
5469 cfg->idle_update = wpas_p2p_idle_update;
5470
5471 group = p2p_group_init(wpa_s->global->p2p, cfg);
5472 if (group == NULL)
5473 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005474 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475 p2p_group_notif_formation_done(group);
5476 wpa_s->p2p_group = group;
5477 return group;
5478}
5479
5480
5481void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5482 int registrar)
5483{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005484 struct wpa_ssid *ssid = wpa_s->current_ssid;
5485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005486 if (!wpa_s->p2p_in_provisioning) {
5487 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5488 "provisioning not in progress");
5489 return;
5490 }
5491
Dmitry Shmidt04949592012-07-19 12:16:46 -07005492 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5493 u8 go_dev_addr[ETH_ALEN];
5494 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5495 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5496 ssid->ssid_len);
5497 /* Clear any stored provisioning info */
5498 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5499 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5502 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005503 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005504 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5505 /*
5506 * Use a separate timeout for initial data connection to
5507 * complete to allow the group to be removed automatically if
5508 * something goes wrong in this step before the P2P group idle
5509 * timeout mechanism is taken into use.
5510 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005511 wpa_dbg(wpa_s, MSG_DEBUG,
5512 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5513 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005514 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5515 wpas_p2p_group_formation_timeout,
5516 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005517 } else if (ssid) {
5518 /*
5519 * Use a separate timeout for initial data connection to
5520 * complete to allow the group to be removed automatically if
5521 * the client does not complete data connection successfully.
5522 */
5523 wpa_dbg(wpa_s, MSG_DEBUG,
5524 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5525 P2P_MAX_INITIAL_CONN_WAIT_GO);
5526 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5527 wpas_p2p_group_formation_timeout,
5528 wpa_s->parent, NULL);
5529 /*
5530 * Complete group formation on first successful data connection
5531 */
5532 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005534 if (wpa_s->global->p2p)
5535 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005536 wpas_group_formation_completed(wpa_s, 1);
5537}
5538
5539
Jouni Malinen75ecf522011-06-27 15:19:46 -07005540void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5541 struct wps_event_fail *fail)
5542{
5543 if (!wpa_s->p2p_in_provisioning) {
5544 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5545 "provisioning not in progress");
5546 return;
5547 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005548
5549 if (wpa_s->go_params) {
5550 p2p_clear_provisioning_info(
5551 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005552 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005553 }
5554
Jouni Malinen75ecf522011-06-27 15:19:46 -07005555 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005556
5557 if (wpa_s == wpa_s->global->p2p_group_formation) {
5558 /*
5559 * Allow some time for the failed WPS negotiation exchange to
5560 * complete, but remove the group since group formation cannot
5561 * succeed after provisioning failure.
5562 */
5563 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5564 wpa_s->global->p2p_fail_on_wps_complete = 1;
5565 eloop_deplete_timeout(0, 50000,
5566 wpas_p2p_group_formation_timeout,
5567 wpa_s->parent, NULL);
5568 }
5569}
5570
5571
5572int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5573{
5574 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5575 !wpa_s->p2p_in_provisioning)
5576 return 0;
5577
5578 wpas_p2p_grpform_fail_after_wps(wpa_s);
5579
5580 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005581}
5582
5583
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005585 const char *config_method,
5586 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005587{
5588 u16 config_methods;
5589
Dmitry Shmidt04949592012-07-19 12:16:46 -07005590 wpa_s->p2p_fallback_to_go_neg = 0;
5591 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005592 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005593 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005594 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005595 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005596 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5597 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005598 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005599 else {
5600 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005601 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005602 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603
Dmitry Shmidt04949592012-07-19 12:16:46 -07005604 if (use == WPAS_P2P_PD_AUTO) {
5605 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5606 wpa_s->pending_pd_config_methods = config_methods;
5607 wpa_s->p2p_auto_pd = 1;
5608 wpa_s->p2p_auto_join = 0;
5609 wpa_s->pending_pd_before_join = 0;
5610 wpa_s->auto_pd_scan_retry = 0;
5611 wpas_p2p_stop_find(wpa_s);
5612 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005613 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005614 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5615 wpa_s->p2p_auto_started.sec,
5616 wpa_s->p2p_auto_started.usec);
5617 wpas_p2p_join_scan(wpa_s, NULL);
5618 return 0;
5619 }
5620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005621 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5622 return -1;
5623
5624 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005626 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005627}
5628
5629
5630int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5631 char *end)
5632{
5633 return p2p_scan_result_text(ies, ies_len, buf, end);
5634}
5635
5636
5637static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5638{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005639 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640 return;
5641
5642 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5643 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005644 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005645}
5646
5647
5648int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5649 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005650 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005651 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652{
5653 wpas_p2p_clear_pending_action_tx(wpa_s);
5654 wpa_s->p2p_long_listen = 0;
5655
Dmitry Shmidt04949592012-07-19 12:16:46 -07005656 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5657 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005658 return -1;
5659
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005660 wpa_supplicant_cancel_sched_scan(wpa_s);
5661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005663 num_req_dev_types, req_dev_types, dev_id,
5664 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005665}
5666
5667
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005668static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005669{
5670 wpas_p2p_clear_pending_action_tx(wpa_s);
5671 wpa_s->p2p_long_listen = 0;
5672 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5673 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674
5675 if (wpa_s->global->p2p)
5676 p2p_stop_find(wpa_s->global->p2p);
5677
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005678 return 0;
5679}
5680
5681
5682void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5683{
5684 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5685 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005686 wpas_p2p_remove_pending_group_interface(wpa_s);
5687}
5688
5689
5690static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5691{
5692 struct wpa_supplicant *wpa_s = eloop_ctx;
5693 wpa_s->p2p_long_listen = 0;
5694}
5695
5696
5697int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5698{
5699 int res;
5700
5701 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5702 return -1;
5703
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005704 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 wpas_p2p_clear_pending_action_tx(wpa_s);
5706
5707 if (timeout == 0) {
5708 /*
5709 * This is a request for unlimited Listen state. However, at
5710 * least for now, this is mapped to a Listen state for one
5711 * hour.
5712 */
5713 timeout = 3600;
5714 }
5715 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5716 wpa_s->p2p_long_listen = 0;
5717
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005718 /*
5719 * Stop previous find/listen operation to avoid trying to request a new
5720 * remain-on-channel operation while the driver is still running the
5721 * previous one.
5722 */
5723 if (wpa_s->global->p2p)
5724 p2p_stop_find(wpa_s->global->p2p);
5725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005726 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5727 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5728 wpa_s->p2p_long_listen = timeout * 1000;
5729 eloop_register_timeout(timeout, 0,
5730 wpas_p2p_long_listen_timeout,
5731 wpa_s, NULL);
5732 }
5733
5734 return res;
5735}
5736
5737
5738int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5739 u8 *buf, size_t len, int p2p_group)
5740{
5741 struct wpabuf *p2p_ie;
5742 int ret;
5743
5744 if (wpa_s->global->p2p_disabled)
5745 return -1;
5746 if (wpa_s->global->p2p == NULL)
5747 return -1;
5748 if (bss == NULL)
5749 return -1;
5750
5751 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5752 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5753 p2p_group, p2p_ie);
5754 wpabuf_free(p2p_ie);
5755
5756 return ret;
5757}
5758
5759
5760int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005761 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005762 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005763{
5764 if (wpa_s->global->p2p_disabled)
5765 return 0;
5766 if (wpa_s->global->p2p == NULL)
5767 return 0;
5768
Dmitry Shmidt04949592012-07-19 12:16:46 -07005769 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5770 ie, ie_len)) {
5771 case P2P_PREQ_NOT_P2P:
5772 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5773 ssi_signal);
5774 /* fall through */
5775 case P2P_PREQ_MALFORMED:
5776 case P2P_PREQ_NOT_LISTEN:
5777 case P2P_PREQ_NOT_PROCESSED:
5778 default: /* make gcc happy */
5779 return 0;
5780 case P2P_PREQ_PROCESSED:
5781 return 1;
5782 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005783}
5784
5785
5786void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5787 const u8 *sa, const u8 *bssid,
5788 u8 category, const u8 *data, size_t len, int freq)
5789{
5790 if (wpa_s->global->p2p_disabled)
5791 return;
5792 if (wpa_s->global->p2p == NULL)
5793 return;
5794
5795 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5796 freq);
5797}
5798
5799
5800void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5801{
5802 if (wpa_s->global->p2p_disabled)
5803 return;
5804 if (wpa_s->global->p2p == NULL)
5805 return;
5806
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005807 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808}
5809
5810
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005811static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812{
5813 p2p_group_deinit(wpa_s->p2p_group);
5814 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005815
5816 wpa_s->ap_configured_cb = NULL;
5817 wpa_s->ap_configured_cb_ctx = NULL;
5818 wpa_s->ap_configured_cb_data = NULL;
5819 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820}
5821
5822
5823int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5824{
5825 wpa_s->p2p_long_listen = 0;
5826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005827 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5828 return -1;
5829
5830 return p2p_reject(wpa_s->global->p2p, addr);
5831}
5832
5833
5834/* Invite to reinvoke a persistent group */
5835int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005836 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005837 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005838{
5839 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005840 u8 *bssid = NULL;
5841 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005842 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005843 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005844
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005845 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005846 if (peer_addr)
5847 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5848 else
5849 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850
Jouni Malinen31be0a42012-08-31 21:20:51 +03005851 wpa_s->p2p_persistent_go_freq = freq;
5852 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 if (ssid->mode == WPAS_MODE_P2P_GO) {
5854 role = P2P_INVITE_ROLE_GO;
5855 if (peer_addr == NULL) {
5856 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5857 "address in invitation command");
5858 return -1;
5859 }
5860 if (wpas_p2p_create_iface(wpa_s)) {
5861 if (wpas_p2p_add_group_interface(wpa_s,
5862 WPA_IF_P2P_GO) < 0) {
5863 wpa_printf(MSG_ERROR, "P2P: Failed to "
5864 "allocate a new interface for the "
5865 "group");
5866 return -1;
5867 }
5868 bssid = wpa_s->pending_interface_addr;
5869 } else
5870 bssid = wpa_s->own_addr;
5871 } else {
5872 role = P2P_INVITE_ROLE_CLIENT;
5873 peer_addr = ssid->bssid;
5874 }
5875 wpa_s->pending_invite_ssid_id = ssid->id;
5876
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005877 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5878 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005879 if (res)
5880 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5883 return -1;
5884
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005885 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5886 no_pref_freq_given && pref_freq > 0 &&
5887 wpa_s->num_multichan_concurrent > 1 &&
5888 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5889 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5890 pref_freq);
5891 pref_freq = 0;
5892 }
5893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005894 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005895 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005896 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005897}
5898
5899
5900/* Invite to join an active group */
5901int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5902 const u8 *peer_addr, const u8 *go_dev_addr)
5903{
5904 struct wpa_global *global = wpa_s->global;
5905 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005906 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005908 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005909 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005910 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005911
Jouni Malinen31be0a42012-08-31 21:20:51 +03005912 wpa_s->p2p_persistent_go_freq = 0;
5913 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005914 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005916 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5917 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5918 break;
5919 }
5920 if (wpa_s == NULL) {
5921 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5922 return -1;
5923 }
5924
5925 ssid = wpa_s->current_ssid;
5926 if (ssid == NULL) {
5927 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5928 "invitation");
5929 return -1;
5930 }
5931
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005932 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005933 persistent = ssid->p2p_persistent_group &&
5934 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5935 ssid->ssid, ssid->ssid_len);
5936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005937 if (ssid->mode == WPAS_MODE_P2P_GO) {
5938 role = P2P_INVITE_ROLE_ACTIVE_GO;
5939 bssid = wpa_s->own_addr;
5940 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005941 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005942 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005943 } else {
5944 role = P2P_INVITE_ROLE_CLIENT;
5945 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5946 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5947 "invite to current group");
5948 return -1;
5949 }
5950 bssid = wpa_s->bssid;
5951 if (go_dev_addr == NULL &&
5952 !is_zero_ether_addr(wpa_s->go_dev_addr))
5953 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005954 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5955 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005956 }
5957 wpa_s->parent->pending_invite_ssid_id = -1;
5958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005959 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5960 return -1;
5961
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005962 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5963 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005964 if (res)
5965 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005966 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005968 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005969 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005970 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005971}
5972
5973
5974void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5975{
5976 struct wpa_ssid *ssid = wpa_s->current_ssid;
5977 const char *ssid_txt;
5978 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005979 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005981 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005982 u8 ip[3 * 4];
5983 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984
Dmitry Shmidt04949592012-07-19 12:16:46 -07005985 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5986 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5987 wpa_s->parent, NULL);
5988 }
5989
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005990 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005991 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992
5993 wpa_s->show_group_started = 0;
5994
5995 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5996 os_memset(go_dev_addr, 0, ETH_ALEN);
5997 if (ssid->bssid_set)
5998 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5999 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6000 ssid->ssid_len);
6001 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6002
6003 if (wpa_s->global->p2p_group_formation == wpa_s)
6004 wpa_s->global->p2p_group_formation = NULL;
6005
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006006 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6007 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006008
6009 ip_addr[0] = '\0';
6010 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6011 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6012 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6013 ip[0], ip[1], ip[2], ip[3],
6014 ip[4], ip[5], ip[6], ip[7],
6015 ip[8], ip[9], ip[10], ip[11]);
6016 }
6017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006018 if (ssid->passphrase == NULL && ssid->psk_set) {
6019 char psk[65];
6020 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006021 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
6022 "%s client ssid=\"%s\" freq=%d psk=%s "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006023 "go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006024 wpa_s->ifname, ssid_txt, freq, psk,
6025 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006026 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006027 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006028 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
6029 "%s client ssid=\"%s\" freq=%d "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006030 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006031 wpa_s->ifname, ssid_txt, freq,
6032 ssid->passphrase ? ssid->passphrase : "",
6033 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006034 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006035 }
6036
6037 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006038 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6039 ssid, go_dev_addr);
6040 if (network_id < 0)
6041 network_id = ssid->id;
6042 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006043}
6044
6045
6046int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6047 u32 interval1, u32 duration2, u32 interval2)
6048{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006049 int ret;
6050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006051 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6052 return -1;
6053
6054 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6055 wpa_s->current_ssid == NULL ||
6056 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6057 return -1;
6058
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006059 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6060 wpa_s->own_addr, wpa_s->assoc_freq,
6061 duration1, interval1, duration2, interval2);
6062 if (ret == 0)
6063 wpa_s->waiting_presence_resp = 1;
6064
6065 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006066}
6067
6068
6069int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6070 unsigned int interval)
6071{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006072 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6073 return -1;
6074
6075 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6076}
6077
6078
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006079static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6080{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006081 if (wpa_s->current_ssid == NULL) {
6082 /*
6083 * current_ssid can be cleared when P2P client interface gets
6084 * disconnected, so assume this interface was used as P2P
6085 * client.
6086 */
6087 return 1;
6088 }
6089 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006090 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6091}
6092
6093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006094static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6095{
6096 struct wpa_supplicant *wpa_s = eloop_ctx;
6097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006098 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006099 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6100 "disabled");
6101 return;
6102 }
6103
Dmitry Shmidt04949592012-07-19 12:16:46 -07006104 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6105 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006106 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006107}
6108
6109
6110static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6111{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006112 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006113
Dmitry Shmidt04949592012-07-19 12:16:46 -07006114 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6115 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6116
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006117 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6118 return;
6119
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006120 timeout = wpa_s->conf->p2p_group_idle;
6121 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6122 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6123 timeout = P2P_MAX_CLIENT_IDLE;
6124
6125 if (timeout == 0)
6126 return;
6127
Dmitry Shmidt04949592012-07-19 12:16:46 -07006128 if (timeout < 0) {
6129 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6130 timeout = 0; /* special client mode no-timeout */
6131 else
6132 return;
6133 }
6134
6135 if (wpa_s->p2p_in_provisioning) {
6136 /*
6137 * Use the normal group formation timeout during the
6138 * provisioning phase to avoid terminating this process too
6139 * early due to group idle timeout.
6140 */
6141 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6142 "during provisioning");
6143 return;
6144 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306145
Dmitry Shmidt04949592012-07-19 12:16:46 -07006146 if (wpa_s->show_group_started) {
6147 /*
6148 * Use the normal group formation timeout between the end of
6149 * the provisioning phase and completion of 4-way handshake to
6150 * avoid terminating this process too early due to group idle
6151 * timeout.
6152 */
6153 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6154 "while waiting for initial 4-way handshake to "
6155 "complete");
6156 return;
6157 }
6158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006159 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006160 timeout);
6161 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6162 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006163}
6164
6165
Jouni Malinen2b89da82012-08-31 22:04:41 +03006166/* Returns 1 if the interface was removed */
6167int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6168 u16 reason_code, const u8 *ie, size_t ie_len,
6169 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006170{
6171 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006172 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173
Dmitry Shmidt04949592012-07-19 12:16:46 -07006174 if (!locally_generated)
6175 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6176 ie_len);
6177
6178 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6179 wpa_s->current_ssid &&
6180 wpa_s->current_ssid->p2p_group &&
6181 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6182 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6183 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006184 if (wpas_p2p_group_delete(wpa_s,
6185 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6186 > 0)
6187 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006188 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006189
6190 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191}
6192
6193
6194void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006195 u16 reason_code, const u8 *ie, size_t ie_len,
6196 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006197{
6198 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6199 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006200
Dmitry Shmidt04949592012-07-19 12:16:46 -07006201 if (!locally_generated)
6202 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6203 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006204}
6205
6206
6207void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6208{
6209 struct p2p_data *p2p = wpa_s->global->p2p;
6210
6211 if (p2p == NULL)
6212 return;
6213
6214 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6215 return;
6216
6217 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6218 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6219
6220 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6221 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6222
6223 if (wpa_s->wps &&
6224 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6225 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6226
6227 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6228 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6229
6230 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6231 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6232 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6233 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6234 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6235 }
6236
6237 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6238 p2p_set_sec_dev_types(p2p,
6239 (void *) wpa_s->conf->sec_device_type,
6240 wpa_s->conf->num_sec_device_types);
6241
6242 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6243 int i;
6244 p2p_remove_wps_vendor_extensions(p2p);
6245 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6246 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6247 continue;
6248 p2p_add_wps_vendor_extension(
6249 p2p, wpa_s->conf->wps_vendor_ext[i]);
6250 }
6251 }
6252
6253 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6254 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6255 char country[3];
6256 country[0] = wpa_s->conf->country[0];
6257 country[1] = wpa_s->conf->country[1];
6258 country[2] = 0x04;
6259 p2p_set_country(p2p, country);
6260 }
6261
6262 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6263 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6264 wpa_s->conf->p2p_ssid_postfix ?
6265 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6266 0);
6267 }
6268
6269 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6270 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006271
6272 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6273 u8 reg_class, channel;
6274 int ret;
6275 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006276 u8 channel_forced;
6277
Jouni Malinen75ecf522011-06-27 15:19:46 -07006278 if (wpa_s->conf->p2p_listen_reg_class &&
6279 wpa_s->conf->p2p_listen_channel) {
6280 reg_class = wpa_s->conf->p2p_listen_reg_class;
6281 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006282 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006283 } else {
6284 reg_class = 81;
6285 /*
6286 * Pick one of the social channels randomly as the
6287 * listen channel.
6288 */
6289 os_get_random((u8 *) &r, sizeof(r));
6290 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006291 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006292 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006293 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6294 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006295 if (ret)
6296 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6297 "failed: %d", ret);
6298 }
6299 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6300 u8 op_reg_class, op_channel, cfg_op_channel;
6301 int ret = 0;
6302 unsigned int r;
6303 if (wpa_s->conf->p2p_oper_reg_class &&
6304 wpa_s->conf->p2p_oper_channel) {
6305 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6306 op_channel = wpa_s->conf->p2p_oper_channel;
6307 cfg_op_channel = 1;
6308 } else {
6309 op_reg_class = 81;
6310 /*
6311 * Use random operation channel from (1, 6, 11)
6312 *if no other preference is indicated.
6313 */
6314 os_get_random((u8 *) &r, sizeof(r));
6315 op_channel = 1 + (r % 3) * 5;
6316 cfg_op_channel = 0;
6317 }
6318 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6319 cfg_op_channel);
6320 if (ret)
6321 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6322 "failed: %d", ret);
6323 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006324
6325 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6326 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6327 wpa_s->conf->p2p_pref_chan) < 0) {
6328 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6329 "update failed");
6330 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006331
6332 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6333 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6334 "update failed");
6335 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006337}
6338
6339
6340int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6341 int duration)
6342{
6343 if (!wpa_s->ap_iface)
6344 return -1;
6345 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6346 duration);
6347}
6348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006349
6350int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6351{
6352 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6353 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006354
6355 wpa_s->global->cross_connection = enabled;
6356 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6357
6358 if (!enabled) {
6359 struct wpa_supplicant *iface;
6360
6361 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6362 {
6363 if (iface->cross_connect_enabled == 0)
6364 continue;
6365
6366 iface->cross_connect_enabled = 0;
6367 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006368 wpa_msg_global(iface->parent, MSG_INFO,
6369 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6370 iface->ifname,
6371 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006372 }
6373 }
6374
6375 return 0;
6376}
6377
6378
6379static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6380{
6381 struct wpa_supplicant *iface;
6382
6383 if (!uplink->global->cross_connection)
6384 return;
6385
6386 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6387 if (!iface->cross_connect_enabled)
6388 continue;
6389 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6390 0)
6391 continue;
6392 if (iface->ap_iface == NULL)
6393 continue;
6394 if (iface->cross_connect_in_use)
6395 continue;
6396
6397 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006398 wpa_msg_global(iface->parent, MSG_INFO,
6399 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6400 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006401 }
6402}
6403
6404
6405static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6406{
6407 struct wpa_supplicant *iface;
6408
6409 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6410 if (!iface->cross_connect_enabled)
6411 continue;
6412 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6413 0)
6414 continue;
6415 if (!iface->cross_connect_in_use)
6416 continue;
6417
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006418 wpa_msg_global(iface->parent, MSG_INFO,
6419 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6420 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006421 iface->cross_connect_in_use = 0;
6422 }
6423}
6424
6425
6426void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6427{
6428 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6429 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6430 wpa_s->cross_connect_disallowed)
6431 wpas_p2p_disable_cross_connect(wpa_s);
6432 else
6433 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006434 if (!wpa_s->ap_iface &&
6435 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6436 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006437}
6438
6439
6440void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6441{
6442 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006443 if (!wpa_s->ap_iface &&
6444 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6445 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006446 wpas_p2p_set_group_idle_timeout(wpa_s);
6447}
6448
6449
6450static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6451{
6452 struct wpa_supplicant *iface;
6453
6454 if (!wpa_s->global->cross_connection)
6455 return;
6456
6457 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6458 if (iface == wpa_s)
6459 continue;
6460 if (iface->drv_flags &
6461 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6462 continue;
6463 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6464 continue;
6465
6466 wpa_s->cross_connect_enabled = 1;
6467 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6468 sizeof(wpa_s->cross_connect_uplink));
6469 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6470 "%s to %s whenever uplink is available",
6471 wpa_s->ifname, wpa_s->cross_connect_uplink);
6472
6473 if (iface->ap_iface || iface->current_ssid == NULL ||
6474 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6475 iface->cross_connect_disallowed ||
6476 iface->wpa_state != WPA_COMPLETED)
6477 break;
6478
6479 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006480 wpa_msg_global(wpa_s->parent, MSG_INFO,
6481 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6482 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483 break;
6484 }
6485}
6486
6487
6488int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6489{
6490 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6491 !wpa_s->p2p_in_provisioning)
6492 return 0; /* not P2P client operation */
6493
6494 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6495 "session overlap");
6496 if (wpa_s != wpa_s->parent)
6497 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006498 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006499 return 1;
6500}
6501
6502
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006503void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6504{
6505 struct wpa_supplicant *wpa_s = eloop_ctx;
6506 wpas_p2p_notif_pbc_overlap(wpa_s);
6507}
6508
6509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006510void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6511{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006512 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006513 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006514
6515 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6516 return;
6517
6518 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006519 os_memset(&cli_chan, 0, sizeof(cli_chan));
6520 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006521 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6522 "channel list");
6523 return;
6524 }
6525
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006526 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006527
6528 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6529 int freq;
6530 if (!ifs->current_ssid ||
6531 !ifs->current_ssid->p2p_group ||
6532 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6533 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6534 continue;
6535 freq = ifs->current_ssid->frequency;
6536 if (freq_included(&chan, freq)) {
6537 wpa_dbg(ifs, MSG_DEBUG,
6538 "P2P GO operating frequency %d MHz in valid range",
6539 freq);
6540 continue;
6541 }
6542
6543 wpa_dbg(ifs, MSG_DEBUG,
6544 "P2P GO operating in invalid frequency %d MHz", freq);
6545 /* TODO: Consider using CSA or removing the group within
6546 * wpa_supplicant */
6547 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006549}
6550
6551
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006552static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6553 struct wpa_scan_results *scan_res)
6554{
6555 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6556}
6557
6558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006559int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6560{
6561 struct wpa_global *global = wpa_s->global;
6562 int found = 0;
6563 const u8 *peer;
6564
6565 if (global->p2p == NULL)
6566 return -1;
6567
6568 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6569
6570 if (wpa_s->pending_interface_name[0] &&
6571 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6572 found = 1;
6573
6574 peer = p2p_get_go_neg_peer(global->p2p);
6575 if (peer) {
6576 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6577 MACSTR, MAC2STR(peer));
6578 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006579 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006580 }
6581
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006582 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6583 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6584 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6585 found = 1;
6586 }
6587
6588 if (wpa_s->pending_pd_before_join) {
6589 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6590 wpa_s->pending_pd_before_join = 0;
6591 found = 1;
6592 }
6593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006594 wpas_p2p_stop_find(wpa_s);
6595
6596 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6597 if (wpa_s == global->p2p_group_formation &&
6598 (wpa_s->p2p_in_provisioning ||
6599 wpa_s->parent->pending_interface_type ==
6600 WPA_IF_P2P_CLIENT)) {
6601 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6602 "formation found - cancelling",
6603 wpa_s->ifname);
6604 found = 1;
6605 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6606 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006607 if (wpa_s->p2p_in_provisioning) {
6608 wpas_group_formation_completed(wpa_s, 0);
6609 break;
6610 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006611 wpas_p2p_group_delete(wpa_s,
6612 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006613 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006614 } else if (wpa_s->p2p_in_invitation) {
6615 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6616 wpa_s->ifname);
6617 found = 1;
6618 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006619 }
6620 }
6621
6622 if (!found) {
6623 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6624 return -1;
6625 }
6626
6627 return 0;
6628}
6629
6630
6631void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6632{
6633 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6634 return;
6635
6636 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6637 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006638 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006639}
6640
6641
6642void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6643 int freq_24, int freq_5, int freq_overall)
6644{
6645 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006646 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006647 return;
6648 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6649}
6650
6651
6652int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6653{
6654 u8 peer[ETH_ALEN];
6655 struct p2p_data *p2p = wpa_s->global->p2p;
6656
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006657 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006658 return -1;
6659
6660 if (hwaddr_aton(addr, peer))
6661 return -1;
6662
6663 return p2p_unauthorize(p2p, peer);
6664}
6665
6666
6667/**
6668 * wpas_p2p_disconnect - Disconnect from a P2P Group
6669 * @wpa_s: Pointer to wpa_supplicant data
6670 * Returns: 0 on success, -1 on failure
6671 *
6672 * This can be used to disconnect from a group in which the local end is a P2P
6673 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6674 * virtual network interface was created for this group, that interface will be
6675 * removed. Otherwise, only the configured P2P group network will be removed
6676 * from the interface.
6677 */
6678int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6679{
6680
6681 if (wpa_s == NULL)
6682 return -1;
6683
Jouni Malinen2b89da82012-08-31 22:04:41 +03006684 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6685 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006686}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006687
6688
6689int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6690{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006691 int ret;
6692
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006693 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6694 return 0;
6695
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006696 ret = p2p_in_progress(wpa_s->global->p2p);
6697 if (ret == 0) {
6698 /*
6699 * Check whether there is an ongoing WPS provisioning step (or
6700 * other parts of group formation) on another interface since
6701 * p2p_in_progress() does not report this to avoid issues for
6702 * scans during such provisioning step.
6703 */
6704 if (wpa_s->global->p2p_group_formation &&
6705 wpa_s->global->p2p_group_formation != wpa_s) {
6706 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6707 "in group formation",
6708 wpa_s->global->p2p_group_formation->ifname);
6709 ret = 1;
6710 }
6711 }
6712
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006713 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006714 struct os_reltime now;
6715 os_get_reltime(&now);
6716 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6717 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006718 /* Wait for the first client has expired */
6719 wpa_s->global->p2p_go_wait_client.sec = 0;
6720 } else {
6721 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6722 ret = 1;
6723 }
6724 }
6725
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006726 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006727}
6728
6729
6730void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6731 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006732{
6733 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6734 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6735 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006736 /**
6737 * Remove the network by scheduling the group formation
6738 * timeout to happen immediately. The teardown code
6739 * needs to be scheduled to run asynch later so that we
6740 * don't delete data from under ourselves unexpectedly.
6741 * Calling wpas_p2p_group_formation_timeout directly
6742 * causes a series of crashes in WPS failure scenarios.
6743 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006744 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6745 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006746 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6747 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006748 }
6749}
6750
6751
6752struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006753 const u8 *addr, const u8 *ssid,
6754 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006755{
6756 struct wpa_ssid *s;
6757 size_t i;
6758
6759 for (s = wpa_s->conf->ssid; s; s = s->next) {
6760 if (s->disabled != 2)
6761 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006762 if (ssid &&
6763 (ssid_len != s->ssid_len ||
6764 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6765 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006766 if (addr == NULL) {
6767 if (s->mode == WPAS_MODE_P2P_GO)
6768 return s;
6769 continue;
6770 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006771 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6772 return s; /* peer is GO in the persistent group */
6773 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6774 continue;
6775 for (i = 0; i < s->num_p2p_clients; i++) {
6776 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6777 addr, ETH_ALEN) == 0)
6778 return s; /* peer is P2P client in persistent
6779 * group */
6780 }
6781 }
6782
6783 return NULL;
6784}
6785
6786
6787void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6788 const u8 *addr)
6789{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006790 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6791 wpa_s->parent, NULL) > 0) {
6792 /*
6793 * This can happen if WPS provisioning step is not terminated
6794 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6795 * peer was able to connect, there is no need to time out group
6796 * formation after this, though. In addition, this is used with
6797 * the initial connection wait on the GO as a separate formation
6798 * timeout and as such, expected to be hit after the initial WPS
6799 * provisioning step.
6800 */
6801 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6802 }
6803 if (!wpa_s->p2p_go_group_formation_completed) {
6804 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6805 wpa_s->p2p_go_group_formation_completed = 1;
6806 wpa_s->global->p2p_group_formation = NULL;
6807 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006808 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006809 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006810 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006811 if (addr == NULL)
6812 return;
6813 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6814}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006815
Dmitry Shmidt04949592012-07-19 12:16:46 -07006816
6817static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6818 int group_added)
6819{
6820 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006821 if (wpa_s->global->p2p_group_formation)
6822 group = wpa_s->global->p2p_group_formation;
6823 wpa_s = wpa_s->parent;
6824 offchannel_send_action_done(wpa_s);
6825 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006826 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6828 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6829 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6830 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6831 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006832 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006833 wpa_s->p2p_go_ht40,
6834 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006835}
6836
6837
6838int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6839{
6840 if (!wpa_s->p2p_fallback_to_go_neg ||
6841 wpa_s->p2p_in_provisioning <= 5)
6842 return 0;
6843
6844 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6845 return 0; /* peer operating as a GO */
6846
6847 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6848 "fallback to GO Negotiation");
6849 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6850
6851 return 1;
6852}
6853
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006854
6855unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6856{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006857 struct wpa_supplicant *ifs;
6858
6859 if (wpa_s->wpa_state > WPA_SCANNING) {
6860 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6861 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006862 wpa_s->conf->p2p_search_delay);
6863 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006864 }
6865
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006866 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6867 radio_list) {
6868 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006869 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6870 "delay due to concurrent operation on "
6871 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006872 wpa_s->conf->p2p_search_delay,
6873 ifs->ifname);
6874 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006875 }
6876 }
6877
6878 return 0;
6879}
6880
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006881
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006882static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6883 struct wpa_ssid *s, const u8 *addr,
6884 int iface_addr)
6885{
6886 struct psk_list_entry *psk, *tmp;
6887 int changed = 0;
6888
6889 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6890 list) {
6891 if ((iface_addr && !psk->p2p &&
6892 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6893 (!iface_addr && psk->p2p &&
6894 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6895 wpa_dbg(wpa_s, MSG_DEBUG,
6896 "P2P: Remove persistent group PSK list entry for "
6897 MACSTR " p2p=%u",
6898 MAC2STR(psk->addr), psk->p2p);
6899 dl_list_del(&psk->list);
6900 os_free(psk);
6901 changed++;
6902 }
6903 }
6904
6905 return changed;
6906}
6907
6908
6909void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6910 const u8 *p2p_dev_addr,
6911 const u8 *psk, size_t psk_len)
6912{
6913 struct wpa_ssid *ssid = wpa_s->current_ssid;
6914 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006915 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006916
6917 if (psk_len != sizeof(p->psk))
6918 return;
6919
6920 if (p2p_dev_addr) {
6921 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6922 " p2p_dev_addr=" MACSTR,
6923 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6924 if (is_zero_ether_addr(p2p_dev_addr))
6925 p2p_dev_addr = NULL;
6926 } else {
6927 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6928 MAC2STR(mac_addr));
6929 }
6930
6931 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6933 /* To be added to persistent group once created */
6934 if (wpa_s->global->add_psk == NULL) {
6935 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6936 if (wpa_s->global->add_psk == NULL)
6937 return;
6938 }
6939 p = wpa_s->global->add_psk;
6940 if (p2p_dev_addr) {
6941 p->p2p = 1;
6942 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6943 } else {
6944 p->p2p = 0;
6945 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6946 }
6947 os_memcpy(p->psk, psk, psk_len);
6948 return;
6949 }
6950
6951 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6952 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6953 return;
6954 }
6955
6956 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6957 ssid->ssid_len);
6958 if (!persistent) {
6959 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6960 return;
6961 }
6962
6963 p = os_zalloc(sizeof(*p));
6964 if (p == NULL)
6965 return;
6966 if (p2p_dev_addr) {
6967 p->p2p = 1;
6968 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6969 } else {
6970 p->p2p = 0;
6971 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6972 }
6973 os_memcpy(p->psk, psk, psk_len);
6974
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07006975 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
6976 (last = dl_list_last(&persistent->psk_list,
6977 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006978 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6979 MACSTR " (p2p=%u) to make room for a new one",
6980 MAC2STR(last->addr), last->p2p);
6981 dl_list_del(&last->list);
6982 os_free(last);
6983 }
6984
6985 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6986 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6987 p2p_dev_addr == NULL);
6988 if (p2p_dev_addr) {
6989 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6990 MACSTR, MAC2STR(p2p_dev_addr));
6991 } else {
6992 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6993 MAC2STR(mac_addr));
6994 }
6995 dl_list_add(&persistent->psk_list, &p->list);
6996
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006997 if (wpa_s->parent->conf->update_config &&
6998 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6999 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007000}
7001
7002
7003static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7004 struct wpa_ssid *s, const u8 *addr,
7005 int iface_addr)
7006{
7007 int res;
7008
7009 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007010 if (res > 0 && wpa_s->conf->update_config &&
7011 wpa_config_write(wpa_s->confname, wpa_s->conf))
7012 wpa_dbg(wpa_s, MSG_DEBUG,
7013 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007014}
7015
7016
7017static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7018 const u8 *peer, int iface_addr)
7019{
7020 struct hostapd_data *hapd;
7021 struct hostapd_wpa_psk *psk, *prev, *rem;
7022 struct sta_info *sta;
7023
7024 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7025 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7026 return;
7027
7028 /* Remove per-station PSK entry */
7029 hapd = wpa_s->ap_iface->bss[0];
7030 prev = NULL;
7031 psk = hapd->conf->ssid.wpa_psk;
7032 while (psk) {
7033 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7034 (!iface_addr &&
7035 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7036 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7037 MACSTR " iface_addr=%d",
7038 MAC2STR(peer), iface_addr);
7039 if (prev)
7040 prev->next = psk->next;
7041 else
7042 hapd->conf->ssid.wpa_psk = psk->next;
7043 rem = psk;
7044 psk = psk->next;
7045 os_free(rem);
7046 } else {
7047 prev = psk;
7048 psk = psk->next;
7049 }
7050 }
7051
7052 /* Disconnect from group */
7053 if (iface_addr)
7054 sta = ap_get_sta(hapd, peer);
7055 else
7056 sta = ap_get_sta_p2p(hapd, peer);
7057 if (sta) {
7058 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7059 " (iface_addr=%d) from group",
7060 MAC2STR(peer), iface_addr);
7061 hostapd_drv_sta_deauth(hapd, sta->addr,
7062 WLAN_REASON_DEAUTH_LEAVING);
7063 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7064 }
7065}
7066
7067
7068void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7069 int iface_addr)
7070{
7071 struct wpa_ssid *s;
7072 struct wpa_supplicant *w;
7073
7074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7075
7076 /* Remove from any persistent group */
7077 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7078 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7079 continue;
7080 if (!iface_addr)
7081 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7082 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7083 }
7084
7085 /* Remove from any operating group */
7086 for (w = wpa_s->global->ifaces; w; w = w->next)
7087 wpas_p2p_remove_client_go(w, peer, iface_addr);
7088}
7089
7090
7091static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7092{
7093 struct wpa_supplicant *wpa_s = eloop_ctx;
7094 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7095}
7096
7097
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007098static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7099{
7100 struct wpa_supplicant *wpa_s = eloop_ctx;
7101
7102 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7103 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7104}
7105
7106
7107int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7108 struct wpa_ssid *ssid)
7109{
7110 struct wpa_supplicant *iface;
7111
7112 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7113 if (!iface->current_ssid ||
7114 iface->current_ssid->frequency == freq ||
7115 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7116 !iface->current_ssid->p2p_group))
7117 continue;
7118
7119 /* Remove the connection with least priority */
7120 if (!wpas_is_p2p_prioritized(iface)) {
7121 /* STA connection has priority over existing
7122 * P2P connection, so remove the interface. */
7123 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7124 eloop_register_timeout(0, 0,
7125 wpas_p2p_group_freq_conflict,
7126 iface, NULL);
7127 /* If connection in progress is P2P connection, do not
7128 * proceed for the connection. */
7129 if (wpa_s == iface)
7130 return -1;
7131 else
7132 return 0;
7133 } else {
7134 /* P2P connection has priority, disable the STA network
7135 */
7136 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7137 ssid);
7138 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7139 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7140 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7141 ETH_ALEN);
7142 /* If P2P connection is in progress, continue
7143 * connecting...*/
7144 if (wpa_s == iface)
7145 return 0;
7146 else
7147 return -1;
7148 }
7149 }
7150
7151 return 0;
7152}
7153
7154
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007155int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7156{
7157 struct wpa_ssid *ssid = wpa_s->current_ssid;
7158
7159 if (ssid == NULL || !ssid->p2p_group)
7160 return 0;
7161
7162 if (wpa_s->p2p_last_4way_hs_fail &&
7163 wpa_s->p2p_last_4way_hs_fail == ssid) {
7164 u8 go_dev_addr[ETH_ALEN];
7165 struct wpa_ssid *persistent;
7166
7167 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7168 ssid->ssid,
7169 ssid->ssid_len) <= 0) {
7170 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7171 goto disconnect;
7172 }
7173
7174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7175 MACSTR, MAC2STR(go_dev_addr));
7176 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7177 ssid->ssid,
7178 ssid->ssid_len);
7179 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7180 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7181 goto disconnect;
7182 }
7183 wpa_msg_global(wpa_s->parent, MSG_INFO,
7184 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7185 persistent->id);
7186 disconnect:
7187 wpa_s->p2p_last_4way_hs_fail = NULL;
7188 /*
7189 * Remove the group from a timeout to avoid issues with caller
7190 * continuing to use the interface if this is on a P2P group
7191 * interface.
7192 */
7193 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7194 wpa_s, NULL);
7195 return 1;
7196 }
7197
7198 wpa_s->p2p_last_4way_hs_fail = ssid;
7199 return 0;
7200}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007201
7202
7203#ifdef CONFIG_WPS_NFC
7204
7205static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7206 struct wpabuf *p2p)
7207{
7208 struct wpabuf *ret;
7209 size_t wsc_len;
7210
7211 if (p2p == NULL) {
7212 wpabuf_free(wsc);
7213 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7214 return NULL;
7215 }
7216
7217 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7218 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7219 if (ret == NULL) {
7220 wpabuf_free(wsc);
7221 wpabuf_free(p2p);
7222 return NULL;
7223 }
7224
7225 wpabuf_put_be16(ret, wsc_len);
7226 if (wsc)
7227 wpabuf_put_buf(ret, wsc);
7228 wpabuf_put_be16(ret, wpabuf_len(p2p));
7229 wpabuf_put_buf(ret, p2p);
7230
7231 wpabuf_free(wsc);
7232 wpabuf_free(p2p);
7233 wpa_hexdump_buf(MSG_DEBUG,
7234 "P2P: Generated NFC connection handover message", ret);
7235
7236 if (ndef && ret) {
7237 struct wpabuf *tmp;
7238 tmp = ndef_build_p2p(ret);
7239 wpabuf_free(ret);
7240 if (tmp == NULL) {
7241 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7242 return NULL;
7243 }
7244 ret = tmp;
7245 }
7246
7247 return ret;
7248}
7249
7250
7251static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7252 struct wpa_ssid **ssid, u8 *go_dev_addr)
7253{
7254 struct wpa_supplicant *iface;
7255
7256 if (go_dev_addr)
7257 os_memset(go_dev_addr, 0, ETH_ALEN);
7258 if (ssid)
7259 *ssid = NULL;
7260 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7261 if (iface->wpa_state < WPA_ASSOCIATING ||
7262 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7263 !iface->current_ssid->p2p_group ||
7264 iface->current_ssid->mode != WPAS_MODE_INFRA)
7265 continue;
7266 if (ssid)
7267 *ssid = iface->current_ssid;
7268 if (go_dev_addr)
7269 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7270 return iface->assoc_freq;
7271 }
7272 return 0;
7273}
7274
7275
7276struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7277 int ndef)
7278{
7279 struct wpabuf *wsc, *p2p;
7280 struct wpa_ssid *ssid;
7281 u8 go_dev_addr[ETH_ALEN];
7282 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7283
7284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7285 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7286 return NULL;
7287 }
7288
7289 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7290 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7291 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7292 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7293 return NULL;
7294 }
7295
7296 if (cli_freq == 0) {
7297 wsc = wps_build_nfc_handover_req_p2p(
7298 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7299 } else
7300 wsc = NULL;
7301 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7302 go_dev_addr, ssid ? ssid->ssid : NULL,
7303 ssid ? ssid->ssid_len : 0);
7304
7305 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7306}
7307
7308
7309struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7310 int ndef, int tag)
7311{
7312 struct wpabuf *wsc, *p2p;
7313 struct wpa_ssid *ssid;
7314 u8 go_dev_addr[ETH_ALEN];
7315 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7316
7317 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7318 return NULL;
7319
7320 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7321 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7322 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7323 return NULL;
7324
7325 if (cli_freq == 0) {
7326 wsc = wps_build_nfc_handover_sel_p2p(
7327 wpa_s->parent->wps,
7328 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7329 DEV_PW_NFC_CONNECTION_HANDOVER,
7330 wpa_s->conf->wps_nfc_dh_pubkey,
7331 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7332 } else
7333 wsc = NULL;
7334 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7335 go_dev_addr, ssid ? ssid->ssid : NULL,
7336 ssid ? ssid->ssid_len : 0);
7337
7338 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7339}
7340
7341
7342static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7343 struct p2p_nfc_params *params)
7344{
7345 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7346 "connection handover (freq=%d)",
7347 params->go_freq);
7348
7349 if (params->go_freq && params->go_ssid_len) {
7350 wpa_s->p2p_wps_method = WPS_NFC;
7351 wpa_s->pending_join_wps_method = WPS_NFC;
7352 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7353 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7354 ETH_ALEN);
7355 return wpas_p2p_join_start(wpa_s, params->go_freq,
7356 params->go_ssid,
7357 params->go_ssid_len);
7358 }
7359
7360 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7361 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7362 params->go_freq, -1, 0, 1, 1);
7363}
7364
7365
7366static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7367 struct p2p_nfc_params *params, int tag)
7368{
7369 int res, persistent;
7370 struct wpa_ssid *ssid;
7371
7372 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7373 "connection handover");
7374 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7375 ssid = wpa_s->current_ssid;
7376 if (ssid == NULL)
7377 continue;
7378 if (ssid->mode != WPAS_MODE_P2P_GO)
7379 continue;
7380 if (wpa_s->ap_iface == NULL)
7381 continue;
7382 break;
7383 }
7384 if (wpa_s == NULL) {
7385 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7386 return -1;
7387 }
7388
7389 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7390 DEV_PW_NFC_CONNECTION_HANDOVER &&
7391 !wpa_s->parent->p2p_oob_dev_pw) {
7392 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7393 return -1;
7394 }
7395 res = wpas_ap_wps_add_nfc_pw(
7396 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7397 wpa_s->parent->p2p_oob_dev_pw,
7398 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7399 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7400 if (res)
7401 return res;
7402
7403 if (!tag) {
7404 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7405 return 0;
7406 }
7407
7408 if (!params->peer ||
7409 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7410 return 0;
7411
7412 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7413 " to join", MAC2STR(params->peer->p2p_device_addr));
7414
7415 wpa_s->global->p2p_invite_group = wpa_s;
7416 persistent = ssid->p2p_persistent_group &&
7417 wpas_p2p_get_persistent(wpa_s->parent,
7418 params->peer->p2p_device_addr,
7419 ssid->ssid, ssid->ssid_len);
7420 wpa_s->parent->pending_invite_ssid_id = -1;
7421
7422 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7423 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7424 ssid->ssid, ssid->ssid_len, ssid->frequency,
7425 wpa_s->global->p2p_dev_addr, persistent, 0,
7426 wpa_s->parent->p2p_oob_dev_pw_id);
7427}
7428
7429
7430static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7431 struct p2p_nfc_params *params,
7432 int forced_freq)
7433{
7434 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7435 "connection handover");
7436 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7437 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7438 forced_freq, -1, 0, 1, 1);
7439}
7440
7441
7442static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7443 struct p2p_nfc_params *params,
7444 int forced_freq)
7445{
7446 int res;
7447
7448 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7449 "connection handover");
7450 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7451 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7452 forced_freq, -1, 0, 1, 1);
7453 if (res)
7454 return res;
7455
7456 res = wpas_p2p_listen(wpa_s, 60);
7457 if (res) {
7458 p2p_unauthorize(wpa_s->global->p2p,
7459 params->peer->p2p_device_addr);
7460 }
7461
7462 return res;
7463}
7464
7465
7466static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7467 const struct wpabuf *data,
7468 int sel, int tag, int forced_freq)
7469{
7470 const u8 *pos, *end;
7471 u16 len, id;
7472 struct p2p_nfc_params params;
7473 int res;
7474
7475 os_memset(&params, 0, sizeof(params));
7476 params.sel = sel;
7477
7478 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7479
7480 pos = wpabuf_head(data);
7481 end = pos + wpabuf_len(data);
7482
7483 if (end - pos < 2) {
7484 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7485 "attributes");
7486 return -1;
7487 }
7488 len = WPA_GET_BE16(pos);
7489 pos += 2;
7490 if (pos + len > end) {
7491 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7492 "attributes");
7493 return -1;
7494 }
7495 params.wsc_attr = pos;
7496 params.wsc_len = len;
7497 pos += len;
7498
7499 if (end - pos < 2) {
7500 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7501 "attributes");
7502 return -1;
7503 }
7504 len = WPA_GET_BE16(pos);
7505 pos += 2;
7506 if (pos + len > end) {
7507 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7508 "attributes");
7509 return -1;
7510 }
7511 params.p2p_attr = pos;
7512 params.p2p_len = len;
7513 pos += len;
7514
7515 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7516 params.wsc_attr, params.wsc_len);
7517 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7518 params.p2p_attr, params.p2p_len);
7519 if (pos < end) {
7520 wpa_hexdump(MSG_DEBUG,
7521 "P2P: Ignored extra data after P2P attributes",
7522 pos, end - pos);
7523 }
7524
7525 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7526 if (res)
7527 return res;
7528
7529 if (params.next_step == NO_ACTION)
7530 return 0;
7531
7532 if (params.next_step == BOTH_GO) {
7533 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7534 MAC2STR(params.peer->p2p_device_addr));
7535 return 0;
7536 }
7537
7538 if (params.next_step == PEER_CLIENT) {
7539 if (!is_zero_ether_addr(params.go_dev_addr)) {
7540 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7541 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7542 " ssid=\"%s\"",
7543 MAC2STR(params.peer->p2p_device_addr),
7544 params.go_freq,
7545 MAC2STR(params.go_dev_addr),
7546 wpa_ssid_txt(params.go_ssid,
7547 params.go_ssid_len));
7548 } else {
7549 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7550 "peer=" MACSTR " freq=%d",
7551 MAC2STR(params.peer->p2p_device_addr),
7552 params.go_freq);
7553 }
7554 return 0;
7555 }
7556
7557 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7558 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7559 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7560 return 0;
7561 }
7562
7563 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7564 wpa_s->p2p_oob_dev_pw = NULL;
7565
7566 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7567 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7568 "received");
7569 return -1;
7570 }
7571
7572 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7573 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7574 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7575 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7576 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7577 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7578 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7579
7580 if (tag) {
7581 if (id < 0x10) {
7582 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7583 "peer OOB Device Password Id %u", id);
7584 return -1;
7585 }
7586 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7587 "Device Password Id %u", id);
7588 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7589 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7590 params.oob_dev_pw_len -
7591 WPS_OOB_PUBKEY_HASH_LEN - 2);
7592 wpa_s->p2p_oob_dev_pw_id = id;
7593 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7594 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7595 params.oob_dev_pw_len -
7596 WPS_OOB_PUBKEY_HASH_LEN - 2);
7597 if (wpa_s->p2p_oob_dev_pw == NULL)
7598 return -1;
7599
7600 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7601 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7602 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7603 return -1;
7604 } else {
7605 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7606 "without Device Password");
7607 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7608 }
7609
7610 switch (params.next_step) {
7611 case NO_ACTION:
7612 case BOTH_GO:
7613 case PEER_CLIENT:
7614 /* already covered above */
7615 return 0;
7616 case JOIN_GROUP:
7617 return wpas_p2p_nfc_join_group(wpa_s, &params);
7618 case AUTH_JOIN:
7619 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7620 case INIT_GO_NEG:
7621 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7622 case RESP_GO_NEG:
7623 /* TODO: use own OOB Dev Pw */
7624 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7625 }
7626
7627 return -1;
7628}
7629
7630
7631int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7632 const struct wpabuf *data, int forced_freq)
7633{
7634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7635 return -1;
7636
7637 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7638}
7639
7640
7641int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7642 const struct wpabuf *req,
7643 const struct wpabuf *sel, int forced_freq)
7644{
7645 struct wpabuf *tmp;
7646 int ret;
7647
7648 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7649 return -1;
7650
7651 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7652
7653 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7654 wpabuf_head(req), wpabuf_len(req));
7655 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7656 wpabuf_head(sel), wpabuf_len(sel));
7657 if (forced_freq)
7658 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7659 tmp = ndef_parse_p2p(init ? sel : req);
7660 if (tmp == NULL) {
7661 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7662 return -1;
7663 }
7664
7665 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7666 forced_freq);
7667 wpabuf_free(tmp);
7668
7669 return ret;
7670}
7671
7672
7673int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7674{
7675 const u8 *if_addr;
7676 int go_intent = wpa_s->conf->p2p_go_intent;
7677 struct wpa_supplicant *iface;
7678
7679 if (wpa_s->global->p2p == NULL)
7680 return -1;
7681
7682 if (!enabled) {
7683 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7684 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7685 {
7686 if (!iface->ap_iface)
7687 continue;
7688 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7689 }
7690 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7691 0, NULL);
7692 if (wpa_s->p2p_nfc_tag_enabled)
7693 wpas_p2p_remove_pending_group_interface(wpa_s);
7694 wpa_s->p2p_nfc_tag_enabled = 0;
7695 return 0;
7696 }
7697
7698 if (wpa_s->global->p2p_disabled)
7699 return -1;
7700
7701 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7702 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7703 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7704 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7705 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7706 "to allow static handover cases");
7707 return -1;
7708 }
7709
7710 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7711
7712 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7713 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7714 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7715 if (wpa_s->p2p_oob_dev_pw == NULL)
7716 return -1;
7717 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7718
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007719 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7720 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7721 /*
7722 * P2P Group Interface present and the command came on group
7723 * interface, so enable the token for the current interface.
7724 */
7725 wpa_s->create_p2p_iface = 0;
7726 } else {
7727 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7728 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007729
7730 if (wpa_s->create_p2p_iface) {
7731 enum wpa_driver_if_type iftype;
7732 /* Prepare to add a new interface for the group */
7733 iftype = WPA_IF_P2P_GROUP;
7734 if (go_intent == 15)
7735 iftype = WPA_IF_P2P_GO;
7736 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7737 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7738 "interface for the group");
7739 return -1;
7740 }
7741
7742 if_addr = wpa_s->pending_interface_addr;
7743 } else
7744 if_addr = wpa_s->own_addr;
7745
7746 wpa_s->p2p_nfc_tag_enabled = enabled;
7747
7748 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7749 struct hostapd_data *hapd;
7750 if (iface->ap_iface == NULL)
7751 continue;
7752 hapd = iface->ap_iface->bss[0];
7753 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7754 hapd->conf->wps_nfc_dh_pubkey =
7755 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7756 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7757 hapd->conf->wps_nfc_dh_privkey =
7758 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7759 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7760 hapd->conf->wps_nfc_dev_pw =
7761 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7762 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7763
7764 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7765 wpa_dbg(iface, MSG_DEBUG,
7766 "P2P: Failed to enable NFC Tag for GO");
7767 }
7768 }
7769 p2p_set_authorized_oob_dev_pw_id(
7770 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7771 if_addr);
7772
7773 return 0;
7774}
7775
7776#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007777
7778
7779static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
7780 struct wpa_used_freq_data *freqs,
7781 unsigned int num)
7782{
7783 u8 curr_chan, cand, chan;
7784 unsigned int i;
7785
7786 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
7787 for (i = 0, cand = 0; i < num; i++) {
7788 ieee80211_freq_to_chan(freqs[i].freq, &chan);
7789 if (curr_chan == chan) {
7790 cand = 0;
7791 break;
7792 }
7793
7794 if (chan == 1 || chan == 6 || chan == 11)
7795 cand = chan;
7796 }
7797
7798 if (cand) {
7799 wpa_dbg(wpa_s, MSG_DEBUG,
7800 "P2P: Update Listen channel to %u baased on operating channel",
7801 cand);
7802 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
7803 }
7804}
7805
7806
7807void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
7808{
7809 struct wpa_used_freq_data *freqs;
7810 unsigned int num = wpa_s->num_multichan_concurrent;
7811
7812 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7813 return;
7814
7815 /*
7816 * If possible, optimize the Listen channel to be a channel that is
7817 * already used by one of the other interfaces.
7818 */
7819 if (!wpa_s->conf->p2p_optimize_listen_chan)
7820 return;
7821
7822 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
7823 return;
7824
7825 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7826 if (!freqs)
7827 return;
7828
7829 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7830
7831 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7832 os_free(freqs);
7833}
7834
7835
7836void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
7837{
7838 if (wpa_s == wpa_s->parent)
7839 wpas_p2p_group_remove(wpa_s, "*");
7840 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
7841 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
7842 "the management interface is being removed");
7843 wpas_p2p_deinit_global(wpa_s->global);
7844 }
7845}
7846
7847
7848void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
7849{
7850 if (wpa_s->ap_iface->bss)
7851 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
7852 wpas_p2p_group_deinit(wpa_s);
7853}