blob: 3ac67130ea7f38b75fb5b924e5c371e8b549c4c3 [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 Shmidtb7b4d0e2013-08-26 12:09:05 -07001345 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001346 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347}
1348
1349
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001350static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1351 char *ifname, size_t len)
1352{
1353 char *ifname_ptr = wpa_s->ifname;
1354
1355 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1356 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1357 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1358 }
1359
1360 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1361 if (os_strlen(ifname) >= IFNAMSIZ &&
1362 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1363 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001364 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001365 }
1366}
1367
1368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1370 enum wpa_driver_if_type type)
1371{
1372 char ifname[120], force_ifname[120];
1373
1374 if (wpa_s->pending_interface_name[0]) {
1375 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1376 "- skip creation of a new one");
1377 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1378 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1379 "unknown?! ifname='%s'",
1380 wpa_s->pending_interface_name);
1381 return -1;
1382 }
1383 return 0;
1384 }
1385
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001386 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387 force_ifname[0] = '\0';
1388
1389 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1390 ifname);
1391 wpa_s->p2p_group_idx++;
1392
1393 wpa_s->pending_interface_type = type;
1394 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1395 wpa_s->pending_interface_addr, NULL) < 0) {
1396 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1397 "interface");
1398 return -1;
1399 }
1400
1401 if (force_ifname[0]) {
1402 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1403 force_ifname);
1404 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1405 sizeof(wpa_s->pending_interface_name));
1406 } else
1407 os_strlcpy(wpa_s->pending_interface_name, ifname,
1408 sizeof(wpa_s->pending_interface_name));
1409 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1410 MACSTR, wpa_s->pending_interface_name,
1411 MAC2STR(wpa_s->pending_interface_addr));
1412
1413 return 0;
1414}
1415
1416
1417static void wpas_p2p_remove_pending_group_interface(
1418 struct wpa_supplicant *wpa_s)
1419{
1420 if (!wpa_s->pending_interface_name[0] ||
1421 is_zero_ether_addr(wpa_s->pending_interface_addr))
1422 return; /* No pending virtual interface */
1423
1424 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1425 wpa_s->pending_interface_name);
1426 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1427 wpa_s->pending_interface_name);
1428 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1429 wpa_s->pending_interface_name[0] = '\0';
1430}
1431
1432
1433static struct wpa_supplicant *
1434wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1435{
1436 struct wpa_interface iface;
1437 struct wpa_supplicant *group_wpa_s;
1438
1439 if (!wpa_s->pending_interface_name[0]) {
1440 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1441 if (!wpas_p2p_create_iface(wpa_s))
1442 return NULL;
1443 /*
1444 * Something has forced us to remove the pending interface; try
1445 * to create a new one and hope for the best that we will get
1446 * the same local address.
1447 */
1448 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1449 WPA_IF_P2P_CLIENT) < 0)
1450 return NULL;
1451 }
1452
1453 os_memset(&iface, 0, sizeof(iface));
1454 iface.ifname = wpa_s->pending_interface_name;
1455 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001456 if (wpa_s->conf->ctrl_interface == NULL &&
1457 wpa_s->parent != wpa_s &&
1458 wpa_s->p2p_mgmt &&
1459 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1460 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1461 else
1462 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463 iface.driver_param = wpa_s->conf->driver_param;
1464 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1465 if (group_wpa_s == NULL) {
1466 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1467 "wpa_supplicant interface");
1468 return NULL;
1469 }
1470 wpa_s->pending_interface_name[0] = '\0';
1471 group_wpa_s->parent = wpa_s;
1472 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1473 P2P_GROUP_INTERFACE_CLIENT;
1474 wpa_s->global->p2p_group_formation = group_wpa_s;
1475
1476 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1477
1478 return group_wpa_s;
1479}
1480
1481
1482static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1483 void *timeout_ctx)
1484{
1485 struct wpa_supplicant *wpa_s = eloop_ctx;
1486 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001487 wpas_p2p_group_formation_failed(wpa_s);
1488}
1489
1490
1491void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1492{
1493 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1494 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 if (wpa_s->global->p2p)
1496 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 wpas_group_formation_completed(wpa_s, 0);
1498}
1499
1500
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001501static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1502{
1503 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1504 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1505 wpa_s->parent, NULL);
1506 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1507 wpa_s->parent, NULL);
1508 wpa_s->global->p2p_fail_on_wps_complete = 0;
1509}
1510
1511
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001512void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1513{
1514 if (wpa_s->global->p2p_group_formation != wpa_s)
1515 return;
1516 /* Speed up group formation timeout since this cannot succeed */
1517 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1518 wpa_s->parent, NULL);
1519 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1520 wpa_s->parent, NULL);
1521}
1522
1523
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001524static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525{
1526 struct wpa_supplicant *wpa_s = ctx;
1527
1528 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1529 wpa_drv_cancel_remain_on_channel(wpa_s);
1530 wpa_s->off_channel_freq = 0;
1531 wpa_s->roc_waiting_drv_freq = 0;
1532 }
1533
1534 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001535 wpa_msg_global(wpa_s, MSG_INFO,
1536 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1537 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001538 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 wpas_p2p_remove_pending_group_interface(wpa_s);
1540 return;
1541 }
1542
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001543 if (wpa_s->p2p_go_ht40)
1544 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001545 if (wpa_s->p2p_go_vht)
1546 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001547
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001548 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1549 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1550 " wps_method=%s",
1551 res->role_go ? "GO" : "client", res->freq, res->ht40,
1552 MAC2STR(res->peer_device_addr),
1553 MAC2STR(res->peer_interface_addr),
1554 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001555 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
Dmitry Shmidt04949592012-07-19 12:16:46 -07001557 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1558 struct wpa_ssid *ssid;
1559 ssid = wpa_config_get_network(wpa_s->conf,
1560 wpa_s->p2p_persistent_id);
1561 if (ssid && ssid->disabled == 2 &&
1562 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1563 size_t len = os_strlen(ssid->passphrase);
1564 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1565 "on requested persistent group");
1566 os_memcpy(res->passphrase, ssid->passphrase, len);
1567 res->passphrase[len] = '\0';
1568 }
1569 }
1570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571 if (wpa_s->create_p2p_iface) {
1572 struct wpa_supplicant *group_wpa_s =
1573 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1574 if (group_wpa_s == NULL) {
1575 wpas_p2p_remove_pending_group_interface(wpa_s);
1576 return;
1577 }
1578 if (group_wpa_s != wpa_s) {
1579 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1580 sizeof(group_wpa_s->p2p_pin));
1581 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1582 }
1583 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1584 wpa_s->pending_interface_name[0] = '\0';
1585 group_wpa_s->p2p_in_provisioning = 1;
1586
1587 if (res->role_go)
1588 wpas_start_wps_go(group_wpa_s, res, 1);
1589 else
1590 wpas_start_wps_enrollee(group_wpa_s, res);
1591 } else {
1592 wpa_s->p2p_in_provisioning = 1;
1593 wpa_s->global->p2p_group_formation = wpa_s;
1594
1595 if (res->role_go)
1596 wpas_start_wps_go(wpa_s, res, 1);
1597 else
1598 wpas_start_wps_enrollee(ctx, res);
1599 }
1600
1601 wpa_s->p2p_long_listen = 0;
1602 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1603
1604 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1605 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1606 (res->peer_config_timeout % 100) * 10000,
1607 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1608}
1609
1610
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001611static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612{
1613 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001614 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1615 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616
1617 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1618}
1619
1620
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001621static void wpas_dev_found(void *ctx, const u8 *addr,
1622 const struct p2p_peer_info *info,
1623 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001625#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 struct wpa_supplicant *wpa_s = ctx;
1627 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001628 char *wfd_dev_info_hex = NULL;
1629
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001630#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001631 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1632 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001633#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001634
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001635 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1636 " p2p_dev_addr=" MACSTR
1637 " pri_dev_type=%s name='%s' config_methods=0x%x "
1638 "dev_capab=0x%x group_capab=0x%x%s%s",
1639 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1640 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1641 sizeof(devtype)),
1642 info->device_name, info->config_methods,
1643 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001644 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1645 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001646#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001648 os_free(wfd_dev_info_hex);
1649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1651}
1652
1653
1654static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1655{
1656 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001657
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001658 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1659 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1662}
1663
1664
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001665static void wpas_find_stopped(void *ctx)
1666{
1667 struct wpa_supplicant *wpa_s = ctx;
1668 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1669}
1670
1671
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001672struct wpas_p2p_listen_work {
1673 unsigned int freq;
1674 unsigned int duration;
1675 struct wpabuf *probe_resp_ie;
1676};
1677
1678
1679static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1680{
1681 if (lwork == NULL)
1682 return;
1683 wpabuf_free(lwork->probe_resp_ie);
1684 os_free(lwork);
1685}
1686
1687
1688static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1689{
1690 struct wpas_p2p_listen_work *lwork;
1691
1692 if (!wpa_s->p2p_listen_work)
1693 return;
1694
1695 lwork = wpa_s->p2p_listen_work->ctx;
1696 wpas_p2p_listen_work_free(lwork);
1697 radio_work_done(wpa_s->p2p_listen_work);
1698 wpa_s->p2p_listen_work = NULL;
1699}
1700
1701
1702static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1703{
1704 struct wpa_supplicant *wpa_s = work->wpa_s;
1705 struct wpas_p2p_listen_work *lwork = work->ctx;
1706
1707 if (deinit) {
1708 wpas_p2p_listen_work_free(lwork);
1709 return;
1710 }
1711
1712 wpa_s->p2p_listen_work = work;
1713
1714 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1715
1716 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1717 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1718 "report received Probe Request frames");
1719 wpas_p2p_listen_work_done(wpa_s);
1720 return;
1721 }
1722
1723 wpa_s->pending_listen_freq = lwork->freq;
1724 wpa_s->pending_listen_duration = lwork->duration;
1725
1726 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1727 {
1728 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1729 "to remain on channel (%u MHz) for Listen "
1730 "state", lwork->freq);
1731 wpas_p2p_listen_work_done(wpa_s);
1732 wpa_s->pending_listen_freq = 0;
1733 return;
1734 }
1735 wpa_s->off_channel_freq = 0;
1736 wpa_s->roc_waiting_drv_freq = lwork->freq;
1737}
1738
1739
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740static int wpas_start_listen(void *ctx, unsigned int freq,
1741 unsigned int duration,
1742 const struct wpabuf *probe_resp_ie)
1743{
1744 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001745 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001747 if (wpa_s->p2p_listen_work) {
1748 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 return -1;
1750 }
1751
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001752 lwork = os_zalloc(sizeof(*lwork));
1753 if (lwork == NULL)
1754 return -1;
1755 lwork->freq = freq;
1756 lwork->duration = duration;
1757 if (probe_resp_ie) {
1758 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1759 if (lwork->probe_resp_ie == NULL) {
1760 wpas_p2p_listen_work_free(lwork);
1761 return -1;
1762 }
1763 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001765 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1766 lwork) < 0) {
1767 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 return -1;
1769 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770
1771 return 0;
1772}
1773
1774
1775static void wpas_stop_listen(void *ctx)
1776{
1777 struct wpa_supplicant *wpa_s = ctx;
1778 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1779 wpa_drv_cancel_remain_on_channel(wpa_s);
1780 wpa_s->off_channel_freq = 0;
1781 wpa_s->roc_waiting_drv_freq = 0;
1782 }
1783 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1784 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001785 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786}
1787
1788
1789static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1790{
1791 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001792 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001793}
1794
1795
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001796/*
1797 * DNS Header section is used only to calculate compression pointers, so the
1798 * contents of this data does not matter, but the length needs to be reserved
1799 * in the virtual packet.
1800 */
1801#define DNS_HEADER_LEN 12
1802
1803/*
1804 * 27-octet in-memory packet from P2P specification containing two implied
1805 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1806 */
1807#define P2P_SD_IN_MEMORY_LEN 27
1808
1809static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1810 u8 **spos, const u8 *end)
1811{
1812 while (*spos < end) {
1813 u8 val = ((*spos)[0] & 0xc0) >> 6;
1814 int len;
1815
1816 if (val == 1 || val == 2) {
1817 /* These are reserved values in RFC 1035 */
1818 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1819 "sequence starting with 0x%x", val);
1820 return -1;
1821 }
1822
1823 if (val == 3) {
1824 u16 offset;
1825 u8 *spos_tmp;
1826
1827 /* Offset */
1828 if (*spos + 2 > end) {
1829 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1830 "DNS offset field");
1831 return -1;
1832 }
1833
1834 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1835 if (offset >= *spos - start) {
1836 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1837 "pointer offset %u", offset);
1838 return -1;
1839 }
1840
1841 (*spos) += 2;
1842 spos_tmp = start + offset;
1843 return p2p_sd_dns_uncompress_label(upos, uend, start,
1844 &spos_tmp,
1845 *spos - 2);
1846 }
1847
1848 /* Label */
1849 len = (*spos)[0] & 0x3f;
1850 if (len == 0)
1851 return 0;
1852
1853 (*spos)++;
1854 if (*spos + len > end) {
1855 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1856 "sequence - no room for label with length "
1857 "%u", len);
1858 return -1;
1859 }
1860
1861 if (*upos + len + 2 > uend)
1862 return -2;
1863
1864 os_memcpy(*upos, *spos, len);
1865 *spos += len;
1866 *upos += len;
1867 (*upos)[0] = '.';
1868 (*upos)++;
1869 (*upos)[0] = '\0';
1870 }
1871
1872 return 0;
1873}
1874
1875
1876/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1877 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1878 * not large enough */
1879static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1880 size_t msg_len, size_t offset)
1881{
1882 /* 27-octet in-memory packet from P2P specification */
1883 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1884 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1885 u8 *tmp, *end, *spos;
1886 char *upos, *uend;
1887 int ret = 0;
1888
1889 if (buf_len < 2)
1890 return -1;
1891 if (offset > msg_len)
1892 return -1;
1893
1894 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1895 if (tmp == NULL)
1896 return -1;
1897 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1898 end = spos + msg_len;
1899 spos += offset;
1900
1901 os_memset(tmp, 0, DNS_HEADER_LEN);
1902 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1903 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1904
1905 upos = buf;
1906 uend = buf + buf_len;
1907
1908 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1909 if (ret) {
1910 os_free(tmp);
1911 return ret;
1912 }
1913
1914 if (upos == buf) {
1915 upos[0] = '.';
1916 upos[1] = '\0';
1917 } else if (upos[-1] == '.')
1918 upos[-1] = '\0';
1919
1920 os_free(tmp);
1921 return 0;
1922}
1923
1924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925static struct p2p_srv_bonjour *
1926wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1927 const struct wpabuf *query)
1928{
1929 struct p2p_srv_bonjour *bsrv;
1930 size_t len;
1931
1932 len = wpabuf_len(query);
1933 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1934 struct p2p_srv_bonjour, list) {
1935 if (len == wpabuf_len(bsrv->query) &&
1936 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1937 len) == 0)
1938 return bsrv;
1939 }
1940 return NULL;
1941}
1942
1943
1944static struct p2p_srv_upnp *
1945wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1946 const char *service)
1947{
1948 struct p2p_srv_upnp *usrv;
1949
1950 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1951 struct p2p_srv_upnp, list) {
1952 if (version == usrv->version &&
1953 os_strcmp(service, usrv->service) == 0)
1954 return usrv;
1955 }
1956 return NULL;
1957}
1958
1959
1960static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1961 u8 srv_trans_id)
1962{
1963 u8 *len_pos;
1964
1965 if (wpabuf_tailroom(resp) < 5)
1966 return;
1967
1968 /* Length (to be filled) */
1969 len_pos = wpabuf_put(resp, 2);
1970 wpabuf_put_u8(resp, srv_proto);
1971 wpabuf_put_u8(resp, srv_trans_id);
1972 /* Status Code */
1973 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1974 /* Response Data: empty */
1975 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1976}
1977
1978
1979static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1980 struct wpabuf *resp, u8 srv_trans_id)
1981{
1982 struct p2p_srv_bonjour *bsrv;
1983 u8 *len_pos;
1984
1985 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1986
1987 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1988 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1989 return;
1990 }
1991
1992 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1993 struct p2p_srv_bonjour, list) {
1994 if (wpabuf_tailroom(resp) <
1995 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1996 return;
1997 /* Length (to be filled) */
1998 len_pos = wpabuf_put(resp, 2);
1999 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2000 wpabuf_put_u8(resp, srv_trans_id);
2001 /* Status Code */
2002 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2003 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2004 wpabuf_head(bsrv->resp),
2005 wpabuf_len(bsrv->resp));
2006 /* Response Data */
2007 wpabuf_put_buf(resp, bsrv->query); /* Key */
2008 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2009 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2010 2);
2011 }
2012}
2013
2014
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002015static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2016 size_t query_len)
2017{
2018 char str_rx[256], str_srv[256];
2019
2020 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2021 return 0; /* Too short to include DNS Type and Version */
2022 if (os_memcmp(query + query_len - 3,
2023 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2024 3) != 0)
2025 return 0; /* Mismatch in DNS Type or Version */
2026 if (query_len == wpabuf_len(bsrv->query) &&
2027 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2028 return 1; /* Binary match */
2029
2030 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2031 0))
2032 return 0; /* Failed to uncompress query */
2033 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2034 wpabuf_head(bsrv->query),
2035 wpabuf_len(bsrv->query) - 3, 0))
2036 return 0; /* Failed to uncompress service */
2037
2038 return os_strcmp(str_rx, str_srv) == 0;
2039}
2040
2041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2043 struct wpabuf *resp, u8 srv_trans_id,
2044 const u8 *query, size_t query_len)
2045{
2046 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002048 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049
2050 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2051 query, query_len);
2052 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2053 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2054 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2055 srv_trans_id);
2056 return;
2057 }
2058
2059 if (query_len == 0) {
2060 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2061 return;
2062 }
2063
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002064 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2065 struct p2p_srv_bonjour, list) {
2066 if (!match_bonjour_query(bsrv, query, query_len))
2067 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002068
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002069 if (wpabuf_tailroom(resp) <
2070 5 + query_len + wpabuf_len(bsrv->resp))
2071 return;
2072
2073 matches++;
2074
2075 /* Length (to be filled) */
2076 len_pos = wpabuf_put(resp, 2);
2077 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2078 wpabuf_put_u8(resp, srv_trans_id);
2079
2080 /* Status Code */
2081 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2082 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2083 wpabuf_head(bsrv->resp),
2084 wpabuf_len(bsrv->resp));
2085
2086 /* Response Data */
2087 wpabuf_put_data(resp, query, query_len); /* Key */
2088 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2089
2090 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2091 }
2092
2093 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2095 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002096 if (wpabuf_tailroom(resp) < 5)
2097 return;
2098
2099 /* Length (to be filled) */
2100 len_pos = wpabuf_put(resp, 2);
2101 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2102 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103
2104 /* Status Code */
2105 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2106 /* Response Data: empty */
2107 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2108 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110}
2111
2112
2113static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2114 struct wpabuf *resp, u8 srv_trans_id)
2115{
2116 struct p2p_srv_upnp *usrv;
2117 u8 *len_pos;
2118
2119 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2120
2121 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2122 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2123 return;
2124 }
2125
2126 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2127 struct p2p_srv_upnp, list) {
2128 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2129 return;
2130
2131 /* Length (to be filled) */
2132 len_pos = wpabuf_put(resp, 2);
2133 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2134 wpabuf_put_u8(resp, srv_trans_id);
2135
2136 /* Status Code */
2137 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2138 /* Response Data */
2139 wpabuf_put_u8(resp, usrv->version);
2140 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2141 usrv->service);
2142 wpabuf_put_str(resp, usrv->service);
2143 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2144 2);
2145 }
2146}
2147
2148
2149static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2150 struct wpabuf *resp, u8 srv_trans_id,
2151 const u8 *query, size_t query_len)
2152{
2153 struct p2p_srv_upnp *usrv;
2154 u8 *len_pos;
2155 u8 version;
2156 char *str;
2157 int count = 0;
2158
2159 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2160 query, query_len);
2161
2162 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2163 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2164 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2165 srv_trans_id);
2166 return;
2167 }
2168
2169 if (query_len == 0) {
2170 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2171 return;
2172 }
2173
2174 if (wpabuf_tailroom(resp) < 5)
2175 return;
2176
2177 /* Length (to be filled) */
2178 len_pos = wpabuf_put(resp, 2);
2179 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2180 wpabuf_put_u8(resp, srv_trans_id);
2181
2182 version = query[0];
2183 str = os_malloc(query_len);
2184 if (str == NULL)
2185 return;
2186 os_memcpy(str, query + 1, query_len - 1);
2187 str[query_len - 1] = '\0';
2188
2189 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2190 struct p2p_srv_upnp, list) {
2191 if (version != usrv->version)
2192 continue;
2193
2194 if (os_strcmp(str, "ssdp:all") != 0 &&
2195 os_strstr(usrv->service, str) == NULL)
2196 continue;
2197
2198 if (wpabuf_tailroom(resp) < 2)
2199 break;
2200 if (count == 0) {
2201 /* Status Code */
2202 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2203 /* Response Data */
2204 wpabuf_put_u8(resp, version);
2205 } else
2206 wpabuf_put_u8(resp, ',');
2207
2208 count++;
2209
2210 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2211 usrv->service);
2212 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2213 break;
2214 wpabuf_put_str(resp, usrv->service);
2215 }
2216 os_free(str);
2217
2218 if (count == 0) {
2219 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2220 "available");
2221 /* Status Code */
2222 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2223 /* Response Data: empty */
2224 }
2225
2226 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2227}
2228
2229
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002230#ifdef CONFIG_WIFI_DISPLAY
2231static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2232 struct wpabuf *resp, u8 srv_trans_id,
2233 const u8 *query, size_t query_len)
2234{
2235 const u8 *pos;
2236 u8 role;
2237 u8 *len_pos;
2238
2239 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2240
2241 if (!wpa_s->global->wifi_display) {
2242 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2243 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2244 srv_trans_id);
2245 return;
2246 }
2247
2248 if (query_len < 1) {
2249 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2250 "Role");
2251 return;
2252 }
2253
2254 if (wpabuf_tailroom(resp) < 5)
2255 return;
2256
2257 pos = query;
2258 role = *pos++;
2259 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2260
2261 /* TODO: role specific handling */
2262
2263 /* Length (to be filled) */
2264 len_pos = wpabuf_put(resp, 2);
2265 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2266 wpabuf_put_u8(resp, srv_trans_id);
2267 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2268
2269 while (pos < query + query_len) {
2270 if (*pos < MAX_WFD_SUBELEMS &&
2271 wpa_s->global->wfd_subelem[*pos] &&
2272 wpabuf_tailroom(resp) >=
2273 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2274 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2275 "subelement %u", *pos);
2276 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2277 }
2278 pos++;
2279 }
2280
2281 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2282}
2283#endif /* CONFIG_WIFI_DISPLAY */
2284
2285
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002286static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2287 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288{
2289 struct wpa_supplicant *wpa_s = ctx;
2290 const u8 *pos = tlvs;
2291 const u8 *end = tlvs + tlvs_len;
2292 const u8 *tlv_end;
2293 u16 slen;
2294 struct wpabuf *resp;
2295 u8 srv_proto, srv_trans_id;
2296 size_t buf_len;
2297 char *buf;
2298
2299 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2300 tlvs, tlvs_len);
2301 buf_len = 2 * tlvs_len + 1;
2302 buf = os_malloc(buf_len);
2303 if (buf) {
2304 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2305 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2306 MACSTR " %u %u %s",
2307 freq, MAC2STR(sa), dialog_token, update_indic,
2308 buf);
2309 os_free(buf);
2310 }
2311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002312 if (wpa_s->p2p_sd_over_ctrl_iface) {
2313 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2314 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317
2318 resp = wpabuf_alloc(10000);
2319 if (resp == NULL)
2320 return;
2321
2322 while (pos + 1 < end) {
2323 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2324 slen = WPA_GET_LE16(pos);
2325 pos += 2;
2326 if (pos + slen > end || slen < 2) {
2327 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2328 "length");
2329 wpabuf_free(resp);
2330 return;
2331 }
2332 tlv_end = pos + slen;
2333
2334 srv_proto = *pos++;
2335 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2336 srv_proto);
2337 srv_trans_id = *pos++;
2338 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2339 srv_trans_id);
2340
2341 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2342 pos, tlv_end - pos);
2343
2344
2345 if (wpa_s->force_long_sd) {
2346 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2347 "response");
2348 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2349 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2350 goto done;
2351 }
2352
2353 switch (srv_proto) {
2354 case P2P_SERV_ALL_SERVICES:
2355 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2356 "for all services");
2357 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2358 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2359 wpa_printf(MSG_DEBUG, "P2P: No service "
2360 "discovery protocols available");
2361 wpas_sd_add_proto_not_avail(
2362 resp, P2P_SERV_ALL_SERVICES,
2363 srv_trans_id);
2364 break;
2365 }
2366 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2367 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2368 break;
2369 case P2P_SERV_BONJOUR:
2370 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2371 pos, tlv_end - pos);
2372 break;
2373 case P2P_SERV_UPNP:
2374 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2375 pos, tlv_end - pos);
2376 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002377#ifdef CONFIG_WIFI_DISPLAY
2378 case P2P_SERV_WIFI_DISPLAY:
2379 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2380 pos, tlv_end - pos);
2381 break;
2382#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 default:
2384 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2385 "protocol %u", srv_proto);
2386 wpas_sd_add_proto_not_avail(resp, srv_proto,
2387 srv_trans_id);
2388 break;
2389 }
2390
2391 pos = tlv_end;
2392 }
2393
2394done:
2395 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2396 update_indic, tlvs, tlvs_len);
2397
2398 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2399
2400 wpabuf_free(resp);
2401}
2402
2403
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002404static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2405 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406{
2407 struct wpa_supplicant *wpa_s = ctx;
2408 const u8 *pos = tlvs;
2409 const u8 *end = tlvs + tlvs_len;
2410 const u8 *tlv_end;
2411 u16 slen;
2412 size_t buf_len;
2413 char *buf;
2414
2415 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2416 tlvs, tlvs_len);
2417 if (tlvs_len > 1500) {
2418 /* TODO: better way for handling this */
2419 wpa_msg_ctrl(wpa_s, MSG_INFO,
2420 P2P_EVENT_SERV_DISC_RESP MACSTR
2421 " %u <long response: %u bytes>",
2422 MAC2STR(sa), update_indic,
2423 (unsigned int) tlvs_len);
2424 } else {
2425 buf_len = 2 * tlvs_len + 1;
2426 buf = os_malloc(buf_len);
2427 if (buf) {
2428 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2429 wpa_msg_ctrl(wpa_s, MSG_INFO,
2430 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2431 MAC2STR(sa), update_indic, buf);
2432 os_free(buf);
2433 }
2434 }
2435
2436 while (pos < end) {
2437 u8 srv_proto, srv_trans_id, status;
2438
2439 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2440 slen = WPA_GET_LE16(pos);
2441 pos += 2;
2442 if (pos + slen > end || slen < 3) {
2443 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2444 "length");
2445 return;
2446 }
2447 tlv_end = pos + slen;
2448
2449 srv_proto = *pos++;
2450 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2451 srv_proto);
2452 srv_trans_id = *pos++;
2453 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2454 srv_trans_id);
2455 status = *pos++;
2456 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2457 status);
2458
2459 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2460 pos, tlv_end - pos);
2461
2462 pos = tlv_end;
2463 }
2464
2465 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2466}
2467
2468
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002469u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2470 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002473 return 0;
2474 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475}
2476
2477
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002478u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2479 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480{
2481 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002482 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483
2484 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2485 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002486 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2488 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2489 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2490 wpabuf_put_u8(tlvs, version);
2491 wpabuf_put_str(tlvs, query);
2492 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2493 wpabuf_free(tlvs);
2494 return ret;
2495}
2496
2497
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002498#ifdef CONFIG_WIFI_DISPLAY
2499
2500static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2501 const struct wpabuf *tlvs)
2502{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002503 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2504 return 0;
2505 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2506}
2507
2508
2509#define MAX_WFD_SD_SUBELEMS 20
2510
2511static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2512 const char *subelems)
2513{
2514 u8 *len;
2515 const char *pos;
2516 int val;
2517 int count = 0;
2518
2519 len = wpabuf_put(tlvs, 2);
2520 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2521 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2522
2523 wpabuf_put_u8(tlvs, role);
2524
2525 pos = subelems;
2526 while (*pos) {
2527 val = atoi(pos);
2528 if (val >= 0 && val < 256) {
2529 wpabuf_put_u8(tlvs, val);
2530 count++;
2531 if (count == MAX_WFD_SD_SUBELEMS)
2532 break;
2533 }
2534 pos = os_strchr(pos + 1, ',');
2535 if (pos == NULL)
2536 break;
2537 pos++;
2538 }
2539
2540 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2541}
2542
2543
2544u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2545 const u8 *dst, const char *role)
2546{
2547 struct wpabuf *tlvs;
2548 u64 ret;
2549 const char *subelems;
2550 u8 id = 1;
2551
2552 subelems = os_strchr(role, ' ');
2553 if (subelems == NULL)
2554 return 0;
2555 subelems++;
2556
2557 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2558 if (tlvs == NULL)
2559 return 0;
2560
2561 if (os_strstr(role, "[source]"))
2562 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2563 if (os_strstr(role, "[pri-sink]"))
2564 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2565 if (os_strstr(role, "[sec-sink]"))
2566 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2567 if (os_strstr(role, "[source+sink]"))
2568 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2569
2570 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2571 wpabuf_free(tlvs);
2572 return ret;
2573}
2574
2575#endif /* CONFIG_WIFI_DISPLAY */
2576
2577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002578int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2581 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002582 return p2p_sd_cancel_request(wpa_s->global->p2p,
2583 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584}
2585
2586
2587void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2588 const u8 *dst, u8 dialog_token,
2589 const struct wpabuf *resp_tlvs)
2590{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2592 return;
2593 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2594 resp_tlvs);
2595}
2596
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2599{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 if (wpa_s->global->p2p)
2601 p2p_sd_service_update(wpa_s->global->p2p);
2602}
2603
2604
2605static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2606{
2607 dl_list_del(&bsrv->list);
2608 wpabuf_free(bsrv->query);
2609 wpabuf_free(bsrv->resp);
2610 os_free(bsrv);
2611}
2612
2613
2614static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2615{
2616 dl_list_del(&usrv->list);
2617 os_free(usrv->service);
2618 os_free(usrv);
2619}
2620
2621
2622void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2623{
2624 struct p2p_srv_bonjour *bsrv, *bn;
2625 struct p2p_srv_upnp *usrv, *un;
2626
2627 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2628 struct p2p_srv_bonjour, list)
2629 wpas_p2p_srv_bonjour_free(bsrv);
2630
2631 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2632 struct p2p_srv_upnp, list)
2633 wpas_p2p_srv_upnp_free(usrv);
2634
2635 wpas_p2p_sd_service_update(wpa_s);
2636}
2637
2638
2639int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2640 struct wpabuf *query, struct wpabuf *resp)
2641{
2642 struct p2p_srv_bonjour *bsrv;
2643
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644 bsrv = os_zalloc(sizeof(*bsrv));
2645 if (bsrv == NULL)
2646 return -1;
2647 bsrv->query = query;
2648 bsrv->resp = resp;
2649 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2650
2651 wpas_p2p_sd_service_update(wpa_s);
2652 return 0;
2653}
2654
2655
2656int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2657 const struct wpabuf *query)
2658{
2659 struct p2p_srv_bonjour *bsrv;
2660
2661 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2662 if (bsrv == NULL)
2663 return -1;
2664 wpas_p2p_srv_bonjour_free(bsrv);
2665 wpas_p2p_sd_service_update(wpa_s);
2666 return 0;
2667}
2668
2669
2670int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2671 const char *service)
2672{
2673 struct p2p_srv_upnp *usrv;
2674
2675 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2676 return 0; /* Already listed */
2677 usrv = os_zalloc(sizeof(*usrv));
2678 if (usrv == NULL)
2679 return -1;
2680 usrv->version = version;
2681 usrv->service = os_strdup(service);
2682 if (usrv->service == NULL) {
2683 os_free(usrv);
2684 return -1;
2685 }
2686 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2687
2688 wpas_p2p_sd_service_update(wpa_s);
2689 return 0;
2690}
2691
2692
2693int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2694 const char *service)
2695{
2696 struct p2p_srv_upnp *usrv;
2697
2698 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2699 if (usrv == NULL)
2700 return -1;
2701 wpas_p2p_srv_upnp_free(usrv);
2702 wpas_p2p_sd_service_update(wpa_s);
2703 return 0;
2704}
2705
2706
2707static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2708 const u8 *peer, const char *params,
2709 unsigned int generated_pin)
2710{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002711 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2712 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713}
2714
2715
2716static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2717 const u8 *peer, const char *params)
2718{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002719 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2720 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721}
2722
2723
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002724static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2725 const u8 *dev_addr, const u8 *pri_dev_type,
2726 const char *dev_name, u16 supp_config_methods,
2727 u8 dev_capab, u8 group_capab, const u8 *group_id,
2728 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729{
2730 struct wpa_supplicant *wpa_s = ctx;
2731 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002732 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 u8 empty_dev_type[8];
2734 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735 struct wpa_supplicant *group = NULL;
2736
2737 if (group_id) {
2738 for (group = wpa_s->global->ifaces; group; group = group->next)
2739 {
2740 struct wpa_ssid *s = group->current_ssid;
2741 if (s != NULL &&
2742 s->mode == WPAS_MODE_P2P_GO &&
2743 group_id_len - ETH_ALEN == s->ssid_len &&
2744 os_memcmp(group_id + ETH_ALEN, s->ssid,
2745 s->ssid_len) == 0)
2746 break;
2747 }
2748 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749
2750 if (pri_dev_type == NULL) {
2751 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2752 pri_dev_type = empty_dev_type;
2753 }
2754 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2755 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002756 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 MAC2STR(dev_addr),
2758 wps_dev_type_bin2str(pri_dev_type, devtype,
2759 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002760 dev_name, supp_config_methods, dev_capab, group_capab,
2761 group ? " group=" : "",
2762 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002763 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002764
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 if (config_methods & WPS_CONFIG_DISPLAY) {
2766 generated_pin = wps_generate_pin();
2767 wpas_prov_disc_local_display(wpa_s, peer, params,
2768 generated_pin);
2769 } else if (config_methods & WPS_CONFIG_KEYPAD)
2770 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2771 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2773 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002774
2775 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2776 P2P_PROV_DISC_SUCCESS,
2777 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778}
2779
2780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002781static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782{
2783 struct wpa_supplicant *wpa_s = ctx;
2784 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002785 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002787 if (wpa_s->pending_pd_before_join &&
2788 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2789 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2790 wpa_s->pending_pd_before_join = 0;
2791 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2792 "join-existing-group operation");
2793 wpas_p2p_join_start(wpa_s);
2794 return;
2795 }
2796
Dmitry Shmidt04949592012-07-19 12:16:46 -07002797 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2798 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2799 os_snprintf(params, sizeof(params), " peer_go=%d",
2800 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2801 else
2802 params[0] = '\0';
2803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002805 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 else if (config_methods & WPS_CONFIG_KEYPAD) {
2807 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002808 wpas_prov_disc_local_display(wpa_s, peer, params,
2809 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002811 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2812 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813
Jouni Malinen75ecf522011-06-27 15:19:46 -07002814 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2815 P2P_PROV_DISC_SUCCESS,
2816 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817}
2818
2819
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002820static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2821 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002822{
2823 struct wpa_supplicant *wpa_s = ctx;
2824
Dmitry Shmidt04949592012-07-19 12:16:46 -07002825 if (wpa_s->p2p_fallback_to_go_neg) {
2826 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2827 "failed - fall back to GO Negotiation");
2828 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2829 return;
2830 }
2831
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002832 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002833 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002834 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2835 "join-existing-group operation (no ACK for PD "
2836 "Req attempts)");
2837 wpas_p2p_join_start(wpa_s);
2838 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002839 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002840
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002841 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2842 " p2p_dev_addr=" MACSTR " status=%d",
2843 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002844
Jouni Malinen75ecf522011-06-27 15:19:46 -07002845 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2846 status, 0, 0);
2847}
2848
2849
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002850static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2851{
2852 if (channels == NULL)
2853 return 1; /* Assume no restrictions */
2854 return p2p_channels_includes_freq(channels, freq);
2855
2856}
2857
2858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2860 const u8 *go_dev_addr, const u8 *ssid,
2861 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002862 int *force_freq, int persistent_group,
2863 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864{
2865 struct wpa_supplicant *wpa_s = ctx;
2866 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 int res;
2868 struct wpa_supplicant *grp;
2869
2870 if (!persistent_group) {
2871 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002872 " to join an active group (SSID: %s)",
2873 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2875 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2876 == 0 ||
2877 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2878 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2879 "authorized invitation");
2880 goto accept_inv;
2881 }
2882 /*
2883 * Do not accept the invitation automatically; notify user and
2884 * request approval.
2885 */
2886 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2887 }
2888
2889 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2890 if (grp) {
2891 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2892 "running persistent group");
2893 if (*go)
2894 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2895 goto accept_inv;
2896 }
2897
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002898 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2899 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2900 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2901 "invitation to re-invoke a persistent group");
2902 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2904
2905 for (s = wpa_s->conf->ssid; s; s = s->next) {
2906 if (s->disabled == 2 &&
2907 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2908 s->ssid_len == ssid_len &&
2909 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2910 break;
2911 }
2912
2913 if (!s) {
2914 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2915 " requested reinvocation of an unknown group",
2916 MAC2STR(sa));
2917 return P2P_SC_FAIL_UNKNOWN_GROUP;
2918 }
2919
2920 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2921 *go = 1;
2922 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2923 wpa_printf(MSG_DEBUG, "P2P: The only available "
2924 "interface is already in use - reject "
2925 "invitation");
2926 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2927 }
2928 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2929 } else if (s->mode == WPAS_MODE_P2P_GO) {
2930 *go = 1;
2931 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2932 {
2933 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2934 "interface address for the group");
2935 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2936 }
2937 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2938 ETH_ALEN);
2939 }
2940
2941accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002942 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2943
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002944 /* Get one of the frequencies currently in use */
2945 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002946 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002947 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002948
2949 if (wpa_s->num_multichan_concurrent < 2 ||
2950 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2951 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2952 *force_freq = res;
2953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002954 }
2955
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002956 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2957 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002958 if (*go == 0) {
2959 /* We are the client */
2960 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2961 "running a GO but we are capable of MCC, "
2962 "figure out the best channel to use");
2963 *force_freq = 0;
2964 } else if (!freq_included(channels, *force_freq)) {
2965 /* We are the GO, and *force_freq is not in the
2966 * intersection */
2967 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2968 "in intersection but we are capable of MCC, "
2969 "figure out the best channel to use",
2970 *force_freq);
2971 *force_freq = 0;
2972 }
2973 }
2974
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 return P2P_SC_SUCCESS;
2976}
2977
2978
2979static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2980 const u8 *ssid, size_t ssid_len,
2981 const u8 *go_dev_addr, u8 status,
2982 int op_freq)
2983{
2984 struct wpa_supplicant *wpa_s = ctx;
2985 struct wpa_ssid *s;
2986
2987 for (s = wpa_s->conf->ssid; s; s = s->next) {
2988 if (s->disabled == 2 &&
2989 s->ssid_len == ssid_len &&
2990 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2991 break;
2992 }
2993
2994 if (status == P2P_SC_SUCCESS) {
2995 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002996 " was accepted; op_freq=%d MHz, SSID=%s",
2997 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002999 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003000 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003001 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003002 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003004 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003006 wpa_s->p2p_wps_method, 0,
3007 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003008 }
3009 return;
3010 }
3011
3012 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3013 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3014 " was rejected (status %u)", MAC2STR(sa), status);
3015 return;
3016 }
3017
3018 if (!s) {
3019 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003020 wpa_msg_global(wpa_s, MSG_INFO,
3021 P2P_EVENT_INVITATION_RECEIVED
3022 "sa=" MACSTR " go_dev_addr=" MACSTR
3023 " bssid=" MACSTR " unknown-network",
3024 MAC2STR(sa), MAC2STR(go_dev_addr),
3025 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003026 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003027 wpa_msg_global(wpa_s, MSG_INFO,
3028 P2P_EVENT_INVITATION_RECEIVED
3029 "sa=" MACSTR " go_dev_addr=" MACSTR
3030 " unknown-network",
3031 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 }
3033 return;
3034 }
3035
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003036 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003037 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3038 "sa=" MACSTR " persistent=%d freq=%d",
3039 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003040 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003041 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3042 "sa=" MACSTR " persistent=%d",
3043 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003044 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045}
3046
3047
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003048static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3049 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003050 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003051{
3052 size_t i;
3053
3054 if (ssid == NULL)
3055 return;
3056
3057 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3058 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3059 ETH_ALEN) == 0)
3060 break;
3061 }
3062 if (i >= ssid->num_p2p_clients) {
3063 if (ssid->mode != WPAS_MODE_P2P_GO &&
3064 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3065 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3066 "due to invitation result", ssid->id);
3067 wpas_notify_network_removed(wpa_s, ssid);
3068 wpa_config_remove_network(wpa_s->conf, ssid->id);
3069 return;
3070 }
3071 return; /* Peer not found in client list */
3072 }
3073
3074 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003075 "group %d client list%s",
3076 MAC2STR(peer), ssid->id,
3077 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003078 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3079 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3080 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3081 ssid->num_p2p_clients--;
3082#ifndef CONFIG_NO_CONFIG_WRITE
3083 if (wpa_s->parent->conf->update_config &&
3084 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3085 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3086#endif /* CONFIG_NO_CONFIG_WRITE */
3087}
3088
3089
3090static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3091 const u8 *peer)
3092{
3093 struct wpa_ssid *ssid;
3094
3095 wpa_s = wpa_s->global->p2p_invite_group;
3096 if (wpa_s == NULL)
3097 return; /* No known invitation group */
3098 ssid = wpa_s->current_ssid;
3099 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3100 !ssid->p2p_persistent_group)
3101 return; /* Not operating as a GO in persistent group */
3102 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3103 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003104 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003105}
3106
3107
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003108static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003109 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003110 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111{
3112 struct wpa_supplicant *wpa_s = ctx;
3113 struct wpa_ssid *ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003114 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115
3116 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003117 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3118 "status=%d " MACSTR,
3119 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003121 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3122 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123 }
3124 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3125
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003126 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3127 status, MAC2STR(peer));
3128 if (wpa_s->pending_invite_ssid_id == -1) {
3129 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3130 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003131 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003132 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003134 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3135 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3136 "invitation exchange to indicate readiness for "
3137 "re-invocation");
3138 }
3139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003141 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3142 ssid = wpa_config_get_network(
3143 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003144 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003145 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003146 wpas_p2p_remove_pending_group_interface(wpa_s);
3147 return;
3148 }
3149
3150 ssid = wpa_config_get_network(wpa_s->conf,
3151 wpa_s->pending_invite_ssid_id);
3152 if (ssid == NULL) {
3153 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3154 "data matching with invitation");
3155 return;
3156 }
3157
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003158 /*
3159 * The peer could have missed our ctrl::ack frame for Invitation
3160 * Response and continue retransmitting the frame. To reduce the
3161 * likelihood of the peer not getting successful TX status for the
3162 * Invitation Response frame, wait a short time here before starting
3163 * the persistent group so that we will remain on the current channel to
3164 * acknowledge any possible retransmission from the peer.
3165 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07003166#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003167 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3168 "starting persistent group");
3169 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07003170#else
3171 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
3172 "starting persistent group");
3173 os_sleep(0, 100000);
3174#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003175
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003176 freq = wpa_s->p2p_persistent_go_freq;
3177 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3178 freq_included(channels, neg_freq)) {
3179 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
3180 neg_freq);
3181 freq = neg_freq;
3182 }
3183
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003185 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003186 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003187 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3188 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003189 ssid->mode == WPAS_MODE_P2P_GO ?
3190 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3191 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192}
3193
3194
Dmitry Shmidt04949592012-07-19 12:16:46 -07003195static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3196 unsigned int freq)
3197{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003198 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003199}
3200
3201
3202static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3203{
3204 reg->channel[reg->channels] = chan;
3205 reg->channels++;
3206}
3207
3208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003210 struct p2p_channels *chan,
3211 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212{
3213 int i, cla = 0;
3214
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003215 os_memset(cli_chan, 0, sizeof(*cli_chan));
3216
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003217 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3218 "band");
3219
3220 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3221 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003222 chan->reg_class[cla].channels = 0;
3223 for (i = 0; i < 11; i++) {
3224 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3225 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3226 }
3227 if (chan->reg_class[cla].channels)
3228 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229
3230 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3231 "band");
3232
3233 /* Operating class 115 - 5 GHz, channels 36-48 */
3234 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003235 chan->reg_class[cla].channels = 0;
3236 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3237 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3238 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3239 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3240 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3241 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3242 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3243 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3244 if (chan->reg_class[cla].channels)
3245 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003246
3247 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3248 "band");
3249
3250 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3251 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003252 chan->reg_class[cla].channels = 0;
3253 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3254 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3255 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3256 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3257 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3258 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3259 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3260 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3261 if (chan->reg_class[cla].channels)
3262 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263
3264 chan->reg_classes = cla;
3265 return 0;
3266}
3267
3268
3269static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3270 u16 num_modes,
3271 enum hostapd_hw_mode mode)
3272{
3273 u16 i;
3274
3275 for (i = 0; i < num_modes; i++) {
3276 if (modes[i].mode == mode)
3277 return &modes[i];
3278 }
3279
3280 return NULL;
3281}
3282
3283
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003284enum chan_allowed {
3285 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3286};
3287
Dmitry Shmidt04949592012-07-19 12:16:46 -07003288static int has_channel(struct wpa_global *global,
3289 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290{
3291 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003292 unsigned int freq;
3293
3294 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3295 chan * 5;
3296 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003297 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298
3299 for (i = 0; i < mode->num_channels; i++) {
3300 if (mode->channels[i].chan == chan) {
3301 if (flags)
3302 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003303 if (mode->channels[i].flag &
3304 (HOSTAPD_CHAN_DISABLED |
3305 HOSTAPD_CHAN_RADAR))
3306 return NOT_ALLOWED;
3307 if (mode->channels[i].flag &
3308 (HOSTAPD_CHAN_PASSIVE_SCAN |
3309 HOSTAPD_CHAN_NO_IBSS))
3310 return PASSIVE_ONLY;
3311 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003312 }
3313 }
3314
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003315 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316}
3317
3318
3319struct p2p_oper_class_map {
3320 enum hostapd_hw_mode mode;
3321 u8 op_class;
3322 u8 min_chan;
3323 u8 max_chan;
3324 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003325 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003326};
3327
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003328static struct p2p_oper_class_map op_class[] = {
3329 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003330#if 0 /* Do not enable HT40 on 2 GHz for now */
3331 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3332 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3333#endif
3334 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3335 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3336 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3337 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3338 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3339 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003340
3341 /*
3342 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3343 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3344 * 80 MHz, but currently use the following definition for simplicity
3345 * (these center frequencies are not actual channels, which makes
3346 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3347 * removing invalid channels.
3348 */
3349 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003350 { -1, 0, 0, 0, 0, BW20 }
3351};
3352
3353
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003354static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3355 struct hostapd_hw_modes *mode,
3356 u8 channel)
3357{
3358 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3359 unsigned int i;
3360
3361 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3362 return 0;
3363
3364 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3365 /*
3366 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3367 * so the center channel is 6 channels away from the start/end.
3368 */
3369 if (channel >= center_channels[i] - 6 &&
3370 channel <= center_channels[i] + 6)
3371 return center_channels[i];
3372
3373 return 0;
3374}
3375
3376
3377static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3378 struct hostapd_hw_modes *mode,
3379 u8 channel, u8 bw)
3380{
3381 u8 center_chan;
3382 int i, flags;
3383 enum chan_allowed res, ret = ALLOWED;
3384
3385 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3386 if (!center_chan)
3387 return NOT_ALLOWED;
3388 if (center_chan >= 58 && center_chan <= 138)
3389 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3390
3391 /* check all the channels are available */
3392 for (i = 0; i < 4; i++) {
3393 int adj_chan = center_chan - 6 + i * 4;
3394
3395 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3396 if (res == NOT_ALLOWED)
3397 return NOT_ALLOWED;
3398 if (res == PASSIVE_ONLY)
3399 ret = PASSIVE_ONLY;
3400
3401 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3402 return NOT_ALLOWED;
3403 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3404 return NOT_ALLOWED;
3405 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3406 return NOT_ALLOWED;
3407 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3408 return NOT_ALLOWED;
3409 }
3410
3411 return ret;
3412}
3413
3414
3415static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3416 struct hostapd_hw_modes *mode,
3417 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003418{
3419 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003420 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003421
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003422 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3423 if (bw == BW40MINUS) {
3424 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3425 return NOT_ALLOWED;
3426 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3427 } else if (bw == BW40PLUS) {
3428 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3429 return NOT_ALLOWED;
3430 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3431 } else if (bw == BW80) {
3432 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3433 }
3434
3435 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3436 return NOT_ALLOWED;
3437 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3438 return PASSIVE_ONLY;
3439 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003440}
3441
3442
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003444 struct p2p_channels *chan,
3445 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003447 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003448 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003450 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003451 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3452 "of all supported channels; assume dualband "
3453 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003454 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 }
3456
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003457 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003458
3459 for (op = 0; op_class[op].op_class; op++) {
3460 struct p2p_oper_class_map *o = &op_class[op];
3461 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003462 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003464 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 if (mode == NULL)
3466 continue;
3467 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003468 enum chan_allowed res;
3469 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3470 if (res == ALLOWED) {
3471 if (reg == NULL) {
3472 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3473 o->op_class);
3474 reg = &chan->reg_class[cla];
3475 cla++;
3476 reg->reg_class = o->op_class;
3477 }
3478 reg->channel[reg->channels] = ch;
3479 reg->channels++;
3480 } else if (res == PASSIVE_ONLY &&
3481 wpa_s->conf->p2p_add_cli_chan) {
3482 if (cli_reg == NULL) {
3483 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3484 o->op_class);
3485 cli_reg = &cli_chan->reg_class[cli_cla];
3486 cli_cla++;
3487 cli_reg->reg_class = o->op_class;
3488 }
3489 cli_reg->channel[cli_reg->channels] = ch;
3490 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003492 }
3493 if (reg) {
3494 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3495 reg->channel, reg->channels);
3496 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003497 if (cli_reg) {
3498 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3499 cli_reg->channel, cli_reg->channels);
3500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 }
3502
3503 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003504 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003506 return 0;
3507}
3508
3509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003510int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3511 struct hostapd_hw_modes *mode, u8 channel)
3512{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003513 int op;
3514 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003515
3516 for (op = 0; op_class[op].op_class; op++) {
3517 struct p2p_oper_class_map *o = &op_class[op];
3518 u8 ch;
3519
3520 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3521 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3522 o->bw == BW20 || ch != channel)
3523 continue;
3524 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003525 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003526 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003527 }
3528 }
3529 return 0;
3530}
3531
3532
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003533int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3534 struct hostapd_hw_modes *mode, u8 channel)
3535{
3536 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3537 return 0;
3538
3539 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3540}
3541
3542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003543static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3544 size_t buf_len)
3545{
3546 struct wpa_supplicant *wpa_s = ctx;
3547
3548 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3549 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3550 break;
3551 }
3552 if (wpa_s == NULL)
3553 return -1;
3554
3555 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3556}
3557
3558
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003559static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3560{
3561 struct wpa_supplicant *wpa_s = ctx;
3562
3563 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3564 struct wpa_ssid *ssid = wpa_s->current_ssid;
3565 if (ssid == NULL)
3566 continue;
3567 if (ssid->mode != WPAS_MODE_INFRA)
3568 continue;
3569 if (wpa_s->wpa_state != WPA_COMPLETED &&
3570 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3571 continue;
3572 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3573 return 1;
3574 }
3575
3576 return 0;
3577}
3578
3579
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003580static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3581{
3582 struct wpa_supplicant *wpa_s = ctx;
3583 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3584}
3585
3586
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003587int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3588{
3589 struct wpa_interface iface;
3590 struct wpa_supplicant *p2pdev_wpa_s;
3591 char ifname[100];
3592 char force_name[100];
3593 int ret;
3594
3595 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3596 wpa_s->ifname);
3597 force_name[0] = '\0';
3598 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3599 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3600 force_name, wpa_s->pending_interface_addr, NULL);
3601 if (ret < 0) {
3602 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3603 return ret;
3604 }
3605 os_strlcpy(wpa_s->pending_interface_name, ifname,
3606 sizeof(wpa_s->pending_interface_name));
3607
3608 os_memset(&iface, 0, sizeof(iface));
3609 iface.p2p_mgmt = 1;
3610 iface.ifname = wpa_s->pending_interface_name;
3611 iface.driver = wpa_s->driver->name;
3612 iface.driver_param = wpa_s->conf->driver_param;
3613 iface.confname = wpa_s->confname;
3614 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3615 if (!p2pdev_wpa_s) {
3616 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3617 return -1;
3618 }
3619 p2pdev_wpa_s->parent = wpa_s;
3620
3621 wpa_s->pending_interface_name[0] = '\0';
3622 return 0;
3623}
3624
3625
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003626static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3627 const u8 *noa, size_t noa_len)
3628{
3629 struct wpa_supplicant *wpa_s, *intf = ctx;
3630 char hex[100];
3631
3632 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3633 if (wpa_s->waiting_presence_resp)
3634 break;
3635 }
3636 if (!wpa_s) {
3637 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3638 return;
3639 }
3640 wpa_s->waiting_presence_resp = 0;
3641
3642 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3643 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3644 " status=%u noa=%s", MAC2STR(src), status, hex);
3645}
3646
3647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648/**
3649 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3650 * @global: Pointer to global data from wpa_supplicant_init()
3651 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3652 * Returns: 0 on success, -1 on failure
3653 */
3654int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3655{
3656 struct p2p_config p2p;
3657 unsigned int r;
3658 int i;
3659
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003660 if (wpa_s->conf->p2p_disabled)
3661 return 0;
3662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3664 return 0;
3665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 if (global->p2p)
3667 return 0;
3668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003671 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 p2p.p2p_scan = wpas_p2p_scan;
3673 p2p.send_action = wpas_send_action;
3674 p2p.send_action_done = wpas_send_action_done;
3675 p2p.go_neg_completed = wpas_go_neg_completed;
3676 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3677 p2p.dev_found = wpas_dev_found;
3678 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003679 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003680 p2p.start_listen = wpas_start_listen;
3681 p2p.stop_listen = wpas_stop_listen;
3682 p2p.send_probe_resp = wpas_send_probe_resp;
3683 p2p.sd_request = wpas_sd_request;
3684 p2p.sd_response = wpas_sd_response;
3685 p2p.prov_disc_req = wpas_prov_disc_req;
3686 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003687 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 p2p.invitation_process = wpas_invitation_process;
3689 p2p.invitation_received = wpas_invitation_received;
3690 p2p.invitation_result = wpas_invitation_result;
3691 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003692 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003693 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694
3695 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003696 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 p2p.dev_name = wpa_s->conf->device_name;
3698 p2p.manufacturer = wpa_s->conf->manufacturer;
3699 p2p.model_name = wpa_s->conf->model_name;
3700 p2p.model_number = wpa_s->conf->model_number;
3701 p2p.serial_number = wpa_s->conf->serial_number;
3702 if (wpa_s->wps) {
3703 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3704 p2p.config_methods = wpa_s->wps->config_methods;
3705 }
3706
3707 if (wpa_s->conf->p2p_listen_reg_class &&
3708 wpa_s->conf->p2p_listen_channel) {
3709 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3710 p2p.channel = wpa_s->conf->p2p_listen_channel;
3711 } else {
3712 p2p.reg_class = 81;
3713 /*
3714 * Pick one of the social channels randomly as the listen
3715 * channel.
3716 */
3717 os_get_random((u8 *) &r, sizeof(r));
3718 p2p.channel = 1 + (r % 3) * 5;
3719 }
3720 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3721
3722 if (wpa_s->conf->p2p_oper_reg_class &&
3723 wpa_s->conf->p2p_oper_channel) {
3724 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3725 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3726 p2p.cfg_op_channel = 1;
3727 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3728 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3729
3730 } else {
3731 p2p.op_reg_class = 81;
3732 /*
3733 * Use random operation channel from (1, 6, 11) if no other
3734 * preference is indicated.
3735 */
3736 os_get_random((u8 *) &r, sizeof(r));
3737 p2p.op_channel = 1 + (r % 3) * 5;
3738 p2p.cfg_op_channel = 0;
3739 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3740 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3741 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003742
3743 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3744 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3745 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3746 }
3747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3749 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3750 p2p.country[2] = 0x04;
3751 } else
3752 os_memcpy(p2p.country, "XX\x04", 3);
3753
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003754 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003755 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3756 "channel list");
3757 return -1;
3758 }
3759
3760 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3761 WPS_DEV_TYPE_LEN);
3762
3763 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3764 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3765 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3766
3767 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3768 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3769
3770 p2p.max_peers = 100;
3771
3772 if (wpa_s->conf->p2p_ssid_postfix) {
3773 p2p.ssid_postfix_len =
3774 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3775 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3776 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3777 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3778 p2p.ssid_postfix_len);
3779 }
3780
3781 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3782
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003783 p2p.max_listen = wpa_s->max_remain_on_chan;
3784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 global->p2p = p2p_init(&p2p);
3786 if (global->p2p == NULL)
3787 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003788 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003789
3790 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3791 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3792 continue;
3793 p2p_add_wps_vendor_extension(
3794 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3795 }
3796
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003797 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 return 0;
3800}
3801
3802
3803/**
3804 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3805 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3806 *
3807 * This function deinitialize per-interface P2P data.
3808 */
3809void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3810{
3811 if (wpa_s->driver && wpa_s->drv_priv)
3812 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003813
3814 if (wpa_s->go_params) {
3815 /* Clear any stored provisioning info */
3816 p2p_clear_provisioning_info(
3817 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003818 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003819 }
3820
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 os_free(wpa_s->go_params);
3822 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3824 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3825 wpa_s->p2p_long_listen = 0;
3826 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3827 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3828 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003829 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003830 wpas_p2p_listen_work_done(wpa_s);
3831 if (wpa_s->p2p_send_action_work) {
3832 os_free(wpa_s->p2p_send_action_work->ctx);
3833 radio_work_done(wpa_s->p2p_send_action_work);
3834 wpa_s->p2p_send_action_work = NULL;
3835 }
3836 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837
3838 /* TODO: remove group interface from the driver if this wpa_s instance
3839 * is on top of a P2P group interface */
3840}
3841
3842
3843/**
3844 * wpas_p2p_deinit_global - Deinitialize global P2P module
3845 * @global: Pointer to global data from wpa_supplicant_init()
3846 *
3847 * This function deinitializes the global (per device) P2P module.
3848 */
3849void wpas_p2p_deinit_global(struct wpa_global *global)
3850{
3851 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003853 wpa_s = global->ifaces;
3854 if (wpa_s)
3855 wpas_p2p_service_flush(wpa_s);
3856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003857 if (global->p2p == NULL)
3858 return;
3859
3860 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3862 wpa_s = wpa_s->next;
3863 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864 tmp = global->ifaces;
3865 while (tmp &&
3866 (tmp == wpa_s ||
3867 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3868 tmp = tmp->next;
3869 }
3870 if (tmp == NULL)
3871 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003872 /* Disconnect from the P2P group and deinit the interface */
3873 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003874 }
3875
3876 /*
3877 * Deinit GO data on any possibly remaining interface (if main
3878 * interface is used as GO).
3879 */
3880 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3881 if (wpa_s->ap_iface)
3882 wpas_p2p_group_deinit(wpa_s);
3883 }
3884
3885 p2p_deinit(global->p2p);
3886 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003887 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888}
3889
3890
3891static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3892{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003893 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3894 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003895 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003896 if (wpa_s->drv_flags &
3897 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3898 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3899 return 1; /* P2P group requires a new interface in every case
3900 */
3901 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3902 return 0; /* driver does not support concurrent operations */
3903 if (wpa_s->global->ifaces->next)
3904 return 1; /* more that one interface already in use */
3905 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3906 return 1; /* this interface is already in use */
3907 return 0;
3908}
3909
3910
3911static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3912 const u8 *peer_addr,
3913 enum p2p_wps_method wps_method,
3914 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003915 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003916 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003917{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003918 if (persistent_group && wpa_s->conf->persistent_reconnect)
3919 persistent_group = 2;
3920
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003921 /*
3922 * Increase GO config timeout if HT40 is used since it takes some time
3923 * to scan channels for coex purposes before the BSS can be started.
3924 */
3925 p2p_set_config_timeout(wpa_s->global->p2p,
3926 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3929 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003930 persistent_group, ssid ? ssid->ssid : NULL,
3931 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003932 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003933}
3934
3935
3936static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3937 const u8 *peer_addr,
3938 enum p2p_wps_method wps_method,
3939 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003940 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003941 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003943 if (persistent_group && wpa_s->conf->persistent_reconnect)
3944 persistent_group = 2;
3945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003946 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3947 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003948 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003949 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003950}
3951
3952
3953static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3954{
3955 wpa_s->p2p_join_scan_count++;
3956 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3957 wpa_s->p2p_join_scan_count);
3958 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3959 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3960 " for join operationg - stop join attempt",
3961 MAC2STR(wpa_s->pending_join_iface_addr));
3962 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003963 if (wpa_s->p2p_auto_pd) {
3964 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003965 wpa_msg_global(wpa_s, MSG_INFO,
3966 P2P_EVENT_PROV_DISC_FAILURE
3967 " p2p_dev_addr=" MACSTR " status=N/A",
3968 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003969 return;
3970 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003971 wpa_msg_global(wpa_s->parent, MSG_INFO,
3972 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003973 }
3974}
3975
3976
Dmitry Shmidt04949592012-07-19 12:16:46 -07003977static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3978{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003979 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003980
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003981 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3982 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003983 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003984 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003985
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003986 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3987 if (!freqs)
3988 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003989
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003990 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3991 wpa_s->num_multichan_concurrent);
3992 if (num < 0) {
3993 res = 1;
3994 goto exit_free;
3995 }
3996
3997 for (i = 0; i < num; i++) {
3998 if (freqs[i] == freq) {
3999 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4000 freq);
4001 res = 0;
4002 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004003 }
4004 }
4005
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004006 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004007
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004008exit_free:
4009 os_free(freqs);
4010 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004011}
4012
4013
4014static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4015 const u8 *peer_dev_addr)
4016{
4017 struct wpa_bss *bss;
4018 int updated;
4019
4020 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4021 if (bss == NULL)
4022 return -1;
4023 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4024 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4025 "last scan");
4026 return 0;
4027 }
4028
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004029 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4030 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004031 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4032 "%ld.%06ld (%supdated in last scan)",
4033 bss->last_update.sec, bss->last_update.usec,
4034 updated ? "": "not ");
4035
4036 return updated;
4037}
4038
4039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004040static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4041 struct wpa_scan_results *scan_res)
4042{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004043 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 int freq;
4045 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004047 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4048
4049 if (wpa_s->global->p2p_disabled)
4050 return;
4051
Dmitry Shmidt04949592012-07-19 12:16:46 -07004052 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4053 scan_res ? (int) scan_res->num : -1,
4054 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055
4056 if (scan_res)
4057 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4058
Dmitry Shmidt04949592012-07-19 12:16:46 -07004059 if (wpa_s->p2p_auto_pd) {
4060 int join = wpas_p2p_peer_go(wpa_s,
4061 wpa_s->pending_join_dev_addr);
4062 if (join == 0 &&
4063 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4064 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004065 bss = wpa_bss_get_bssid_latest(
4066 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004067 if (bss) {
4068 freq = bss->freq;
4069 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4070 "the peer " MACSTR " at %d MHz",
4071 wpa_s->auto_pd_scan_retry,
4072 MAC2STR(wpa_s->
4073 pending_join_dev_addr),
4074 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004075 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004076 return;
4077 }
4078 }
4079
4080 if (join < 0)
4081 join = 0;
4082
4083 wpa_s->p2p_auto_pd = 0;
4084 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4085 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4086 MAC2STR(wpa_s->pending_join_dev_addr), join);
4087 if (p2p_prov_disc_req(wpa_s->global->p2p,
4088 wpa_s->pending_join_dev_addr,
4089 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004090 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004091 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004092 wpa_msg_global(wpa_s, MSG_INFO,
4093 P2P_EVENT_PROV_DISC_FAILURE
4094 " p2p_dev_addr=" MACSTR " status=N/A",
4095 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004096 }
4097 return;
4098 }
4099
4100 if (wpa_s->p2p_auto_join) {
4101 int join = wpas_p2p_peer_go(wpa_s,
4102 wpa_s->pending_join_dev_addr);
4103 if (join < 0) {
4104 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4105 "running a GO -> use GO Negotiation");
4106 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4107 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4108 wpa_s->p2p_persistent_group, 0, 0, 0,
4109 wpa_s->p2p_go_intent,
4110 wpa_s->p2p_connect_freq,
4111 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004112 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004113 wpa_s->p2p_go_ht40,
4114 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004115 return;
4116 }
4117
4118 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4119 "try to join the group", join ? "" :
4120 " in older scan");
4121 if (!join)
4122 wpa_s->p2p_fallback_to_go_neg = 1;
4123 }
4124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4126 wpa_s->pending_join_iface_addr);
4127 if (freq < 0 &&
4128 p2p_get_interface_addr(wpa_s->global->p2p,
4129 wpa_s->pending_join_dev_addr,
4130 iface_addr) == 0 &&
4131 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4132 {
4133 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4134 "address for join from " MACSTR " to " MACSTR
4135 " based on newly discovered P2P peer entry",
4136 MAC2STR(wpa_s->pending_join_iface_addr),
4137 MAC2STR(iface_addr));
4138 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4139 ETH_ALEN);
4140
4141 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4142 wpa_s->pending_join_iface_addr);
4143 }
4144 if (freq >= 0) {
4145 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4146 "from P2P peer table: %d MHz", freq);
4147 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004148 if (wpa_s->p2p_join_ssid_len) {
4149 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4150 MACSTR " and SSID %s",
4151 MAC2STR(wpa_s->pending_join_iface_addr),
4152 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4153 wpa_s->p2p_join_ssid_len));
4154 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4155 wpa_s->p2p_join_ssid,
4156 wpa_s->p2p_join_ssid_len);
4157 }
4158 if (!bss) {
4159 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4160 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4161 bss = wpa_bss_get_bssid_latest(wpa_s,
4162 wpa_s->pending_join_iface_addr);
4163 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004164 if (bss) {
4165 freq = bss->freq;
4166 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004167 "from BSS table: %d MHz (SSID %s)", freq,
4168 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 }
4170 if (freq > 0) {
4171 u16 method;
4172
Dmitry Shmidt04949592012-07-19 12:16:46 -07004173 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004174 wpa_msg_global(wpa_s->parent, MSG_INFO,
4175 P2P_EVENT_GROUP_FORMATION_FAILURE
4176 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004177 return;
4178 }
4179
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004180 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4181 "prior to joining an existing group (GO " MACSTR
4182 " freq=%u MHz)",
4183 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4184 wpa_s->pending_pd_before_join = 1;
4185
4186 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004187 case WPS_PIN_DISPLAY:
4188 method = WPS_CONFIG_KEYPAD;
4189 break;
4190 case WPS_PIN_KEYPAD:
4191 method = WPS_CONFIG_DISPLAY;
4192 break;
4193 case WPS_PBC:
4194 method = WPS_CONFIG_PUSHBUTTON;
4195 break;
4196 default:
4197 method = 0;
4198 break;
4199 }
4200
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004201 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4202 wpa_s->pending_join_dev_addr) ==
4203 method)) {
4204 /*
4205 * We have already performed provision discovery for
4206 * joining the group. Proceed directly to join
4207 * operation without duplicated provision discovery. */
4208 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4209 "with " MACSTR " already done - proceed to "
4210 "join",
4211 MAC2STR(wpa_s->pending_join_dev_addr));
4212 wpa_s->pending_pd_before_join = 0;
4213 goto start;
4214 }
4215
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004216 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004217 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004218 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4220 "Discovery Request before joining an "
4221 "existing group");
4222 wpa_s->pending_pd_before_join = 0;
4223 goto start;
4224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004225 return;
4226 }
4227
4228 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4229 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4230 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4231 wpas_p2p_check_join_scan_limit(wpa_s);
4232 return;
4233
4234start:
4235 /* Start join operation immediately */
4236 wpas_p2p_join_start(wpa_s);
4237}
4238
4239
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004240static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4241 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004242{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004243 int ret;
4244 struct wpa_driver_scan_params params;
4245 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004246 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004247 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004248#ifdef ANDROID_P2P
4249 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004250
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004251 /* If freq is not provided, check the operating freq of the GO and do a
4252 * a directed scan to save time
4253 */
4254 if(!freq) {
4255 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4256 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
4257 }
4258#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 os_memset(&params, 0, sizeof(params));
4260
4261 /* P2P Wildcard SSID */
4262 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004263 if (ssid && ssid_len) {
4264 params.ssids[0].ssid = ssid;
4265 params.ssids[0].ssid_len = ssid_len;
4266 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4267 wpa_s->p2p_join_ssid_len = ssid_len;
4268 } else {
4269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4271 wpa_s->p2p_join_ssid_len = 0;
4272 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004273
4274 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004275 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4276 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4277 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004278 if (wps_ie == NULL) {
4279 wpas_p2p_scan_res_join(wpa_s, NULL);
4280 return;
4281 }
4282
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004283 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4284 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285 if (ies == NULL) {
4286 wpabuf_free(wps_ie);
4287 wpas_p2p_scan_res_join(wpa_s, NULL);
4288 return;
4289 }
4290 wpabuf_put_buf(ies, wps_ie);
4291 wpabuf_free(wps_ie);
4292
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004293 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004294
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004295 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004296 params.extra_ies = wpabuf_head(ies);
4297 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004298 if (freq > 0) {
4299 freqs[0] = freq;
4300 params.freqs = freqs;
4301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004302
4303 /*
4304 * Run a scan to update BSS table and start Provision Discovery once
4305 * the new scan results become available.
4306 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004307 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004308 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004309 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004310 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004311 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004312 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004313
4314 wpabuf_free(ies);
4315
4316 if (ret) {
4317 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4318 "try again later");
4319 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4320 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4321 wpas_p2p_check_join_scan_limit(wpa_s);
4322 }
4323}
4324
4325
Dmitry Shmidt04949592012-07-19 12:16:46 -07004326static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4327{
4328 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004329 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004330}
4331
4332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004333static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004334 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004335 int auto_join, const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336{
4337 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004338 MACSTR " dev " MACSTR ")%s",
4339 MAC2STR(iface_addr), MAC2STR(dev_addr),
4340 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004341 if (ssid && ssid_len) {
4342 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4343 wpa_ssid_txt(ssid, ssid_len));
4344 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004345
Dmitry Shmidt04949592012-07-19 12:16:46 -07004346 wpa_s->p2p_auto_pd = 0;
4347 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4349 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4350 wpa_s->pending_join_wps_method = wps_method;
4351
4352 /* Make sure we are not running find during connection establishment */
4353 wpas_p2p_stop_find(wpa_s);
4354
4355 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004356 wpas_p2p_join_scan_req(wpa_s, 0, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004357 return 0;
4358}
4359
4360
4361static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4362{
4363 struct wpa_supplicant *group;
4364 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004365 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004366
4367 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4368 if (group == NULL)
4369 return -1;
4370 if (group != wpa_s) {
4371 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4372 sizeof(group->p2p_pin));
4373 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004374 } else {
4375 /*
4376 * Need to mark the current interface for p2p_group_formation
4377 * when a separate group interface is not used. This is needed
4378 * to allow p2p_cancel stop a pending p2p_connect-join.
4379 * wpas_p2p_init_group_interface() addresses this for the case
4380 * where a separate group interface is used.
4381 */
4382 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383 }
4384
4385 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004386 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004387
4388 os_memset(&res, 0, sizeof(res));
4389 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4390 ETH_ALEN);
4391 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004392 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004393 if (bss) {
4394 res.freq = bss->freq;
4395 res.ssid_len = bss->ssid_len;
4396 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004397 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4398 "from BSS table: %d MHz (SSID %s)", bss->freq,
4399 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004400 }
4401
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004402 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4403 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4404 "starting client");
4405 wpa_drv_cancel_remain_on_channel(wpa_s);
4406 wpa_s->off_channel_freq = 0;
4407 wpa_s->roc_waiting_drv_freq = 0;
4408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 wpas_start_wps_enrollee(group, &res);
4410
4411 /*
4412 * Allow a longer timeout for join-a-running-group than normal 15
4413 * second group formation timeout since the GO may not have authorized
4414 * our connection yet.
4415 */
4416 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4417 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4418 wpa_s, NULL);
4419
4420 return 0;
4421}
4422
4423
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004424static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004425 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004426{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004427 int *freqs, res;
4428 unsigned int freq_in_use = 0, num, i;
4429
4430 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4431 if (!freqs)
4432 return -1;
4433
4434 num = get_shared_radio_freqs(wpa_s, freqs,
4435 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004436 wpa_printf(MSG_DEBUG,
4437 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4438 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004439
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004440 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004441 int ret;
4442 if (go)
4443 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4444 else
4445 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4446 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004447 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4448 "(%u MHz) is not supported for P2P uses",
4449 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004450 res = -3;
4451 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004452 }
4453
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004454 for (i = 0; i < num; i++) {
4455 if (freqs[i] == freq)
4456 freq_in_use = 1;
4457 }
4458
4459 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4460 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4461 freq);
4462 res = -2;
4463 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004464 }
4465 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4466 "requested channel (%u MHz)", freq);
4467 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004468 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004469 }
4470
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004471 for (i = 0; i < num; i++) {
4472 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4473 continue;
4474
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004475 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004476 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4477 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004478 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004479 } else {
4480 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 -08004481 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004482 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004483 }
4484 break;
4485 }
4486
4487 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004488 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004489 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4490 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004491 } else if (num < wpa_s->num_multichan_concurrent) {
4492 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4493 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004494 } else {
4495 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4496 res = -2;
4497 goto exit_free;
4498 }
4499 }
4500
4501exit_ok:
4502 res = 0;
4503exit_free:
4504 os_free(freqs);
4505 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004506}
4507
4508
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004509/**
4510 * wpas_p2p_connect - Request P2P Group Formation to be started
4511 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4512 * @peer_addr: Address of the peer P2P Device
4513 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4514 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004515 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004516 * @join: Whether to join an existing group (as a client) instead of starting
4517 * Group Owner negotiation; @peer_addr is BSSID in that case
4518 * @auth: Whether to only authorize the connection instead of doing that and
4519 * initiating Group Owner negotiation
4520 * @go_intent: GO Intent or -1 to use default
4521 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004522 * @persistent_id: Persistent group credentials to use for forcing GO
4523 * parameters or -1 to generate new values (SSID/passphrase)
4524 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4525 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004526 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004527 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4529 * failure, -2 on failure due to channel not currently available,
4530 * -3 if forced channel is not supported
4531 */
4532int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4533 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004534 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004535 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004536 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004537{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004538 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004539 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004540 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004541 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004542 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543
4544 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4545 return -1;
4546
Dmitry Shmidt04949592012-07-19 12:16:46 -07004547 if (persistent_id >= 0) {
4548 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4549 if (ssid == NULL || ssid->disabled != 2 ||
4550 ssid->mode != WPAS_MODE_P2P_GO)
4551 return -1;
4552 }
4553
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004554 os_free(wpa_s->global->add_psk);
4555 wpa_s->global->add_psk = NULL;
4556
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004557 wpa_s->global->p2p_fail_on_wps_complete = 0;
4558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559 if (go_intent < 0)
4560 go_intent = wpa_s->conf->p2p_go_intent;
4561
4562 if (!auth)
4563 wpa_s->p2p_long_listen = 0;
4564
4565 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004566 wpa_s->p2p_persistent_group = !!persistent_group;
4567 wpa_s->p2p_persistent_id = persistent_id;
4568 wpa_s->p2p_go_intent = go_intent;
4569 wpa_s->p2p_connect_freq = freq;
4570 wpa_s->p2p_fallback_to_go_neg = 0;
4571 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004572 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004573 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574
4575 if (pin)
4576 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4577 else if (wps_method == WPS_PIN_DISPLAY) {
4578 ret = wps_generate_pin();
4579 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4580 ret);
4581 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4582 wpa_s->p2p_pin);
4583 } else
4584 wpa_s->p2p_pin[0] = '\0';
4585
Dmitry Shmidt04949592012-07-19 12:16:46 -07004586 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004587 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4588 if (auth) {
4589 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4590 "connect a running group from " MACSTR,
4591 MAC2STR(peer_addr));
4592 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4593 return ret;
4594 }
4595 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4596 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4597 iface_addr) < 0) {
4598 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4599 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4600 dev_addr);
4601 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004602 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004603 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004604 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4605 "%ld.%06ld",
4606 wpa_s->p2p_auto_started.sec,
4607 wpa_s->p2p_auto_started.usec);
4608 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004609 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004610 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004611 auto_join, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 return -1;
4613 return ret;
4614 }
4615
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004616 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4617 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004618 if (res)
4619 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004620 wpas_p2p_set_own_freq_preference(wpa_s,
4621 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004622
4623 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4624
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004625 if (wpa_s->create_p2p_iface) {
4626 /* Prepare to add a new interface for the group */
4627 iftype = WPA_IF_P2P_GROUP;
4628 if (go_intent == 15)
4629 iftype = WPA_IF_P2P_GO;
4630 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4631 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4632 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004635
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004636 if_addr = wpa_s->pending_interface_addr;
4637 } else
4638 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639
4640 if (auth) {
4641 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004642 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004643 force_freq, persistent_group, ssid,
4644 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004645 return -1;
4646 return ret;
4647 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004648
4649 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4650 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004651 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004652 if (wpa_s->create_p2p_iface)
4653 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004654 return -1;
4655 }
4656 return ret;
4657}
4658
4659
4660/**
4661 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4662 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4663 * @freq: Frequency of the channel in MHz
4664 * @duration: Duration of the stay on the channel in milliseconds
4665 *
4666 * This callback is called when the driver indicates that it has started the
4667 * requested remain-on-channel duration.
4668 */
4669void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4670 unsigned int freq, unsigned int duration)
4671{
4672 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4673 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004674 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4675 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4676 wpa_s->pending_listen_duration);
4677 wpa_s->pending_listen_freq = 0;
4678 }
4679}
4680
4681
4682static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4683 unsigned int timeout)
4684{
4685 /* Limit maximum Listen state time based on driver limitation. */
4686 if (timeout > wpa_s->max_remain_on_chan)
4687 timeout = wpa_s->max_remain_on_chan;
4688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004689 return p2p_listen(wpa_s->global->p2p, timeout);
4690}
4691
4692
4693/**
4694 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4695 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4696 * @freq: Frequency of the channel in MHz
4697 *
4698 * This callback is called when the driver indicates that a remain-on-channel
4699 * operation has been completed, i.e., the duration on the requested channel
4700 * has timed out.
4701 */
4702void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4703 unsigned int freq)
4704{
4705 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4706 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004707 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004708 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004709 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4710 return;
4711 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4712 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004713 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714 return;
4715 if (wpa_s->p2p_long_listen > 0)
4716 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4717 if (wpa_s->p2p_long_listen > 0) {
4718 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4719 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004720 } else {
4721 /*
4722 * When listen duration is over, stop listen & update p2p_state
4723 * to IDLE.
4724 */
4725 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004726 }
4727}
4728
4729
4730/**
4731 * wpas_p2p_group_remove - Remove a P2P group
4732 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4733 * @ifname: Network interface name of the group interface or "*" to remove all
4734 * groups
4735 * Returns: 0 on success, -1 on failure
4736 *
4737 * This function is used to remove a P2P group. This can be used to disconnect
4738 * from a group in which the local end is a P2P Client or to end a P2P Group in
4739 * case the local end is the Group Owner. If a virtual network interface was
4740 * created for this group, that interface will be removed. Otherwise, only the
4741 * configured P2P group network will be removed from the interface.
4742 */
4743int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4744{
4745 struct wpa_global *global = wpa_s->global;
4746
4747 if (os_strcmp(ifname, "*") == 0) {
4748 struct wpa_supplicant *prev;
4749 wpa_s = global->ifaces;
4750 while (wpa_s) {
4751 prev = wpa_s;
4752 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004753 if (prev->p2p_group_interface !=
4754 NOT_P2P_GROUP_INTERFACE ||
4755 (prev->current_ssid &&
4756 prev->current_ssid->p2p_group))
4757 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 }
4759 return 0;
4760 }
4761
4762 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4763 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4764 break;
4765 }
4766
4767 return wpas_p2p_disconnect(wpa_s);
4768}
4769
4770
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004771static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4772{
4773 unsigned int r;
4774
4775 if (freq == 2) {
4776 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4777 "band");
4778 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004779 p2p_supported_freq_go(wpa_s->global->p2p,
4780 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004781 freq = wpa_s->best_24_freq;
4782 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4783 "channel: %d MHz", freq);
4784 } else {
4785 os_get_random((u8 *) &r, sizeof(r));
4786 freq = 2412 + (r % 3) * 25;
4787 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4788 "channel: %d MHz", freq);
4789 }
4790 }
4791
4792 if (freq == 5) {
4793 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4794 "band");
4795 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004796 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004797 wpa_s->best_5_freq)) {
4798 freq = wpa_s->best_5_freq;
4799 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4800 "channel: %d MHz", freq);
4801 } else {
4802 os_get_random((u8 *) &r, sizeof(r));
4803 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004804 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004805 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4806 "5 GHz channel for P2P group");
4807 return -1;
4808 }
4809 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4810 "channel: %d MHz", freq);
4811 }
4812 }
4813
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004814 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004815 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4816 "(%u MHz) is not supported for P2P uses",
4817 freq);
4818 return -1;
4819 }
4820
4821 return freq;
4822}
4823
4824
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004825static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4826 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004827 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004828 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004829{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004830 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004831 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004832 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004833
4834 os_memset(params, 0, sizeof(*params));
4835 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004836 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004837 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004838 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004839 if (!freq_included(channels, freq)) {
4840 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4841 "accepted", freq);
4842 return -1;
4843 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004844 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4845 "frequency %d MHz", freq);
4846 params->freq = freq;
4847 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4848 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004849 wpa_s->conf->p2p_oper_channel <= 11 &&
4850 freq_included(channels,
4851 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004852 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4853 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4854 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004855 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4856 wpa_s->conf->p2p_oper_reg_class == 116 ||
4857 wpa_s->conf->p2p_oper_reg_class == 117 ||
4858 wpa_s->conf->p2p_oper_reg_class == 124 ||
4859 wpa_s->conf->p2p_oper_reg_class == 126 ||
4860 wpa_s->conf->p2p_oper_reg_class == 127) &&
4861 freq_included(channels,
4862 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004863 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4864 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4865 "frequency %d MHz", params->freq);
4866 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4867 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004868 p2p_supported_freq_go(wpa_s->global->p2p,
4869 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004870 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004871 params->freq = wpa_s->best_overall_freq;
4872 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4873 "channel %d MHz", params->freq);
4874 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4875 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004876 p2p_supported_freq_go(wpa_s->global->p2p,
4877 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004878 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879 params->freq = wpa_s->best_24_freq;
4880 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4881 "channel %d MHz", params->freq);
4882 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4883 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004884 p2p_supported_freq_go(wpa_s->global->p2p,
4885 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004886 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004887 params->freq = wpa_s->best_5_freq;
4888 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4889 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004890 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4891 channels))) {
4892 params->freq = pref_freq;
4893 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4894 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004895 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004896 int chan;
4897 for (chan = 0; chan < 11; chan++) {
4898 params->freq = 2412 + chan * 5;
4899 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004900 params->freq) &&
4901 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004902 break;
4903 }
4904 if (chan == 11) {
4905 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4906 "allowed");
4907 return -1;
4908 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004909 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4910 "known)", params->freq);
4911 }
4912
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004913 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4914 if (!freqs)
4915 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004916
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004917 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4918 wpa_s->num_multichan_concurrent);
4919 if (res < 0) {
4920 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004921 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004923 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004924
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004925 for (i = 0; i < num; i++) {
4926 if (freq && freqs[i] == freq)
4927 break;
4928 if (!freq && freq_included(channels, freqs[i])) {
4929 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4930 freqs[i]);
4931 params->freq = freqs[i];
4932 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004933 }
4934 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004935
4936 if (i == num) {
4937 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4938 if (freq)
4939 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4940 else
4941 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4942 os_free(freqs);
4943 return -1;
4944 } else if (num == 0) {
4945 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4946 } else {
4947 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4948 }
4949 }
4950
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004951 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953}
4954
4955
4956static struct wpa_supplicant *
4957wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4958 int go)
4959{
4960 struct wpa_supplicant *group_wpa_s;
4961
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004962 if (!wpas_p2p_create_iface(wpa_s)) {
4963 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4964 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004965 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004967 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004968
4969 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004970 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004971 wpa_msg_global(wpa_s, MSG_ERROR,
4972 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004974 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4976 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004977 wpa_msg_global(wpa_s, MSG_ERROR,
4978 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 wpas_p2p_remove_pending_group_interface(wpa_s);
4980 return NULL;
4981 }
4982
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4984 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004985 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004986 return group_wpa_s;
4987}
4988
4989
4990/**
4991 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4992 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4993 * @persistent_group: Whether to create a persistent group
4994 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004995 * @ht40: Start GO with 40 MHz channel width
4996 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004997 * Returns: 0 on success, -1 on failure
4998 *
4999 * This function creates a new P2P group with the local end as the Group Owner,
5000 * i.e., without using Group Owner Negotiation.
5001 */
5002int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005003 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005004{
5005 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006
5007 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5008 return -1;
5009
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005010 os_free(wpa_s->global->add_psk);
5011 wpa_s->global->add_psk = NULL;
5012
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005013 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005014 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005015 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005016
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005017 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5018 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005019 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005021 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005022 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005023 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005024 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005025 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5026 "(%u MHz) is not supported for P2P uses",
5027 params.freq);
5028 return -1;
5029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 p2p_go_params(wpa_s->global->p2p, &params);
5031 params.persistent_group = persistent_group;
5032
5033 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5034 if (wpa_s == NULL)
5035 return -1;
5036 wpas_start_wps_go(wpa_s, &params, 0);
5037
5038 return 0;
5039}
5040
5041
5042static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5043 struct wpa_ssid *params, int addr_allocated)
5044{
5045 struct wpa_ssid *ssid;
5046
5047 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5048 if (wpa_s == NULL)
5049 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005050 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005051
5052 wpa_supplicant_ap_deinit(wpa_s);
5053
5054 ssid = wpa_config_add_network(wpa_s->conf);
5055 if (ssid == NULL)
5056 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005057 wpa_config_set_network_defaults(ssid);
5058 ssid->temporary = 1;
5059 ssid->proto = WPA_PROTO_RSN;
5060 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5061 ssid->group_cipher = WPA_CIPHER_CCMP;
5062 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5063 ssid->ssid = os_malloc(params->ssid_len);
5064 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065 wpa_config_remove_network(wpa_s->conf, ssid->id);
5066 return -1;
5067 }
5068 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5069 ssid->ssid_len = params->ssid_len;
5070 ssid->p2p_group = 1;
5071 ssid->export_keys = 1;
5072 if (params->psk_set) {
5073 os_memcpy(ssid->psk, params->psk, 32);
5074 ssid->psk_set = 1;
5075 }
5076 if (params->passphrase)
5077 ssid->passphrase = os_strdup(params->passphrase);
5078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 wpa_s->show_group_started = 1;
5080
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005081 wpa_supplicant_select_network(wpa_s, ssid);
5082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083 return 0;
5084}
5085
5086
5087int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5088 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005089 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005090 const struct p2p_channels *channels,
5091 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005092{
5093 struct p2p_go_neg_results params;
5094 int go = 0;
5095
5096 if (ssid->disabled != 2 || ssid->ssid == NULL)
5097 return -1;
5098
5099 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5100 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5101 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5102 "already running");
5103 return 0;
5104 }
5105
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005106 os_free(wpa_s->global->add_psk);
5107 wpa_s->global->add_psk = NULL;
5108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005110 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005111
Dmitry Shmidt04949592012-07-19 12:16:46 -07005112 wpa_s->p2p_fallback_to_go_neg = 0;
5113
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005114 if (ssid->mode == WPAS_MODE_INFRA)
5115 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5116
5117 if (ssid->mode != WPAS_MODE_P2P_GO)
5118 return -1;
5119
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005120 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5121 if (freq < 0)
5122 return -1;
5123
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005124 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005125 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005126
5127 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005128 params.psk_set = ssid->psk_set;
5129 if (params.psk_set)
5130 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005131 if (ssid->passphrase) {
5132 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5133 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5134 "persistent group");
5135 return -1;
5136 }
5137 os_strlcpy(params.passphrase, ssid->passphrase,
5138 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5141 params.ssid_len = ssid->ssid_len;
5142 params.persistent_group = 1;
5143
5144 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5145 if (wpa_s == NULL)
5146 return -1;
5147
Dmitry Shmidt56052862013-10-04 10:23:25 -07005148 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005149 wpas_start_wps_go(wpa_s, &params, 0);
5150
5151 return 0;
5152}
5153
5154
5155static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5156 struct wpabuf *proberesp_ies)
5157{
5158 struct wpa_supplicant *wpa_s = ctx;
5159 if (wpa_s->ap_iface) {
5160 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005161 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5162 wpabuf_free(beacon_ies);
5163 wpabuf_free(proberesp_ies);
5164 return;
5165 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005166 if (beacon_ies) {
5167 wpabuf_free(hapd->p2p_beacon_ie);
5168 hapd->p2p_beacon_ie = beacon_ies;
5169 }
5170 wpabuf_free(hapd->p2p_probe_resp_ie);
5171 hapd->p2p_probe_resp_ie = proberesp_ies;
5172 } else {
5173 wpabuf_free(beacon_ies);
5174 wpabuf_free(proberesp_ies);
5175 }
5176 wpa_supplicant_ap_update_beacon(wpa_s);
5177}
5178
5179
5180static void wpas_p2p_idle_update(void *ctx, int idle)
5181{
5182 struct wpa_supplicant *wpa_s = ctx;
5183 if (!wpa_s->ap_iface)
5184 return;
5185 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005186 if (idle) {
5187 if (wpa_s->global->p2p_fail_on_wps_complete &&
5188 wpa_s->p2p_in_provisioning) {
5189 wpas_p2p_grpform_fail_after_wps(wpa_s);
5190 return;
5191 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005192 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005193 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005194 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5195}
5196
5197
5198struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005199 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200{
5201 struct p2p_group *group;
5202 struct p2p_group_config *cfg;
5203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005204 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5205 return NULL;
5206
5207 cfg = os_zalloc(sizeof(*cfg));
5208 if (cfg == NULL)
5209 return NULL;
5210
Dmitry Shmidt04949592012-07-19 12:16:46 -07005211 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005212 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005213 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005214 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005215 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5216 if (wpa_s->max_stations &&
5217 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5218 cfg->max_clients = wpa_s->max_stations;
5219 else
5220 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005221 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5222 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223 cfg->cb_ctx = wpa_s;
5224 cfg->ie_update = wpas_p2p_ie_update;
5225 cfg->idle_update = wpas_p2p_idle_update;
5226
5227 group = p2p_group_init(wpa_s->global->p2p, cfg);
5228 if (group == NULL)
5229 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005230 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005231 p2p_group_notif_formation_done(group);
5232 wpa_s->p2p_group = group;
5233 return group;
5234}
5235
5236
5237void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5238 int registrar)
5239{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005240 struct wpa_ssid *ssid = wpa_s->current_ssid;
5241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005242 if (!wpa_s->p2p_in_provisioning) {
5243 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5244 "provisioning not in progress");
5245 return;
5246 }
5247
Dmitry Shmidt04949592012-07-19 12:16:46 -07005248 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5249 u8 go_dev_addr[ETH_ALEN];
5250 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5251 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5252 ssid->ssid_len);
5253 /* Clear any stored provisioning info */
5254 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5255 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005257 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5258 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005259 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005260 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5261 /*
5262 * Use a separate timeout for initial data connection to
5263 * complete to allow the group to be removed automatically if
5264 * something goes wrong in this step before the P2P group idle
5265 * timeout mechanism is taken into use.
5266 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005267 wpa_dbg(wpa_s, MSG_DEBUG,
5268 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5269 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005270 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5271 wpas_p2p_group_formation_timeout,
5272 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005273 } else if (ssid) {
5274 /*
5275 * Use a separate timeout for initial data connection to
5276 * complete to allow the group to be removed automatically if
5277 * the client does not complete data connection successfully.
5278 */
5279 wpa_dbg(wpa_s, MSG_DEBUG,
5280 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5281 P2P_MAX_INITIAL_CONN_WAIT_GO);
5282 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5283 wpas_p2p_group_formation_timeout,
5284 wpa_s->parent, NULL);
5285 /*
5286 * Complete group formation on first successful data connection
5287 */
5288 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290 if (wpa_s->global->p2p)
5291 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292 wpas_group_formation_completed(wpa_s, 1);
5293}
5294
5295
Jouni Malinen75ecf522011-06-27 15:19:46 -07005296void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5297 struct wps_event_fail *fail)
5298{
5299 if (!wpa_s->p2p_in_provisioning) {
5300 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5301 "provisioning not in progress");
5302 return;
5303 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005304
5305 if (wpa_s->go_params) {
5306 p2p_clear_provisioning_info(
5307 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005308 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005309 }
5310
Jouni Malinen75ecf522011-06-27 15:19:46 -07005311 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005312
5313 if (wpa_s == wpa_s->global->p2p_group_formation) {
5314 /*
5315 * Allow some time for the failed WPS negotiation exchange to
5316 * complete, but remove the group since group formation cannot
5317 * succeed after provisioning failure.
5318 */
5319 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5320 wpa_s->global->p2p_fail_on_wps_complete = 1;
5321 eloop_deplete_timeout(0, 50000,
5322 wpas_p2p_group_formation_timeout,
5323 wpa_s->parent, NULL);
5324 }
5325}
5326
5327
5328int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5329{
5330 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5331 !wpa_s->p2p_in_provisioning)
5332 return 0;
5333
5334 wpas_p2p_grpform_fail_after_wps(wpa_s);
5335
5336 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005337}
5338
5339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005340int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005341 const char *config_method,
5342 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005343{
5344 u16 config_methods;
5345
Dmitry Shmidt04949592012-07-19 12:16:46 -07005346 wpa_s->p2p_fallback_to_go_neg = 0;
5347 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005348 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005350 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005351 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005352 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5353 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005354 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005355 else {
5356 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005357 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005358 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005359
Dmitry Shmidt04949592012-07-19 12:16:46 -07005360 if (use == WPAS_P2P_PD_AUTO) {
5361 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5362 wpa_s->pending_pd_config_methods = config_methods;
5363 wpa_s->p2p_auto_pd = 1;
5364 wpa_s->p2p_auto_join = 0;
5365 wpa_s->pending_pd_before_join = 0;
5366 wpa_s->auto_pd_scan_retry = 0;
5367 wpas_p2p_stop_find(wpa_s);
5368 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005369 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005370 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5371 wpa_s->p2p_auto_started.sec,
5372 wpa_s->p2p_auto_started.usec);
5373 wpas_p2p_join_scan(wpa_s, NULL);
5374 return 0;
5375 }
5376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5378 return -1;
5379
5380 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005381 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005382 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383}
5384
5385
5386int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5387 char *end)
5388{
5389 return p2p_scan_result_text(ies, ies_len, buf, end);
5390}
5391
5392
5393static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5394{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005395 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005396 return;
5397
5398 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5399 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005400 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005401}
5402
5403
5404int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5405 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005406 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005407 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005408{
5409 wpas_p2p_clear_pending_action_tx(wpa_s);
5410 wpa_s->p2p_long_listen = 0;
5411
Dmitry Shmidt04949592012-07-19 12:16:46 -07005412 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5413 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005414 return -1;
5415
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005416 wpa_supplicant_cancel_sched_scan(wpa_s);
5417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005418 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005419 num_req_dev_types, req_dev_types, dev_id,
5420 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005421}
5422
5423
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005424static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425{
5426 wpas_p2p_clear_pending_action_tx(wpa_s);
5427 wpa_s->p2p_long_listen = 0;
5428 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5429 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430
5431 if (wpa_s->global->p2p)
5432 p2p_stop_find(wpa_s->global->p2p);
5433
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005434 return 0;
5435}
5436
5437
5438void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5439{
5440 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5441 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005442 wpas_p2p_remove_pending_group_interface(wpa_s);
5443}
5444
5445
5446static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5447{
5448 struct wpa_supplicant *wpa_s = eloop_ctx;
5449 wpa_s->p2p_long_listen = 0;
5450}
5451
5452
5453int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5454{
5455 int res;
5456
5457 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5458 return -1;
5459
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005460 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 wpas_p2p_clear_pending_action_tx(wpa_s);
5462
5463 if (timeout == 0) {
5464 /*
5465 * This is a request for unlimited Listen state. However, at
5466 * least for now, this is mapped to a Listen state for one
5467 * hour.
5468 */
5469 timeout = 3600;
5470 }
5471 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5472 wpa_s->p2p_long_listen = 0;
5473
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005474 /*
5475 * Stop previous find/listen operation to avoid trying to request a new
5476 * remain-on-channel operation while the driver is still running the
5477 * previous one.
5478 */
5479 if (wpa_s->global->p2p)
5480 p2p_stop_find(wpa_s->global->p2p);
5481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5483 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5484 wpa_s->p2p_long_listen = timeout * 1000;
5485 eloop_register_timeout(timeout, 0,
5486 wpas_p2p_long_listen_timeout,
5487 wpa_s, NULL);
5488 }
5489
5490 return res;
5491}
5492
5493
5494int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5495 u8 *buf, size_t len, int p2p_group)
5496{
5497 struct wpabuf *p2p_ie;
5498 int ret;
5499
5500 if (wpa_s->global->p2p_disabled)
5501 return -1;
5502 if (wpa_s->global->p2p == NULL)
5503 return -1;
5504 if (bss == NULL)
5505 return -1;
5506
5507 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5508 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5509 p2p_group, p2p_ie);
5510 wpabuf_free(p2p_ie);
5511
5512 return ret;
5513}
5514
5515
5516int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005517 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005518 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519{
5520 if (wpa_s->global->p2p_disabled)
5521 return 0;
5522 if (wpa_s->global->p2p == NULL)
5523 return 0;
5524
Dmitry Shmidt04949592012-07-19 12:16:46 -07005525 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5526 ie, ie_len)) {
5527 case P2P_PREQ_NOT_P2P:
5528 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5529 ssi_signal);
5530 /* fall through */
5531 case P2P_PREQ_MALFORMED:
5532 case P2P_PREQ_NOT_LISTEN:
5533 case P2P_PREQ_NOT_PROCESSED:
5534 default: /* make gcc happy */
5535 return 0;
5536 case P2P_PREQ_PROCESSED:
5537 return 1;
5538 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005539}
5540
5541
5542void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5543 const u8 *sa, const u8 *bssid,
5544 u8 category, const u8 *data, size_t len, int freq)
5545{
5546 if (wpa_s->global->p2p_disabled)
5547 return;
5548 if (wpa_s->global->p2p == NULL)
5549 return;
5550
5551 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5552 freq);
5553}
5554
5555
5556void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5557{
5558 if (wpa_s->global->p2p_disabled)
5559 return;
5560 if (wpa_s->global->p2p == NULL)
5561 return;
5562
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005563 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005564}
5565
5566
5567void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5568{
5569 p2p_group_deinit(wpa_s->p2p_group);
5570 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005571
5572 wpa_s->ap_configured_cb = NULL;
5573 wpa_s->ap_configured_cb_ctx = NULL;
5574 wpa_s->ap_configured_cb_data = NULL;
5575 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576}
5577
5578
5579int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5580{
5581 wpa_s->p2p_long_listen = 0;
5582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005583 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5584 return -1;
5585
5586 return p2p_reject(wpa_s->global->p2p, addr);
5587}
5588
5589
5590/* Invite to reinvoke a persistent group */
5591int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005592 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005593 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594{
5595 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005596 u8 *bssid = NULL;
5597 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005598 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005599 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005600
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005601 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005602 if (peer_addr)
5603 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5604 else
5605 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005606
Jouni Malinen31be0a42012-08-31 21:20:51 +03005607 wpa_s->p2p_persistent_go_freq = freq;
5608 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005609 if (ssid->mode == WPAS_MODE_P2P_GO) {
5610 role = P2P_INVITE_ROLE_GO;
5611 if (peer_addr == NULL) {
5612 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5613 "address in invitation command");
5614 return -1;
5615 }
5616 if (wpas_p2p_create_iface(wpa_s)) {
5617 if (wpas_p2p_add_group_interface(wpa_s,
5618 WPA_IF_P2P_GO) < 0) {
5619 wpa_printf(MSG_ERROR, "P2P: Failed to "
5620 "allocate a new interface for the "
5621 "group");
5622 return -1;
5623 }
5624 bssid = wpa_s->pending_interface_addr;
5625 } else
5626 bssid = wpa_s->own_addr;
5627 } else {
5628 role = P2P_INVITE_ROLE_CLIENT;
5629 peer_addr = ssid->bssid;
5630 }
5631 wpa_s->pending_invite_ssid_id = ssid->id;
5632
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005633 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5634 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005635 if (res)
5636 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005637
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005638 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5639 return -1;
5640
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005641 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5642 no_pref_freq_given && pref_freq > 0 &&
5643 wpa_s->num_multichan_concurrent > 1 &&
5644 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5645 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5646 pref_freq);
5647 pref_freq = 0;
5648 }
5649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005651 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5652 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005653}
5654
5655
5656/* Invite to join an active group */
5657int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5658 const u8 *peer_addr, const u8 *go_dev_addr)
5659{
5660 struct wpa_global *global = wpa_s->global;
5661 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005662 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005663 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005664 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005665 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005666 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005667
Jouni Malinen31be0a42012-08-31 21:20:51 +03005668 wpa_s->p2p_persistent_go_freq = 0;
5669 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005670 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005672 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5673 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5674 break;
5675 }
5676 if (wpa_s == NULL) {
5677 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5678 return -1;
5679 }
5680
5681 ssid = wpa_s->current_ssid;
5682 if (ssid == NULL) {
5683 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5684 "invitation");
5685 return -1;
5686 }
5687
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005688 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005689 persistent = ssid->p2p_persistent_group &&
5690 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5691 ssid->ssid, ssid->ssid_len);
5692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693 if (ssid->mode == WPAS_MODE_P2P_GO) {
5694 role = P2P_INVITE_ROLE_ACTIVE_GO;
5695 bssid = wpa_s->own_addr;
5696 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005697 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005698 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699 } else {
5700 role = P2P_INVITE_ROLE_CLIENT;
5701 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5702 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5703 "invite to current group");
5704 return -1;
5705 }
5706 bssid = wpa_s->bssid;
5707 if (go_dev_addr == NULL &&
5708 !is_zero_ether_addr(wpa_s->go_dev_addr))
5709 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005710 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5711 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005712 }
5713 wpa_s->parent->pending_invite_ssid_id = -1;
5714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5716 return -1;
5717
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005718 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5719 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005720 if (res)
5721 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005722 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005725 ssid->ssid, ssid->ssid_len, force_freq,
5726 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005727}
5728
5729
5730void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5731{
5732 struct wpa_ssid *ssid = wpa_s->current_ssid;
5733 const char *ssid_txt;
5734 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005735 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005736 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005737 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005738
Dmitry Shmidt04949592012-07-19 12:16:46 -07005739 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5740 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5741 wpa_s->parent, NULL);
5742 }
5743
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005744 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005745 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005746
5747 wpa_s->show_group_started = 0;
5748
5749 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5750 os_memset(go_dev_addr, 0, ETH_ALEN);
5751 if (ssid->bssid_set)
5752 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5753 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5754 ssid->ssid_len);
5755 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5756
5757 if (wpa_s->global->p2p_group_formation == wpa_s)
5758 wpa_s->global->p2p_group_formation = NULL;
5759
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005760 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5761 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005762 if (ssid->passphrase == NULL && ssid->psk_set) {
5763 char psk[65];
5764 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005765 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5766 "%s client ssid=\"%s\" freq=%d psk=%s "
5767 "go_dev_addr=" MACSTR "%s",
5768 wpa_s->ifname, ssid_txt, freq, psk,
5769 MAC2STR(go_dev_addr),
5770 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005772 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5773 "%s client ssid=\"%s\" freq=%d "
5774 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5775 wpa_s->ifname, ssid_txt, freq,
5776 ssid->passphrase ? ssid->passphrase : "",
5777 MAC2STR(go_dev_addr),
5778 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005779 }
5780
5781 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005782 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5783 ssid, go_dev_addr);
5784 if (network_id < 0)
5785 network_id = ssid->id;
5786 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005787}
5788
5789
5790int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5791 u32 interval1, u32 duration2, u32 interval2)
5792{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005793 int ret;
5794
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5796 return -1;
5797
5798 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5799 wpa_s->current_ssid == NULL ||
5800 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5801 return -1;
5802
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005803 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5804 wpa_s->own_addr, wpa_s->assoc_freq,
5805 duration1, interval1, duration2, interval2);
5806 if (ret == 0)
5807 wpa_s->waiting_presence_resp = 1;
5808
5809 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005810}
5811
5812
5813int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5814 unsigned int interval)
5815{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005816 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5817 return -1;
5818
5819 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5820}
5821
5822
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005823static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5824{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005825 if (wpa_s->current_ssid == NULL) {
5826 /*
5827 * current_ssid can be cleared when P2P client interface gets
5828 * disconnected, so assume this interface was used as P2P
5829 * client.
5830 */
5831 return 1;
5832 }
5833 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005834 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5835}
5836
5837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005838static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5839{
5840 struct wpa_supplicant *wpa_s = eloop_ctx;
5841
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005842 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5844 "disabled");
5845 return;
5846 }
5847
Dmitry Shmidt04949592012-07-19 12:16:46 -07005848 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5849 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005850 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005851}
5852
5853
5854static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5855{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005856 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857
Dmitry Shmidt04949592012-07-19 12:16:46 -07005858 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5859 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005861 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5862 return;
5863
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005864 timeout = wpa_s->conf->p2p_group_idle;
5865 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5866 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5867 timeout = P2P_MAX_CLIENT_IDLE;
5868
5869 if (timeout == 0)
5870 return;
5871
Dmitry Shmidt04949592012-07-19 12:16:46 -07005872 if (timeout < 0) {
5873 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5874 timeout = 0; /* special client mode no-timeout */
5875 else
5876 return;
5877 }
5878
5879 if (wpa_s->p2p_in_provisioning) {
5880 /*
5881 * Use the normal group formation timeout during the
5882 * provisioning phase to avoid terminating this process too
5883 * early due to group idle timeout.
5884 */
5885 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5886 "during provisioning");
5887 return;
5888 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305889
Dmitry Shmidt04949592012-07-19 12:16:46 -07005890 if (wpa_s->show_group_started) {
5891 /*
5892 * Use the normal group formation timeout between the end of
5893 * the provisioning phase and completion of 4-way handshake to
5894 * avoid terminating this process too early due to group idle
5895 * timeout.
5896 */
5897 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5898 "while waiting for initial 4-way handshake to "
5899 "complete");
5900 return;
5901 }
5902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005903 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005904 timeout);
5905 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5906 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005907}
5908
5909
Jouni Malinen2b89da82012-08-31 22:04:41 +03005910/* Returns 1 if the interface was removed */
5911int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5912 u16 reason_code, const u8 *ie, size_t ie_len,
5913 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914{
5915 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005916 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917
Dmitry Shmidt04949592012-07-19 12:16:46 -07005918 if (!locally_generated)
5919 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5920 ie_len);
5921
5922 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5923 wpa_s->current_ssid &&
5924 wpa_s->current_ssid->p2p_group &&
5925 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5926 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5927 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005928 if (wpas_p2p_group_delete(wpa_s,
5929 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5930 > 0)
5931 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005932 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005933
5934 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935}
5936
5937
5938void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005939 u16 reason_code, const u8 *ie, size_t ie_len,
5940 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005941{
5942 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5943 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005944
Dmitry Shmidt04949592012-07-19 12:16:46 -07005945 if (!locally_generated)
5946 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5947 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005948}
5949
5950
5951void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5952{
5953 struct p2p_data *p2p = wpa_s->global->p2p;
5954
5955 if (p2p == NULL)
5956 return;
5957
5958 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5959 return;
5960
5961 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5962 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5963
5964 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5965 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5966
5967 if (wpa_s->wps &&
5968 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5969 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5970
5971 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5972 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5973
5974 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5975 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5976 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5977 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5978 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5979 }
5980
5981 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5982 p2p_set_sec_dev_types(p2p,
5983 (void *) wpa_s->conf->sec_device_type,
5984 wpa_s->conf->num_sec_device_types);
5985
5986 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5987 int i;
5988 p2p_remove_wps_vendor_extensions(p2p);
5989 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5990 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5991 continue;
5992 p2p_add_wps_vendor_extension(
5993 p2p, wpa_s->conf->wps_vendor_ext[i]);
5994 }
5995 }
5996
5997 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5998 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5999 char country[3];
6000 country[0] = wpa_s->conf->country[0];
6001 country[1] = wpa_s->conf->country[1];
6002 country[2] = 0x04;
6003 p2p_set_country(p2p, country);
6004 }
6005
6006 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6007 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6008 wpa_s->conf->p2p_ssid_postfix ?
6009 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6010 0);
6011 }
6012
6013 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6014 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006015
6016 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6017 u8 reg_class, channel;
6018 int ret;
6019 unsigned int r;
6020 if (wpa_s->conf->p2p_listen_reg_class &&
6021 wpa_s->conf->p2p_listen_channel) {
6022 reg_class = wpa_s->conf->p2p_listen_reg_class;
6023 channel = wpa_s->conf->p2p_listen_channel;
6024 } else {
6025 reg_class = 81;
6026 /*
6027 * Pick one of the social channels randomly as the
6028 * listen channel.
6029 */
6030 os_get_random((u8 *) &r, sizeof(r));
6031 channel = 1 + (r % 3) * 5;
6032 }
6033 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6034 if (ret)
6035 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6036 "failed: %d", ret);
6037 }
6038 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6039 u8 op_reg_class, op_channel, cfg_op_channel;
6040 int ret = 0;
6041 unsigned int r;
6042 if (wpa_s->conf->p2p_oper_reg_class &&
6043 wpa_s->conf->p2p_oper_channel) {
6044 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6045 op_channel = wpa_s->conf->p2p_oper_channel;
6046 cfg_op_channel = 1;
6047 } else {
6048 op_reg_class = 81;
6049 /*
6050 * Use random operation channel from (1, 6, 11)
6051 *if no other preference is indicated.
6052 */
6053 os_get_random((u8 *) &r, sizeof(r));
6054 op_channel = 1 + (r % 3) * 5;
6055 cfg_op_channel = 0;
6056 }
6057 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6058 cfg_op_channel);
6059 if (ret)
6060 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6061 "failed: %d", ret);
6062 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006063
6064 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6065 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6066 wpa_s->conf->p2p_pref_chan) < 0) {
6067 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6068 "update failed");
6069 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006070
6071 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6072 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6073 "update failed");
6074 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006075 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006076}
6077
6078
6079int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6080 int duration)
6081{
6082 if (!wpa_s->ap_iface)
6083 return -1;
6084 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6085 duration);
6086}
6087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088
6089int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6090{
6091 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6092 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006093
6094 wpa_s->global->cross_connection = enabled;
6095 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6096
6097 if (!enabled) {
6098 struct wpa_supplicant *iface;
6099
6100 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6101 {
6102 if (iface->cross_connect_enabled == 0)
6103 continue;
6104
6105 iface->cross_connect_enabled = 0;
6106 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006107 wpa_msg_global(iface->parent, MSG_INFO,
6108 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6109 iface->ifname,
6110 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006111 }
6112 }
6113
6114 return 0;
6115}
6116
6117
6118static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6119{
6120 struct wpa_supplicant *iface;
6121
6122 if (!uplink->global->cross_connection)
6123 return;
6124
6125 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6126 if (!iface->cross_connect_enabled)
6127 continue;
6128 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6129 0)
6130 continue;
6131 if (iface->ap_iface == NULL)
6132 continue;
6133 if (iface->cross_connect_in_use)
6134 continue;
6135
6136 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006137 wpa_msg_global(iface->parent, MSG_INFO,
6138 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6139 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006140 }
6141}
6142
6143
6144static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6145{
6146 struct wpa_supplicant *iface;
6147
6148 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6149 if (!iface->cross_connect_enabled)
6150 continue;
6151 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6152 0)
6153 continue;
6154 if (!iface->cross_connect_in_use)
6155 continue;
6156
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006157 wpa_msg_global(iface->parent, MSG_INFO,
6158 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6159 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006160 iface->cross_connect_in_use = 0;
6161 }
6162}
6163
6164
6165void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6166{
6167 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6168 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6169 wpa_s->cross_connect_disallowed)
6170 wpas_p2p_disable_cross_connect(wpa_s);
6171 else
6172 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006173 if (!wpa_s->ap_iface &&
6174 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6175 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006176}
6177
6178
6179void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6180{
6181 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006182 if (!wpa_s->ap_iface &&
6183 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6184 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006185 wpas_p2p_set_group_idle_timeout(wpa_s);
6186}
6187
6188
6189static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6190{
6191 struct wpa_supplicant *iface;
6192
6193 if (!wpa_s->global->cross_connection)
6194 return;
6195
6196 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6197 if (iface == wpa_s)
6198 continue;
6199 if (iface->drv_flags &
6200 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6201 continue;
6202 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6203 continue;
6204
6205 wpa_s->cross_connect_enabled = 1;
6206 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6207 sizeof(wpa_s->cross_connect_uplink));
6208 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6209 "%s to %s whenever uplink is available",
6210 wpa_s->ifname, wpa_s->cross_connect_uplink);
6211
6212 if (iface->ap_iface || iface->current_ssid == NULL ||
6213 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6214 iface->cross_connect_disallowed ||
6215 iface->wpa_state != WPA_COMPLETED)
6216 break;
6217
6218 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006219 wpa_msg_global(wpa_s->parent, MSG_INFO,
6220 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6221 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006222 break;
6223 }
6224}
6225
6226
6227int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6228{
6229 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6230 !wpa_s->p2p_in_provisioning)
6231 return 0; /* not P2P client operation */
6232
6233 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6234 "session overlap");
6235 if (wpa_s != wpa_s->parent)
6236 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006237 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238 return 1;
6239}
6240
6241
6242void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6243{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006244 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006245
6246 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6247 return;
6248
6249 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006250 os_memset(&cli_chan, 0, sizeof(cli_chan));
6251 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6253 "channel list");
6254 return;
6255 }
6256
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006257 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258}
6259
6260
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006261static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6262 struct wpa_scan_results *scan_res)
6263{
6264 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6265}
6266
6267
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006268int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6269{
6270 struct wpa_global *global = wpa_s->global;
6271 int found = 0;
6272 const u8 *peer;
6273
6274 if (global->p2p == NULL)
6275 return -1;
6276
6277 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6278
6279 if (wpa_s->pending_interface_name[0] &&
6280 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6281 found = 1;
6282
6283 peer = p2p_get_go_neg_peer(global->p2p);
6284 if (peer) {
6285 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6286 MACSTR, MAC2STR(peer));
6287 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006288 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006289 }
6290
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006291 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6292 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6293 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6294 found = 1;
6295 }
6296
6297 if (wpa_s->pending_pd_before_join) {
6298 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6299 wpa_s->pending_pd_before_join = 0;
6300 found = 1;
6301 }
6302
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006303 wpas_p2p_stop_find(wpa_s);
6304
6305 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6306 if (wpa_s == global->p2p_group_formation &&
6307 (wpa_s->p2p_in_provisioning ||
6308 wpa_s->parent->pending_interface_type ==
6309 WPA_IF_P2P_CLIENT)) {
6310 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6311 "formation found - cancelling",
6312 wpa_s->ifname);
6313 found = 1;
6314 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6315 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006316 if (wpa_s->p2p_in_provisioning) {
6317 wpas_group_formation_completed(wpa_s, 0);
6318 break;
6319 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006320 wpas_p2p_group_delete(wpa_s,
6321 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006322 break;
6323 }
6324 }
6325
6326 if (!found) {
6327 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6328 return -1;
6329 }
6330
6331 return 0;
6332}
6333
6334
6335void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6336{
6337 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6338 return;
6339
6340 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6341 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006342 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006343}
6344
6345
6346void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6347 int freq_24, int freq_5, int freq_overall)
6348{
6349 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006350 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006351 return;
6352 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6353}
6354
6355
6356int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6357{
6358 u8 peer[ETH_ALEN];
6359 struct p2p_data *p2p = wpa_s->global->p2p;
6360
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006361 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006362 return -1;
6363
6364 if (hwaddr_aton(addr, peer))
6365 return -1;
6366
6367 return p2p_unauthorize(p2p, peer);
6368}
6369
6370
6371/**
6372 * wpas_p2p_disconnect - Disconnect from a P2P Group
6373 * @wpa_s: Pointer to wpa_supplicant data
6374 * Returns: 0 on success, -1 on failure
6375 *
6376 * This can be used to disconnect from a group in which the local end is a P2P
6377 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6378 * virtual network interface was created for this group, that interface will be
6379 * removed. Otherwise, only the configured P2P group network will be removed
6380 * from the interface.
6381 */
6382int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6383{
6384
6385 if (wpa_s == NULL)
6386 return -1;
6387
Jouni Malinen2b89da82012-08-31 22:04:41 +03006388 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6389 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006390}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006391
6392
6393int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6394{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006395 int ret;
6396
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006397 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6398 return 0;
6399
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006400 ret = p2p_in_progress(wpa_s->global->p2p);
6401 if (ret == 0) {
6402 /*
6403 * Check whether there is an ongoing WPS provisioning step (or
6404 * other parts of group formation) on another interface since
6405 * p2p_in_progress() does not report this to avoid issues for
6406 * scans during such provisioning step.
6407 */
6408 if (wpa_s->global->p2p_group_formation &&
6409 wpa_s->global->p2p_group_formation != wpa_s) {
6410 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6411 "in group formation",
6412 wpa_s->global->p2p_group_formation->ifname);
6413 ret = 1;
6414 }
6415 }
6416
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006417 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006418 struct os_reltime now;
6419 os_get_reltime(&now);
6420 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6421 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006422 /* Wait for the first client has expired */
6423 wpa_s->global->p2p_go_wait_client.sec = 0;
6424 } else {
6425 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6426 ret = 1;
6427 }
6428 }
6429
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006430 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006431}
6432
6433
6434void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6435 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006436{
6437 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6438 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6439 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006440 /**
6441 * Remove the network by scheduling the group formation
6442 * timeout to happen immediately. The teardown code
6443 * needs to be scheduled to run asynch later so that we
6444 * don't delete data from under ourselves unexpectedly.
6445 * Calling wpas_p2p_group_formation_timeout directly
6446 * causes a series of crashes in WPS failure scenarios.
6447 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006448 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6449 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006450 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6451 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006452 }
6453}
6454
6455
6456struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006457 const u8 *addr, const u8 *ssid,
6458 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006459{
6460 struct wpa_ssid *s;
6461 size_t i;
6462
6463 for (s = wpa_s->conf->ssid; s; s = s->next) {
6464 if (s->disabled != 2)
6465 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006466 if (ssid &&
6467 (ssid_len != s->ssid_len ||
6468 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6469 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006470 if (addr == NULL) {
6471 if (s->mode == WPAS_MODE_P2P_GO)
6472 return s;
6473 continue;
6474 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006475 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6476 return s; /* peer is GO in the persistent group */
6477 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6478 continue;
6479 for (i = 0; i < s->num_p2p_clients; i++) {
6480 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6481 addr, ETH_ALEN) == 0)
6482 return s; /* peer is P2P client in persistent
6483 * group */
6484 }
6485 }
6486
6487 return NULL;
6488}
6489
6490
6491void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6492 const u8 *addr)
6493{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006494 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6495 wpa_s->parent, NULL) > 0) {
6496 /*
6497 * This can happen if WPS provisioning step is not terminated
6498 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6499 * peer was able to connect, there is no need to time out group
6500 * formation after this, though. In addition, this is used with
6501 * the initial connection wait on the GO as a separate formation
6502 * timeout and as such, expected to be hit after the initial WPS
6503 * provisioning step.
6504 */
6505 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6506 }
6507 if (!wpa_s->p2p_go_group_formation_completed) {
6508 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6509 wpa_s->p2p_go_group_formation_completed = 1;
6510 wpa_s->global->p2p_group_formation = NULL;
6511 wpa_s->p2p_in_provisioning = 0;
6512 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006513 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006514 if (addr == NULL)
6515 return;
6516 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6517}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006518
Dmitry Shmidt04949592012-07-19 12:16:46 -07006519
6520static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6521 int group_added)
6522{
6523 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006524 if (wpa_s->global->p2p_group_formation)
6525 group = wpa_s->global->p2p_group_formation;
6526 wpa_s = wpa_s->parent;
6527 offchannel_send_action_done(wpa_s);
6528 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006529 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006530 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6531 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6532 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6533 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6534 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006535 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006536 wpa_s->p2p_go_ht40,
6537 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006538}
6539
6540
6541int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6542{
6543 if (!wpa_s->p2p_fallback_to_go_neg ||
6544 wpa_s->p2p_in_provisioning <= 5)
6545 return 0;
6546
6547 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6548 return 0; /* peer operating as a GO */
6549
6550 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6551 "fallback to GO Negotiation");
6552 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6553
6554 return 1;
6555}
6556
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006557
6558unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6559{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006560 struct wpa_supplicant *ifs;
6561
6562 if (wpa_s->wpa_state > WPA_SCANNING) {
6563 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6564 "concurrent operation",
6565 P2P_CONCURRENT_SEARCH_DELAY);
6566 return P2P_CONCURRENT_SEARCH_DELAY;
6567 }
6568
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006569 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6570 radio_list) {
6571 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006572 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6573 "delay due to concurrent operation on "
6574 "interface %s",
6575 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6576 return P2P_CONCURRENT_SEARCH_DELAY;
6577 }
6578 }
6579
6580 return 0;
6581}
6582
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006583
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006584static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6585 struct wpa_ssid *s, const u8 *addr,
6586 int iface_addr)
6587{
6588 struct psk_list_entry *psk, *tmp;
6589 int changed = 0;
6590
6591 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6592 list) {
6593 if ((iface_addr && !psk->p2p &&
6594 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6595 (!iface_addr && psk->p2p &&
6596 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6597 wpa_dbg(wpa_s, MSG_DEBUG,
6598 "P2P: Remove persistent group PSK list entry for "
6599 MACSTR " p2p=%u",
6600 MAC2STR(psk->addr), psk->p2p);
6601 dl_list_del(&psk->list);
6602 os_free(psk);
6603 changed++;
6604 }
6605 }
6606
6607 return changed;
6608}
6609
6610
6611void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6612 const u8 *p2p_dev_addr,
6613 const u8 *psk, size_t psk_len)
6614{
6615 struct wpa_ssid *ssid = wpa_s->current_ssid;
6616 struct wpa_ssid *persistent;
6617 struct psk_list_entry *p;
6618
6619 if (psk_len != sizeof(p->psk))
6620 return;
6621
6622 if (p2p_dev_addr) {
6623 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6624 " p2p_dev_addr=" MACSTR,
6625 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6626 if (is_zero_ether_addr(p2p_dev_addr))
6627 p2p_dev_addr = NULL;
6628 } else {
6629 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6630 MAC2STR(mac_addr));
6631 }
6632
6633 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6634 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6635 /* To be added to persistent group once created */
6636 if (wpa_s->global->add_psk == NULL) {
6637 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6638 if (wpa_s->global->add_psk == NULL)
6639 return;
6640 }
6641 p = wpa_s->global->add_psk;
6642 if (p2p_dev_addr) {
6643 p->p2p = 1;
6644 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6645 } else {
6646 p->p2p = 0;
6647 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6648 }
6649 os_memcpy(p->psk, psk, psk_len);
6650 return;
6651 }
6652
6653 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6654 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6655 return;
6656 }
6657
6658 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6659 ssid->ssid_len);
6660 if (!persistent) {
6661 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6662 return;
6663 }
6664
6665 p = os_zalloc(sizeof(*p));
6666 if (p == NULL)
6667 return;
6668 if (p2p_dev_addr) {
6669 p->p2p = 1;
6670 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6671 } else {
6672 p->p2p = 0;
6673 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6674 }
6675 os_memcpy(p->psk, psk, psk_len);
6676
6677 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6678 struct psk_list_entry *last;
6679 last = dl_list_last(&persistent->psk_list,
6680 struct psk_list_entry, list);
6681 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6682 MACSTR " (p2p=%u) to make room for a new one",
6683 MAC2STR(last->addr), last->p2p);
6684 dl_list_del(&last->list);
6685 os_free(last);
6686 }
6687
6688 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6689 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6690 p2p_dev_addr == NULL);
6691 if (p2p_dev_addr) {
6692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6693 MACSTR, MAC2STR(p2p_dev_addr));
6694 } else {
6695 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6696 MAC2STR(mac_addr));
6697 }
6698 dl_list_add(&persistent->psk_list, &p->list);
6699
6700#ifndef CONFIG_NO_CONFIG_WRITE
6701 if (wpa_s->parent->conf->update_config &&
6702 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6703 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6704#endif /* CONFIG_NO_CONFIG_WRITE */
6705}
6706
6707
6708static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6709 struct wpa_ssid *s, const u8 *addr,
6710 int iface_addr)
6711{
6712 int res;
6713
6714 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6715 if (res > 0) {
6716#ifndef CONFIG_NO_CONFIG_WRITE
6717 if (wpa_s->conf->update_config &&
6718 wpa_config_write(wpa_s->confname, wpa_s->conf))
6719 wpa_dbg(wpa_s, MSG_DEBUG,
6720 "P2P: Failed to update configuration");
6721#endif /* CONFIG_NO_CONFIG_WRITE */
6722 }
6723}
6724
6725
6726static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6727 const u8 *peer, int iface_addr)
6728{
6729 struct hostapd_data *hapd;
6730 struct hostapd_wpa_psk *psk, *prev, *rem;
6731 struct sta_info *sta;
6732
6733 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6734 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6735 return;
6736
6737 /* Remove per-station PSK entry */
6738 hapd = wpa_s->ap_iface->bss[0];
6739 prev = NULL;
6740 psk = hapd->conf->ssid.wpa_psk;
6741 while (psk) {
6742 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6743 (!iface_addr &&
6744 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6745 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6746 MACSTR " iface_addr=%d",
6747 MAC2STR(peer), iface_addr);
6748 if (prev)
6749 prev->next = psk->next;
6750 else
6751 hapd->conf->ssid.wpa_psk = psk->next;
6752 rem = psk;
6753 psk = psk->next;
6754 os_free(rem);
6755 } else {
6756 prev = psk;
6757 psk = psk->next;
6758 }
6759 }
6760
6761 /* Disconnect from group */
6762 if (iface_addr)
6763 sta = ap_get_sta(hapd, peer);
6764 else
6765 sta = ap_get_sta_p2p(hapd, peer);
6766 if (sta) {
6767 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6768 " (iface_addr=%d) from group",
6769 MAC2STR(peer), iface_addr);
6770 hostapd_drv_sta_deauth(hapd, sta->addr,
6771 WLAN_REASON_DEAUTH_LEAVING);
6772 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6773 }
6774}
6775
6776
6777void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6778 int iface_addr)
6779{
6780 struct wpa_ssid *s;
6781 struct wpa_supplicant *w;
6782
6783 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6784
6785 /* Remove from any persistent group */
6786 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6787 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6788 continue;
6789 if (!iface_addr)
6790 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6791 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6792 }
6793
6794 /* Remove from any operating group */
6795 for (w = wpa_s->global->ifaces; w; w = w->next)
6796 wpas_p2p_remove_client_go(w, peer, iface_addr);
6797}
6798
6799
6800static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6801{
6802 struct wpa_supplicant *wpa_s = eloop_ctx;
6803 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6804}
6805
6806
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006807static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6808{
6809 struct wpa_supplicant *wpa_s = eloop_ctx;
6810
6811 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6812 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6813}
6814
6815
6816int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6817 struct wpa_ssid *ssid)
6818{
6819 struct wpa_supplicant *iface;
6820
6821 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6822 if (!iface->current_ssid ||
6823 iface->current_ssid->frequency == freq ||
6824 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6825 !iface->current_ssid->p2p_group))
6826 continue;
6827
6828 /* Remove the connection with least priority */
6829 if (!wpas_is_p2p_prioritized(iface)) {
6830 /* STA connection has priority over existing
6831 * P2P connection, so remove the interface. */
6832 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6833 eloop_register_timeout(0, 0,
6834 wpas_p2p_group_freq_conflict,
6835 iface, NULL);
6836 /* If connection in progress is P2P connection, do not
6837 * proceed for the connection. */
6838 if (wpa_s == iface)
6839 return -1;
6840 else
6841 return 0;
6842 } else {
6843 /* P2P connection has priority, disable the STA network
6844 */
6845 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6846 ssid);
6847 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6848 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6849 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6850 ETH_ALEN);
6851 /* If P2P connection is in progress, continue
6852 * connecting...*/
6853 if (wpa_s == iface)
6854 return 0;
6855 else
6856 return -1;
6857 }
6858 }
6859
6860 return 0;
6861}
6862
6863
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006864int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6865{
6866 struct wpa_ssid *ssid = wpa_s->current_ssid;
6867
6868 if (ssid == NULL || !ssid->p2p_group)
6869 return 0;
6870
6871 if (wpa_s->p2p_last_4way_hs_fail &&
6872 wpa_s->p2p_last_4way_hs_fail == ssid) {
6873 u8 go_dev_addr[ETH_ALEN];
6874 struct wpa_ssid *persistent;
6875
6876 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6877 ssid->ssid,
6878 ssid->ssid_len) <= 0) {
6879 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6880 goto disconnect;
6881 }
6882
6883 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6884 MACSTR, MAC2STR(go_dev_addr));
6885 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6886 ssid->ssid,
6887 ssid->ssid_len);
6888 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6889 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6890 goto disconnect;
6891 }
6892 wpa_msg_global(wpa_s->parent, MSG_INFO,
6893 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6894 persistent->id);
6895 disconnect:
6896 wpa_s->p2p_last_4way_hs_fail = NULL;
6897 /*
6898 * Remove the group from a timeout to avoid issues with caller
6899 * continuing to use the interface if this is on a P2P group
6900 * interface.
6901 */
6902 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6903 wpa_s, NULL);
6904 return 1;
6905 }
6906
6907 wpa_s->p2p_last_4way_hs_fail = ssid;
6908 return 0;
6909}