blob: 889a949f2d09f9e5ccc6aa5a8d062b62e4bb46b5 [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 Shmidt8d520ff2011-05-09 14:06:53 -070023#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070024#include "eapol_supp/eapol_supp_sm.h"
25#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "wpa_supplicant_i.h"
27#include "driver_i.h"
28#include "ap.h"
29#include "config_ssid.h"
30#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031#include "notify.h"
32#include "scan.h"
33#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080034#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035#include "wps_supplicant.h"
36#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080037#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038
39
40/*
41 * How many times to try to scan to find the GO before giving up on join
42 * request.
43 */
44#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
45
Dmitry Shmidt04949592012-07-19 12:16:46 -070046#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
47
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080048#ifndef P2P_MAX_CLIENT_IDLE
49/*
50 * How many seconds to try to reconnect to the GO when connection in P2P client
51 * role has been lost.
52 */
53#define P2P_MAX_CLIENT_IDLE 10
54#endif /* P2P_MAX_CLIENT_IDLE */
55
Dmitry Shmidt04949592012-07-19 12:16:46 -070056#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070064#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
65/*
66 * How many seconds to wait for initial 4-way handshake to get completed after
67 * WPS provisioning step on the GO. This controls the extra time the P2P
68 * operation is considered to be in progress (e.g., to delay other scans) after
69 * WPS provisioning has been completed on the GO during group formation.
70 */
71#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
72#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
73
Dmitry Shmidt56052862013-10-04 10:23:25 -070074#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
75/*
76 * How many seconds to wait for initial 4-way handshake to get completed after
77 * re-invocation of a persistent group on the GO when the client is expected
78 * to connect automatically (no user interaction).
79 */
80#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
81#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
82
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070083#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070084#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085#endif /* P2P_CONCURRENT_SEARCH_DELAY */
86
Dmitry Shmidt34af3062013-07-11 10:46:32 -070087#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
88
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070089enum p2p_group_removal_reason {
90 P2P_GROUP_REMOVAL_UNKNOWN,
91 P2P_GROUP_REMOVAL_SILENT,
92 P2P_GROUP_REMOVAL_FORMATION_FAILED,
93 P2P_GROUP_REMOVAL_REQUESTED,
94 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
95 P2P_GROUP_REMOVAL_UNAVAILABLE,
96 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080097 P2P_GROUP_REMOVAL_PSK_FAILURE,
98 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070099};
100
Jouni Malinendc7b7132012-09-14 12:53:47 -0700101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
103static struct wpa_supplicant *
104wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
105 int go);
106static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800107static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
110static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700111 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800112 int auto_join, 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 Shmidt8d520ff2011-05-09 14:06:53 -0700123
124
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700125/*
126 * Get the number of concurrent channels that the HW can operate, but that are
127 * currently not in use by any of the wpa_supplicant interfaces.
128 */
129static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
130{
131 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800132 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700133
134 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
135 if (!freqs)
136 return -1;
137
138 num = get_shared_radio_freqs(wpa_s, freqs,
139 wpa_s->num_multichan_concurrent);
140 os_free(freqs);
141
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800142 unused = wpa_s->num_multichan_concurrent - num;
143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
144 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700145}
146
147
148/*
149 * Get the frequencies that are currently in use by one or more of the virtual
150 * interfaces, and that are also valid for P2P operation.
151 */
152static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
153 int *p2p_freqs, unsigned int len)
154{
155 int *freqs;
156 unsigned int num, i, j;
157
158 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
159 if (!freqs)
160 return -1;
161
162 num = get_shared_radio_freqs(wpa_s, freqs,
163 wpa_s->num_multichan_concurrent);
164
165 os_memset(p2p_freqs, 0, sizeof(int) * len);
166
167 for (i = 0, j = 0; i < num && j < len; i++) {
168 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
169 p2p_freqs[j++] = freqs[i];
170 }
171
172 os_free(freqs);
173
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800174 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
175
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700176 return j;
177}
178
179
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700180static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
181 int freq)
182{
183 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
184 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800185 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
186 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
187 wpas_p2p_num_unused_channels(wpa_s) > 0) {
188 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
189 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700190 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800191 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700192 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
193}
194
195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
197 struct wpa_scan_results *scan_res)
198{
199 size_t i;
200
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800201 if (wpa_s->p2p_scan_work) {
202 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
203 wpa_s->p2p_scan_work = NULL;
204 radio_work_done(work);
205 }
206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
208 return;
209
210 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
211 (int) scan_res->num);
212
213 for (i = 0; i < scan_res->num; i++) {
214 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800215 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700216 const u8 *ies;
217 size_t ies_len;
218
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800219 time_tmp_age.sec = bss->age / 1000;
220 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800221 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700222
223 ies = (const u8 *) (bss + 1);
224 ies_len = bss->ie_len;
225 if (bss->beacon_ie_len > 0 &&
226 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
227 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
228 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
229 MACSTR, MAC2STR(bss->bssid));
230 ies = ies + ies_len;
231 ies_len = bss->beacon_ie_len;
232 }
233
234
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800236 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700237 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 break;
239 }
240
241 p2p_scan_res_handled(wpa_s->global->p2p);
242}
243
244
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800245static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
246{
247 struct wpa_supplicant *wpa_s = work->wpa_s;
248 struct wpa_driver_scan_params *params = work->ctx;
249 int ret;
250
251 if (deinit) {
252 wpa_scan_free_params(params);
253 return;
254 }
255
256 ret = wpa_drv_scan(wpa_s, params);
257 wpa_scan_free_params(params);
258 work->ctx = NULL;
259 if (ret) {
260 radio_work_done(work);
261 return;
262 }
263
264 os_get_reltime(&wpa_s->scan_trigger_time);
265 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
266 wpa_s->own_scan_requested = 1;
267 wpa_s->p2p_scan_work = work;
268}
269
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
272 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274{
275 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800276 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700277 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800279 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280
281 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
282 return -1;
283
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800284 if (wpa_s->p2p_scan_work) {
285 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
286 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700287 }
288
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800289 params = os_zalloc(sizeof(*params));
290 if (params == NULL)
291 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292
293 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294 params->num_ssids = 1;
295 n = os_malloc(P2P_WILDCARD_SSID_LEN);
296 if (n == NULL)
297 goto fail;
298 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
299 params->ssids[0].ssid = n;
300 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301
302 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700303 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
304 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 num_req_dev_types, req_dev_types);
306 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800309 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
310 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311 if (ies == NULL) {
312 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800313 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 }
315 wpabuf_put_buf(ies, wps_ie);
316 wpabuf_free(wps_ie);
317
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800318 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800320 params->p2p_probe = 1;
321 n = os_malloc(wpabuf_len(ies));
322 if (n == NULL) {
323 wpabuf_free(ies);
324 goto fail;
325 }
326 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
327 params->extra_ies = n;
328 params->extra_ies_len = wpabuf_len(ies);
329 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330
331 switch (type) {
332 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800333 params->freqs = os_malloc(4 * sizeof(int));
334 if (params->freqs == NULL)
335 goto fail;
336 params->freqs[0] = 2412;
337 params->freqs[1] = 2437;
338 params->freqs[2] = 2462;
339 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 break;
341 case P2P_SCAN_FULL:
342 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800344 params->freqs = os_malloc(5 * 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] = freq;
351 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 break;
353 }
354
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800355 radio_remove_unstarted_work(wpa_s, "p2p-scan");
356 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
357 params) < 0)
358 goto fail;
359 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800361fail:
362 wpa_scan_free_params(params);
363 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364}
365
366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700367static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
368{
369 switch (p2p_group_interface) {
370 case P2P_GROUP_INTERFACE_PENDING:
371 return WPA_IF_P2P_GROUP;
372 case P2P_GROUP_INTERFACE_GO:
373 return WPA_IF_P2P_GO;
374 case P2P_GROUP_INTERFACE_CLIENT:
375 return WPA_IF_P2P_CLIENT;
376 }
377
378 return WPA_IF_P2P_GROUP;
379}
380
381
382static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
383 const u8 *ssid,
384 size_t ssid_len, int *go)
385{
386 struct wpa_ssid *s;
387
388 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
389 for (s = wpa_s->conf->ssid; s; s = s->next) {
390 if (s->disabled != 0 || !s->p2p_group ||
391 s->ssid_len != ssid_len ||
392 os_memcmp(ssid, s->ssid, ssid_len) != 0)
393 continue;
394 if (s->mode == WPAS_MODE_P2P_GO &&
395 s != wpa_s->current_ssid)
396 continue;
397 if (go)
398 *go = s->mode == WPAS_MODE_P2P_GO;
399 return wpa_s;
400 }
401 }
402
403 return NULL;
404}
405
406
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700407static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
408 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409{
410 struct wpa_ssid *ssid;
411 char *gtype;
412 const char *reason;
413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 ssid = wpa_s->current_ssid;
415 if (ssid == NULL) {
416 /*
417 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700418 * pending P2P group interface waiting for provisioning or a
419 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 */
421 ssid = wpa_s->conf->ssid;
422 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700423 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 break;
425 ssid = ssid->next;
426 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300427 if (ssid == NULL &&
428 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
429 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700430 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
431 "not found");
432 return -1;
433 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 }
435 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
436 gtype = "GO";
437 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
438 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
439 wpa_s->reassociate = 0;
440 wpa_s->disconnected = 1;
441 wpa_supplicant_deauthenticate(wpa_s,
442 WLAN_REASON_DEAUTH_LEAVING);
443 gtype = "client";
444 } else
445 gtype = "GO";
446 if (wpa_s->cross_connect_in_use) {
447 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700448 wpa_msg_global(wpa_s->parent, MSG_INFO,
449 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
450 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700451 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700452 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 case P2P_GROUP_REMOVAL_REQUESTED:
454 reason = " reason=REQUESTED";
455 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700456 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
457 reason = " reason=FORMATION_FAILED";
458 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
460 reason = " reason=IDLE";
461 break;
462 case P2P_GROUP_REMOVAL_UNAVAILABLE:
463 reason = " reason=UNAVAILABLE";
464 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700465 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
466 reason = " reason=GO_ENDING_SESSION";
467 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700468 case P2P_GROUP_REMOVAL_PSK_FAILURE:
469 reason = " reason=PSK_FAILURE";
470 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800471 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
472 reason = " reason=FREQ_CONFLICT";
473 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 default:
475 reason = "";
476 break;
477 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700478 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700479 wpa_msg_global(wpa_s->parent, MSG_INFO,
480 P2P_EVENT_GROUP_REMOVED "%s %s%s",
481 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700482 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800484 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
485 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700486 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
487 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800488 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700489 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800490 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
491 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700492 wpa_s->p2p_in_provisioning = 0;
493 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700494
Dmitry Shmidt96571392013-10-14 12:54:46 -0700495 /*
496 * Make sure wait for the first client does not remain active after the
497 * group has been removed.
498 */
499 wpa_s->global->p2p_go_wait_client.sec = 0;
500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700501 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
503
504 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
505 struct wpa_global *global;
506 char *ifname;
507 enum wpa_driver_if_type type;
508 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
509 wpa_s->ifname);
510 global = wpa_s->global;
511 ifname = os_strdup(wpa_s->ifname);
512 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700513 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 wpa_s = global->ifaces;
515 if (wpa_s && ifname)
516 wpa_drv_if_remove(wpa_s, type, ifname);
517 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300518 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 }
520
Dmitry Shmidt96571392013-10-14 12:54:46 -0700521 if (!wpa_s->p2p_go_group_formation_completed) {
522 wpa_s->global->p2p_group_formation = NULL;
523 wpa_s->p2p_in_provisioning = 0;
524 }
525
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800526 wpa_s->show_group_started = 0;
527 os_free(wpa_s->go_params);
528 wpa_s->go_params = NULL;
529
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800530 wpa_s->waiting_presence_resp = 0;
531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
533 if (ssid && (ssid->p2p_group ||
534 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
535 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
536 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700537 if (ssid == wpa_s->current_ssid) {
538 wpa_sm_set_config(wpa_s->wpa, NULL);
539 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700541 }
542 /*
543 * Networks objects created during any P2P activities are not
544 * exposed out as they might/will confuse certain non-P2P aware
545 * applications since these network objects won't behave like
546 * regular ones.
547 *
548 * Likewise, we don't send out network removed signals for such
549 * network objects.
550 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551 wpa_config_remove_network(wpa_s->conf, id);
552 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800553 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 } else {
555 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
556 "found");
557 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700558 if (wpa_s->ap_iface)
559 wpa_supplicant_ap_deinit(wpa_s);
560 else
561 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700562
563 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564}
565
566
567static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
568 u8 *go_dev_addr,
569 const u8 *ssid, size_t ssid_len)
570{
571 struct wpa_bss *bss;
572 const u8 *bssid;
573 struct wpabuf *p2p;
574 u8 group_capab;
575 const u8 *addr;
576
577 if (wpa_s->go_params)
578 bssid = wpa_s->go_params->peer_interface_addr;
579 else
580 bssid = wpa_s->bssid;
581
582 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
583 if (bss == NULL) {
584 u8 iface_addr[ETH_ALEN];
585 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
586 iface_addr) == 0)
587 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
588 }
589 if (bss == NULL) {
590 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
591 "group is persistent - BSS " MACSTR " not found",
592 MAC2STR(bssid));
593 return 0;
594 }
595
596 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700597 if (p2p == NULL)
598 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
599 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 if (p2p == NULL) {
601 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
602 "group is persistent - BSS " MACSTR
603 " did not include P2P IE", MAC2STR(bssid));
604 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
605 (u8 *) (bss + 1), bss->ie_len);
606 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
607 ((u8 *) bss + 1) + bss->ie_len,
608 bss->beacon_ie_len);
609 return 0;
610 }
611
612 group_capab = p2p_get_group_capab(p2p);
613 addr = p2p_get_go_dev_addr(p2p);
614 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
615 "group_capab=0x%x", group_capab);
616 if (addr) {
617 os_memcpy(go_dev_addr, addr, ETH_ALEN);
618 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
619 MAC2STR(addr));
620 } else
621 os_memset(go_dev_addr, 0, ETH_ALEN);
622 wpabuf_free(p2p);
623
624 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
625 "go_dev_addr=" MACSTR,
626 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
627
628 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
629}
630
631
Jouni Malinen75ecf522011-06-27 15:19:46 -0700632static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
633 struct wpa_ssid *ssid,
634 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635{
636 struct wpa_ssid *s;
637 int changed = 0;
638
639 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
640 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
641 for (s = wpa_s->conf->ssid; s; s = s->next) {
642 if (s->disabled == 2 &&
643 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
644 s->ssid_len == ssid->ssid_len &&
645 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
646 break;
647 }
648
649 if (s) {
650 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
651 "entry");
652 if (ssid->passphrase && !s->passphrase)
653 changed = 1;
654 else if (ssid->passphrase && s->passphrase &&
655 os_strcmp(ssid->passphrase, s->passphrase) != 0)
656 changed = 1;
657 } else {
658 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
659 "entry");
660 changed = 1;
661 s = wpa_config_add_network(wpa_s->conf);
662 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700663 return -1;
664
665 /*
666 * Instead of network_added we emit persistent_group_added
667 * notification. Also to keep the defense checks in
668 * persistent_group obj registration method, we set the
669 * relevant flags in s to designate it as a persistent group.
670 */
671 s->p2p_group = 1;
672 s->p2p_persistent_group = 1;
673 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 wpa_config_set_network_defaults(s);
675 }
676
677 s->p2p_group = 1;
678 s->p2p_persistent_group = 1;
679 s->disabled = 2;
680 s->bssid_set = 1;
681 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
682 s->mode = ssid->mode;
683 s->auth_alg = WPA_AUTH_ALG_OPEN;
684 s->key_mgmt = WPA_KEY_MGMT_PSK;
685 s->proto = WPA_PROTO_RSN;
686 s->pairwise_cipher = WPA_CIPHER_CCMP;
687 s->export_keys = 1;
688 if (ssid->passphrase) {
689 os_free(s->passphrase);
690 s->passphrase = os_strdup(ssid->passphrase);
691 }
692 if (ssid->psk_set) {
693 s->psk_set = 1;
694 os_memcpy(s->psk, ssid->psk, 32);
695 }
696 if (s->passphrase && !s->psk_set)
697 wpa_config_update_psk(s);
698 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
699 os_free(s->ssid);
700 s->ssid = os_malloc(ssid->ssid_len);
701 }
702 if (s->ssid) {
703 s->ssid_len = ssid->ssid_len;
704 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
705 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700706 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
707 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
708 wpa_s->global->add_psk = NULL;
709 changed = 1;
710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711
712#ifndef CONFIG_NO_CONFIG_WRITE
713 if (changed && wpa_s->conf->update_config &&
714 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
715 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
716 }
717#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700718
719 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720}
721
722
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800723static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
724 const u8 *addr)
725{
726 struct wpa_ssid *ssid, *s;
727 u8 *n;
728 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800730
731 ssid = wpa_s->current_ssid;
732 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
733 !ssid->p2p_persistent_group)
734 return;
735
736 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
737 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
738 continue;
739
740 if (s->ssid_len == ssid->ssid_len &&
741 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
742 break;
743 }
744
745 if (s == NULL)
746 return;
747
748 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
749 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700750 ETH_ALEN) != 0)
751 continue;
752
753 if (i == s->num_p2p_clients - 1)
754 return; /* already the most recent entry */
755
756 /* move the entry to mark it most recent */
757 os_memmove(s->p2p_client_list + i * ETH_ALEN,
758 s->p2p_client_list + (i + 1) * ETH_ALEN,
759 (s->num_p2p_clients - i - 1) * ETH_ALEN);
760 os_memcpy(s->p2p_client_list +
761 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
762 found = 1;
763 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 }
765
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700766 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
767 n = os_realloc_array(s->p2p_client_list,
768 s->num_p2p_clients + 1, ETH_ALEN);
769 if (n == NULL)
770 return;
771 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
772 s->p2p_client_list = n;
773 s->num_p2p_clients++;
774 } else if (!found) {
775 /* Not enough room for an additional entry - drop the oldest
776 * entry */
777 os_memmove(s->p2p_client_list,
778 s->p2p_client_list + ETH_ALEN,
779 (s->num_p2p_clients - 1) * ETH_ALEN);
780 os_memcpy(s->p2p_client_list +
781 (s->num_p2p_clients - 1) * ETH_ALEN,
782 addr, ETH_ALEN);
783 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800784
785#ifndef CONFIG_NO_CONFIG_WRITE
786 if (wpa_s->parent->conf->update_config &&
787 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
788 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
789#endif /* CONFIG_NO_CONFIG_WRITE */
790}
791
792
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
794 int success)
795{
796 struct wpa_ssid *ssid;
797 const char *ssid_txt;
798 int client;
799 int persistent;
800 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700801 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802
803 /*
804 * This callback is likely called for the main interface. Update wpa_s
805 * to use the group interface if a new interface was created for the
806 * group.
807 */
808 if (wpa_s->global->p2p_group_formation)
809 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700810 if (wpa_s->p2p_go_group_formation_completed) {
811 wpa_s->global->p2p_group_formation = NULL;
812 wpa_s->p2p_in_provisioning = 0;
813 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814
815 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700816 wpa_msg_global(wpa_s->parent, MSG_INFO,
817 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700818 wpas_p2p_group_delete(wpa_s,
819 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 return;
821 }
822
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700823 wpa_msg_global(wpa_s->parent, MSG_INFO,
824 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825
826 ssid = wpa_s->current_ssid;
827 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
828 ssid->mode = WPAS_MODE_P2P_GO;
829 p2p_group_notif_formation_done(wpa_s->p2p_group);
830 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
831 }
832
833 persistent = 0;
834 if (ssid) {
835 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
836 client = ssid->mode == WPAS_MODE_INFRA;
837 if (ssid->mode == WPAS_MODE_P2P_GO) {
838 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700839 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
840 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 } else
842 persistent = wpas_p2p_persistent_group(wpa_s,
843 go_dev_addr,
844 ssid->ssid,
845 ssid->ssid_len);
846 } else {
847 ssid_txt = "";
848 client = wpa_s->p2p_group_interface ==
849 P2P_GROUP_INTERFACE_CLIENT;
850 os_memset(go_dev_addr, 0, ETH_ALEN);
851 }
852
853 wpa_s->show_group_started = 0;
854 if (client) {
855 /*
856 * Indicate event only after successfully completed 4-way
857 * handshake, i.e., when the interface is ready for data
858 * packets.
859 */
860 wpa_s->show_group_started = 1;
861 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
862 char psk[65];
863 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700864 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
865 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
866 MACSTR "%s",
867 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
868 MAC2STR(go_dev_addr),
869 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 wpas_p2p_cross_connect_setup(wpa_s);
871 wpas_p2p_set_group_idle_timeout(wpa_s);
872 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700873 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
874 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
875 "go_dev_addr=" MACSTR "%s",
876 wpa_s->ifname, ssid_txt,
877 ssid ? ssid->frequency : 0,
878 ssid && ssid->passphrase ? ssid->passphrase : "",
879 MAC2STR(go_dev_addr),
880 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpas_p2p_cross_connect_setup(wpa_s);
882 wpas_p2p_set_group_idle_timeout(wpa_s);
883 }
884
885 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700886 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
887 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700888 else {
889 os_free(wpa_s->global->add_psk);
890 wpa_s->global->add_psk = NULL;
891 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800892 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700893 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700894 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700895 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800896 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700898}
899
900
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800901struct send_action_work {
902 unsigned int freq;
903 u8 dst[ETH_ALEN];
904 u8 src[ETH_ALEN];
905 u8 bssid[ETH_ALEN];
906 size_t len;
907 unsigned int wait_time;
908 u8 buf[0];
909};
910
911
912static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
913 void *timeout_ctx)
914{
915 struct wpa_supplicant *wpa_s = eloop_ctx;
916
917 if (!wpa_s->p2p_send_action_work)
918 return;
919
920 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
921 os_free(wpa_s->p2p_send_action_work->ctx);
922 radio_work_done(wpa_s->p2p_send_action_work);
923 wpa_s->p2p_send_action_work = NULL;
924}
925
926
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800927static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
928 unsigned int freq,
929 const u8 *dst, const u8 *src,
930 const u8 *bssid,
931 const u8 *data, size_t data_len,
932 enum offchannel_send_action_result
933 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800935 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800937 if (wpa_s->p2p_send_action_work) {
938 struct send_action_work *awork;
939 awork = wpa_s->p2p_send_action_work->ctx;
940 if (awork->wait_time == 0) {
941 os_free(awork);
942 radio_work_done(wpa_s->p2p_send_action_work);
943 wpa_s->p2p_send_action_work = NULL;
944 } else {
945 /*
946 * In theory, this should not be needed, but number of
947 * places in the P2P code is still using non-zero wait
948 * time for the last Action frame in the sequence and
949 * some of these do not call send_action_done().
950 */
951 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
952 wpa_s, NULL);
953 eloop_register_timeout(
954 0, awork->wait_time * 1000,
955 wpas_p2p_send_action_work_timeout,
956 wpa_s, NULL);
957 }
958 }
959
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
961 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800963 switch (result) {
964 case OFFCHANNEL_SEND_ACTION_SUCCESS:
965 res = P2P_SEND_ACTION_SUCCESS;
966 break;
967 case OFFCHANNEL_SEND_ACTION_NO_ACK:
968 res = P2P_SEND_ACTION_NO_ACK;
969 break;
970 case OFFCHANNEL_SEND_ACTION_FAILED:
971 res = P2P_SEND_ACTION_FAILED;
972 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 }
974
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800975 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800977 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
978 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800979 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800980 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
981 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
984 "during p2p_connect-auto");
985 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
986 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 }
988}
989
990
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800991static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
992{
993 struct wpa_supplicant *wpa_s = work->wpa_s;
994 struct send_action_work *awork = work->ctx;
995
996 if (deinit) {
997 os_free(awork);
998 return;
999 }
1000
1001 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1002 awork->bssid, awork->buf, awork->len,
1003 awork->wait_time,
1004 wpas_p2p_send_action_tx_status, 1) < 0) {
1005 os_free(awork);
1006 radio_work_done(work);
1007 return;
1008 }
1009 wpa_s->p2p_send_action_work = work;
1010}
1011
1012
1013static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1014 unsigned int freq, const u8 *dst,
1015 const u8 *src, const u8 *bssid, const u8 *buf,
1016 size_t len, unsigned int wait_time)
1017{
1018 struct send_action_work *awork;
1019
1020 if (wpa_s->p2p_send_action_work) {
1021 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1022 return -1;
1023 }
1024
1025 awork = os_zalloc(sizeof(*awork) + len);
1026 if (awork == NULL)
1027 return -1;
1028
1029 awork->freq = freq;
1030 os_memcpy(awork->dst, dst, ETH_ALEN);
1031 os_memcpy(awork->src, src, ETH_ALEN);
1032 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1033 awork->len = len;
1034 awork->wait_time = wait_time;
1035 os_memcpy(awork->buf, buf, len);
1036
1037 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1038 wpas_send_action_cb, awork) < 0) {
1039 os_free(awork);
1040 return -1;
1041 }
1042
1043 return 0;
1044}
1045
1046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1048 const u8 *src, const u8 *bssid, const u8 *buf,
1049 size_t len, unsigned int wait_time)
1050{
1051 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001052 int listen_freq = -1, send_freq = -1;
1053
1054 if (wpa_s->p2p_listen_work)
1055 listen_freq = wpa_s->p2p_listen_work->freq;
1056 if (wpa_s->p2p_send_action_work)
1057 send_freq = wpa_s->p2p_send_action_work->freq;
1058 if (listen_freq != (int) freq && send_freq != (int) freq) {
1059 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1060 listen_freq, send_freq);
1061 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1062 len, wait_time);
1063 }
1064
1065 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001066 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1067 wait_time,
1068 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069}
1070
1071
1072static void wpas_send_action_done(void *ctx)
1073{
1074 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001075
1076 if (wpa_s->p2p_send_action_work) {
1077 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1078 wpa_s, NULL);
1079 os_free(wpa_s->p2p_send_action_work->ctx);
1080 radio_work_done(wpa_s->p2p_send_action_work);
1081 wpa_s->p2p_send_action_work = NULL;
1082 }
1083
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085}
1086
1087
1088static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1089 struct p2p_go_neg_results *params)
1090{
1091 if (wpa_s->go_params == NULL) {
1092 wpa_s->go_params = os_malloc(sizeof(*params));
1093 if (wpa_s->go_params == NULL)
1094 return -1;
1095 }
1096 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1097 return 0;
1098}
1099
1100
1101static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1102 struct p2p_go_neg_results *res)
1103{
1104 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
1105 MAC2STR(res->peer_interface_addr));
1106 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1107 res->ssid, res->ssid_len);
1108 wpa_supplicant_ap_deinit(wpa_s);
1109 wpas_copy_go_neg_results(wpa_s, res);
1110 if (res->wps_method == WPS_PBC)
1111 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1112 else {
1113 u16 dev_pw_id = DEV_PW_DEFAULT;
1114 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1115 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1116 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1117 wpa_s->p2p_pin, 1, dev_pw_id);
1118 }
1119}
1120
1121
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001122static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1123 struct wpa_ssid *ssid)
1124{
1125 struct wpa_ssid *persistent;
1126 struct psk_list_entry *psk;
1127 struct hostapd_data *hapd;
1128
1129 if (!wpa_s->ap_iface)
1130 return;
1131
1132 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1133 ssid->ssid_len);
1134 if (persistent == NULL)
1135 return;
1136
1137 hapd = wpa_s->ap_iface->bss[0];
1138
1139 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1140 list) {
1141 struct hostapd_wpa_psk *hpsk;
1142
1143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1144 MACSTR " psk=%d",
1145 MAC2STR(psk->addr), psk->p2p);
1146 hpsk = os_zalloc(sizeof(*hpsk));
1147 if (hpsk == NULL)
1148 break;
1149 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1150 if (psk->p2p)
1151 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1152 else
1153 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1154 hpsk->next = hapd->conf->ssid.wpa_psk;
1155 hapd->conf->ssid.wpa_psk = hpsk;
1156 }
1157}
1158
1159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160static void p2p_go_configured(void *ctx, void *data)
1161{
1162 struct wpa_supplicant *wpa_s = ctx;
1163 struct p2p_go_neg_results *params = data;
1164 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001165 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166
1167 ssid = wpa_s->current_ssid;
1168 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1169 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1170 if (wpa_s->global->p2p_group_formation == wpa_s)
1171 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001172 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001173 wpa_msg_global(wpa_s->parent, MSG_INFO,
1174 P2P_EVENT_GROUP_STARTED
1175 "%s GO ssid=\"%s\" freq=%d "
1176 "passphrase=\"%s\" go_dev_addr=" MACSTR
1177 "%s", wpa_s->ifname,
1178 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1179 ssid->frequency, params->passphrase,
1180 MAC2STR(wpa_s->global->p2p_dev_addr),
1181 params->persistent_group ?
1182 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001183 } else {
1184 char psk[65];
1185 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1186 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001187 wpa_msg_global(wpa_s->parent, MSG_INFO,
1188 P2P_EVENT_GROUP_STARTED
1189 "%s GO ssid=\"%s\" freq=%d psk=%s "
1190 "go_dev_addr=" MACSTR "%s",
1191 wpa_s->ifname,
1192 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1193 ssid->frequency, psk,
1194 MAC2STR(wpa_s->global->p2p_dev_addr),
1195 params->persistent_group ?
1196 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001197 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001199 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001200 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001201 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001203 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001204 wpas_p2p_add_psk_list(wpa_s, ssid);
1205 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001206 if (network_id < 0)
1207 network_id = ssid->id;
1208 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209 wpas_p2p_cross_connect_setup(wpa_s);
1210 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001211
1212 if (wpa_s->p2p_first_connection_timeout) {
1213 wpa_dbg(wpa_s, MSG_DEBUG,
1214 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1215 wpa_s->p2p_first_connection_timeout);
1216 wpa_s->p2p_go_group_formation_completed = 0;
1217 wpa_s->global->p2p_group_formation = wpa_s;
1218 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1219 wpa_s->parent, NULL);
1220 eloop_register_timeout(
1221 wpa_s->p2p_first_connection_timeout, 0,
1222 wpas_p2p_group_formation_timeout,
1223 wpa_s->parent, NULL);
1224 }
1225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 return;
1227 }
1228
1229 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1230 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1231 params->peer_interface_addr)) {
1232 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1233 "filtering");
1234 return;
1235 }
1236 if (params->wps_method == WPS_PBC)
1237 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001238 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239 else if (wpa_s->p2p_pin[0])
1240 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001241 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 os_free(wpa_s->go_params);
1243 wpa_s->go_params = NULL;
1244}
1245
1246
1247static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1248 struct p2p_go_neg_results *params,
1249 int group_formation)
1250{
1251 struct wpa_ssid *ssid;
1252
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001253 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1254 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1255 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1256 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259
1260 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001261 if (ssid == NULL) {
1262 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001264 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001266 wpa_s->show_group_started = 0;
1267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 wpa_config_set_network_defaults(ssid);
1269 ssid->temporary = 1;
1270 ssid->p2p_group = 1;
1271 ssid->p2p_persistent_group = params->persistent_group;
1272 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1273 WPAS_MODE_P2P_GO;
1274 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001275 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001276 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 ssid->ssid = os_zalloc(params->ssid_len + 1);
1278 if (ssid->ssid) {
1279 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1280 ssid->ssid_len = params->ssid_len;
1281 }
1282 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1283 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1284 ssid->proto = WPA_PROTO_RSN;
1285 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001286 if (os_strlen(params->passphrase) > 0) {
1287 ssid->passphrase = os_strdup(params->passphrase);
1288 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001289 wpa_msg_global(wpa_s, MSG_ERROR,
1290 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001291 wpa_config_remove_network(wpa_s->conf, ssid->id);
1292 return;
1293 }
1294 } else
1295 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001296 ssid->psk_set = params->psk_set;
1297 if (ssid->psk_set)
1298 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001299 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001300 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001301 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001302
1303 wpa_s->ap_configured_cb = p2p_go_configured;
1304 wpa_s->ap_configured_cb_ctx = wpa_s;
1305 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001306 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001307 wpa_s->reassociate = 1;
1308 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1310 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311 wpa_supplicant_req_scan(wpa_s, 0, 0);
1312}
1313
1314
1315static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1316 const struct wpa_supplicant *src)
1317{
1318 struct wpa_config *d;
1319 const struct wpa_config *s;
1320
1321 d = dst->conf;
1322 s = src->conf;
1323
1324#define C(n) if (s->n) d->n = os_strdup(s->n)
1325 C(device_name);
1326 C(manufacturer);
1327 C(model_name);
1328 C(model_number);
1329 C(serial_number);
1330 C(config_methods);
1331#undef C
1332
1333 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1334 os_memcpy(d->sec_device_type, s->sec_device_type,
1335 sizeof(d->sec_device_type));
1336 d->num_sec_device_types = s->num_sec_device_types;
1337
1338 d->p2p_group_idle = s->p2p_group_idle;
1339 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001340 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001341 d->max_num_sta = s->max_num_sta;
1342 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001343 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001344 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001345 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001346 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001347 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348}
1349
1350
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001351static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1352 char *ifname, size_t len)
1353{
1354 char *ifname_ptr = wpa_s->ifname;
1355
1356 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1357 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1358 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1359 }
1360
1361 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1362 if (os_strlen(ifname) >= IFNAMSIZ &&
1363 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1364 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001365 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001366 }
1367}
1368
1369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1371 enum wpa_driver_if_type type)
1372{
1373 char ifname[120], force_ifname[120];
1374
1375 if (wpa_s->pending_interface_name[0]) {
1376 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1377 "- skip creation of a new one");
1378 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1379 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1380 "unknown?! ifname='%s'",
1381 wpa_s->pending_interface_name);
1382 return -1;
1383 }
1384 return 0;
1385 }
1386
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001387 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 force_ifname[0] = '\0';
1389
1390 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1391 ifname);
1392 wpa_s->p2p_group_idx++;
1393
1394 wpa_s->pending_interface_type = type;
1395 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1396 wpa_s->pending_interface_addr, NULL) < 0) {
1397 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1398 "interface");
1399 return -1;
1400 }
1401
1402 if (force_ifname[0]) {
1403 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1404 force_ifname);
1405 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1406 sizeof(wpa_s->pending_interface_name));
1407 } else
1408 os_strlcpy(wpa_s->pending_interface_name, ifname,
1409 sizeof(wpa_s->pending_interface_name));
1410 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1411 MACSTR, wpa_s->pending_interface_name,
1412 MAC2STR(wpa_s->pending_interface_addr));
1413
1414 return 0;
1415}
1416
1417
1418static void wpas_p2p_remove_pending_group_interface(
1419 struct wpa_supplicant *wpa_s)
1420{
1421 if (!wpa_s->pending_interface_name[0] ||
1422 is_zero_ether_addr(wpa_s->pending_interface_addr))
1423 return; /* No pending virtual interface */
1424
1425 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1426 wpa_s->pending_interface_name);
1427 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1428 wpa_s->pending_interface_name);
1429 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1430 wpa_s->pending_interface_name[0] = '\0';
1431}
1432
1433
1434static struct wpa_supplicant *
1435wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1436{
1437 struct wpa_interface iface;
1438 struct wpa_supplicant *group_wpa_s;
1439
1440 if (!wpa_s->pending_interface_name[0]) {
1441 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1442 if (!wpas_p2p_create_iface(wpa_s))
1443 return NULL;
1444 /*
1445 * Something has forced us to remove the pending interface; try
1446 * to create a new one and hope for the best that we will get
1447 * the same local address.
1448 */
1449 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1450 WPA_IF_P2P_CLIENT) < 0)
1451 return NULL;
1452 }
1453
1454 os_memset(&iface, 0, sizeof(iface));
1455 iface.ifname = wpa_s->pending_interface_name;
1456 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001457 if (wpa_s->conf->ctrl_interface == NULL &&
1458 wpa_s->parent != wpa_s &&
1459 wpa_s->p2p_mgmt &&
1460 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1461 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1462 else
1463 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 iface.driver_param = wpa_s->conf->driver_param;
1465 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1466 if (group_wpa_s == NULL) {
1467 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1468 "wpa_supplicant interface");
1469 return NULL;
1470 }
1471 wpa_s->pending_interface_name[0] = '\0';
1472 group_wpa_s->parent = wpa_s;
1473 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1474 P2P_GROUP_INTERFACE_CLIENT;
1475 wpa_s->global->p2p_group_formation = group_wpa_s;
1476
1477 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1478
1479 return group_wpa_s;
1480}
1481
1482
1483static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1484 void *timeout_ctx)
1485{
1486 struct wpa_supplicant *wpa_s = eloop_ctx;
1487 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001488 wpas_p2p_group_formation_failed(wpa_s);
1489}
1490
1491
1492void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1493{
1494 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1495 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 if (wpa_s->global->p2p)
1497 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498 wpas_group_formation_completed(wpa_s, 0);
1499}
1500
1501
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001502static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1503{
1504 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1505 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1506 wpa_s->parent, NULL);
1507 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1508 wpa_s->parent, NULL);
1509 wpa_s->global->p2p_fail_on_wps_complete = 0;
1510}
1511
1512
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001513void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1514{
1515 if (wpa_s->global->p2p_group_formation != wpa_s)
1516 return;
1517 /* Speed up group formation timeout since this cannot succeed */
1518 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1519 wpa_s->parent, NULL);
1520 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1521 wpa_s->parent, NULL);
1522}
1523
1524
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001525static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526{
1527 struct wpa_supplicant *wpa_s = ctx;
1528
1529 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1530 wpa_drv_cancel_remain_on_channel(wpa_s);
1531 wpa_s->off_channel_freq = 0;
1532 wpa_s->roc_waiting_drv_freq = 0;
1533 }
1534
1535 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001536 wpa_msg_global(wpa_s, MSG_INFO,
1537 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1538 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001539 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540 wpas_p2p_remove_pending_group_interface(wpa_s);
1541 return;
1542 }
1543
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001544 if (wpa_s->p2p_go_ht40)
1545 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001546 if (wpa_s->p2p_go_vht)
1547 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001548
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001549 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1550 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1551 " wps_method=%s",
1552 res->role_go ? "GO" : "client", res->freq, res->ht40,
1553 MAC2STR(res->peer_device_addr),
1554 MAC2STR(res->peer_interface_addr),
1555 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001556 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557
Dmitry Shmidt04949592012-07-19 12:16:46 -07001558 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1559 struct wpa_ssid *ssid;
1560 ssid = wpa_config_get_network(wpa_s->conf,
1561 wpa_s->p2p_persistent_id);
1562 if (ssid && ssid->disabled == 2 &&
1563 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1564 size_t len = os_strlen(ssid->passphrase);
1565 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1566 "on requested persistent group");
1567 os_memcpy(res->passphrase, ssid->passphrase, len);
1568 res->passphrase[len] = '\0';
1569 }
1570 }
1571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 if (wpa_s->create_p2p_iface) {
1573 struct wpa_supplicant *group_wpa_s =
1574 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1575 if (group_wpa_s == NULL) {
1576 wpas_p2p_remove_pending_group_interface(wpa_s);
1577 return;
1578 }
1579 if (group_wpa_s != wpa_s) {
1580 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1581 sizeof(group_wpa_s->p2p_pin));
1582 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1583 }
1584 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1585 wpa_s->pending_interface_name[0] = '\0';
1586 group_wpa_s->p2p_in_provisioning = 1;
1587
1588 if (res->role_go)
1589 wpas_start_wps_go(group_wpa_s, res, 1);
1590 else
1591 wpas_start_wps_enrollee(group_wpa_s, res);
1592 } else {
1593 wpa_s->p2p_in_provisioning = 1;
1594 wpa_s->global->p2p_group_formation = wpa_s;
1595
1596 if (res->role_go)
1597 wpas_start_wps_go(wpa_s, res, 1);
1598 else
1599 wpas_start_wps_enrollee(ctx, res);
1600 }
1601
1602 wpa_s->p2p_long_listen = 0;
1603 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1604
1605 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1606 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1607 (res->peer_config_timeout % 100) * 10000,
1608 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1609}
1610
1611
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001612static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613{
1614 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001615 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1616 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617
1618 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1619}
1620
1621
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001622static void wpas_dev_found(void *ctx, const u8 *addr,
1623 const struct p2p_peer_info *info,
1624 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001626#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 struct wpa_supplicant *wpa_s = ctx;
1628 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001629 char *wfd_dev_info_hex = NULL;
1630
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001631#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001632 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1633 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001634#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001635
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001636 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1637 " p2p_dev_addr=" MACSTR
1638 " pri_dev_type=%s name='%s' config_methods=0x%x "
1639 "dev_capab=0x%x group_capab=0x%x%s%s",
1640 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1641 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1642 sizeof(devtype)),
1643 info->device_name, info->config_methods,
1644 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001645 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1646 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001647#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001649 os_free(wfd_dev_info_hex);
1650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1652}
1653
1654
1655static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1656{
1657 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001658
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001659 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1660 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1663}
1664
1665
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001666static void wpas_find_stopped(void *ctx)
1667{
1668 struct wpa_supplicant *wpa_s = ctx;
1669 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1670}
1671
1672
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001673struct wpas_p2p_listen_work {
1674 unsigned int freq;
1675 unsigned int duration;
1676 struct wpabuf *probe_resp_ie;
1677};
1678
1679
1680static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1681{
1682 if (lwork == NULL)
1683 return;
1684 wpabuf_free(lwork->probe_resp_ie);
1685 os_free(lwork);
1686}
1687
1688
1689static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1690{
1691 struct wpas_p2p_listen_work *lwork;
1692
1693 if (!wpa_s->p2p_listen_work)
1694 return;
1695
1696 lwork = wpa_s->p2p_listen_work->ctx;
1697 wpas_p2p_listen_work_free(lwork);
1698 radio_work_done(wpa_s->p2p_listen_work);
1699 wpa_s->p2p_listen_work = NULL;
1700}
1701
1702
1703static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1704{
1705 struct wpa_supplicant *wpa_s = work->wpa_s;
1706 struct wpas_p2p_listen_work *lwork = work->ctx;
1707
1708 if (deinit) {
1709 wpas_p2p_listen_work_free(lwork);
1710 return;
1711 }
1712
1713 wpa_s->p2p_listen_work = work;
1714
1715 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1716
1717 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1718 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1719 "report received Probe Request frames");
1720 wpas_p2p_listen_work_done(wpa_s);
1721 return;
1722 }
1723
1724 wpa_s->pending_listen_freq = lwork->freq;
1725 wpa_s->pending_listen_duration = lwork->duration;
1726
1727 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1728 {
1729 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1730 "to remain on channel (%u MHz) for Listen "
1731 "state", lwork->freq);
1732 wpas_p2p_listen_work_done(wpa_s);
1733 wpa_s->pending_listen_freq = 0;
1734 return;
1735 }
1736 wpa_s->off_channel_freq = 0;
1737 wpa_s->roc_waiting_drv_freq = lwork->freq;
1738}
1739
1740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741static int wpas_start_listen(void *ctx, unsigned int freq,
1742 unsigned int duration,
1743 const struct wpabuf *probe_resp_ie)
1744{
1745 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001746 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001748 if (wpa_s->p2p_listen_work) {
1749 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 return -1;
1751 }
1752
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001753 lwork = os_zalloc(sizeof(*lwork));
1754 if (lwork == NULL)
1755 return -1;
1756 lwork->freq = freq;
1757 lwork->duration = duration;
1758 if (probe_resp_ie) {
1759 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1760 if (lwork->probe_resp_ie == NULL) {
1761 wpas_p2p_listen_work_free(lwork);
1762 return -1;
1763 }
1764 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001766 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1767 lwork) < 0) {
1768 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769 return -1;
1770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
1772 return 0;
1773}
1774
1775
1776static void wpas_stop_listen(void *ctx)
1777{
1778 struct wpa_supplicant *wpa_s = ctx;
1779 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1780 wpa_drv_cancel_remain_on_channel(wpa_s);
1781 wpa_s->off_channel_freq = 0;
1782 wpa_s->roc_waiting_drv_freq = 0;
1783 }
1784 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1785 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001786 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787}
1788
1789
1790static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1791{
1792 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001793 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794}
1795
1796
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001797/*
1798 * DNS Header section is used only to calculate compression pointers, so the
1799 * contents of this data does not matter, but the length needs to be reserved
1800 * in the virtual packet.
1801 */
1802#define DNS_HEADER_LEN 12
1803
1804/*
1805 * 27-octet in-memory packet from P2P specification containing two implied
1806 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1807 */
1808#define P2P_SD_IN_MEMORY_LEN 27
1809
1810static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1811 u8 **spos, const u8 *end)
1812{
1813 while (*spos < end) {
1814 u8 val = ((*spos)[0] & 0xc0) >> 6;
1815 int len;
1816
1817 if (val == 1 || val == 2) {
1818 /* These are reserved values in RFC 1035 */
1819 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1820 "sequence starting with 0x%x", val);
1821 return -1;
1822 }
1823
1824 if (val == 3) {
1825 u16 offset;
1826 u8 *spos_tmp;
1827
1828 /* Offset */
1829 if (*spos + 2 > end) {
1830 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1831 "DNS offset field");
1832 return -1;
1833 }
1834
1835 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1836 if (offset >= *spos - start) {
1837 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1838 "pointer offset %u", offset);
1839 return -1;
1840 }
1841
1842 (*spos) += 2;
1843 spos_tmp = start + offset;
1844 return p2p_sd_dns_uncompress_label(upos, uend, start,
1845 &spos_tmp,
1846 *spos - 2);
1847 }
1848
1849 /* Label */
1850 len = (*spos)[0] & 0x3f;
1851 if (len == 0)
1852 return 0;
1853
1854 (*spos)++;
1855 if (*spos + len > end) {
1856 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1857 "sequence - no room for label with length "
1858 "%u", len);
1859 return -1;
1860 }
1861
1862 if (*upos + len + 2 > uend)
1863 return -2;
1864
1865 os_memcpy(*upos, *spos, len);
1866 *spos += len;
1867 *upos += len;
1868 (*upos)[0] = '.';
1869 (*upos)++;
1870 (*upos)[0] = '\0';
1871 }
1872
1873 return 0;
1874}
1875
1876
1877/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1878 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1879 * not large enough */
1880static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1881 size_t msg_len, size_t offset)
1882{
1883 /* 27-octet in-memory packet from P2P specification */
1884 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1885 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1886 u8 *tmp, *end, *spos;
1887 char *upos, *uend;
1888 int ret = 0;
1889
1890 if (buf_len < 2)
1891 return -1;
1892 if (offset > msg_len)
1893 return -1;
1894
1895 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1896 if (tmp == NULL)
1897 return -1;
1898 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1899 end = spos + msg_len;
1900 spos += offset;
1901
1902 os_memset(tmp, 0, DNS_HEADER_LEN);
1903 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1904 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1905
1906 upos = buf;
1907 uend = buf + buf_len;
1908
1909 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1910 if (ret) {
1911 os_free(tmp);
1912 return ret;
1913 }
1914
1915 if (upos == buf) {
1916 upos[0] = '.';
1917 upos[1] = '\0';
1918 } else if (upos[-1] == '.')
1919 upos[-1] = '\0';
1920
1921 os_free(tmp);
1922 return 0;
1923}
1924
1925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001926static struct p2p_srv_bonjour *
1927wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1928 const struct wpabuf *query)
1929{
1930 struct p2p_srv_bonjour *bsrv;
1931 size_t len;
1932
1933 len = wpabuf_len(query);
1934 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1935 struct p2p_srv_bonjour, list) {
1936 if (len == wpabuf_len(bsrv->query) &&
1937 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1938 len) == 0)
1939 return bsrv;
1940 }
1941 return NULL;
1942}
1943
1944
1945static struct p2p_srv_upnp *
1946wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1947 const char *service)
1948{
1949 struct p2p_srv_upnp *usrv;
1950
1951 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1952 struct p2p_srv_upnp, list) {
1953 if (version == usrv->version &&
1954 os_strcmp(service, usrv->service) == 0)
1955 return usrv;
1956 }
1957 return NULL;
1958}
1959
1960
1961static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1962 u8 srv_trans_id)
1963{
1964 u8 *len_pos;
1965
1966 if (wpabuf_tailroom(resp) < 5)
1967 return;
1968
1969 /* Length (to be filled) */
1970 len_pos = wpabuf_put(resp, 2);
1971 wpabuf_put_u8(resp, srv_proto);
1972 wpabuf_put_u8(resp, srv_trans_id);
1973 /* Status Code */
1974 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1975 /* Response Data: empty */
1976 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1977}
1978
1979
1980static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1981 struct wpabuf *resp, u8 srv_trans_id)
1982{
1983 struct p2p_srv_bonjour *bsrv;
1984 u8 *len_pos;
1985
1986 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1987
1988 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1989 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1990 return;
1991 }
1992
1993 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1994 struct p2p_srv_bonjour, list) {
1995 if (wpabuf_tailroom(resp) <
1996 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1997 return;
1998 /* Length (to be filled) */
1999 len_pos = wpabuf_put(resp, 2);
2000 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2001 wpabuf_put_u8(resp, srv_trans_id);
2002 /* Status Code */
2003 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2004 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2005 wpabuf_head(bsrv->resp),
2006 wpabuf_len(bsrv->resp));
2007 /* Response Data */
2008 wpabuf_put_buf(resp, bsrv->query); /* Key */
2009 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2010 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2011 2);
2012 }
2013}
2014
2015
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002016static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2017 size_t query_len)
2018{
2019 char str_rx[256], str_srv[256];
2020
2021 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2022 return 0; /* Too short to include DNS Type and Version */
2023 if (os_memcmp(query + query_len - 3,
2024 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2025 3) != 0)
2026 return 0; /* Mismatch in DNS Type or Version */
2027 if (query_len == wpabuf_len(bsrv->query) &&
2028 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2029 return 1; /* Binary match */
2030
2031 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2032 0))
2033 return 0; /* Failed to uncompress query */
2034 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2035 wpabuf_head(bsrv->query),
2036 wpabuf_len(bsrv->query) - 3, 0))
2037 return 0; /* Failed to uncompress service */
2038
2039 return os_strcmp(str_rx, str_srv) == 0;
2040}
2041
2042
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2044 struct wpabuf *resp, u8 srv_trans_id,
2045 const u8 *query, size_t query_len)
2046{
2047 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002049 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050
2051 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2052 query, query_len);
2053 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2054 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2055 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2056 srv_trans_id);
2057 return;
2058 }
2059
2060 if (query_len == 0) {
2061 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2062 return;
2063 }
2064
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002065 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2066 struct p2p_srv_bonjour, list) {
2067 if (!match_bonjour_query(bsrv, query, query_len))
2068 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002069
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002070 if (wpabuf_tailroom(resp) <
2071 5 + query_len + wpabuf_len(bsrv->resp))
2072 return;
2073
2074 matches++;
2075
2076 /* Length (to be filled) */
2077 len_pos = wpabuf_put(resp, 2);
2078 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2079 wpabuf_put_u8(resp, srv_trans_id);
2080
2081 /* Status Code */
2082 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2083 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2084 wpabuf_head(bsrv->resp),
2085 wpabuf_len(bsrv->resp));
2086
2087 /* Response Data */
2088 wpabuf_put_data(resp, query, query_len); /* Key */
2089 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2090
2091 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2092 }
2093
2094 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2096 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002097 if (wpabuf_tailroom(resp) < 5)
2098 return;
2099
2100 /* Length (to be filled) */
2101 len_pos = wpabuf_put(resp, 2);
2102 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2103 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104
2105 /* Status Code */
2106 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2107 /* Response Data: empty */
2108 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2109 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111}
2112
2113
2114static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2115 struct wpabuf *resp, u8 srv_trans_id)
2116{
2117 struct p2p_srv_upnp *usrv;
2118 u8 *len_pos;
2119
2120 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2121
2122 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2123 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2124 return;
2125 }
2126
2127 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2128 struct p2p_srv_upnp, list) {
2129 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2130 return;
2131
2132 /* Length (to be filled) */
2133 len_pos = wpabuf_put(resp, 2);
2134 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2135 wpabuf_put_u8(resp, srv_trans_id);
2136
2137 /* Status Code */
2138 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2139 /* Response Data */
2140 wpabuf_put_u8(resp, usrv->version);
2141 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2142 usrv->service);
2143 wpabuf_put_str(resp, usrv->service);
2144 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2145 2);
2146 }
2147}
2148
2149
2150static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2151 struct wpabuf *resp, u8 srv_trans_id,
2152 const u8 *query, size_t query_len)
2153{
2154 struct p2p_srv_upnp *usrv;
2155 u8 *len_pos;
2156 u8 version;
2157 char *str;
2158 int count = 0;
2159
2160 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2161 query, query_len);
2162
2163 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2164 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2165 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2166 srv_trans_id);
2167 return;
2168 }
2169
2170 if (query_len == 0) {
2171 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2172 return;
2173 }
2174
2175 if (wpabuf_tailroom(resp) < 5)
2176 return;
2177
2178 /* Length (to be filled) */
2179 len_pos = wpabuf_put(resp, 2);
2180 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2181 wpabuf_put_u8(resp, srv_trans_id);
2182
2183 version = query[0];
2184 str = os_malloc(query_len);
2185 if (str == NULL)
2186 return;
2187 os_memcpy(str, query + 1, query_len - 1);
2188 str[query_len - 1] = '\0';
2189
2190 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2191 struct p2p_srv_upnp, list) {
2192 if (version != usrv->version)
2193 continue;
2194
2195 if (os_strcmp(str, "ssdp:all") != 0 &&
2196 os_strstr(usrv->service, str) == NULL)
2197 continue;
2198
2199 if (wpabuf_tailroom(resp) < 2)
2200 break;
2201 if (count == 0) {
2202 /* Status Code */
2203 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2204 /* Response Data */
2205 wpabuf_put_u8(resp, version);
2206 } else
2207 wpabuf_put_u8(resp, ',');
2208
2209 count++;
2210
2211 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2212 usrv->service);
2213 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2214 break;
2215 wpabuf_put_str(resp, usrv->service);
2216 }
2217 os_free(str);
2218
2219 if (count == 0) {
2220 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2221 "available");
2222 /* Status Code */
2223 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2224 /* Response Data: empty */
2225 }
2226
2227 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2228}
2229
2230
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002231#ifdef CONFIG_WIFI_DISPLAY
2232static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2233 struct wpabuf *resp, u8 srv_trans_id,
2234 const u8 *query, size_t query_len)
2235{
2236 const u8 *pos;
2237 u8 role;
2238 u8 *len_pos;
2239
2240 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2241
2242 if (!wpa_s->global->wifi_display) {
2243 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2244 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2245 srv_trans_id);
2246 return;
2247 }
2248
2249 if (query_len < 1) {
2250 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2251 "Role");
2252 return;
2253 }
2254
2255 if (wpabuf_tailroom(resp) < 5)
2256 return;
2257
2258 pos = query;
2259 role = *pos++;
2260 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2261
2262 /* TODO: role specific handling */
2263
2264 /* Length (to be filled) */
2265 len_pos = wpabuf_put(resp, 2);
2266 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2267 wpabuf_put_u8(resp, srv_trans_id);
2268 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2269
2270 while (pos < query + query_len) {
2271 if (*pos < MAX_WFD_SUBELEMS &&
2272 wpa_s->global->wfd_subelem[*pos] &&
2273 wpabuf_tailroom(resp) >=
2274 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2275 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2276 "subelement %u", *pos);
2277 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2278 }
2279 pos++;
2280 }
2281
2282 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2283}
2284#endif /* CONFIG_WIFI_DISPLAY */
2285
2286
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002287static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2288 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289{
2290 struct wpa_supplicant *wpa_s = ctx;
2291 const u8 *pos = tlvs;
2292 const u8 *end = tlvs + tlvs_len;
2293 const u8 *tlv_end;
2294 u16 slen;
2295 struct wpabuf *resp;
2296 u8 srv_proto, srv_trans_id;
2297 size_t buf_len;
2298 char *buf;
2299
2300 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2301 tlvs, tlvs_len);
2302 buf_len = 2 * tlvs_len + 1;
2303 buf = os_malloc(buf_len);
2304 if (buf) {
2305 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2306 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2307 MACSTR " %u %u %s",
2308 freq, MAC2STR(sa), dialog_token, update_indic,
2309 buf);
2310 os_free(buf);
2311 }
2312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002313 if (wpa_s->p2p_sd_over_ctrl_iface) {
2314 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2315 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002317 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318
2319 resp = wpabuf_alloc(10000);
2320 if (resp == NULL)
2321 return;
2322
2323 while (pos + 1 < end) {
2324 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2325 slen = WPA_GET_LE16(pos);
2326 pos += 2;
2327 if (pos + slen > end || slen < 2) {
2328 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2329 "length");
2330 wpabuf_free(resp);
2331 return;
2332 }
2333 tlv_end = pos + slen;
2334
2335 srv_proto = *pos++;
2336 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2337 srv_proto);
2338 srv_trans_id = *pos++;
2339 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2340 srv_trans_id);
2341
2342 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2343 pos, tlv_end - pos);
2344
2345
2346 if (wpa_s->force_long_sd) {
2347 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2348 "response");
2349 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2350 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2351 goto done;
2352 }
2353
2354 switch (srv_proto) {
2355 case P2P_SERV_ALL_SERVICES:
2356 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2357 "for all services");
2358 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2359 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2360 wpa_printf(MSG_DEBUG, "P2P: No service "
2361 "discovery protocols available");
2362 wpas_sd_add_proto_not_avail(
2363 resp, P2P_SERV_ALL_SERVICES,
2364 srv_trans_id);
2365 break;
2366 }
2367 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2368 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2369 break;
2370 case P2P_SERV_BONJOUR:
2371 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2372 pos, tlv_end - pos);
2373 break;
2374 case P2P_SERV_UPNP:
2375 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2376 pos, tlv_end - pos);
2377 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002378#ifdef CONFIG_WIFI_DISPLAY
2379 case P2P_SERV_WIFI_DISPLAY:
2380 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2381 pos, tlv_end - pos);
2382 break;
2383#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384 default:
2385 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2386 "protocol %u", srv_proto);
2387 wpas_sd_add_proto_not_avail(resp, srv_proto,
2388 srv_trans_id);
2389 break;
2390 }
2391
2392 pos = tlv_end;
2393 }
2394
2395done:
2396 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2397 update_indic, tlvs, tlvs_len);
2398
2399 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2400
2401 wpabuf_free(resp);
2402}
2403
2404
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002405static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2406 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407{
2408 struct wpa_supplicant *wpa_s = ctx;
2409 const u8 *pos = tlvs;
2410 const u8 *end = tlvs + tlvs_len;
2411 const u8 *tlv_end;
2412 u16 slen;
2413 size_t buf_len;
2414 char *buf;
2415
2416 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2417 tlvs, tlvs_len);
2418 if (tlvs_len > 1500) {
2419 /* TODO: better way for handling this */
2420 wpa_msg_ctrl(wpa_s, MSG_INFO,
2421 P2P_EVENT_SERV_DISC_RESP MACSTR
2422 " %u <long response: %u bytes>",
2423 MAC2STR(sa), update_indic,
2424 (unsigned int) tlvs_len);
2425 } else {
2426 buf_len = 2 * tlvs_len + 1;
2427 buf = os_malloc(buf_len);
2428 if (buf) {
2429 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2430 wpa_msg_ctrl(wpa_s, MSG_INFO,
2431 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2432 MAC2STR(sa), update_indic, buf);
2433 os_free(buf);
2434 }
2435 }
2436
2437 while (pos < end) {
2438 u8 srv_proto, srv_trans_id, status;
2439
2440 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2441 slen = WPA_GET_LE16(pos);
2442 pos += 2;
2443 if (pos + slen > end || slen < 3) {
2444 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2445 "length");
2446 return;
2447 }
2448 tlv_end = pos + slen;
2449
2450 srv_proto = *pos++;
2451 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2452 srv_proto);
2453 srv_trans_id = *pos++;
2454 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2455 srv_trans_id);
2456 status = *pos++;
2457 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2458 status);
2459
2460 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2461 pos, tlv_end - pos);
2462
2463 pos = tlv_end;
2464 }
2465
2466 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2467}
2468
2469
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002470u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2471 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002474 return 0;
2475 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476}
2477
2478
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002479u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2480 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002481{
2482 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002483 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002484
2485 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2486 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002487 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2489 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2490 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2491 wpabuf_put_u8(tlvs, version);
2492 wpabuf_put_str(tlvs, query);
2493 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2494 wpabuf_free(tlvs);
2495 return ret;
2496}
2497
2498
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002499#ifdef CONFIG_WIFI_DISPLAY
2500
2501static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2502 const struct wpabuf *tlvs)
2503{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002504 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2505 return 0;
2506 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2507}
2508
2509
2510#define MAX_WFD_SD_SUBELEMS 20
2511
2512static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2513 const char *subelems)
2514{
2515 u8 *len;
2516 const char *pos;
2517 int val;
2518 int count = 0;
2519
2520 len = wpabuf_put(tlvs, 2);
2521 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2522 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2523
2524 wpabuf_put_u8(tlvs, role);
2525
2526 pos = subelems;
2527 while (*pos) {
2528 val = atoi(pos);
2529 if (val >= 0 && val < 256) {
2530 wpabuf_put_u8(tlvs, val);
2531 count++;
2532 if (count == MAX_WFD_SD_SUBELEMS)
2533 break;
2534 }
2535 pos = os_strchr(pos + 1, ',');
2536 if (pos == NULL)
2537 break;
2538 pos++;
2539 }
2540
2541 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2542}
2543
2544
2545u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2546 const u8 *dst, const char *role)
2547{
2548 struct wpabuf *tlvs;
2549 u64 ret;
2550 const char *subelems;
2551 u8 id = 1;
2552
2553 subelems = os_strchr(role, ' ');
2554 if (subelems == NULL)
2555 return 0;
2556 subelems++;
2557
2558 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2559 if (tlvs == NULL)
2560 return 0;
2561
2562 if (os_strstr(role, "[source]"))
2563 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2564 if (os_strstr(role, "[pri-sink]"))
2565 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2566 if (os_strstr(role, "[sec-sink]"))
2567 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2568 if (os_strstr(role, "[source+sink]"))
2569 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2570
2571 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2572 wpabuf_free(tlvs);
2573 return ret;
2574}
2575
2576#endif /* CONFIG_WIFI_DISPLAY */
2577
2578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002579int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2582 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002583 return p2p_sd_cancel_request(wpa_s->global->p2p,
2584 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002585}
2586
2587
2588void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2589 const u8 *dst, u8 dialog_token,
2590 const struct wpabuf *resp_tlvs)
2591{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002592 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2593 return;
2594 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2595 resp_tlvs);
2596}
2597
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002598
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2600{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002601 if (wpa_s->global->p2p)
2602 p2p_sd_service_update(wpa_s->global->p2p);
2603}
2604
2605
2606static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2607{
2608 dl_list_del(&bsrv->list);
2609 wpabuf_free(bsrv->query);
2610 wpabuf_free(bsrv->resp);
2611 os_free(bsrv);
2612}
2613
2614
2615static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2616{
2617 dl_list_del(&usrv->list);
2618 os_free(usrv->service);
2619 os_free(usrv);
2620}
2621
2622
2623void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2624{
2625 struct p2p_srv_bonjour *bsrv, *bn;
2626 struct p2p_srv_upnp *usrv, *un;
2627
2628 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2629 struct p2p_srv_bonjour, list)
2630 wpas_p2p_srv_bonjour_free(bsrv);
2631
2632 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2633 struct p2p_srv_upnp, list)
2634 wpas_p2p_srv_upnp_free(usrv);
2635
2636 wpas_p2p_sd_service_update(wpa_s);
2637}
2638
2639
2640int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2641 struct wpabuf *query, struct wpabuf *resp)
2642{
2643 struct p2p_srv_bonjour *bsrv;
2644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 bsrv = os_zalloc(sizeof(*bsrv));
2646 if (bsrv == NULL)
2647 return -1;
2648 bsrv->query = query;
2649 bsrv->resp = resp;
2650 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2651
2652 wpas_p2p_sd_service_update(wpa_s);
2653 return 0;
2654}
2655
2656
2657int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2658 const struct wpabuf *query)
2659{
2660 struct p2p_srv_bonjour *bsrv;
2661
2662 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2663 if (bsrv == NULL)
2664 return -1;
2665 wpas_p2p_srv_bonjour_free(bsrv);
2666 wpas_p2p_sd_service_update(wpa_s);
2667 return 0;
2668}
2669
2670
2671int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2672 const char *service)
2673{
2674 struct p2p_srv_upnp *usrv;
2675
2676 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2677 return 0; /* Already listed */
2678 usrv = os_zalloc(sizeof(*usrv));
2679 if (usrv == NULL)
2680 return -1;
2681 usrv->version = version;
2682 usrv->service = os_strdup(service);
2683 if (usrv->service == NULL) {
2684 os_free(usrv);
2685 return -1;
2686 }
2687 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2688
2689 wpas_p2p_sd_service_update(wpa_s);
2690 return 0;
2691}
2692
2693
2694int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2695 const char *service)
2696{
2697 struct p2p_srv_upnp *usrv;
2698
2699 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2700 if (usrv == NULL)
2701 return -1;
2702 wpas_p2p_srv_upnp_free(usrv);
2703 wpas_p2p_sd_service_update(wpa_s);
2704 return 0;
2705}
2706
2707
2708static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2709 const u8 *peer, const char *params,
2710 unsigned int generated_pin)
2711{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002712 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2713 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714}
2715
2716
2717static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2718 const u8 *peer, const char *params)
2719{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002720 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2721 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002722}
2723
2724
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002725static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2726 const u8 *dev_addr, const u8 *pri_dev_type,
2727 const char *dev_name, u16 supp_config_methods,
2728 u8 dev_capab, u8 group_capab, const u8 *group_id,
2729 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730{
2731 struct wpa_supplicant *wpa_s = ctx;
2732 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002733 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 u8 empty_dev_type[8];
2735 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002736 struct wpa_supplicant *group = NULL;
2737
2738 if (group_id) {
2739 for (group = wpa_s->global->ifaces; group; group = group->next)
2740 {
2741 struct wpa_ssid *s = group->current_ssid;
2742 if (s != NULL &&
2743 s->mode == WPAS_MODE_P2P_GO &&
2744 group_id_len - ETH_ALEN == s->ssid_len &&
2745 os_memcmp(group_id + ETH_ALEN, s->ssid,
2746 s->ssid_len) == 0)
2747 break;
2748 }
2749 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750
2751 if (pri_dev_type == NULL) {
2752 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2753 pri_dev_type = empty_dev_type;
2754 }
2755 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2756 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002757 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 MAC2STR(dev_addr),
2759 wps_dev_type_bin2str(pri_dev_type, devtype,
2760 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002761 dev_name, supp_config_methods, dev_capab, group_capab,
2762 group ? " group=" : "",
2763 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766 if (config_methods & WPS_CONFIG_DISPLAY) {
2767 generated_pin = wps_generate_pin();
2768 wpas_prov_disc_local_display(wpa_s, peer, params,
2769 generated_pin);
2770 } else if (config_methods & WPS_CONFIG_KEYPAD)
2771 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2772 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002773 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2774 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002775
2776 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2777 P2P_PROV_DISC_SUCCESS,
2778 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779}
2780
2781
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002782static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783{
2784 struct wpa_supplicant *wpa_s = ctx;
2785 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002786 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002788 if (wpa_s->pending_pd_before_join &&
2789 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2790 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2791 wpa_s->pending_pd_before_join = 0;
2792 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2793 "join-existing-group operation");
2794 wpas_p2p_join_start(wpa_s);
2795 return;
2796 }
2797
Dmitry Shmidt04949592012-07-19 12:16:46 -07002798 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2799 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2800 os_snprintf(params, sizeof(params), " peer_go=%d",
2801 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2802 else
2803 params[0] = '\0';
2804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002806 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807 else if (config_methods & WPS_CONFIG_KEYPAD) {
2808 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002809 wpas_prov_disc_local_display(wpa_s, peer, params,
2810 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002812 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2813 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814
Jouni Malinen75ecf522011-06-27 15:19:46 -07002815 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2816 P2P_PROV_DISC_SUCCESS,
2817 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818}
2819
2820
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002821static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2822 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002823{
2824 struct wpa_supplicant *wpa_s = ctx;
2825
Dmitry Shmidt04949592012-07-19 12:16:46 -07002826 if (wpa_s->p2p_fallback_to_go_neg) {
2827 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2828 "failed - fall back to GO Negotiation");
2829 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2830 return;
2831 }
2832
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002833 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002834 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002835 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2836 "join-existing-group operation (no ACK for PD "
2837 "Req attempts)");
2838 wpas_p2p_join_start(wpa_s);
2839 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002840 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002841
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002842 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2843 " p2p_dev_addr=" MACSTR " status=%d",
2844 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002845
Jouni Malinen75ecf522011-06-27 15:19:46 -07002846 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2847 status, 0, 0);
2848}
2849
2850
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002851static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2852{
2853 if (channels == NULL)
2854 return 1; /* Assume no restrictions */
2855 return p2p_channels_includes_freq(channels, freq);
2856
2857}
2858
2859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2861 const u8 *go_dev_addr, const u8 *ssid,
2862 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002863 int *force_freq, int persistent_group,
2864 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865{
2866 struct wpa_supplicant *wpa_s = ctx;
2867 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868 int res;
2869 struct wpa_supplicant *grp;
2870
2871 if (!persistent_group) {
2872 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002873 " to join an active group (SSID: %s)",
2874 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002875 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2876 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2877 == 0 ||
2878 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2879 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2880 "authorized invitation");
2881 goto accept_inv;
2882 }
2883 /*
2884 * Do not accept the invitation automatically; notify user and
2885 * request approval.
2886 */
2887 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2888 }
2889
2890 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2891 if (grp) {
2892 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2893 "running persistent group");
2894 if (*go)
2895 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2896 goto accept_inv;
2897 }
2898
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002899 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2900 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2901 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2902 "invitation to re-invoke a persistent group");
2903 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2905
2906 for (s = wpa_s->conf->ssid; s; s = s->next) {
2907 if (s->disabled == 2 &&
2908 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2909 s->ssid_len == ssid_len &&
2910 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2911 break;
2912 }
2913
2914 if (!s) {
2915 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2916 " requested reinvocation of an unknown group",
2917 MAC2STR(sa));
2918 return P2P_SC_FAIL_UNKNOWN_GROUP;
2919 }
2920
2921 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2922 *go = 1;
2923 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2924 wpa_printf(MSG_DEBUG, "P2P: The only available "
2925 "interface is already in use - reject "
2926 "invitation");
2927 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2928 }
2929 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2930 } else if (s->mode == WPAS_MODE_P2P_GO) {
2931 *go = 1;
2932 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2933 {
2934 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2935 "interface address for the group");
2936 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2937 }
2938 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2939 ETH_ALEN);
2940 }
2941
2942accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002943 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2944
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002945 /* Get one of the frequencies currently in use */
2946 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002947 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002948 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002949
2950 if (wpa_s->num_multichan_concurrent < 2 ||
2951 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2952 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2953 *force_freq = res;
2954 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 }
2956
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002957 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2958 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002959 if (*go == 0) {
2960 /* We are the client */
2961 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2962 "running a GO but we are capable of MCC, "
2963 "figure out the best channel to use");
2964 *force_freq = 0;
2965 } else if (!freq_included(channels, *force_freq)) {
2966 /* We are the GO, and *force_freq is not in the
2967 * intersection */
2968 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2969 "in intersection but we are capable of MCC, "
2970 "figure out the best channel to use",
2971 *force_freq);
2972 *force_freq = 0;
2973 }
2974 }
2975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 return P2P_SC_SUCCESS;
2977}
2978
2979
2980static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2981 const u8 *ssid, size_t ssid_len,
2982 const u8 *go_dev_addr, u8 status,
2983 int op_freq)
2984{
2985 struct wpa_supplicant *wpa_s = ctx;
2986 struct wpa_ssid *s;
2987
2988 for (s = wpa_s->conf->ssid; s; s = s->next) {
2989 if (s->disabled == 2 &&
2990 s->ssid_len == ssid_len &&
2991 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2992 break;
2993 }
2994
2995 if (status == P2P_SC_SUCCESS) {
2996 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002997 " was accepted; op_freq=%d MHz, SSID=%s",
2998 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003000 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003002 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003003 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003004 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003005 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003007 wpa_s->p2p_wps_method, 0,
3008 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003009 }
3010 return;
3011 }
3012
3013 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3014 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3015 " was rejected (status %u)", MAC2STR(sa), status);
3016 return;
3017 }
3018
3019 if (!s) {
3020 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003021 wpa_msg_global(wpa_s, MSG_INFO,
3022 P2P_EVENT_INVITATION_RECEIVED
3023 "sa=" MACSTR " go_dev_addr=" MACSTR
3024 " bssid=" MACSTR " unknown-network",
3025 MAC2STR(sa), MAC2STR(go_dev_addr),
3026 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003028 wpa_msg_global(wpa_s, MSG_INFO,
3029 P2P_EVENT_INVITATION_RECEIVED
3030 "sa=" MACSTR " go_dev_addr=" MACSTR
3031 " unknown-network",
3032 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003033 }
3034 return;
3035 }
3036
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003037 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003038 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3039 "sa=" MACSTR " persistent=%d freq=%d",
3040 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003041 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003042 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3043 "sa=" MACSTR " persistent=%d",
3044 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003045 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046}
3047
3048
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003049static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3050 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003051 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003052{
3053 size_t i;
3054
3055 if (ssid == NULL)
3056 return;
3057
3058 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3059 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3060 ETH_ALEN) == 0)
3061 break;
3062 }
3063 if (i >= ssid->num_p2p_clients) {
3064 if (ssid->mode != WPAS_MODE_P2P_GO &&
3065 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3066 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3067 "due to invitation result", ssid->id);
3068 wpas_notify_network_removed(wpa_s, ssid);
3069 wpa_config_remove_network(wpa_s->conf, ssid->id);
3070 return;
3071 }
3072 return; /* Peer not found in client list */
3073 }
3074
3075 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003076 "group %d client list%s",
3077 MAC2STR(peer), ssid->id,
3078 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003079 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3080 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3081 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3082 ssid->num_p2p_clients--;
3083#ifndef CONFIG_NO_CONFIG_WRITE
3084 if (wpa_s->parent->conf->update_config &&
3085 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3086 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3087#endif /* CONFIG_NO_CONFIG_WRITE */
3088}
3089
3090
3091static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3092 const u8 *peer)
3093{
3094 struct wpa_ssid *ssid;
3095
3096 wpa_s = wpa_s->global->p2p_invite_group;
3097 if (wpa_s == NULL)
3098 return; /* No known invitation group */
3099 ssid = wpa_s->current_ssid;
3100 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3101 !ssid->p2p_persistent_group)
3102 return; /* Not operating as a GO in persistent group */
3103 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3104 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003105 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003106}
3107
3108
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003109static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003110 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003111 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112{
3113 struct wpa_supplicant *wpa_s = ctx;
3114 struct wpa_ssid *ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003115 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116
3117 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003118 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3119 "status=%d " MACSTR,
3120 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003122 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3123 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003124 }
3125 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3126
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003127 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3128 status, MAC2STR(peer));
3129 if (wpa_s->pending_invite_ssid_id == -1) {
3130 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3131 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003135 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3136 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3137 "invitation exchange to indicate readiness for "
3138 "re-invocation");
3139 }
3140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003142 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3143 ssid = wpa_config_get_network(
3144 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003145 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003146 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003147 wpas_p2p_remove_pending_group_interface(wpa_s);
3148 return;
3149 }
3150
3151 ssid = wpa_config_get_network(wpa_s->conf,
3152 wpa_s->pending_invite_ssid_id);
3153 if (ssid == NULL) {
3154 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3155 "data matching with invitation");
3156 return;
3157 }
3158
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003159 /*
3160 * The peer could have missed our ctrl::ack frame for Invitation
3161 * Response and continue retransmitting the frame. To reduce the
3162 * likelihood of the peer not getting successful TX status for the
3163 * Invitation Response frame, wait a short time here before starting
3164 * the persistent group so that we will remain on the current channel to
3165 * acknowledge any possible retransmission from the peer.
3166 */
3167 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3168 "starting persistent group");
3169 os_sleep(0, 50000);
3170
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003171 freq = wpa_s->p2p_persistent_go_freq;
3172 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3173 freq_included(channels, neg_freq)) {
3174 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
3175 neg_freq);
3176 freq = neg_freq;
3177 }
3178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003180 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003181 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003182 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3183 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003184 ssid->mode == WPAS_MODE_P2P_GO ?
3185 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3186 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187}
3188
3189
Dmitry Shmidt04949592012-07-19 12:16:46 -07003190static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3191 unsigned int freq)
3192{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003193 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003194}
3195
3196
3197static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3198{
3199 reg->channel[reg->channels] = chan;
3200 reg->channels++;
3201}
3202
3203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003204static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003205 struct p2p_channels *chan,
3206 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003207{
3208 int i, cla = 0;
3209
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003210 os_memset(cli_chan, 0, sizeof(*cli_chan));
3211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3213 "band");
3214
3215 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3216 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003217 chan->reg_class[cla].channels = 0;
3218 for (i = 0; i < 11; i++) {
3219 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3220 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3221 }
3222 if (chan->reg_class[cla].channels)
3223 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224
3225 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3226 "band");
3227
3228 /* Operating class 115 - 5 GHz, channels 36-48 */
3229 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003230 chan->reg_class[cla].channels = 0;
3231 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3232 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3233 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3234 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3235 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3236 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3237 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3238 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3239 if (chan->reg_class[cla].channels)
3240 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003241
3242 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3243 "band");
3244
3245 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3246 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003247 chan->reg_class[cla].channels = 0;
3248 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3249 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3250 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3251 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3252 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3253 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3254 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3255 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3256 if (chan->reg_class[cla].channels)
3257 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258
3259 chan->reg_classes = cla;
3260 return 0;
3261}
3262
3263
3264static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3265 u16 num_modes,
3266 enum hostapd_hw_mode mode)
3267{
3268 u16 i;
3269
3270 for (i = 0; i < num_modes; i++) {
3271 if (modes[i].mode == mode)
3272 return &modes[i];
3273 }
3274
3275 return NULL;
3276}
3277
3278
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003279enum chan_allowed {
3280 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3281};
3282
Dmitry Shmidt04949592012-07-19 12:16:46 -07003283static int has_channel(struct wpa_global *global,
3284 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285{
3286 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003287 unsigned int freq;
3288
3289 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3290 chan * 5;
3291 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003292 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293
3294 for (i = 0; i < mode->num_channels; i++) {
3295 if (mode->channels[i].chan == chan) {
3296 if (flags)
3297 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003298 if (mode->channels[i].flag &
3299 (HOSTAPD_CHAN_DISABLED |
3300 HOSTAPD_CHAN_RADAR))
3301 return NOT_ALLOWED;
3302 if (mode->channels[i].flag &
3303 (HOSTAPD_CHAN_PASSIVE_SCAN |
3304 HOSTAPD_CHAN_NO_IBSS))
3305 return PASSIVE_ONLY;
3306 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 }
3308 }
3309
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003310 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311}
3312
3313
3314struct p2p_oper_class_map {
3315 enum hostapd_hw_mode mode;
3316 u8 op_class;
3317 u8 min_chan;
3318 u8 max_chan;
3319 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003320 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321};
3322
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003323static struct p2p_oper_class_map op_class[] = {
3324 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003325#if 0 /* Do not enable HT40 on 2 GHz for now */
3326 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3327 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3328#endif
3329 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3330 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3331 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3332 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3333 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3334 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003335
3336 /*
3337 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3338 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3339 * 80 MHz, but currently use the following definition for simplicity
3340 * (these center frequencies are not actual channels, which makes
3341 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3342 * removing invalid channels.
3343 */
3344 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003345 { -1, 0, 0, 0, 0, BW20 }
3346};
3347
3348
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003349static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3350 struct hostapd_hw_modes *mode,
3351 u8 channel)
3352{
3353 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3354 unsigned int i;
3355
3356 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3357 return 0;
3358
3359 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3360 /*
3361 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3362 * so the center channel is 6 channels away from the start/end.
3363 */
3364 if (channel >= center_channels[i] - 6 &&
3365 channel <= center_channels[i] + 6)
3366 return center_channels[i];
3367
3368 return 0;
3369}
3370
3371
3372static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3373 struct hostapd_hw_modes *mode,
3374 u8 channel, u8 bw)
3375{
3376 u8 center_chan;
3377 int i, flags;
3378 enum chan_allowed res, ret = ALLOWED;
3379
3380 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3381 if (!center_chan)
3382 return NOT_ALLOWED;
3383 if (center_chan >= 58 && center_chan <= 138)
3384 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3385
3386 /* check all the channels are available */
3387 for (i = 0; i < 4; i++) {
3388 int adj_chan = center_chan - 6 + i * 4;
3389
3390 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3391 if (res == NOT_ALLOWED)
3392 return NOT_ALLOWED;
3393 if (res == PASSIVE_ONLY)
3394 ret = PASSIVE_ONLY;
3395
3396 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3397 return NOT_ALLOWED;
3398 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3399 return NOT_ALLOWED;
3400 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3401 return NOT_ALLOWED;
3402 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3403 return NOT_ALLOWED;
3404 }
3405
3406 return ret;
3407}
3408
3409
3410static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3411 struct hostapd_hw_modes *mode,
3412 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003413{
3414 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003415 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003416
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003417 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3418 if (bw == BW40MINUS) {
3419 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3420 return NOT_ALLOWED;
3421 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3422 } else if (bw == BW40PLUS) {
3423 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3424 return NOT_ALLOWED;
3425 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3426 } else if (bw == BW80) {
3427 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3428 }
3429
3430 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3431 return NOT_ALLOWED;
3432 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3433 return PASSIVE_ONLY;
3434 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003435}
3436
3437
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003439 struct p2p_channels *chan,
3440 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003442 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003443 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003445 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3447 "of all supported channels; assume dualband "
3448 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003449 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 }
3451
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003452 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453
3454 for (op = 0; op_class[op].op_class; op++) {
3455 struct p2p_oper_class_map *o = &op_class[op];
3456 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003457 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003459 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 if (mode == NULL)
3461 continue;
3462 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003463 enum chan_allowed res;
3464 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3465 if (res == ALLOWED) {
3466 if (reg == NULL) {
3467 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3468 o->op_class);
3469 reg = &chan->reg_class[cla];
3470 cla++;
3471 reg->reg_class = o->op_class;
3472 }
3473 reg->channel[reg->channels] = ch;
3474 reg->channels++;
3475 } else if (res == PASSIVE_ONLY &&
3476 wpa_s->conf->p2p_add_cli_chan) {
3477 if (cli_reg == NULL) {
3478 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3479 o->op_class);
3480 cli_reg = &cli_chan->reg_class[cli_cla];
3481 cli_cla++;
3482 cli_reg->reg_class = o->op_class;
3483 }
3484 cli_reg->channel[cli_reg->channels] = ch;
3485 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003486 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003487 }
3488 if (reg) {
3489 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3490 reg->channel, reg->channels);
3491 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003492 if (cli_reg) {
3493 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3494 cli_reg->channel, cli_reg->channels);
3495 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003496 }
3497
3498 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003499 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 return 0;
3502}
3503
3504
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003505int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3506 struct hostapd_hw_modes *mode, u8 channel)
3507{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003508 int op;
3509 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003510
3511 for (op = 0; op_class[op].op_class; op++) {
3512 struct p2p_oper_class_map *o = &op_class[op];
3513 u8 ch;
3514
3515 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3516 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3517 o->bw == BW20 || ch != channel)
3518 continue;
3519 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003520 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003521 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003522 }
3523 }
3524 return 0;
3525}
3526
3527
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003528int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3529 struct hostapd_hw_modes *mode, u8 channel)
3530{
3531 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3532 return 0;
3533
3534 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3535}
3536
3537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3539 size_t buf_len)
3540{
3541 struct wpa_supplicant *wpa_s = ctx;
3542
3543 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3544 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3545 break;
3546 }
3547 if (wpa_s == NULL)
3548 return -1;
3549
3550 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3551}
3552
3553
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003554static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3555{
3556 struct wpa_supplicant *wpa_s = ctx;
3557
3558 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3559 struct wpa_ssid *ssid = wpa_s->current_ssid;
3560 if (ssid == NULL)
3561 continue;
3562 if (ssid->mode != WPAS_MODE_INFRA)
3563 continue;
3564 if (wpa_s->wpa_state != WPA_COMPLETED &&
3565 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3566 continue;
3567 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3568 return 1;
3569 }
3570
3571 return 0;
3572}
3573
3574
Dmitry Shmidt18463232014-01-24 12:29:41 -08003575static int wpas_is_concurrent_session_active(void *ctx)
3576{
3577 struct wpa_supplicant *wpa_s = ctx;
3578 struct wpa_supplicant *ifs;
3579
3580 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3581 if (ifs == wpa_s)
3582 continue;
3583 if (ifs->wpa_state > WPA_ASSOCIATED)
3584 return 1;
3585 }
3586 return 0;
3587}
3588
3589
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003590static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3591{
3592 struct wpa_supplicant *wpa_s = ctx;
3593 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3594}
3595
3596
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003597int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3598{
3599 struct wpa_interface iface;
3600 struct wpa_supplicant *p2pdev_wpa_s;
3601 char ifname[100];
3602 char force_name[100];
3603 int ret;
3604
3605 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3606 wpa_s->ifname);
3607 force_name[0] = '\0';
3608 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3609 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3610 force_name, wpa_s->pending_interface_addr, NULL);
3611 if (ret < 0) {
3612 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3613 return ret;
3614 }
3615 os_strlcpy(wpa_s->pending_interface_name, ifname,
3616 sizeof(wpa_s->pending_interface_name));
3617
3618 os_memset(&iface, 0, sizeof(iface));
3619 iface.p2p_mgmt = 1;
3620 iface.ifname = wpa_s->pending_interface_name;
3621 iface.driver = wpa_s->driver->name;
3622 iface.driver_param = wpa_s->conf->driver_param;
3623 iface.confname = wpa_s->confname;
3624 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3625 if (!p2pdev_wpa_s) {
3626 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3627 return -1;
3628 }
3629 p2pdev_wpa_s->parent = wpa_s;
3630
3631 wpa_s->pending_interface_name[0] = '\0';
3632 return 0;
3633}
3634
3635
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003636static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3637 const u8 *noa, size_t noa_len)
3638{
3639 struct wpa_supplicant *wpa_s, *intf = ctx;
3640 char hex[100];
3641
3642 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3643 if (wpa_s->waiting_presence_resp)
3644 break;
3645 }
3646 if (!wpa_s) {
3647 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3648 return;
3649 }
3650 wpa_s->waiting_presence_resp = 0;
3651
3652 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3653 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3654 " status=%u noa=%s", MAC2STR(src), status, hex);
3655}
3656
3657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003658/**
3659 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3660 * @global: Pointer to global data from wpa_supplicant_init()
3661 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3662 * Returns: 0 on success, -1 on failure
3663 */
3664int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3665{
3666 struct p2p_config p2p;
3667 unsigned int r;
3668 int i;
3669
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003670 if (wpa_s->conf->p2p_disabled)
3671 return 0;
3672
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003673 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3674 return 0;
3675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003676 if (global->p2p)
3677 return 0;
3678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003681 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 p2p.p2p_scan = wpas_p2p_scan;
3683 p2p.send_action = wpas_send_action;
3684 p2p.send_action_done = wpas_send_action_done;
3685 p2p.go_neg_completed = wpas_go_neg_completed;
3686 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3687 p2p.dev_found = wpas_dev_found;
3688 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003689 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003690 p2p.start_listen = wpas_start_listen;
3691 p2p.stop_listen = wpas_stop_listen;
3692 p2p.send_probe_resp = wpas_send_probe_resp;
3693 p2p.sd_request = wpas_sd_request;
3694 p2p.sd_response = wpas_sd_response;
3695 p2p.prov_disc_req = wpas_prov_disc_req;
3696 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003697 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003698 p2p.invitation_process = wpas_invitation_process;
3699 p2p.invitation_received = wpas_invitation_received;
3700 p2p.invitation_result = wpas_invitation_result;
3701 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003702 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003703 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003704 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003705
3706 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003707 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003708 p2p.dev_name = wpa_s->conf->device_name;
3709 p2p.manufacturer = wpa_s->conf->manufacturer;
3710 p2p.model_name = wpa_s->conf->model_name;
3711 p2p.model_number = wpa_s->conf->model_number;
3712 p2p.serial_number = wpa_s->conf->serial_number;
3713 if (wpa_s->wps) {
3714 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3715 p2p.config_methods = wpa_s->wps->config_methods;
3716 }
3717
3718 if (wpa_s->conf->p2p_listen_reg_class &&
3719 wpa_s->conf->p2p_listen_channel) {
3720 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3721 p2p.channel = wpa_s->conf->p2p_listen_channel;
3722 } else {
3723 p2p.reg_class = 81;
3724 /*
3725 * Pick one of the social channels randomly as the listen
3726 * channel.
3727 */
3728 os_get_random((u8 *) &r, sizeof(r));
3729 p2p.channel = 1 + (r % 3) * 5;
3730 }
3731 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3732
3733 if (wpa_s->conf->p2p_oper_reg_class &&
3734 wpa_s->conf->p2p_oper_channel) {
3735 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3736 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3737 p2p.cfg_op_channel = 1;
3738 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3739 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3740
3741 } else {
3742 p2p.op_reg_class = 81;
3743 /*
3744 * Use random operation channel from (1, 6, 11) if no other
3745 * preference is indicated.
3746 */
3747 os_get_random((u8 *) &r, sizeof(r));
3748 p2p.op_channel = 1 + (r % 3) * 5;
3749 p2p.cfg_op_channel = 0;
3750 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3751 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3752 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003753
3754 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3755 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3756 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3757 }
3758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003759 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3760 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3761 p2p.country[2] = 0x04;
3762 } else
3763 os_memcpy(p2p.country, "XX\x04", 3);
3764
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003765 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3767 "channel list");
3768 return -1;
3769 }
3770
3771 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3772 WPS_DEV_TYPE_LEN);
3773
3774 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3775 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3776 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3777
3778 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3779 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3780
3781 p2p.max_peers = 100;
3782
3783 if (wpa_s->conf->p2p_ssid_postfix) {
3784 p2p.ssid_postfix_len =
3785 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3786 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3787 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3788 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3789 p2p.ssid_postfix_len);
3790 }
3791
3792 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3793
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003794 p2p.max_listen = wpa_s->max_remain_on_chan;
3795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 global->p2p = p2p_init(&p2p);
3797 if (global->p2p == NULL)
3798 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003799 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800
3801 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3802 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3803 continue;
3804 p2p_add_wps_vendor_extension(
3805 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3806 }
3807
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003808 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003810 return 0;
3811}
3812
3813
3814/**
3815 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3816 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3817 *
3818 * This function deinitialize per-interface P2P data.
3819 */
3820void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3821{
3822 if (wpa_s->driver && wpa_s->drv_priv)
3823 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003824
3825 if (wpa_s->go_params) {
3826 /* Clear any stored provisioning info */
3827 p2p_clear_provisioning_info(
3828 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003829 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003830 }
3831
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 os_free(wpa_s->go_params);
3833 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3835 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3836 wpa_s->p2p_long_listen = 0;
3837 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3838 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3839 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003840 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003841 wpas_p2p_listen_work_done(wpa_s);
3842 if (wpa_s->p2p_send_action_work) {
3843 os_free(wpa_s->p2p_send_action_work->ctx);
3844 radio_work_done(wpa_s->p2p_send_action_work);
3845 wpa_s->p2p_send_action_work = NULL;
3846 }
3847 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003848
3849 /* TODO: remove group interface from the driver if this wpa_s instance
3850 * is on top of a P2P group interface */
3851}
3852
3853
3854/**
3855 * wpas_p2p_deinit_global - Deinitialize global P2P module
3856 * @global: Pointer to global data from wpa_supplicant_init()
3857 *
3858 * This function deinitializes the global (per device) P2P module.
3859 */
3860void wpas_p2p_deinit_global(struct wpa_global *global)
3861{
3862 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003863
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003864 wpa_s = global->ifaces;
3865 if (wpa_s)
3866 wpas_p2p_service_flush(wpa_s);
3867
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003868 if (global->p2p == NULL)
3869 return;
3870
3871 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3873 wpa_s = wpa_s->next;
3874 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003875 tmp = global->ifaces;
3876 while (tmp &&
3877 (tmp == wpa_s ||
3878 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3879 tmp = tmp->next;
3880 }
3881 if (tmp == NULL)
3882 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003883 /* Disconnect from the P2P group and deinit the interface */
3884 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 }
3886
3887 /*
3888 * Deinit GO data on any possibly remaining interface (if main
3889 * interface is used as GO).
3890 */
3891 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3892 if (wpa_s->ap_iface)
3893 wpas_p2p_group_deinit(wpa_s);
3894 }
3895
3896 p2p_deinit(global->p2p);
3897 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003898 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003899}
3900
3901
3902static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3903{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003904 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3905 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003906 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 if (wpa_s->drv_flags &
3908 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3909 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3910 return 1; /* P2P group requires a new interface in every case
3911 */
3912 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3913 return 0; /* driver does not support concurrent operations */
3914 if (wpa_s->global->ifaces->next)
3915 return 1; /* more that one interface already in use */
3916 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3917 return 1; /* this interface is already in use */
3918 return 0;
3919}
3920
3921
3922static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3923 const u8 *peer_addr,
3924 enum p2p_wps_method wps_method,
3925 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003926 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003927 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003929 if (persistent_group && wpa_s->conf->persistent_reconnect)
3930 persistent_group = 2;
3931
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003932 /*
3933 * Increase GO config timeout if HT40 is used since it takes some time
3934 * to scan channels for coex purposes before the BSS can be started.
3935 */
3936 p2p_set_config_timeout(wpa_s->global->p2p,
3937 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3938
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3940 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003941 persistent_group, ssid ? ssid->ssid : NULL,
3942 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003943 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944}
3945
3946
3947static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3948 const u8 *peer_addr,
3949 enum p2p_wps_method wps_method,
3950 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003951 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003952 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003953{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003954 if (persistent_group && wpa_s->conf->persistent_reconnect)
3955 persistent_group = 2;
3956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3958 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003959 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003960 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003961}
3962
3963
3964static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3965{
3966 wpa_s->p2p_join_scan_count++;
3967 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3968 wpa_s->p2p_join_scan_count);
3969 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3970 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3971 " for join operationg - stop join attempt",
3972 MAC2STR(wpa_s->pending_join_iface_addr));
3973 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003974 if (wpa_s->p2p_auto_pd) {
3975 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003976 wpa_msg_global(wpa_s, MSG_INFO,
3977 P2P_EVENT_PROV_DISC_FAILURE
3978 " p2p_dev_addr=" MACSTR " status=N/A",
3979 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003980 return;
3981 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003982 wpa_msg_global(wpa_s->parent, MSG_INFO,
3983 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003984 }
3985}
3986
3987
Dmitry Shmidt04949592012-07-19 12:16:46 -07003988static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3989{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003990 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003991
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003992 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3993 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003994 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003995 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003996
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003997 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3998 if (!freqs)
3999 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004000
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004001 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4002 wpa_s->num_multichan_concurrent);
4003 if (num < 0) {
4004 res = 1;
4005 goto exit_free;
4006 }
4007
4008 for (i = 0; i < num; i++) {
4009 if (freqs[i] == freq) {
4010 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4011 freq);
4012 res = 0;
4013 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004014 }
4015 }
4016
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004017 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004018
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004019exit_free:
4020 os_free(freqs);
4021 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004022}
4023
4024
4025static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4026 const u8 *peer_dev_addr)
4027{
4028 struct wpa_bss *bss;
4029 int updated;
4030
4031 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4032 if (bss == NULL)
4033 return -1;
4034 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4035 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4036 "last scan");
4037 return 0;
4038 }
4039
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004040 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4041 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004042 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4043 "%ld.%06ld (%supdated in last scan)",
4044 bss->last_update.sec, bss->last_update.usec,
4045 updated ? "": "not ");
4046
4047 return updated;
4048}
4049
4050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4052 struct wpa_scan_results *scan_res)
4053{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004054 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 int freq;
4056 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004057
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004058 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4059
4060 if (wpa_s->global->p2p_disabled)
4061 return;
4062
Dmitry Shmidt04949592012-07-19 12:16:46 -07004063 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4064 scan_res ? (int) scan_res->num : -1,
4065 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004066
4067 if (scan_res)
4068 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4069
Dmitry Shmidt04949592012-07-19 12:16:46 -07004070 if (wpa_s->p2p_auto_pd) {
4071 int join = wpas_p2p_peer_go(wpa_s,
4072 wpa_s->pending_join_dev_addr);
4073 if (join == 0 &&
4074 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4075 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004076 bss = wpa_bss_get_bssid_latest(
4077 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004078 if (bss) {
4079 freq = bss->freq;
4080 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4081 "the peer " MACSTR " at %d MHz",
4082 wpa_s->auto_pd_scan_retry,
4083 MAC2STR(wpa_s->
4084 pending_join_dev_addr),
4085 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004086 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004087 return;
4088 }
4089 }
4090
4091 if (join < 0)
4092 join = 0;
4093
4094 wpa_s->p2p_auto_pd = 0;
4095 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4096 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4097 MAC2STR(wpa_s->pending_join_dev_addr), join);
4098 if (p2p_prov_disc_req(wpa_s->global->p2p,
4099 wpa_s->pending_join_dev_addr,
4100 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004101 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004102 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004103 wpa_msg_global(wpa_s, MSG_INFO,
4104 P2P_EVENT_PROV_DISC_FAILURE
4105 " p2p_dev_addr=" MACSTR " status=N/A",
4106 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004107 }
4108 return;
4109 }
4110
4111 if (wpa_s->p2p_auto_join) {
4112 int join = wpas_p2p_peer_go(wpa_s,
4113 wpa_s->pending_join_dev_addr);
4114 if (join < 0) {
4115 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4116 "running a GO -> use GO Negotiation");
4117 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4118 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4119 wpa_s->p2p_persistent_group, 0, 0, 0,
4120 wpa_s->p2p_go_intent,
4121 wpa_s->p2p_connect_freq,
4122 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004123 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004124 wpa_s->p2p_go_ht40,
4125 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004126 return;
4127 }
4128
4129 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4130 "try to join the group", join ? "" :
4131 " in older scan");
4132 if (!join)
4133 wpa_s->p2p_fallback_to_go_neg = 1;
4134 }
4135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4137 wpa_s->pending_join_iface_addr);
4138 if (freq < 0 &&
4139 p2p_get_interface_addr(wpa_s->global->p2p,
4140 wpa_s->pending_join_dev_addr,
4141 iface_addr) == 0 &&
4142 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4143 {
4144 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4145 "address for join from " MACSTR " to " MACSTR
4146 " based on newly discovered P2P peer entry",
4147 MAC2STR(wpa_s->pending_join_iface_addr),
4148 MAC2STR(iface_addr));
4149 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4150 ETH_ALEN);
4151
4152 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4153 wpa_s->pending_join_iface_addr);
4154 }
4155 if (freq >= 0) {
4156 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4157 "from P2P peer table: %d MHz", freq);
4158 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004159 if (wpa_s->p2p_join_ssid_len) {
4160 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4161 MACSTR " and SSID %s",
4162 MAC2STR(wpa_s->pending_join_iface_addr),
4163 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4164 wpa_s->p2p_join_ssid_len));
4165 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4166 wpa_s->p2p_join_ssid,
4167 wpa_s->p2p_join_ssid_len);
4168 }
4169 if (!bss) {
4170 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4171 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4172 bss = wpa_bss_get_bssid_latest(wpa_s,
4173 wpa_s->pending_join_iface_addr);
4174 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004175 if (bss) {
4176 freq = bss->freq;
4177 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004178 "from BSS table: %d MHz (SSID %s)", freq,
4179 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 }
4181 if (freq > 0) {
4182 u16 method;
4183
Dmitry Shmidt04949592012-07-19 12:16:46 -07004184 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004185 wpa_msg_global(wpa_s->parent, MSG_INFO,
4186 P2P_EVENT_GROUP_FORMATION_FAILURE
4187 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004188 return;
4189 }
4190
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004191 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4192 "prior to joining an existing group (GO " MACSTR
4193 " freq=%u MHz)",
4194 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4195 wpa_s->pending_pd_before_join = 1;
4196
4197 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 case WPS_PIN_DISPLAY:
4199 method = WPS_CONFIG_KEYPAD;
4200 break;
4201 case WPS_PIN_KEYPAD:
4202 method = WPS_CONFIG_DISPLAY;
4203 break;
4204 case WPS_PBC:
4205 method = WPS_CONFIG_PUSHBUTTON;
4206 break;
4207 default:
4208 method = 0;
4209 break;
4210 }
4211
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004212 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4213 wpa_s->pending_join_dev_addr) ==
4214 method)) {
4215 /*
4216 * We have already performed provision discovery for
4217 * joining the group. Proceed directly to join
4218 * operation without duplicated provision discovery. */
4219 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4220 "with " MACSTR " already done - proceed to "
4221 "join",
4222 MAC2STR(wpa_s->pending_join_dev_addr));
4223 wpa_s->pending_pd_before_join = 0;
4224 goto start;
4225 }
4226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004227 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004228 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004229 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004230 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4231 "Discovery Request before joining an "
4232 "existing group");
4233 wpa_s->pending_pd_before_join = 0;
4234 goto start;
4235 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 return;
4237 }
4238
4239 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4240 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4241 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4242 wpas_p2p_check_join_scan_limit(wpa_s);
4243 return;
4244
4245start:
4246 /* Start join operation immediately */
4247 wpas_p2p_join_start(wpa_s);
4248}
4249
4250
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004251static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4252 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004254 int ret;
4255 struct wpa_driver_scan_params params;
4256 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004257 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004258 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259
4260 os_memset(&params, 0, sizeof(params));
4261
4262 /* P2P Wildcard SSID */
4263 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004264 if (ssid && ssid_len) {
4265 params.ssids[0].ssid = ssid;
4266 params.ssids[0].ssid_len = ssid_len;
4267 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4268 wpa_s->p2p_join_ssid_len = ssid_len;
4269 } else {
4270 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4271 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4272 wpa_s->p2p_join_ssid_len = 0;
4273 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274
4275 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004276 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4277 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4278 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004279 if (wps_ie == NULL) {
4280 wpas_p2p_scan_res_join(wpa_s, NULL);
4281 return;
4282 }
4283
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004284 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4285 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286 if (ies == NULL) {
4287 wpabuf_free(wps_ie);
4288 wpas_p2p_scan_res_join(wpa_s, NULL);
4289 return;
4290 }
4291 wpabuf_put_buf(ies, wps_ie);
4292 wpabuf_free(wps_ie);
4293
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004294 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004295
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004296 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004297 params.extra_ies = wpabuf_head(ies);
4298 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004299
4300 if (!freq) {
4301 int oper_freq;
4302 /*
4303 * If freq is not provided, check the operating freq of the GO
4304 * and use a single channel scan on if possible.
4305 */
4306 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4307 wpa_s->pending_join_iface_addr);
4308 if (oper_freq > 0)
4309 freq = oper_freq;
4310 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004311 if (freq > 0) {
4312 freqs[0] = freq;
4313 params.freqs = freqs;
4314 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004315
4316 /*
4317 * Run a scan to update BSS table and start Provision Discovery once
4318 * the new scan results become available.
4319 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004320 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004321 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004322 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004323 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004324 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004325 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326
4327 wpabuf_free(ies);
4328
4329 if (ret) {
4330 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4331 "try again later");
4332 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4333 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4334 wpas_p2p_check_join_scan_limit(wpa_s);
4335 }
4336}
4337
4338
Dmitry Shmidt04949592012-07-19 12:16:46 -07004339static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4340{
4341 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004342 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004343}
4344
4345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004346static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004347 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004348 int auto_join, const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349{
4350 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004351 MACSTR " dev " MACSTR ")%s",
4352 MAC2STR(iface_addr), MAC2STR(dev_addr),
4353 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004354 if (ssid && ssid_len) {
4355 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4356 wpa_ssid_txt(ssid, ssid_len));
4357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004358
Dmitry Shmidt04949592012-07-19 12:16:46 -07004359 wpa_s->p2p_auto_pd = 0;
4360 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4362 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4363 wpa_s->pending_join_wps_method = wps_method;
4364
4365 /* Make sure we are not running find during connection establishment */
4366 wpas_p2p_stop_find(wpa_s);
4367
4368 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004369 wpas_p2p_join_scan_req(wpa_s, 0, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 return 0;
4371}
4372
4373
4374static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4375{
4376 struct wpa_supplicant *group;
4377 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004378 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379
4380 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4381 if (group == NULL)
4382 return -1;
4383 if (group != wpa_s) {
4384 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4385 sizeof(group->p2p_pin));
4386 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004387 } else {
4388 /*
4389 * Need to mark the current interface for p2p_group_formation
4390 * when a separate group interface is not used. This is needed
4391 * to allow p2p_cancel stop a pending p2p_connect-join.
4392 * wpas_p2p_init_group_interface() addresses this for the case
4393 * where a separate group interface is used.
4394 */
4395 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004396 }
4397
4398 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004399 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004400
4401 os_memset(&res, 0, sizeof(res));
4402 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4403 ETH_ALEN);
4404 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004405 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004406 if (bss) {
4407 res.freq = bss->freq;
4408 res.ssid_len = bss->ssid_len;
4409 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004410 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4411 "from BSS table: %d MHz (SSID %s)", bss->freq,
4412 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004413 }
4414
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004415 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4416 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4417 "starting client");
4418 wpa_drv_cancel_remain_on_channel(wpa_s);
4419 wpa_s->off_channel_freq = 0;
4420 wpa_s->roc_waiting_drv_freq = 0;
4421 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004422 wpas_start_wps_enrollee(group, &res);
4423
4424 /*
4425 * Allow a longer timeout for join-a-running-group than normal 15
4426 * second group formation timeout since the GO may not have authorized
4427 * our connection yet.
4428 */
4429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4430 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4431 wpa_s, NULL);
4432
4433 return 0;
4434}
4435
4436
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004437static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004438 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004439{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004440 int *freqs, res;
4441 unsigned int freq_in_use = 0, num, i;
4442
4443 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4444 if (!freqs)
4445 return -1;
4446
4447 num = get_shared_radio_freqs(wpa_s, freqs,
4448 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004449 wpa_printf(MSG_DEBUG,
4450 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4451 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004452
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004453 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004454 int ret;
4455 if (go)
4456 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4457 else
4458 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4459 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004460 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4461 "(%u MHz) is not supported for P2P uses",
4462 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004463 res = -3;
4464 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004465 }
4466
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004467 for (i = 0; i < num; i++) {
4468 if (freqs[i] == freq)
4469 freq_in_use = 1;
4470 }
4471
4472 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4473 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4474 freq);
4475 res = -2;
4476 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004477 }
4478 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4479 "requested channel (%u MHz)", freq);
4480 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004481 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004482 }
4483
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004484 for (i = 0; i < num; i++) {
4485 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4486 continue;
4487
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004488 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004489 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4490 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004491 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004492 } else {
4493 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004494 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004495 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004496 }
4497 break;
4498 }
4499
4500 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004501 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004502 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4503 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004504 } else if (num < wpa_s->num_multichan_concurrent) {
4505 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4506 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004507 } else {
4508 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4509 res = -2;
4510 goto exit_free;
4511 }
4512 }
4513
4514exit_ok:
4515 res = 0;
4516exit_free:
4517 os_free(freqs);
4518 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004519}
4520
4521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004522/**
4523 * wpas_p2p_connect - Request P2P Group Formation to be started
4524 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4525 * @peer_addr: Address of the peer P2P Device
4526 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4527 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004528 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529 * @join: Whether to join an existing group (as a client) instead of starting
4530 * Group Owner negotiation; @peer_addr is BSSID in that case
4531 * @auth: Whether to only authorize the connection instead of doing that and
4532 * initiating Group Owner negotiation
4533 * @go_intent: GO Intent or -1 to use default
4534 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004535 * @persistent_id: Persistent group credentials to use for forcing GO
4536 * parameters or -1 to generate new values (SSID/passphrase)
4537 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4538 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004539 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004540 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004541 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4542 * failure, -2 on failure due to channel not currently available,
4543 * -3 if forced channel is not supported
4544 */
4545int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4546 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004547 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004548 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004549 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004550{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004551 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004552 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004553 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004554 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004555 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556
4557 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4558 return -1;
4559
Dmitry Shmidt04949592012-07-19 12:16:46 -07004560 if (persistent_id >= 0) {
4561 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4562 if (ssid == NULL || ssid->disabled != 2 ||
4563 ssid->mode != WPAS_MODE_P2P_GO)
4564 return -1;
4565 }
4566
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004567 os_free(wpa_s->global->add_psk);
4568 wpa_s->global->add_psk = NULL;
4569
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004570 wpa_s->global->p2p_fail_on_wps_complete = 0;
4571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004572 if (go_intent < 0)
4573 go_intent = wpa_s->conf->p2p_go_intent;
4574
4575 if (!auth)
4576 wpa_s->p2p_long_listen = 0;
4577
4578 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004579 wpa_s->p2p_persistent_group = !!persistent_group;
4580 wpa_s->p2p_persistent_id = persistent_id;
4581 wpa_s->p2p_go_intent = go_intent;
4582 wpa_s->p2p_connect_freq = freq;
4583 wpa_s->p2p_fallback_to_go_neg = 0;
4584 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004585 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004586 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587
4588 if (pin)
4589 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4590 else if (wps_method == WPS_PIN_DISPLAY) {
4591 ret = wps_generate_pin();
4592 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4593 ret);
4594 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4595 wpa_s->p2p_pin);
4596 } else
4597 wpa_s->p2p_pin[0] = '\0';
4598
Dmitry Shmidt04949592012-07-19 12:16:46 -07004599 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004600 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4601 if (auth) {
4602 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4603 "connect a running group from " MACSTR,
4604 MAC2STR(peer_addr));
4605 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4606 return ret;
4607 }
4608 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4609 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4610 iface_addr) < 0) {
4611 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4612 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4613 dev_addr);
4614 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004615 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004616 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004617 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4618 "%ld.%06ld",
4619 wpa_s->p2p_auto_started.sec,
4620 wpa_s->p2p_auto_started.usec);
4621 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004622 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004623 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004624 auto_join, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 return -1;
4626 return ret;
4627 }
4628
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004629 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4630 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004631 if (res)
4632 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004633 wpas_p2p_set_own_freq_preference(wpa_s,
4634 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635
4636 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004638 if (wpa_s->create_p2p_iface) {
4639 /* Prepare to add a new interface for the group */
4640 iftype = WPA_IF_P2P_GROUP;
4641 if (go_intent == 15)
4642 iftype = WPA_IF_P2P_GO;
4643 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4644 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4645 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004648
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004649 if_addr = wpa_s->pending_interface_addr;
4650 } else
4651 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652
4653 if (auth) {
4654 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004655 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004656 force_freq, persistent_group, ssid,
4657 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 return -1;
4659 return ret;
4660 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004661
4662 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4663 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004664 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004665 if (wpa_s->create_p2p_iface)
4666 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004667 return -1;
4668 }
4669 return ret;
4670}
4671
4672
4673/**
4674 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4675 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4676 * @freq: Frequency of the channel in MHz
4677 * @duration: Duration of the stay on the channel in milliseconds
4678 *
4679 * This callback is called when the driver indicates that it has started the
4680 * requested remain-on-channel duration.
4681 */
4682void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4683 unsigned int freq, unsigned int duration)
4684{
4685 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4686 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004687 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4688 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4689 wpa_s->pending_listen_duration);
4690 wpa_s->pending_listen_freq = 0;
4691 }
4692}
4693
4694
4695static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4696 unsigned int timeout)
4697{
4698 /* Limit maximum Listen state time based on driver limitation. */
4699 if (timeout > wpa_s->max_remain_on_chan)
4700 timeout = wpa_s->max_remain_on_chan;
4701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004702 return p2p_listen(wpa_s->global->p2p, timeout);
4703}
4704
4705
4706/**
4707 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4708 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4709 * @freq: Frequency of the channel in MHz
4710 *
4711 * This callback is called when the driver indicates that a remain-on-channel
4712 * operation has been completed, i.e., the duration on the requested channel
4713 * has timed out.
4714 */
4715void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4716 unsigned int freq)
4717{
4718 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4719 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004720 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004721 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004722 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4723 return;
4724 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4725 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004726 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 return;
4728 if (wpa_s->p2p_long_listen > 0)
4729 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4730 if (wpa_s->p2p_long_listen > 0) {
4731 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4732 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004733 } else {
4734 /*
4735 * When listen duration is over, stop listen & update p2p_state
4736 * to IDLE.
4737 */
4738 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004739 }
4740}
4741
4742
4743/**
4744 * wpas_p2p_group_remove - Remove a P2P group
4745 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4746 * @ifname: Network interface name of the group interface or "*" to remove all
4747 * groups
4748 * Returns: 0 on success, -1 on failure
4749 *
4750 * This function is used to remove a P2P group. This can be used to disconnect
4751 * from a group in which the local end is a P2P Client or to end a P2P Group in
4752 * case the local end is the Group Owner. If a virtual network interface was
4753 * created for this group, that interface will be removed. Otherwise, only the
4754 * configured P2P group network will be removed from the interface.
4755 */
4756int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4757{
4758 struct wpa_global *global = wpa_s->global;
4759
4760 if (os_strcmp(ifname, "*") == 0) {
4761 struct wpa_supplicant *prev;
4762 wpa_s = global->ifaces;
4763 while (wpa_s) {
4764 prev = wpa_s;
4765 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004766 if (prev->p2p_group_interface !=
4767 NOT_P2P_GROUP_INTERFACE ||
4768 (prev->current_ssid &&
4769 prev->current_ssid->p2p_group))
4770 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004771 }
4772 return 0;
4773 }
4774
4775 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4776 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4777 break;
4778 }
4779
4780 return wpas_p2p_disconnect(wpa_s);
4781}
4782
4783
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004784static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4785{
4786 unsigned int r;
4787
4788 if (freq == 2) {
4789 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4790 "band");
4791 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004792 p2p_supported_freq_go(wpa_s->global->p2p,
4793 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004794 freq = wpa_s->best_24_freq;
4795 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4796 "channel: %d MHz", freq);
4797 } else {
4798 os_get_random((u8 *) &r, sizeof(r));
4799 freq = 2412 + (r % 3) * 25;
4800 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4801 "channel: %d MHz", freq);
4802 }
4803 }
4804
4805 if (freq == 5) {
4806 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4807 "band");
4808 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004809 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004810 wpa_s->best_5_freq)) {
4811 freq = wpa_s->best_5_freq;
4812 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4813 "channel: %d MHz", freq);
4814 } else {
4815 os_get_random((u8 *) &r, sizeof(r));
4816 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004817 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004818 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4819 "5 GHz channel for P2P group");
4820 return -1;
4821 }
4822 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4823 "channel: %d MHz", freq);
4824 }
4825 }
4826
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004827 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004828 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4829 "(%u MHz) is not supported for P2P uses",
4830 freq);
4831 return -1;
4832 }
4833
4834 return freq;
4835}
4836
4837
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004838static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4839 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004840 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004841 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004842{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004843 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004844 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004845 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004846
4847 os_memset(params, 0, sizeof(*params));
4848 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004849 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004850 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004851 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004852 if (!freq_included(channels, freq)) {
4853 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4854 "accepted", freq);
4855 return -1;
4856 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004857 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4858 "frequency %d MHz", freq);
4859 params->freq = freq;
4860 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4861 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004862 wpa_s->conf->p2p_oper_channel <= 11 &&
4863 freq_included(channels,
4864 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4866 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4867 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004868 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4869 wpa_s->conf->p2p_oper_reg_class == 116 ||
4870 wpa_s->conf->p2p_oper_reg_class == 117 ||
4871 wpa_s->conf->p2p_oper_reg_class == 124 ||
4872 wpa_s->conf->p2p_oper_reg_class == 126 ||
4873 wpa_s->conf->p2p_oper_reg_class == 127) &&
4874 freq_included(channels,
4875 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004876 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4877 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4878 "frequency %d MHz", params->freq);
4879 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4880 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004881 p2p_supported_freq_go(wpa_s->global->p2p,
4882 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004883 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004884 params->freq = wpa_s->best_overall_freq;
4885 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4886 "channel %d MHz", params->freq);
4887 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4888 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004889 p2p_supported_freq_go(wpa_s->global->p2p,
4890 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004891 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004892 params->freq = wpa_s->best_24_freq;
4893 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4894 "channel %d MHz", params->freq);
4895 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4896 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004897 p2p_supported_freq_go(wpa_s->global->p2p,
4898 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004899 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 params->freq = wpa_s->best_5_freq;
4901 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4902 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004903 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4904 channels))) {
4905 params->freq = pref_freq;
4906 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4907 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004908 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004909 int chan;
4910 for (chan = 0; chan < 11; chan++) {
4911 params->freq = 2412 + chan * 5;
4912 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004913 params->freq) &&
4914 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004915 break;
4916 }
4917 if (chan == 11) {
4918 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4919 "allowed");
4920 return -1;
4921 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4923 "known)", params->freq);
4924 }
4925
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004926 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4927 if (!freqs)
4928 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004930 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4931 wpa_s->num_multichan_concurrent);
4932 if (res < 0) {
4933 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004934 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004936 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004937
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004938 for (i = 0; i < num; i++) {
4939 if (freq && freqs[i] == freq)
4940 break;
4941 if (!freq && freq_included(channels, freqs[i])) {
4942 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4943 freqs[i]);
4944 params->freq = freqs[i];
4945 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004946 }
4947 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004948
4949 if (i == num) {
4950 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4951 if (freq)
4952 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4953 else
4954 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4955 os_free(freqs);
4956 return -1;
4957 } else if (num == 0) {
4958 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4959 } else {
4960 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4961 }
4962 }
4963
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004964 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004965 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966}
4967
4968
4969static struct wpa_supplicant *
4970wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4971 int go)
4972{
4973 struct wpa_supplicant *group_wpa_s;
4974
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004975 if (!wpas_p2p_create_iface(wpa_s)) {
4976 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4977 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004978 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004980 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981
4982 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004983 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004984 wpa_msg_global(wpa_s, MSG_ERROR,
4985 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004987 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4989 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004990 wpa_msg_global(wpa_s, MSG_ERROR,
4991 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004992 wpas_p2p_remove_pending_group_interface(wpa_s);
4993 return NULL;
4994 }
4995
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4997 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004998 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 return group_wpa_s;
5000}
5001
5002
5003/**
5004 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5005 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5006 * @persistent_group: Whether to create a persistent group
5007 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005008 * @ht40: Start GO with 40 MHz channel width
5009 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005010 * Returns: 0 on success, -1 on failure
5011 *
5012 * This function creates a new P2P group with the local end as the Group Owner,
5013 * i.e., without using Group Owner Negotiation.
5014 */
5015int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005016 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005017{
5018 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019
5020 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5021 return -1;
5022
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005023 os_free(wpa_s->global->add_psk);
5024 wpa_s->global->add_psk = NULL;
5025
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005026 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005027 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005028 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005029
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005030 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5031 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005032 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005033
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005034 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005035 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005036 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005037 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005038 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5039 "(%u MHz) is not supported for P2P uses",
5040 params.freq);
5041 return -1;
5042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043 p2p_go_params(wpa_s->global->p2p, &params);
5044 params.persistent_group = persistent_group;
5045
5046 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5047 if (wpa_s == NULL)
5048 return -1;
5049 wpas_start_wps_go(wpa_s, &params, 0);
5050
5051 return 0;
5052}
5053
5054
5055static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5056 struct wpa_ssid *params, int addr_allocated)
5057{
5058 struct wpa_ssid *ssid;
5059
5060 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5061 if (wpa_s == NULL)
5062 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005063 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064
5065 wpa_supplicant_ap_deinit(wpa_s);
5066
5067 ssid = wpa_config_add_network(wpa_s->conf);
5068 if (ssid == NULL)
5069 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 wpa_config_set_network_defaults(ssid);
5071 ssid->temporary = 1;
5072 ssid->proto = WPA_PROTO_RSN;
5073 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5074 ssid->group_cipher = WPA_CIPHER_CCMP;
5075 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5076 ssid->ssid = os_malloc(params->ssid_len);
5077 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 wpa_config_remove_network(wpa_s->conf, ssid->id);
5079 return -1;
5080 }
5081 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5082 ssid->ssid_len = params->ssid_len;
5083 ssid->p2p_group = 1;
5084 ssid->export_keys = 1;
5085 if (params->psk_set) {
5086 os_memcpy(ssid->psk, params->psk, 32);
5087 ssid->psk_set = 1;
5088 }
5089 if (params->passphrase)
5090 ssid->passphrase = os_strdup(params->passphrase);
5091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092 wpa_s->show_group_started = 1;
5093
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005094 wpa_supplicant_select_network(wpa_s, ssid);
5095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096 return 0;
5097}
5098
5099
5100int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5101 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005102 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005103 const struct p2p_channels *channels,
5104 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005105{
5106 struct p2p_go_neg_results params;
5107 int go = 0;
5108
5109 if (ssid->disabled != 2 || ssid->ssid == NULL)
5110 return -1;
5111
5112 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5113 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5114 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5115 "already running");
5116 return 0;
5117 }
5118
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005119 os_free(wpa_s->global->add_psk);
5120 wpa_s->global->add_psk = NULL;
5121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005122 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005123 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005124
Dmitry Shmidt04949592012-07-19 12:16:46 -07005125 wpa_s->p2p_fallback_to_go_neg = 0;
5126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127 if (ssid->mode == WPAS_MODE_INFRA)
5128 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5129
5130 if (ssid->mode != WPAS_MODE_P2P_GO)
5131 return -1;
5132
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005133 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5134 if (freq < 0)
5135 return -1;
5136
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005137 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005138 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139
5140 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005141 params.psk_set = ssid->psk_set;
5142 if (params.psk_set)
5143 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005144 if (ssid->passphrase) {
5145 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5146 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5147 "persistent group");
5148 return -1;
5149 }
5150 os_strlcpy(params.passphrase, ssid->passphrase,
5151 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005152 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5154 params.ssid_len = ssid->ssid_len;
5155 params.persistent_group = 1;
5156
5157 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5158 if (wpa_s == NULL)
5159 return -1;
5160
Dmitry Shmidt56052862013-10-04 10:23:25 -07005161 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 wpas_start_wps_go(wpa_s, &params, 0);
5163
5164 return 0;
5165}
5166
5167
5168static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5169 struct wpabuf *proberesp_ies)
5170{
5171 struct wpa_supplicant *wpa_s = ctx;
5172 if (wpa_s->ap_iface) {
5173 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005174 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5175 wpabuf_free(beacon_ies);
5176 wpabuf_free(proberesp_ies);
5177 return;
5178 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005179 if (beacon_ies) {
5180 wpabuf_free(hapd->p2p_beacon_ie);
5181 hapd->p2p_beacon_ie = beacon_ies;
5182 }
5183 wpabuf_free(hapd->p2p_probe_resp_ie);
5184 hapd->p2p_probe_resp_ie = proberesp_ies;
5185 } else {
5186 wpabuf_free(beacon_ies);
5187 wpabuf_free(proberesp_ies);
5188 }
5189 wpa_supplicant_ap_update_beacon(wpa_s);
5190}
5191
5192
5193static void wpas_p2p_idle_update(void *ctx, int idle)
5194{
5195 struct wpa_supplicant *wpa_s = ctx;
5196 if (!wpa_s->ap_iface)
5197 return;
5198 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005199 if (idle) {
5200 if (wpa_s->global->p2p_fail_on_wps_complete &&
5201 wpa_s->p2p_in_provisioning) {
5202 wpas_p2p_grpform_fail_after_wps(wpa_s);
5203 return;
5204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005205 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005206 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5208}
5209
5210
5211struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005212 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213{
5214 struct p2p_group *group;
5215 struct p2p_group_config *cfg;
5216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5218 return NULL;
5219
5220 cfg = os_zalloc(sizeof(*cfg));
5221 if (cfg == NULL)
5222 return NULL;
5223
Dmitry Shmidt04949592012-07-19 12:16:46 -07005224 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005225 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005226 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005227 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005228 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5229 if (wpa_s->max_stations &&
5230 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5231 cfg->max_clients = wpa_s->max_stations;
5232 else
5233 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005234 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5235 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005236 cfg->cb_ctx = wpa_s;
5237 cfg->ie_update = wpas_p2p_ie_update;
5238 cfg->idle_update = wpas_p2p_idle_update;
5239
5240 group = p2p_group_init(wpa_s->global->p2p, cfg);
5241 if (group == NULL)
5242 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005243 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005244 p2p_group_notif_formation_done(group);
5245 wpa_s->p2p_group = group;
5246 return group;
5247}
5248
5249
5250void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5251 int registrar)
5252{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005253 struct wpa_ssid *ssid = wpa_s->current_ssid;
5254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 if (!wpa_s->p2p_in_provisioning) {
5256 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5257 "provisioning not in progress");
5258 return;
5259 }
5260
Dmitry Shmidt04949592012-07-19 12:16:46 -07005261 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5262 u8 go_dev_addr[ETH_ALEN];
5263 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5264 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5265 ssid->ssid_len);
5266 /* Clear any stored provisioning info */
5267 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5268 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005270 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5271 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005272 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005273 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5274 /*
5275 * Use a separate timeout for initial data connection to
5276 * complete to allow the group to be removed automatically if
5277 * something goes wrong in this step before the P2P group idle
5278 * timeout mechanism is taken into use.
5279 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005280 wpa_dbg(wpa_s, MSG_DEBUG,
5281 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5282 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005283 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5284 wpas_p2p_group_formation_timeout,
5285 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005286 } else if (ssid) {
5287 /*
5288 * Use a separate timeout for initial data connection to
5289 * complete to allow the group to be removed automatically if
5290 * the client does not complete data connection successfully.
5291 */
5292 wpa_dbg(wpa_s, MSG_DEBUG,
5293 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5294 P2P_MAX_INITIAL_CONN_WAIT_GO);
5295 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5296 wpas_p2p_group_formation_timeout,
5297 wpa_s->parent, NULL);
5298 /*
5299 * Complete group formation on first successful data connection
5300 */
5301 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005303 if (wpa_s->global->p2p)
5304 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 wpas_group_formation_completed(wpa_s, 1);
5306}
5307
5308
Jouni Malinen75ecf522011-06-27 15:19:46 -07005309void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5310 struct wps_event_fail *fail)
5311{
5312 if (!wpa_s->p2p_in_provisioning) {
5313 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5314 "provisioning not in progress");
5315 return;
5316 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005317
5318 if (wpa_s->go_params) {
5319 p2p_clear_provisioning_info(
5320 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005321 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005322 }
5323
Jouni Malinen75ecf522011-06-27 15:19:46 -07005324 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005325
5326 if (wpa_s == wpa_s->global->p2p_group_formation) {
5327 /*
5328 * Allow some time for the failed WPS negotiation exchange to
5329 * complete, but remove the group since group formation cannot
5330 * succeed after provisioning failure.
5331 */
5332 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5333 wpa_s->global->p2p_fail_on_wps_complete = 1;
5334 eloop_deplete_timeout(0, 50000,
5335 wpas_p2p_group_formation_timeout,
5336 wpa_s->parent, NULL);
5337 }
5338}
5339
5340
5341int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5342{
5343 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5344 !wpa_s->p2p_in_provisioning)
5345 return 0;
5346
5347 wpas_p2p_grpform_fail_after_wps(wpa_s);
5348
5349 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005350}
5351
5352
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005353int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005354 const char *config_method,
5355 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356{
5357 u16 config_methods;
5358
Dmitry Shmidt04949592012-07-19 12:16:46 -07005359 wpa_s->p2p_fallback_to_go_neg = 0;
5360 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005361 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005365 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5366 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005367 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005368 else {
5369 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005372
Dmitry Shmidt04949592012-07-19 12:16:46 -07005373 if (use == WPAS_P2P_PD_AUTO) {
5374 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5375 wpa_s->pending_pd_config_methods = config_methods;
5376 wpa_s->p2p_auto_pd = 1;
5377 wpa_s->p2p_auto_join = 0;
5378 wpa_s->pending_pd_before_join = 0;
5379 wpa_s->auto_pd_scan_retry = 0;
5380 wpas_p2p_stop_find(wpa_s);
5381 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005382 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005383 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5384 wpa_s->p2p_auto_started.sec,
5385 wpa_s->p2p_auto_started.usec);
5386 wpas_p2p_join_scan(wpa_s, NULL);
5387 return 0;
5388 }
5389
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5391 return -1;
5392
5393 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005394 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005395 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005396}
5397
5398
5399int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5400 char *end)
5401{
5402 return p2p_scan_result_text(ies, ies_len, buf, end);
5403}
5404
5405
5406static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5407{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005408 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005409 return;
5410
5411 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5412 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005413 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414}
5415
5416
5417int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5418 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005419 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005420 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005421{
5422 wpas_p2p_clear_pending_action_tx(wpa_s);
5423 wpa_s->p2p_long_listen = 0;
5424
Dmitry Shmidt04949592012-07-19 12:16:46 -07005425 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5426 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005427 return -1;
5428
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005429 wpa_supplicant_cancel_sched_scan(wpa_s);
5430
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005431 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005432 num_req_dev_types, req_dev_types, dev_id,
5433 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434}
5435
5436
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005437static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438{
5439 wpas_p2p_clear_pending_action_tx(wpa_s);
5440 wpa_s->p2p_long_listen = 0;
5441 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5442 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005443
5444 if (wpa_s->global->p2p)
5445 p2p_stop_find(wpa_s->global->p2p);
5446
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005447 return 0;
5448}
5449
5450
5451void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5452{
5453 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5454 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 wpas_p2p_remove_pending_group_interface(wpa_s);
5456}
5457
5458
5459static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5460{
5461 struct wpa_supplicant *wpa_s = eloop_ctx;
5462 wpa_s->p2p_long_listen = 0;
5463}
5464
5465
5466int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5467{
5468 int res;
5469
5470 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5471 return -1;
5472
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005473 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005474 wpas_p2p_clear_pending_action_tx(wpa_s);
5475
5476 if (timeout == 0) {
5477 /*
5478 * This is a request for unlimited Listen state. However, at
5479 * least for now, this is mapped to a Listen state for one
5480 * hour.
5481 */
5482 timeout = 3600;
5483 }
5484 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5485 wpa_s->p2p_long_listen = 0;
5486
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005487 /*
5488 * Stop previous find/listen operation to avoid trying to request a new
5489 * remain-on-channel operation while the driver is still running the
5490 * previous one.
5491 */
5492 if (wpa_s->global->p2p)
5493 p2p_stop_find(wpa_s->global->p2p);
5494
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005495 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5496 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5497 wpa_s->p2p_long_listen = timeout * 1000;
5498 eloop_register_timeout(timeout, 0,
5499 wpas_p2p_long_listen_timeout,
5500 wpa_s, NULL);
5501 }
5502
5503 return res;
5504}
5505
5506
5507int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5508 u8 *buf, size_t len, int p2p_group)
5509{
5510 struct wpabuf *p2p_ie;
5511 int ret;
5512
5513 if (wpa_s->global->p2p_disabled)
5514 return -1;
5515 if (wpa_s->global->p2p == NULL)
5516 return -1;
5517 if (bss == NULL)
5518 return -1;
5519
5520 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5521 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5522 p2p_group, p2p_ie);
5523 wpabuf_free(p2p_ie);
5524
5525 return ret;
5526}
5527
5528
5529int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005530 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005531 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532{
5533 if (wpa_s->global->p2p_disabled)
5534 return 0;
5535 if (wpa_s->global->p2p == NULL)
5536 return 0;
5537
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5539 ie, ie_len)) {
5540 case P2P_PREQ_NOT_P2P:
5541 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5542 ssi_signal);
5543 /* fall through */
5544 case P2P_PREQ_MALFORMED:
5545 case P2P_PREQ_NOT_LISTEN:
5546 case P2P_PREQ_NOT_PROCESSED:
5547 default: /* make gcc happy */
5548 return 0;
5549 case P2P_PREQ_PROCESSED:
5550 return 1;
5551 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005552}
5553
5554
5555void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5556 const u8 *sa, const u8 *bssid,
5557 u8 category, const u8 *data, size_t len, int freq)
5558{
5559 if (wpa_s->global->p2p_disabled)
5560 return;
5561 if (wpa_s->global->p2p == NULL)
5562 return;
5563
5564 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5565 freq);
5566}
5567
5568
5569void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5570{
5571 if (wpa_s->global->p2p_disabled)
5572 return;
5573 if (wpa_s->global->p2p == NULL)
5574 return;
5575
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005576 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005577}
5578
5579
5580void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5581{
5582 p2p_group_deinit(wpa_s->p2p_group);
5583 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005584
5585 wpa_s->ap_configured_cb = NULL;
5586 wpa_s->ap_configured_cb_ctx = NULL;
5587 wpa_s->ap_configured_cb_data = NULL;
5588 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589}
5590
5591
5592int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5593{
5594 wpa_s->p2p_long_listen = 0;
5595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005596 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5597 return -1;
5598
5599 return p2p_reject(wpa_s->global->p2p, addr);
5600}
5601
5602
5603/* Invite to reinvoke a persistent group */
5604int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005605 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005606 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607{
5608 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005609 u8 *bssid = NULL;
5610 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005611 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005612 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005613
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005614 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005615 if (peer_addr)
5616 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5617 else
5618 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619
Jouni Malinen31be0a42012-08-31 21:20:51 +03005620 wpa_s->p2p_persistent_go_freq = freq;
5621 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622 if (ssid->mode == WPAS_MODE_P2P_GO) {
5623 role = P2P_INVITE_ROLE_GO;
5624 if (peer_addr == NULL) {
5625 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5626 "address in invitation command");
5627 return -1;
5628 }
5629 if (wpas_p2p_create_iface(wpa_s)) {
5630 if (wpas_p2p_add_group_interface(wpa_s,
5631 WPA_IF_P2P_GO) < 0) {
5632 wpa_printf(MSG_ERROR, "P2P: Failed to "
5633 "allocate a new interface for the "
5634 "group");
5635 return -1;
5636 }
5637 bssid = wpa_s->pending_interface_addr;
5638 } else
5639 bssid = wpa_s->own_addr;
5640 } else {
5641 role = P2P_INVITE_ROLE_CLIENT;
5642 peer_addr = ssid->bssid;
5643 }
5644 wpa_s->pending_invite_ssid_id = ssid->id;
5645
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005646 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5647 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005648 if (res)
5649 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5652 return -1;
5653
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005654 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5655 no_pref_freq_given && pref_freq > 0 &&
5656 wpa_s->num_multichan_concurrent > 1 &&
5657 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5658 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5659 pref_freq);
5660 pref_freq = 0;
5661 }
5662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005664 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5665 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005666}
5667
5668
5669/* Invite to join an active group */
5670int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5671 const u8 *peer_addr, const u8 *go_dev_addr)
5672{
5673 struct wpa_global *global = wpa_s->global;
5674 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005675 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005677 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005678 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005679 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005680
Jouni Malinen31be0a42012-08-31 21:20:51 +03005681 wpa_s->p2p_persistent_go_freq = 0;
5682 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005683 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5686 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5687 break;
5688 }
5689 if (wpa_s == NULL) {
5690 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5691 return -1;
5692 }
5693
5694 ssid = wpa_s->current_ssid;
5695 if (ssid == NULL) {
5696 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5697 "invitation");
5698 return -1;
5699 }
5700
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005701 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005702 persistent = ssid->p2p_persistent_group &&
5703 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5704 ssid->ssid, ssid->ssid_len);
5705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706 if (ssid->mode == WPAS_MODE_P2P_GO) {
5707 role = P2P_INVITE_ROLE_ACTIVE_GO;
5708 bssid = wpa_s->own_addr;
5709 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005710 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005711 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712 } else {
5713 role = P2P_INVITE_ROLE_CLIENT;
5714 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5715 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5716 "invite to current group");
5717 return -1;
5718 }
5719 bssid = wpa_s->bssid;
5720 if (go_dev_addr == NULL &&
5721 !is_zero_ether_addr(wpa_s->go_dev_addr))
5722 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005723 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5724 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725 }
5726 wpa_s->parent->pending_invite_ssid_id = -1;
5727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005728 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5729 return -1;
5730
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005731 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5732 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005733 if (res)
5734 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005735 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005738 ssid->ssid, ssid->ssid_len, force_freq,
5739 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740}
5741
5742
5743void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5744{
5745 struct wpa_ssid *ssid = wpa_s->current_ssid;
5746 const char *ssid_txt;
5747 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005748 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005749 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005750 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751
Dmitry Shmidt04949592012-07-19 12:16:46 -07005752 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5753 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5754 wpa_s->parent, NULL);
5755 }
5756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005758 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005759
5760 wpa_s->show_group_started = 0;
5761
5762 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5763 os_memset(go_dev_addr, 0, ETH_ALEN);
5764 if (ssid->bssid_set)
5765 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5766 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5767 ssid->ssid_len);
5768 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5769
5770 if (wpa_s->global->p2p_group_formation == wpa_s)
5771 wpa_s->global->p2p_group_formation = NULL;
5772
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005773 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5774 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005775 if (ssid->passphrase == NULL && ssid->psk_set) {
5776 char psk[65];
5777 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005778 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5779 "%s client ssid=\"%s\" freq=%d psk=%s "
5780 "go_dev_addr=" MACSTR "%s",
5781 wpa_s->ifname, ssid_txt, freq, psk,
5782 MAC2STR(go_dev_addr),
5783 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005784 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005785 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5786 "%s client ssid=\"%s\" freq=%d "
5787 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5788 wpa_s->ifname, ssid_txt, freq,
5789 ssid->passphrase ? ssid->passphrase : "",
5790 MAC2STR(go_dev_addr),
5791 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792 }
5793
5794 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005795 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5796 ssid, go_dev_addr);
5797 if (network_id < 0)
5798 network_id = ssid->id;
5799 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800}
5801
5802
5803int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5804 u32 interval1, u32 duration2, u32 interval2)
5805{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005806 int ret;
5807
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005808 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5809 return -1;
5810
5811 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5812 wpa_s->current_ssid == NULL ||
5813 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5814 return -1;
5815
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005816 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5817 wpa_s->own_addr, wpa_s->assoc_freq,
5818 duration1, interval1, duration2, interval2);
5819 if (ret == 0)
5820 wpa_s->waiting_presence_resp = 1;
5821
5822 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005823}
5824
5825
5826int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5827 unsigned int interval)
5828{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005829 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5830 return -1;
5831
5832 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5833}
5834
5835
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005836static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5837{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005838 if (wpa_s->current_ssid == NULL) {
5839 /*
5840 * current_ssid can be cleared when P2P client interface gets
5841 * disconnected, so assume this interface was used as P2P
5842 * client.
5843 */
5844 return 1;
5845 }
5846 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005847 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5848}
5849
5850
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5852{
5853 struct wpa_supplicant *wpa_s = eloop_ctx;
5854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005855 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005856 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5857 "disabled");
5858 return;
5859 }
5860
Dmitry Shmidt04949592012-07-19 12:16:46 -07005861 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5862 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005863 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005864}
5865
5866
5867static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5868{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005869 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005870
Dmitry Shmidt04949592012-07-19 12:16:46 -07005871 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5872 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005874 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5875 return;
5876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005877 timeout = wpa_s->conf->p2p_group_idle;
5878 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5879 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5880 timeout = P2P_MAX_CLIENT_IDLE;
5881
5882 if (timeout == 0)
5883 return;
5884
Dmitry Shmidt04949592012-07-19 12:16:46 -07005885 if (timeout < 0) {
5886 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5887 timeout = 0; /* special client mode no-timeout */
5888 else
5889 return;
5890 }
5891
5892 if (wpa_s->p2p_in_provisioning) {
5893 /*
5894 * Use the normal group formation timeout during the
5895 * provisioning phase to avoid terminating this process too
5896 * early due to group idle timeout.
5897 */
5898 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5899 "during provisioning");
5900 return;
5901 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305902
Dmitry Shmidt04949592012-07-19 12:16:46 -07005903 if (wpa_s->show_group_started) {
5904 /*
5905 * Use the normal group formation timeout between the end of
5906 * the provisioning phase and completion of 4-way handshake to
5907 * avoid terminating this process too early due to group idle
5908 * timeout.
5909 */
5910 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5911 "while waiting for initial 4-way handshake to "
5912 "complete");
5913 return;
5914 }
5915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005916 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005917 timeout);
5918 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5919 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005920}
5921
5922
Jouni Malinen2b89da82012-08-31 22:04:41 +03005923/* Returns 1 if the interface was removed */
5924int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5925 u16 reason_code, const u8 *ie, size_t ie_len,
5926 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005927{
5928 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005929 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005930
Dmitry Shmidt04949592012-07-19 12:16:46 -07005931 if (!locally_generated)
5932 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5933 ie_len);
5934
5935 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5936 wpa_s->current_ssid &&
5937 wpa_s->current_ssid->p2p_group &&
5938 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5939 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5940 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005941 if (wpas_p2p_group_delete(wpa_s,
5942 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5943 > 0)
5944 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005945 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005946
5947 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005948}
5949
5950
5951void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005952 u16 reason_code, const u8 *ie, size_t ie_len,
5953 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005954{
5955 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5956 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957
Dmitry Shmidt04949592012-07-19 12:16:46 -07005958 if (!locally_generated)
5959 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5960 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961}
5962
5963
5964void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5965{
5966 struct p2p_data *p2p = wpa_s->global->p2p;
5967
5968 if (p2p == NULL)
5969 return;
5970
5971 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5972 return;
5973
5974 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5975 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5976
5977 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5978 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5979
5980 if (wpa_s->wps &&
5981 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5982 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5983
5984 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5985 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5986
5987 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5988 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5989 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5990 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5991 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5992 }
5993
5994 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5995 p2p_set_sec_dev_types(p2p,
5996 (void *) wpa_s->conf->sec_device_type,
5997 wpa_s->conf->num_sec_device_types);
5998
5999 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6000 int i;
6001 p2p_remove_wps_vendor_extensions(p2p);
6002 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6003 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6004 continue;
6005 p2p_add_wps_vendor_extension(
6006 p2p, wpa_s->conf->wps_vendor_ext[i]);
6007 }
6008 }
6009
6010 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6011 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6012 char country[3];
6013 country[0] = wpa_s->conf->country[0];
6014 country[1] = wpa_s->conf->country[1];
6015 country[2] = 0x04;
6016 p2p_set_country(p2p, country);
6017 }
6018
6019 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6020 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6021 wpa_s->conf->p2p_ssid_postfix ?
6022 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6023 0);
6024 }
6025
6026 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6027 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006028
6029 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6030 u8 reg_class, channel;
6031 int ret;
6032 unsigned int r;
6033 if (wpa_s->conf->p2p_listen_reg_class &&
6034 wpa_s->conf->p2p_listen_channel) {
6035 reg_class = wpa_s->conf->p2p_listen_reg_class;
6036 channel = wpa_s->conf->p2p_listen_channel;
6037 } else {
6038 reg_class = 81;
6039 /*
6040 * Pick one of the social channels randomly as the
6041 * listen channel.
6042 */
6043 os_get_random((u8 *) &r, sizeof(r));
6044 channel = 1 + (r % 3) * 5;
6045 }
6046 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6047 if (ret)
6048 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6049 "failed: %d", ret);
6050 }
6051 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6052 u8 op_reg_class, op_channel, cfg_op_channel;
6053 int ret = 0;
6054 unsigned int r;
6055 if (wpa_s->conf->p2p_oper_reg_class &&
6056 wpa_s->conf->p2p_oper_channel) {
6057 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6058 op_channel = wpa_s->conf->p2p_oper_channel;
6059 cfg_op_channel = 1;
6060 } else {
6061 op_reg_class = 81;
6062 /*
6063 * Use random operation channel from (1, 6, 11)
6064 *if no other preference is indicated.
6065 */
6066 os_get_random((u8 *) &r, sizeof(r));
6067 op_channel = 1 + (r % 3) * 5;
6068 cfg_op_channel = 0;
6069 }
6070 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6071 cfg_op_channel);
6072 if (ret)
6073 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6074 "failed: %d", ret);
6075 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006076
6077 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6078 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6079 wpa_s->conf->p2p_pref_chan) < 0) {
6080 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6081 "update failed");
6082 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006083
6084 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6085 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6086 "update failed");
6087 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006088 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089}
6090
6091
6092int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6093 int duration)
6094{
6095 if (!wpa_s->ap_iface)
6096 return -1;
6097 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6098 duration);
6099}
6100
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006101
6102int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6103{
6104 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6105 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006106
6107 wpa_s->global->cross_connection = enabled;
6108 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6109
6110 if (!enabled) {
6111 struct wpa_supplicant *iface;
6112
6113 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6114 {
6115 if (iface->cross_connect_enabled == 0)
6116 continue;
6117
6118 iface->cross_connect_enabled = 0;
6119 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006120 wpa_msg_global(iface->parent, MSG_INFO,
6121 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6122 iface->ifname,
6123 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006124 }
6125 }
6126
6127 return 0;
6128}
6129
6130
6131static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6132{
6133 struct wpa_supplicant *iface;
6134
6135 if (!uplink->global->cross_connection)
6136 return;
6137
6138 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6139 if (!iface->cross_connect_enabled)
6140 continue;
6141 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6142 0)
6143 continue;
6144 if (iface->ap_iface == NULL)
6145 continue;
6146 if (iface->cross_connect_in_use)
6147 continue;
6148
6149 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006150 wpa_msg_global(iface->parent, MSG_INFO,
6151 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6152 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006153 }
6154}
6155
6156
6157static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6158{
6159 struct wpa_supplicant *iface;
6160
6161 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6162 if (!iface->cross_connect_enabled)
6163 continue;
6164 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6165 0)
6166 continue;
6167 if (!iface->cross_connect_in_use)
6168 continue;
6169
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006170 wpa_msg_global(iface->parent, MSG_INFO,
6171 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6172 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173 iface->cross_connect_in_use = 0;
6174 }
6175}
6176
6177
6178void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6179{
6180 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6181 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6182 wpa_s->cross_connect_disallowed)
6183 wpas_p2p_disable_cross_connect(wpa_s);
6184 else
6185 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006186 if (!wpa_s->ap_iface &&
6187 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6188 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006189}
6190
6191
6192void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6193{
6194 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006195 if (!wpa_s->ap_iface &&
6196 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6197 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 wpas_p2p_set_group_idle_timeout(wpa_s);
6199}
6200
6201
6202static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6203{
6204 struct wpa_supplicant *iface;
6205
6206 if (!wpa_s->global->cross_connection)
6207 return;
6208
6209 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6210 if (iface == wpa_s)
6211 continue;
6212 if (iface->drv_flags &
6213 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6214 continue;
6215 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6216 continue;
6217
6218 wpa_s->cross_connect_enabled = 1;
6219 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6220 sizeof(wpa_s->cross_connect_uplink));
6221 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6222 "%s to %s whenever uplink is available",
6223 wpa_s->ifname, wpa_s->cross_connect_uplink);
6224
6225 if (iface->ap_iface || iface->current_ssid == NULL ||
6226 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6227 iface->cross_connect_disallowed ||
6228 iface->wpa_state != WPA_COMPLETED)
6229 break;
6230
6231 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006232 wpa_msg_global(wpa_s->parent, MSG_INFO,
6233 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6234 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006235 break;
6236 }
6237}
6238
6239
6240int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6241{
6242 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6243 !wpa_s->p2p_in_provisioning)
6244 return 0; /* not P2P client operation */
6245
6246 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6247 "session overlap");
6248 if (wpa_s != wpa_s->parent)
6249 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006250 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251 return 1;
6252}
6253
6254
6255void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6256{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006257 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258
6259 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6260 return;
6261
6262 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006263 os_memset(&cli_chan, 0, sizeof(cli_chan));
6264 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6266 "channel list");
6267 return;
6268 }
6269
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006270 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006271}
6272
6273
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006274static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6275 struct wpa_scan_results *scan_res)
6276{
6277 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6278}
6279
6280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006281int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6282{
6283 struct wpa_global *global = wpa_s->global;
6284 int found = 0;
6285 const u8 *peer;
6286
6287 if (global->p2p == NULL)
6288 return -1;
6289
6290 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6291
6292 if (wpa_s->pending_interface_name[0] &&
6293 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6294 found = 1;
6295
6296 peer = p2p_get_go_neg_peer(global->p2p);
6297 if (peer) {
6298 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6299 MACSTR, MAC2STR(peer));
6300 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006301 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006302 }
6303
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006304 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6305 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6306 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6307 found = 1;
6308 }
6309
6310 if (wpa_s->pending_pd_before_join) {
6311 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6312 wpa_s->pending_pd_before_join = 0;
6313 found = 1;
6314 }
6315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006316 wpas_p2p_stop_find(wpa_s);
6317
6318 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6319 if (wpa_s == global->p2p_group_formation &&
6320 (wpa_s->p2p_in_provisioning ||
6321 wpa_s->parent->pending_interface_type ==
6322 WPA_IF_P2P_CLIENT)) {
6323 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6324 "formation found - cancelling",
6325 wpa_s->ifname);
6326 found = 1;
6327 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6328 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006329 if (wpa_s->p2p_in_provisioning) {
6330 wpas_group_formation_completed(wpa_s, 0);
6331 break;
6332 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006333 wpas_p2p_group_delete(wpa_s,
6334 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006335 break;
6336 }
6337 }
6338
6339 if (!found) {
6340 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6341 return -1;
6342 }
6343
6344 return 0;
6345}
6346
6347
6348void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6349{
6350 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6351 return;
6352
6353 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6354 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006355 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006356}
6357
6358
6359void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6360 int freq_24, int freq_5, int freq_overall)
6361{
6362 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006363 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364 return;
6365 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6366}
6367
6368
6369int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6370{
6371 u8 peer[ETH_ALEN];
6372 struct p2p_data *p2p = wpa_s->global->p2p;
6373
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006374 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006375 return -1;
6376
6377 if (hwaddr_aton(addr, peer))
6378 return -1;
6379
6380 return p2p_unauthorize(p2p, peer);
6381}
6382
6383
6384/**
6385 * wpas_p2p_disconnect - Disconnect from a P2P Group
6386 * @wpa_s: Pointer to wpa_supplicant data
6387 * Returns: 0 on success, -1 on failure
6388 *
6389 * This can be used to disconnect from a group in which the local end is a P2P
6390 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6391 * virtual network interface was created for this group, that interface will be
6392 * removed. Otherwise, only the configured P2P group network will be removed
6393 * from the interface.
6394 */
6395int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6396{
6397
6398 if (wpa_s == NULL)
6399 return -1;
6400
Jouni Malinen2b89da82012-08-31 22:04:41 +03006401 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6402 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006403}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006404
6405
6406int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6407{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006408 int ret;
6409
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006410 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6411 return 0;
6412
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006413 ret = p2p_in_progress(wpa_s->global->p2p);
6414 if (ret == 0) {
6415 /*
6416 * Check whether there is an ongoing WPS provisioning step (or
6417 * other parts of group formation) on another interface since
6418 * p2p_in_progress() does not report this to avoid issues for
6419 * scans during such provisioning step.
6420 */
6421 if (wpa_s->global->p2p_group_formation &&
6422 wpa_s->global->p2p_group_formation != wpa_s) {
6423 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6424 "in group formation",
6425 wpa_s->global->p2p_group_formation->ifname);
6426 ret = 1;
6427 }
6428 }
6429
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006430 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006431 struct os_reltime now;
6432 os_get_reltime(&now);
6433 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6434 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006435 /* Wait for the first client has expired */
6436 wpa_s->global->p2p_go_wait_client.sec = 0;
6437 } else {
6438 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6439 ret = 1;
6440 }
6441 }
6442
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006443 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006444}
6445
6446
6447void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6448 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006449{
6450 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6451 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6452 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006453 /**
6454 * Remove the network by scheduling the group formation
6455 * timeout to happen immediately. The teardown code
6456 * needs to be scheduled to run asynch later so that we
6457 * don't delete data from under ourselves unexpectedly.
6458 * Calling wpas_p2p_group_formation_timeout directly
6459 * causes a series of crashes in WPS failure scenarios.
6460 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006461 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6462 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006463 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6464 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006465 }
6466}
6467
6468
6469struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006470 const u8 *addr, const u8 *ssid,
6471 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006472{
6473 struct wpa_ssid *s;
6474 size_t i;
6475
6476 for (s = wpa_s->conf->ssid; s; s = s->next) {
6477 if (s->disabled != 2)
6478 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006479 if (ssid &&
6480 (ssid_len != s->ssid_len ||
6481 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6482 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006483 if (addr == NULL) {
6484 if (s->mode == WPAS_MODE_P2P_GO)
6485 return s;
6486 continue;
6487 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006488 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6489 return s; /* peer is GO in the persistent group */
6490 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6491 continue;
6492 for (i = 0; i < s->num_p2p_clients; i++) {
6493 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6494 addr, ETH_ALEN) == 0)
6495 return s; /* peer is P2P client in persistent
6496 * group */
6497 }
6498 }
6499
6500 return NULL;
6501}
6502
6503
6504void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6505 const u8 *addr)
6506{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006507 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6508 wpa_s->parent, NULL) > 0) {
6509 /*
6510 * This can happen if WPS provisioning step is not terminated
6511 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6512 * peer was able to connect, there is no need to time out group
6513 * formation after this, though. In addition, this is used with
6514 * the initial connection wait on the GO as a separate formation
6515 * timeout and as such, expected to be hit after the initial WPS
6516 * provisioning step.
6517 */
6518 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6519 }
6520 if (!wpa_s->p2p_go_group_formation_completed) {
6521 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6522 wpa_s->p2p_go_group_formation_completed = 1;
6523 wpa_s->global->p2p_group_formation = NULL;
6524 wpa_s->p2p_in_provisioning = 0;
6525 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006526 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006527 if (addr == NULL)
6528 return;
6529 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6530}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006531
Dmitry Shmidt04949592012-07-19 12:16:46 -07006532
6533static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6534 int group_added)
6535{
6536 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006537 if (wpa_s->global->p2p_group_formation)
6538 group = wpa_s->global->p2p_group_formation;
6539 wpa_s = wpa_s->parent;
6540 offchannel_send_action_done(wpa_s);
6541 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006542 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006543 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6544 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6545 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6546 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6547 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006548 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006549 wpa_s->p2p_go_ht40,
6550 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006551}
6552
6553
6554int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6555{
6556 if (!wpa_s->p2p_fallback_to_go_neg ||
6557 wpa_s->p2p_in_provisioning <= 5)
6558 return 0;
6559
6560 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6561 return 0; /* peer operating as a GO */
6562
6563 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6564 "fallback to GO Negotiation");
6565 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6566
6567 return 1;
6568}
6569
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006570
6571unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6572{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006573 struct wpa_supplicant *ifs;
6574
6575 if (wpa_s->wpa_state > WPA_SCANNING) {
6576 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6577 "concurrent operation",
6578 P2P_CONCURRENT_SEARCH_DELAY);
6579 return P2P_CONCURRENT_SEARCH_DELAY;
6580 }
6581
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006582 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6583 radio_list) {
6584 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006585 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6586 "delay due to concurrent operation on "
6587 "interface %s",
6588 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6589 return P2P_CONCURRENT_SEARCH_DELAY;
6590 }
6591 }
6592
6593 return 0;
6594}
6595
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006596
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006597static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6598 struct wpa_ssid *s, const u8 *addr,
6599 int iface_addr)
6600{
6601 struct psk_list_entry *psk, *tmp;
6602 int changed = 0;
6603
6604 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6605 list) {
6606 if ((iface_addr && !psk->p2p &&
6607 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6608 (!iface_addr && psk->p2p &&
6609 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6610 wpa_dbg(wpa_s, MSG_DEBUG,
6611 "P2P: Remove persistent group PSK list entry for "
6612 MACSTR " p2p=%u",
6613 MAC2STR(psk->addr), psk->p2p);
6614 dl_list_del(&psk->list);
6615 os_free(psk);
6616 changed++;
6617 }
6618 }
6619
6620 return changed;
6621}
6622
6623
6624void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6625 const u8 *p2p_dev_addr,
6626 const u8 *psk, size_t psk_len)
6627{
6628 struct wpa_ssid *ssid = wpa_s->current_ssid;
6629 struct wpa_ssid *persistent;
6630 struct psk_list_entry *p;
6631
6632 if (psk_len != sizeof(p->psk))
6633 return;
6634
6635 if (p2p_dev_addr) {
6636 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6637 " p2p_dev_addr=" MACSTR,
6638 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6639 if (is_zero_ether_addr(p2p_dev_addr))
6640 p2p_dev_addr = NULL;
6641 } else {
6642 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6643 MAC2STR(mac_addr));
6644 }
6645
6646 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6647 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6648 /* To be added to persistent group once created */
6649 if (wpa_s->global->add_psk == NULL) {
6650 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6651 if (wpa_s->global->add_psk == NULL)
6652 return;
6653 }
6654 p = wpa_s->global->add_psk;
6655 if (p2p_dev_addr) {
6656 p->p2p = 1;
6657 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6658 } else {
6659 p->p2p = 0;
6660 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6661 }
6662 os_memcpy(p->psk, psk, psk_len);
6663 return;
6664 }
6665
6666 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6667 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6668 return;
6669 }
6670
6671 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6672 ssid->ssid_len);
6673 if (!persistent) {
6674 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6675 return;
6676 }
6677
6678 p = os_zalloc(sizeof(*p));
6679 if (p == NULL)
6680 return;
6681 if (p2p_dev_addr) {
6682 p->p2p = 1;
6683 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6684 } else {
6685 p->p2p = 0;
6686 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6687 }
6688 os_memcpy(p->psk, psk, psk_len);
6689
6690 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6691 struct psk_list_entry *last;
6692 last = dl_list_last(&persistent->psk_list,
6693 struct psk_list_entry, list);
6694 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6695 MACSTR " (p2p=%u) to make room for a new one",
6696 MAC2STR(last->addr), last->p2p);
6697 dl_list_del(&last->list);
6698 os_free(last);
6699 }
6700
6701 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6702 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6703 p2p_dev_addr == NULL);
6704 if (p2p_dev_addr) {
6705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6706 MACSTR, MAC2STR(p2p_dev_addr));
6707 } else {
6708 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6709 MAC2STR(mac_addr));
6710 }
6711 dl_list_add(&persistent->psk_list, &p->list);
6712
6713#ifndef CONFIG_NO_CONFIG_WRITE
6714 if (wpa_s->parent->conf->update_config &&
6715 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6716 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6717#endif /* CONFIG_NO_CONFIG_WRITE */
6718}
6719
6720
6721static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6722 struct wpa_ssid *s, const u8 *addr,
6723 int iface_addr)
6724{
6725 int res;
6726
6727 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6728 if (res > 0) {
6729#ifndef CONFIG_NO_CONFIG_WRITE
6730 if (wpa_s->conf->update_config &&
6731 wpa_config_write(wpa_s->confname, wpa_s->conf))
6732 wpa_dbg(wpa_s, MSG_DEBUG,
6733 "P2P: Failed to update configuration");
6734#endif /* CONFIG_NO_CONFIG_WRITE */
6735 }
6736}
6737
6738
6739static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6740 const u8 *peer, int iface_addr)
6741{
6742 struct hostapd_data *hapd;
6743 struct hostapd_wpa_psk *psk, *prev, *rem;
6744 struct sta_info *sta;
6745
6746 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6747 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6748 return;
6749
6750 /* Remove per-station PSK entry */
6751 hapd = wpa_s->ap_iface->bss[0];
6752 prev = NULL;
6753 psk = hapd->conf->ssid.wpa_psk;
6754 while (psk) {
6755 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6756 (!iface_addr &&
6757 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6758 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6759 MACSTR " iface_addr=%d",
6760 MAC2STR(peer), iface_addr);
6761 if (prev)
6762 prev->next = psk->next;
6763 else
6764 hapd->conf->ssid.wpa_psk = psk->next;
6765 rem = psk;
6766 psk = psk->next;
6767 os_free(rem);
6768 } else {
6769 prev = psk;
6770 psk = psk->next;
6771 }
6772 }
6773
6774 /* Disconnect from group */
6775 if (iface_addr)
6776 sta = ap_get_sta(hapd, peer);
6777 else
6778 sta = ap_get_sta_p2p(hapd, peer);
6779 if (sta) {
6780 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6781 " (iface_addr=%d) from group",
6782 MAC2STR(peer), iface_addr);
6783 hostapd_drv_sta_deauth(hapd, sta->addr,
6784 WLAN_REASON_DEAUTH_LEAVING);
6785 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6786 }
6787}
6788
6789
6790void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6791 int iface_addr)
6792{
6793 struct wpa_ssid *s;
6794 struct wpa_supplicant *w;
6795
6796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6797
6798 /* Remove from any persistent group */
6799 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6800 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6801 continue;
6802 if (!iface_addr)
6803 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6804 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6805 }
6806
6807 /* Remove from any operating group */
6808 for (w = wpa_s->global->ifaces; w; w = w->next)
6809 wpas_p2p_remove_client_go(w, peer, iface_addr);
6810}
6811
6812
6813static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6814{
6815 struct wpa_supplicant *wpa_s = eloop_ctx;
6816 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6817}
6818
6819
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006820static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6821{
6822 struct wpa_supplicant *wpa_s = eloop_ctx;
6823
6824 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6825 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6826}
6827
6828
6829int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6830 struct wpa_ssid *ssid)
6831{
6832 struct wpa_supplicant *iface;
6833
6834 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6835 if (!iface->current_ssid ||
6836 iface->current_ssid->frequency == freq ||
6837 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6838 !iface->current_ssid->p2p_group))
6839 continue;
6840
6841 /* Remove the connection with least priority */
6842 if (!wpas_is_p2p_prioritized(iface)) {
6843 /* STA connection has priority over existing
6844 * P2P connection, so remove the interface. */
6845 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6846 eloop_register_timeout(0, 0,
6847 wpas_p2p_group_freq_conflict,
6848 iface, NULL);
6849 /* If connection in progress is P2P connection, do not
6850 * proceed for the connection. */
6851 if (wpa_s == iface)
6852 return -1;
6853 else
6854 return 0;
6855 } else {
6856 /* P2P connection has priority, disable the STA network
6857 */
6858 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6859 ssid);
6860 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6861 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6862 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6863 ETH_ALEN);
6864 /* If P2P connection is in progress, continue
6865 * connecting...*/
6866 if (wpa_s == iface)
6867 return 0;
6868 else
6869 return -1;
6870 }
6871 }
6872
6873 return 0;
6874}
6875
6876
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006877int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6878{
6879 struct wpa_ssid *ssid = wpa_s->current_ssid;
6880
6881 if (ssid == NULL || !ssid->p2p_group)
6882 return 0;
6883
6884 if (wpa_s->p2p_last_4way_hs_fail &&
6885 wpa_s->p2p_last_4way_hs_fail == ssid) {
6886 u8 go_dev_addr[ETH_ALEN];
6887 struct wpa_ssid *persistent;
6888
6889 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6890 ssid->ssid,
6891 ssid->ssid_len) <= 0) {
6892 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6893 goto disconnect;
6894 }
6895
6896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6897 MACSTR, MAC2STR(go_dev_addr));
6898 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6899 ssid->ssid,
6900 ssid->ssid_len);
6901 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6902 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6903 goto disconnect;
6904 }
6905 wpa_msg_global(wpa_s->parent, MSG_INFO,
6906 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6907 persistent->id);
6908 disconnect:
6909 wpa_s->p2p_last_4way_hs_fail = NULL;
6910 /*
6911 * Remove the group from a timeout to avoid issues with caller
6912 * continuing to use the interface if this is on a P2P group
6913 * interface.
6914 */
6915 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6916 wpa_s, NULL);
6917 return 1;
6918 }
6919
6920 wpa_s->p2p_last_4way_hs_fail = ssid;
6921 return 0;
6922}