blob: 41d5102c95b000b7f9fefefcedf575f426c9677e [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 Shmidt04949592012-07-19 12:16:46 -0700107static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
113static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
114static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
115static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800116static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
117 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800118static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700119static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
120 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800121static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122
123
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700124/*
125 * Get the number of concurrent channels that the HW can operate, but that are
126 * currently not in use by any of the wpa_supplicant interfaces.
127 */
128static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
129{
130 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800131 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700132
133 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
134 if (!freqs)
135 return -1;
136
137 num = get_shared_radio_freqs(wpa_s, freqs,
138 wpa_s->num_multichan_concurrent);
139 os_free(freqs);
140
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800141 unused = wpa_s->num_multichan_concurrent - num;
142 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
143 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700144}
145
146
147/*
148 * Get the frequencies that are currently in use by one or more of the virtual
149 * interfaces, and that are also valid for P2P operation.
150 */
151static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
152 int *p2p_freqs, unsigned int len)
153{
154 int *freqs;
155 unsigned int num, i, j;
156
157 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
158 if (!freqs)
159 return -1;
160
161 num = get_shared_radio_freqs(wpa_s, freqs,
162 wpa_s->num_multichan_concurrent);
163
164 os_memset(p2p_freqs, 0, sizeof(int) * len);
165
166 for (i = 0, j = 0; i < num && j < len; i++) {
167 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
168 p2p_freqs[j++] = freqs[i];
169 }
170
171 os_free(freqs);
172
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800173 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
174
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175 return j;
176}
177
178
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700179static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
180 int freq)
181{
182 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
183 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800184 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
185 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
186 wpas_p2p_num_unused_channels(wpa_s) > 0) {
187 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
188 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700189 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800190 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700191 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
192}
193
194
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
196 struct wpa_scan_results *scan_res)
197{
198 size_t i;
199
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800200 if (wpa_s->p2p_scan_work) {
201 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
202 wpa_s->p2p_scan_work = NULL;
203 radio_work_done(work);
204 }
205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
207 return;
208
209 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
210 (int) scan_res->num);
211
212 for (i = 0; i < scan_res->num; i++) {
213 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800214 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700215 const u8 *ies;
216 size_t ies_len;
217
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800218 time_tmp_age.sec = bss->age / 1000;
219 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800220 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700221
222 ies = (const u8 *) (bss + 1);
223 ies_len = bss->ie_len;
224 if (bss->beacon_ie_len > 0 &&
225 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
226 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
227 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
228 MACSTR, MAC2STR(bss->bssid));
229 ies = ies + ies_len;
230 ies_len = bss->beacon_ie_len;
231 }
232
233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800235 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700236 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237 break;
238 }
239
240 p2p_scan_res_handled(wpa_s->global->p2p);
241}
242
243
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800244static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
245{
246 struct wpa_supplicant *wpa_s = work->wpa_s;
247 struct wpa_driver_scan_params *params = work->ctx;
248 int ret;
249
250 if (deinit) {
251 wpa_scan_free_params(params);
252 return;
253 }
254
255 ret = wpa_drv_scan(wpa_s, params);
256 wpa_scan_free_params(params);
257 work->ctx = NULL;
258 if (ret) {
259 radio_work_done(work);
260 return;
261 }
262
263 os_get_reltime(&wpa_s->scan_trigger_time);
264 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
265 wpa_s->own_scan_requested = 1;
266 wpa_s->p2p_scan_work = work;
267}
268
269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
271 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700272 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273{
274 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800275 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800277 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800278 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700279
280 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
281 return -1;
282
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800283 if (wpa_s->p2p_scan_work) {
284 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
285 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700286 }
287
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 params = os_zalloc(sizeof(*params));
289 if (params == NULL)
290 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
292 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 params->num_ssids = 1;
294 n = os_malloc(P2P_WILDCARD_SSID_LEN);
295 if (n == NULL)
296 goto fail;
297 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
298 params->ssids[0].ssid = n;
299 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300
301 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700302 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
303 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 num_req_dev_types, req_dev_types);
305 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800306 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
309 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 if (ies == NULL) {
311 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800312 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313 }
314 wpabuf_put_buf(ies, wps_ie);
315 wpabuf_free(wps_ie);
316
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800317 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800319 params->p2p_probe = 1;
320 n = os_malloc(wpabuf_len(ies));
321 if (n == NULL) {
322 wpabuf_free(ies);
323 goto fail;
324 }
325 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
326 params->extra_ies = n;
327 params->extra_ies_len = wpabuf_len(ies);
328 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329
330 switch (type) {
331 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800332 params->freqs = os_malloc(4 * sizeof(int));
333 if (params->freqs == NULL)
334 goto fail;
335 params->freqs[0] = 2412;
336 params->freqs[1] = 2437;
337 params->freqs[2] = 2462;
338 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 break;
340 case P2P_SCAN_FULL:
341 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800343 params->freqs = os_malloc(5 * sizeof(int));
344 if (params->freqs == NULL)
345 goto fail;
346 params->freqs[0] = 2412;
347 params->freqs[1] = 2437;
348 params->freqs[2] = 2462;
349 params->freqs[3] = freq;
350 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 break;
352 }
353
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800354 radio_remove_unstarted_work(wpa_s, "p2p-scan");
355 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
356 params) < 0)
357 goto fail;
358 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800360fail:
361 wpa_scan_free_params(params);
362 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363}
364
365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
367{
368 switch (p2p_group_interface) {
369 case P2P_GROUP_INTERFACE_PENDING:
370 return WPA_IF_P2P_GROUP;
371 case P2P_GROUP_INTERFACE_GO:
372 return WPA_IF_P2P_GO;
373 case P2P_GROUP_INTERFACE_CLIENT:
374 return WPA_IF_P2P_CLIENT;
375 }
376
377 return WPA_IF_P2P_GROUP;
378}
379
380
381static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
382 const u8 *ssid,
383 size_t ssid_len, int *go)
384{
385 struct wpa_ssid *s;
386
387 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
388 for (s = wpa_s->conf->ssid; s; s = s->next) {
389 if (s->disabled != 0 || !s->p2p_group ||
390 s->ssid_len != ssid_len ||
391 os_memcmp(ssid, s->ssid, ssid_len) != 0)
392 continue;
393 if (s->mode == WPAS_MODE_P2P_GO &&
394 s != wpa_s->current_ssid)
395 continue;
396 if (go)
397 *go = s->mode == WPAS_MODE_P2P_GO;
398 return wpa_s;
399 }
400 }
401
402 return NULL;
403}
404
405
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700406static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
407 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408{
409 struct wpa_ssid *ssid;
410 char *gtype;
411 const char *reason;
412
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 ssid = wpa_s->current_ssid;
414 if (ssid == NULL) {
415 /*
416 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700417 * pending P2P group interface waiting for provisioning or a
418 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 */
420 ssid = wpa_s->conf->ssid;
421 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700422 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 break;
424 ssid = ssid->next;
425 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300426 if (ssid == NULL &&
427 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
428 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
430 "not found");
431 return -1;
432 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 }
434 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
435 gtype = "GO";
436 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
437 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
438 wpa_s->reassociate = 0;
439 wpa_s->disconnected = 1;
440 wpa_supplicant_deauthenticate(wpa_s,
441 WLAN_REASON_DEAUTH_LEAVING);
442 gtype = "client";
443 } else
444 gtype = "GO";
445 if (wpa_s->cross_connect_in_use) {
446 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700447 wpa_msg_global(wpa_s->parent, MSG_INFO,
448 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
449 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700451 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 case P2P_GROUP_REMOVAL_REQUESTED:
453 reason = " reason=REQUESTED";
454 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700455 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
456 reason = " reason=FORMATION_FAILED";
457 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
459 reason = " reason=IDLE";
460 break;
461 case P2P_GROUP_REMOVAL_UNAVAILABLE:
462 reason = " reason=UNAVAILABLE";
463 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700464 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
465 reason = " reason=GO_ENDING_SESSION";
466 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700467 case P2P_GROUP_REMOVAL_PSK_FAILURE:
468 reason = " reason=PSK_FAILURE";
469 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800470 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
471 reason = " reason=FREQ_CONFLICT";
472 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 default:
474 reason = "";
475 break;
476 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700477 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700478 wpa_msg_global(wpa_s->parent, MSG_INFO,
479 P2P_EVENT_GROUP_REMOVED "%s %s%s",
480 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700481 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700482
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800483 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
484 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700485 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
486 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800487 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700488 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800489 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
490 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700491 wpa_s->p2p_in_provisioning = 0;
492 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493
Dmitry Shmidt96571392013-10-14 12:54:46 -0700494 /*
495 * Make sure wait for the first client does not remain active after the
496 * group has been removed.
497 */
498 wpa_s->global->p2p_go_wait_client.sec = 0;
499
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700500 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
502
503 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
504 struct wpa_global *global;
505 char *ifname;
506 enum wpa_driver_if_type type;
507 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
508 wpa_s->ifname);
509 global = wpa_s->global;
510 ifname = os_strdup(wpa_s->ifname);
511 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700512 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 wpa_s = global->ifaces;
514 if (wpa_s && ifname)
515 wpa_drv_if_remove(wpa_s, type, ifname);
516 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300517 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 }
519
Dmitry Shmidt96571392013-10-14 12:54:46 -0700520 if (!wpa_s->p2p_go_group_formation_completed) {
521 wpa_s->global->p2p_group_formation = NULL;
522 wpa_s->p2p_in_provisioning = 0;
523 }
524
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800525 wpa_s->show_group_started = 0;
526 os_free(wpa_s->go_params);
527 wpa_s->go_params = NULL;
528
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800529 wpa_s->waiting_presence_resp = 0;
530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
532 if (ssid && (ssid->p2p_group ||
533 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
534 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
535 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700536 if (ssid == wpa_s->current_ssid) {
537 wpa_sm_set_config(wpa_s->wpa, NULL);
538 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700540 }
541 /*
542 * Networks objects created during any P2P activities are not
543 * exposed out as they might/will confuse certain non-P2P aware
544 * applications since these network objects won't behave like
545 * regular ones.
546 *
547 * Likewise, we don't send out network removed signals for such
548 * network objects.
549 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 wpa_config_remove_network(wpa_s->conf, id);
551 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800552 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 } else {
554 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
555 "found");
556 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700557 if (wpa_s->ap_iface)
558 wpa_supplicant_ap_deinit(wpa_s);
559 else
560 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700561
562 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563}
564
565
566static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
567 u8 *go_dev_addr,
568 const u8 *ssid, size_t ssid_len)
569{
570 struct wpa_bss *bss;
571 const u8 *bssid;
572 struct wpabuf *p2p;
573 u8 group_capab;
574 const u8 *addr;
575
576 if (wpa_s->go_params)
577 bssid = wpa_s->go_params->peer_interface_addr;
578 else
579 bssid = wpa_s->bssid;
580
581 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
582 if (bss == NULL) {
583 u8 iface_addr[ETH_ALEN];
584 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
585 iface_addr) == 0)
586 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
587 }
588 if (bss == NULL) {
589 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
590 "group is persistent - BSS " MACSTR " not found",
591 MAC2STR(bssid));
592 return 0;
593 }
594
595 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700596 if (p2p == NULL)
597 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
598 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 if (p2p == NULL) {
600 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
601 "group is persistent - BSS " MACSTR
602 " did not include P2P IE", MAC2STR(bssid));
603 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
604 (u8 *) (bss + 1), bss->ie_len);
605 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
606 ((u8 *) bss + 1) + bss->ie_len,
607 bss->beacon_ie_len);
608 return 0;
609 }
610
611 group_capab = p2p_get_group_capab(p2p);
612 addr = p2p_get_go_dev_addr(p2p);
613 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
614 "group_capab=0x%x", group_capab);
615 if (addr) {
616 os_memcpy(go_dev_addr, addr, ETH_ALEN);
617 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
618 MAC2STR(addr));
619 } else
620 os_memset(go_dev_addr, 0, ETH_ALEN);
621 wpabuf_free(p2p);
622
623 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
624 "go_dev_addr=" MACSTR,
625 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
626
627 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
628}
629
630
Jouni Malinen75ecf522011-06-27 15:19:46 -0700631static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
632 struct wpa_ssid *ssid,
633 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634{
635 struct wpa_ssid *s;
636 int changed = 0;
637
638 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
639 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
640 for (s = wpa_s->conf->ssid; s; s = s->next) {
641 if (s->disabled == 2 &&
642 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
643 s->ssid_len == ssid->ssid_len &&
644 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
645 break;
646 }
647
648 if (s) {
649 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
650 "entry");
651 if (ssid->passphrase && !s->passphrase)
652 changed = 1;
653 else if (ssid->passphrase && s->passphrase &&
654 os_strcmp(ssid->passphrase, s->passphrase) != 0)
655 changed = 1;
656 } else {
657 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
658 "entry");
659 changed = 1;
660 s = wpa_config_add_network(wpa_s->conf);
661 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700662 return -1;
663
664 /*
665 * Instead of network_added we emit persistent_group_added
666 * notification. Also to keep the defense checks in
667 * persistent_group obj registration method, we set the
668 * relevant flags in s to designate it as a persistent group.
669 */
670 s->p2p_group = 1;
671 s->p2p_persistent_group = 1;
672 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673 wpa_config_set_network_defaults(s);
674 }
675
676 s->p2p_group = 1;
677 s->p2p_persistent_group = 1;
678 s->disabled = 2;
679 s->bssid_set = 1;
680 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
681 s->mode = ssid->mode;
682 s->auth_alg = WPA_AUTH_ALG_OPEN;
683 s->key_mgmt = WPA_KEY_MGMT_PSK;
684 s->proto = WPA_PROTO_RSN;
685 s->pairwise_cipher = WPA_CIPHER_CCMP;
686 s->export_keys = 1;
687 if (ssid->passphrase) {
688 os_free(s->passphrase);
689 s->passphrase = os_strdup(ssid->passphrase);
690 }
691 if (ssid->psk_set) {
692 s->psk_set = 1;
693 os_memcpy(s->psk, ssid->psk, 32);
694 }
695 if (s->passphrase && !s->psk_set)
696 wpa_config_update_psk(s);
697 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
698 os_free(s->ssid);
699 s->ssid = os_malloc(ssid->ssid_len);
700 }
701 if (s->ssid) {
702 s->ssid_len = ssid->ssid_len;
703 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
704 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700705 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
706 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
707 wpa_s->global->add_psk = NULL;
708 changed = 1;
709 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710
711#ifndef CONFIG_NO_CONFIG_WRITE
712 if (changed && wpa_s->conf->update_config &&
713 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
714 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
715 }
716#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700717
718 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719}
720
721
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800722static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
723 const u8 *addr)
724{
725 struct wpa_ssid *ssid, *s;
726 u8 *n;
727 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700728 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800729
730 ssid = wpa_s->current_ssid;
731 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
732 !ssid->p2p_persistent_group)
733 return;
734
735 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
736 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
737 continue;
738
739 if (s->ssid_len == ssid->ssid_len &&
740 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
741 break;
742 }
743
744 if (s == NULL)
745 return;
746
747 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
748 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700749 ETH_ALEN) != 0)
750 continue;
751
752 if (i == s->num_p2p_clients - 1)
753 return; /* already the most recent entry */
754
755 /* move the entry to mark it most recent */
756 os_memmove(s->p2p_client_list + i * ETH_ALEN,
757 s->p2p_client_list + (i + 1) * ETH_ALEN,
758 (s->num_p2p_clients - i - 1) * ETH_ALEN);
759 os_memcpy(s->p2p_client_list +
760 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
761 found = 1;
762 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763 }
764
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700765 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
766 n = os_realloc_array(s->p2p_client_list,
767 s->num_p2p_clients + 1, ETH_ALEN);
768 if (n == NULL)
769 return;
770 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
771 s->p2p_client_list = n;
772 s->num_p2p_clients++;
773 } else if (!found) {
774 /* Not enough room for an additional entry - drop the oldest
775 * entry */
776 os_memmove(s->p2p_client_list,
777 s->p2p_client_list + ETH_ALEN,
778 (s->num_p2p_clients - 1) * ETH_ALEN);
779 os_memcpy(s->p2p_client_list +
780 (s->num_p2p_clients - 1) * ETH_ALEN,
781 addr, ETH_ALEN);
782 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800783
784#ifndef CONFIG_NO_CONFIG_WRITE
785 if (wpa_s->parent->conf->update_config &&
786 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
787 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
788#endif /* CONFIG_NO_CONFIG_WRITE */
789}
790
791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
793 int success)
794{
795 struct wpa_ssid *ssid;
796 const char *ssid_txt;
797 int client;
798 int persistent;
799 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700800 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
802 /*
803 * This callback is likely called for the main interface. Update wpa_s
804 * to use the group interface if a new interface was created for the
805 * group.
806 */
807 if (wpa_s->global->p2p_group_formation)
808 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700809 if (wpa_s->p2p_go_group_formation_completed) {
810 wpa_s->global->p2p_group_formation = NULL;
811 wpa_s->p2p_in_provisioning = 0;
812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813
814 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700815 wpa_msg_global(wpa_s->parent, MSG_INFO,
816 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700817 wpas_p2p_group_delete(wpa_s,
818 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 return;
820 }
821
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700822 wpa_msg_global(wpa_s->parent, MSG_INFO,
823 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824
825 ssid = wpa_s->current_ssid;
826 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
827 ssid->mode = WPAS_MODE_P2P_GO;
828 p2p_group_notif_formation_done(wpa_s->p2p_group);
829 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
830 }
831
832 persistent = 0;
833 if (ssid) {
834 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
835 client = ssid->mode == WPAS_MODE_INFRA;
836 if (ssid->mode == WPAS_MODE_P2P_GO) {
837 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700838 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
839 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 } else
841 persistent = wpas_p2p_persistent_group(wpa_s,
842 go_dev_addr,
843 ssid->ssid,
844 ssid->ssid_len);
845 } else {
846 ssid_txt = "";
847 client = wpa_s->p2p_group_interface ==
848 P2P_GROUP_INTERFACE_CLIENT;
849 os_memset(go_dev_addr, 0, ETH_ALEN);
850 }
851
852 wpa_s->show_group_started = 0;
853 if (client) {
854 /*
855 * Indicate event only after successfully completed 4-way
856 * handshake, i.e., when the interface is ready for data
857 * packets.
858 */
859 wpa_s->show_group_started = 1;
860 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
861 char psk[65];
862 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700863 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
864 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
865 MACSTR "%s",
866 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
867 MAC2STR(go_dev_addr),
868 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 wpas_p2p_cross_connect_setup(wpa_s);
870 wpas_p2p_set_group_idle_timeout(wpa_s);
871 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700872 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
873 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
874 "go_dev_addr=" MACSTR "%s",
875 wpa_s->ifname, ssid_txt,
876 ssid ? ssid->frequency : 0,
877 ssid && ssid->passphrase ? ssid->passphrase : "",
878 MAC2STR(go_dev_addr),
879 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 wpas_p2p_cross_connect_setup(wpa_s);
881 wpas_p2p_set_group_idle_timeout(wpa_s);
882 }
883
884 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700885 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
886 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700887 else {
888 os_free(wpa_s->global->add_psk);
889 wpa_s->global->add_psk = NULL;
890 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800891 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700892 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700893 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700894 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800895 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700896 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897}
898
899
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800900struct send_action_work {
901 unsigned int freq;
902 u8 dst[ETH_ALEN];
903 u8 src[ETH_ALEN];
904 u8 bssid[ETH_ALEN];
905 size_t len;
906 unsigned int wait_time;
907 u8 buf[0];
908};
909
910
911static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
912 void *timeout_ctx)
913{
914 struct wpa_supplicant *wpa_s = eloop_ctx;
915
916 if (!wpa_s->p2p_send_action_work)
917 return;
918
919 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
920 os_free(wpa_s->p2p_send_action_work->ctx);
921 radio_work_done(wpa_s->p2p_send_action_work);
922 wpa_s->p2p_send_action_work = NULL;
923}
924
925
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800926static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
927 unsigned int freq,
928 const u8 *dst, const u8 *src,
929 const u8 *bssid,
930 const u8 *data, size_t data_len,
931 enum offchannel_send_action_result
932 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700933{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800934 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800936 if (wpa_s->p2p_send_action_work) {
937 struct send_action_work *awork;
938 awork = wpa_s->p2p_send_action_work->ctx;
939 if (awork->wait_time == 0) {
940 os_free(awork);
941 radio_work_done(wpa_s->p2p_send_action_work);
942 wpa_s->p2p_send_action_work = NULL;
943 } else {
944 /*
945 * In theory, this should not be needed, but number of
946 * places in the P2P code is still using non-zero wait
947 * time for the last Action frame in the sequence and
948 * some of these do not call send_action_done().
949 */
950 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
951 wpa_s, NULL);
952 eloop_register_timeout(
953 0, awork->wait_time * 1000,
954 wpas_p2p_send_action_work_timeout,
955 wpa_s, NULL);
956 }
957 }
958
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
960 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800962 switch (result) {
963 case OFFCHANNEL_SEND_ACTION_SUCCESS:
964 res = P2P_SEND_ACTION_SUCCESS;
965 break;
966 case OFFCHANNEL_SEND_ACTION_NO_ACK:
967 res = P2P_SEND_ACTION_NO_ACK;
968 break;
969 case OFFCHANNEL_SEND_ACTION_FAILED:
970 res = P2P_SEND_ACTION_FAILED;
971 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972 }
973
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800974 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800976 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
977 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800978 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800979 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
980 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800982 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
983 "during p2p_connect-auto");
984 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
985 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 }
987}
988
989
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800990static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
991{
992 struct wpa_supplicant *wpa_s = work->wpa_s;
993 struct send_action_work *awork = work->ctx;
994
995 if (deinit) {
996 os_free(awork);
997 return;
998 }
999
1000 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1001 awork->bssid, awork->buf, awork->len,
1002 awork->wait_time,
1003 wpas_p2p_send_action_tx_status, 1) < 0) {
1004 os_free(awork);
1005 radio_work_done(work);
1006 return;
1007 }
1008 wpa_s->p2p_send_action_work = work;
1009}
1010
1011
1012static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1013 unsigned int freq, const u8 *dst,
1014 const u8 *src, const u8 *bssid, const u8 *buf,
1015 size_t len, unsigned int wait_time)
1016{
1017 struct send_action_work *awork;
1018
1019 if (wpa_s->p2p_send_action_work) {
1020 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1021 return -1;
1022 }
1023
1024 awork = os_zalloc(sizeof(*awork) + len);
1025 if (awork == NULL)
1026 return -1;
1027
1028 awork->freq = freq;
1029 os_memcpy(awork->dst, dst, ETH_ALEN);
1030 os_memcpy(awork->src, src, ETH_ALEN);
1031 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1032 awork->len = len;
1033 awork->wait_time = wait_time;
1034 os_memcpy(awork->buf, buf, len);
1035
1036 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1037 wpas_send_action_cb, awork) < 0) {
1038 os_free(awork);
1039 return -1;
1040 }
1041
1042 return 0;
1043}
1044
1045
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1047 const u8 *src, const u8 *bssid, const u8 *buf,
1048 size_t len, unsigned int wait_time)
1049{
1050 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001051 int listen_freq = -1, send_freq = -1;
1052
1053 if (wpa_s->p2p_listen_work)
1054 listen_freq = wpa_s->p2p_listen_work->freq;
1055 if (wpa_s->p2p_send_action_work)
1056 send_freq = wpa_s->p2p_send_action_work->freq;
1057 if (listen_freq != (int) freq && send_freq != (int) freq) {
1058 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1059 listen_freq, send_freq);
1060 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1061 len, wait_time);
1062 }
1063
1064 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1066 wait_time,
1067 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068}
1069
1070
1071static void wpas_send_action_done(void *ctx)
1072{
1073 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001074
1075 if (wpa_s->p2p_send_action_work) {
1076 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1077 wpa_s, NULL);
1078 os_free(wpa_s->p2p_send_action_work->ctx);
1079 radio_work_done(wpa_s->p2p_send_action_work);
1080 wpa_s->p2p_send_action_work = NULL;
1081 }
1082
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001083 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001084}
1085
1086
1087static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1088 struct p2p_go_neg_results *params)
1089{
1090 if (wpa_s->go_params == NULL) {
1091 wpa_s->go_params = os_malloc(sizeof(*params));
1092 if (wpa_s->go_params == NULL)
1093 return -1;
1094 }
1095 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1096 return 0;
1097}
1098
1099
1100static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1101 struct p2p_go_neg_results *res)
1102{
1103 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
1104 MAC2STR(res->peer_interface_addr));
1105 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1106 res->ssid, res->ssid_len);
1107 wpa_supplicant_ap_deinit(wpa_s);
1108 wpas_copy_go_neg_results(wpa_s, res);
1109 if (res->wps_method == WPS_PBC)
1110 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1111 else {
1112 u16 dev_pw_id = DEV_PW_DEFAULT;
1113 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1114 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1115 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1116 wpa_s->p2p_pin, 1, dev_pw_id);
1117 }
1118}
1119
1120
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001121static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1122 struct wpa_ssid *ssid)
1123{
1124 struct wpa_ssid *persistent;
1125 struct psk_list_entry *psk;
1126 struct hostapd_data *hapd;
1127
1128 if (!wpa_s->ap_iface)
1129 return;
1130
1131 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1132 ssid->ssid_len);
1133 if (persistent == NULL)
1134 return;
1135
1136 hapd = wpa_s->ap_iface->bss[0];
1137
1138 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1139 list) {
1140 struct hostapd_wpa_psk *hpsk;
1141
1142 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1143 MACSTR " psk=%d",
1144 MAC2STR(psk->addr), psk->p2p);
1145 hpsk = os_zalloc(sizeof(*hpsk));
1146 if (hpsk == NULL)
1147 break;
1148 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1149 if (psk->p2p)
1150 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1151 else
1152 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1153 hpsk->next = hapd->conf->ssid.wpa_psk;
1154 hapd->conf->ssid.wpa_psk = hpsk;
1155 }
1156}
1157
1158
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159static void p2p_go_configured(void *ctx, void *data)
1160{
1161 struct wpa_supplicant *wpa_s = ctx;
1162 struct p2p_go_neg_results *params = data;
1163 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001164 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165
1166 ssid = wpa_s->current_ssid;
1167 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1168 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1169 if (wpa_s->global->p2p_group_formation == wpa_s)
1170 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001171 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001172 wpa_msg_global(wpa_s->parent, MSG_INFO,
1173 P2P_EVENT_GROUP_STARTED
1174 "%s GO ssid=\"%s\" freq=%d "
1175 "passphrase=\"%s\" go_dev_addr=" MACSTR
1176 "%s", wpa_s->ifname,
1177 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1178 ssid->frequency, params->passphrase,
1179 MAC2STR(wpa_s->global->p2p_dev_addr),
1180 params->persistent_group ?
1181 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001182 } else {
1183 char psk[65];
1184 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1185 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001186 wpa_msg_global(wpa_s->parent, MSG_INFO,
1187 P2P_EVENT_GROUP_STARTED
1188 "%s GO ssid=\"%s\" freq=%d psk=%s "
1189 "go_dev_addr=" MACSTR "%s",
1190 wpa_s->ifname,
1191 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1192 ssid->frequency, psk,
1193 MAC2STR(wpa_s->global->p2p_dev_addr),
1194 params->persistent_group ?
1195 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001196 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001198 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001199 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001200 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001201 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001202 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001203 wpas_p2p_add_psk_list(wpa_s, ssid);
1204 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001205 if (network_id < 0)
1206 network_id = ssid->id;
1207 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 wpas_p2p_cross_connect_setup(wpa_s);
1209 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001210
1211 if (wpa_s->p2p_first_connection_timeout) {
1212 wpa_dbg(wpa_s, MSG_DEBUG,
1213 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1214 wpa_s->p2p_first_connection_timeout);
1215 wpa_s->p2p_go_group_formation_completed = 0;
1216 wpa_s->global->p2p_group_formation = wpa_s;
1217 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1218 wpa_s->parent, NULL);
1219 eloop_register_timeout(
1220 wpa_s->p2p_first_connection_timeout, 0,
1221 wpas_p2p_group_formation_timeout,
1222 wpa_s->parent, NULL);
1223 }
1224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225 return;
1226 }
1227
1228 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1229 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1230 params->peer_interface_addr)) {
1231 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1232 "filtering");
1233 return;
1234 }
1235 if (params->wps_method == WPS_PBC)
1236 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001237 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 else if (wpa_s->p2p_pin[0])
1239 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001240 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 os_free(wpa_s->go_params);
1242 wpa_s->go_params = NULL;
1243}
1244
1245
1246static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1247 struct p2p_go_neg_results *params,
1248 int group_formation)
1249{
1250 struct wpa_ssid *ssid;
1251
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001252 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1253 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1254 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1255 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001258
1259 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001260 if (ssid == NULL) {
1261 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001263 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001265 wpa_s->show_group_started = 0;
1266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001267 wpa_config_set_network_defaults(ssid);
1268 ssid->temporary = 1;
1269 ssid->p2p_group = 1;
1270 ssid->p2p_persistent_group = params->persistent_group;
1271 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1272 WPAS_MODE_P2P_GO;
1273 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001274 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001275 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001276 ssid->ssid = os_zalloc(params->ssid_len + 1);
1277 if (ssid->ssid) {
1278 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1279 ssid->ssid_len = params->ssid_len;
1280 }
1281 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1282 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1283 ssid->proto = WPA_PROTO_RSN;
1284 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001285 if (os_strlen(params->passphrase) > 0) {
1286 ssid->passphrase = os_strdup(params->passphrase);
1287 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001288 wpa_msg_global(wpa_s, MSG_ERROR,
1289 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001290 wpa_config_remove_network(wpa_s->conf, ssid->id);
1291 return;
1292 }
1293 } else
1294 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001295 ssid->psk_set = params->psk_set;
1296 if (ssid->psk_set)
1297 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001298 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001299 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001300 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301
1302 wpa_s->ap_configured_cb = p2p_go_configured;
1303 wpa_s->ap_configured_cb_ctx = wpa_s;
1304 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001305 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306 wpa_s->reassociate = 1;
1307 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001308 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1309 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 wpa_supplicant_req_scan(wpa_s, 0, 0);
1311}
1312
1313
1314static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1315 const struct wpa_supplicant *src)
1316{
1317 struct wpa_config *d;
1318 const struct wpa_config *s;
1319
1320 d = dst->conf;
1321 s = src->conf;
1322
1323#define C(n) if (s->n) d->n = os_strdup(s->n)
1324 C(device_name);
1325 C(manufacturer);
1326 C(model_name);
1327 C(model_number);
1328 C(serial_number);
1329 C(config_methods);
1330#undef C
1331
1332 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1333 os_memcpy(d->sec_device_type, s->sec_device_type,
1334 sizeof(d->sec_device_type));
1335 d->num_sec_device_types = s->num_sec_device_types;
1336
1337 d->p2p_group_idle = s->p2p_group_idle;
1338 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001339 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001340 d->max_num_sta = s->max_num_sta;
1341 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001342 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001343 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001344 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001345 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346}
1347
1348
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001349static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1350 char *ifname, size_t len)
1351{
1352 char *ifname_ptr = wpa_s->ifname;
1353
1354 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1355 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1356 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1357 }
1358
1359 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1360 if (os_strlen(ifname) >= IFNAMSIZ &&
1361 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1362 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001363 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001364 }
1365}
1366
1367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1369 enum wpa_driver_if_type type)
1370{
1371 char ifname[120], force_ifname[120];
1372
1373 if (wpa_s->pending_interface_name[0]) {
1374 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1375 "- skip creation of a new one");
1376 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1377 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1378 "unknown?! ifname='%s'",
1379 wpa_s->pending_interface_name);
1380 return -1;
1381 }
1382 return 0;
1383 }
1384
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001385 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 force_ifname[0] = '\0';
1387
1388 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1389 ifname);
1390 wpa_s->p2p_group_idx++;
1391
1392 wpa_s->pending_interface_type = type;
1393 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1394 wpa_s->pending_interface_addr, NULL) < 0) {
1395 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1396 "interface");
1397 return -1;
1398 }
1399
1400 if (force_ifname[0]) {
1401 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1402 force_ifname);
1403 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1404 sizeof(wpa_s->pending_interface_name));
1405 } else
1406 os_strlcpy(wpa_s->pending_interface_name, ifname,
1407 sizeof(wpa_s->pending_interface_name));
1408 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1409 MACSTR, wpa_s->pending_interface_name,
1410 MAC2STR(wpa_s->pending_interface_addr));
1411
1412 return 0;
1413}
1414
1415
1416static void wpas_p2p_remove_pending_group_interface(
1417 struct wpa_supplicant *wpa_s)
1418{
1419 if (!wpa_s->pending_interface_name[0] ||
1420 is_zero_ether_addr(wpa_s->pending_interface_addr))
1421 return; /* No pending virtual interface */
1422
1423 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1424 wpa_s->pending_interface_name);
1425 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1426 wpa_s->pending_interface_name);
1427 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1428 wpa_s->pending_interface_name[0] = '\0';
1429}
1430
1431
1432static struct wpa_supplicant *
1433wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1434{
1435 struct wpa_interface iface;
1436 struct wpa_supplicant *group_wpa_s;
1437
1438 if (!wpa_s->pending_interface_name[0]) {
1439 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1440 if (!wpas_p2p_create_iface(wpa_s))
1441 return NULL;
1442 /*
1443 * Something has forced us to remove the pending interface; try
1444 * to create a new one and hope for the best that we will get
1445 * the same local address.
1446 */
1447 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1448 WPA_IF_P2P_CLIENT) < 0)
1449 return NULL;
1450 }
1451
1452 os_memset(&iface, 0, sizeof(iface));
1453 iface.ifname = wpa_s->pending_interface_name;
1454 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001455 if (wpa_s->conf->ctrl_interface == NULL &&
1456 wpa_s->parent != wpa_s &&
1457 wpa_s->p2p_mgmt &&
1458 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1459 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1460 else
1461 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 iface.driver_param = wpa_s->conf->driver_param;
1463 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1464 if (group_wpa_s == NULL) {
1465 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1466 "wpa_supplicant interface");
1467 return NULL;
1468 }
1469 wpa_s->pending_interface_name[0] = '\0';
1470 group_wpa_s->parent = wpa_s;
1471 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1472 P2P_GROUP_INTERFACE_CLIENT;
1473 wpa_s->global->p2p_group_formation = group_wpa_s;
1474
1475 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1476
1477 return group_wpa_s;
1478}
1479
1480
1481static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1482 void *timeout_ctx)
1483{
1484 struct wpa_supplicant *wpa_s = eloop_ctx;
1485 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001486 wpas_p2p_group_formation_failed(wpa_s);
1487}
1488
1489
1490void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1491{
1492 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1493 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 if (wpa_s->global->p2p)
1495 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 wpas_group_formation_completed(wpa_s, 0);
1497}
1498
1499
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001500static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1501{
1502 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1503 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1504 wpa_s->parent, NULL);
1505 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1506 wpa_s->parent, NULL);
1507 wpa_s->global->p2p_fail_on_wps_complete = 0;
1508}
1509
1510
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001511void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1512{
1513 if (wpa_s->global->p2p_group_formation != wpa_s)
1514 return;
1515 /* Speed up group formation timeout since this cannot succeed */
1516 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1517 wpa_s->parent, NULL);
1518 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1519 wpa_s->parent, NULL);
1520}
1521
1522
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001523static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524{
1525 struct wpa_supplicant *wpa_s = ctx;
1526
1527 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1528 wpa_drv_cancel_remain_on_channel(wpa_s);
1529 wpa_s->off_channel_freq = 0;
1530 wpa_s->roc_waiting_drv_freq = 0;
1531 }
1532
1533 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001534 wpa_msg_global(wpa_s, MSG_INFO,
1535 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1536 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001537 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001538 wpas_p2p_remove_pending_group_interface(wpa_s);
1539 return;
1540 }
1541
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001542 if (wpa_s->p2p_go_ht40)
1543 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001544 if (wpa_s->p2p_go_vht)
1545 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001546
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001547 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1548 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1549 " wps_method=%s",
1550 res->role_go ? "GO" : "client", res->freq, res->ht40,
1551 MAC2STR(res->peer_device_addr),
1552 MAC2STR(res->peer_interface_addr),
1553 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001554 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555
Dmitry Shmidt04949592012-07-19 12:16:46 -07001556 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1557 struct wpa_ssid *ssid;
1558 ssid = wpa_config_get_network(wpa_s->conf,
1559 wpa_s->p2p_persistent_id);
1560 if (ssid && ssid->disabled == 2 &&
1561 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1562 size_t len = os_strlen(ssid->passphrase);
1563 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1564 "on requested persistent group");
1565 os_memcpy(res->passphrase, ssid->passphrase, len);
1566 res->passphrase[len] = '\0';
1567 }
1568 }
1569
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570 if (wpa_s->create_p2p_iface) {
1571 struct wpa_supplicant *group_wpa_s =
1572 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1573 if (group_wpa_s == NULL) {
1574 wpas_p2p_remove_pending_group_interface(wpa_s);
1575 return;
1576 }
1577 if (group_wpa_s != wpa_s) {
1578 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1579 sizeof(group_wpa_s->p2p_pin));
1580 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1581 }
1582 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1583 wpa_s->pending_interface_name[0] = '\0';
1584 group_wpa_s->p2p_in_provisioning = 1;
1585
1586 if (res->role_go)
1587 wpas_start_wps_go(group_wpa_s, res, 1);
1588 else
1589 wpas_start_wps_enrollee(group_wpa_s, res);
1590 } else {
1591 wpa_s->p2p_in_provisioning = 1;
1592 wpa_s->global->p2p_group_formation = wpa_s;
1593
1594 if (res->role_go)
1595 wpas_start_wps_go(wpa_s, res, 1);
1596 else
1597 wpas_start_wps_enrollee(ctx, res);
1598 }
1599
1600 wpa_s->p2p_long_listen = 0;
1601 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1602
1603 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1604 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1605 (res->peer_config_timeout % 100) * 10000,
1606 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1607}
1608
1609
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001610static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611{
1612 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001613 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1614 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615
1616 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1617}
1618
1619
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001620static void wpas_dev_found(void *ctx, const u8 *addr,
1621 const struct p2p_peer_info *info,
1622 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001623{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001624#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 struct wpa_supplicant *wpa_s = ctx;
1626 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001627 char *wfd_dev_info_hex = NULL;
1628
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001629#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001630 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1631 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001632#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001633
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001634 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1635 " p2p_dev_addr=" MACSTR
1636 " pri_dev_type=%s name='%s' config_methods=0x%x "
1637 "dev_capab=0x%x group_capab=0x%x%s%s",
1638 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1639 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1640 sizeof(devtype)),
1641 info->device_name, info->config_methods,
1642 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001643 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1644 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001645#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001647 os_free(wfd_dev_info_hex);
1648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1650}
1651
1652
1653static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1654{
1655 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001656
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001657 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1658 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1661}
1662
1663
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001664static void wpas_find_stopped(void *ctx)
1665{
1666 struct wpa_supplicant *wpa_s = ctx;
1667 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1668}
1669
1670
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001671struct wpas_p2p_listen_work {
1672 unsigned int freq;
1673 unsigned int duration;
1674 struct wpabuf *probe_resp_ie;
1675};
1676
1677
1678static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1679{
1680 if (lwork == NULL)
1681 return;
1682 wpabuf_free(lwork->probe_resp_ie);
1683 os_free(lwork);
1684}
1685
1686
1687static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1688{
1689 struct wpas_p2p_listen_work *lwork;
1690
1691 if (!wpa_s->p2p_listen_work)
1692 return;
1693
1694 lwork = wpa_s->p2p_listen_work->ctx;
1695 wpas_p2p_listen_work_free(lwork);
1696 radio_work_done(wpa_s->p2p_listen_work);
1697 wpa_s->p2p_listen_work = NULL;
1698}
1699
1700
1701static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1702{
1703 struct wpa_supplicant *wpa_s = work->wpa_s;
1704 struct wpas_p2p_listen_work *lwork = work->ctx;
1705
1706 if (deinit) {
1707 wpas_p2p_listen_work_free(lwork);
1708 return;
1709 }
1710
1711 wpa_s->p2p_listen_work = work;
1712
1713 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1714
1715 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1716 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1717 "report received Probe Request frames");
1718 wpas_p2p_listen_work_done(wpa_s);
1719 return;
1720 }
1721
1722 wpa_s->pending_listen_freq = lwork->freq;
1723 wpa_s->pending_listen_duration = lwork->duration;
1724
1725 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1726 {
1727 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1728 "to remain on channel (%u MHz) for Listen "
1729 "state", lwork->freq);
1730 wpas_p2p_listen_work_done(wpa_s);
1731 wpa_s->pending_listen_freq = 0;
1732 return;
1733 }
1734 wpa_s->off_channel_freq = 0;
1735 wpa_s->roc_waiting_drv_freq = lwork->freq;
1736}
1737
1738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739static int wpas_start_listen(void *ctx, unsigned int freq,
1740 unsigned int duration,
1741 const struct wpabuf *probe_resp_ie)
1742{
1743 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001744 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001746 if (wpa_s->p2p_listen_work) {
1747 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 return -1;
1749 }
1750
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001751 lwork = os_zalloc(sizeof(*lwork));
1752 if (lwork == NULL)
1753 return -1;
1754 lwork->freq = freq;
1755 lwork->duration = duration;
1756 if (probe_resp_ie) {
1757 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1758 if (lwork->probe_resp_ie == NULL) {
1759 wpas_p2p_listen_work_free(lwork);
1760 return -1;
1761 }
1762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001764 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1765 lwork) < 0) {
1766 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 return -1;
1768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769
1770 return 0;
1771}
1772
1773
1774static void wpas_stop_listen(void *ctx)
1775{
1776 struct wpa_supplicant *wpa_s = ctx;
1777 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1778 wpa_drv_cancel_remain_on_channel(wpa_s);
1779 wpa_s->off_channel_freq = 0;
1780 wpa_s->roc_waiting_drv_freq = 0;
1781 }
1782 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1783 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001784 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785}
1786
1787
1788static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1789{
1790 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001791 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792}
1793
1794
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001795/*
1796 * DNS Header section is used only to calculate compression pointers, so the
1797 * contents of this data does not matter, but the length needs to be reserved
1798 * in the virtual packet.
1799 */
1800#define DNS_HEADER_LEN 12
1801
1802/*
1803 * 27-octet in-memory packet from P2P specification containing two implied
1804 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1805 */
1806#define P2P_SD_IN_MEMORY_LEN 27
1807
1808static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1809 u8 **spos, const u8 *end)
1810{
1811 while (*spos < end) {
1812 u8 val = ((*spos)[0] & 0xc0) >> 6;
1813 int len;
1814
1815 if (val == 1 || val == 2) {
1816 /* These are reserved values in RFC 1035 */
1817 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1818 "sequence starting with 0x%x", val);
1819 return -1;
1820 }
1821
1822 if (val == 3) {
1823 u16 offset;
1824 u8 *spos_tmp;
1825
1826 /* Offset */
1827 if (*spos + 2 > end) {
1828 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1829 "DNS offset field");
1830 return -1;
1831 }
1832
1833 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1834 if (offset >= *spos - start) {
1835 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1836 "pointer offset %u", offset);
1837 return -1;
1838 }
1839
1840 (*spos) += 2;
1841 spos_tmp = start + offset;
1842 return p2p_sd_dns_uncompress_label(upos, uend, start,
1843 &spos_tmp,
1844 *spos - 2);
1845 }
1846
1847 /* Label */
1848 len = (*spos)[0] & 0x3f;
1849 if (len == 0)
1850 return 0;
1851
1852 (*spos)++;
1853 if (*spos + len > end) {
1854 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1855 "sequence - no room for label with length "
1856 "%u", len);
1857 return -1;
1858 }
1859
1860 if (*upos + len + 2 > uend)
1861 return -2;
1862
1863 os_memcpy(*upos, *spos, len);
1864 *spos += len;
1865 *upos += len;
1866 (*upos)[0] = '.';
1867 (*upos)++;
1868 (*upos)[0] = '\0';
1869 }
1870
1871 return 0;
1872}
1873
1874
1875/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1876 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1877 * not large enough */
1878static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1879 size_t msg_len, size_t offset)
1880{
1881 /* 27-octet in-memory packet from P2P specification */
1882 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1883 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1884 u8 *tmp, *end, *spos;
1885 char *upos, *uend;
1886 int ret = 0;
1887
1888 if (buf_len < 2)
1889 return -1;
1890 if (offset > msg_len)
1891 return -1;
1892
1893 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1894 if (tmp == NULL)
1895 return -1;
1896 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1897 end = spos + msg_len;
1898 spos += offset;
1899
1900 os_memset(tmp, 0, DNS_HEADER_LEN);
1901 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1902 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1903
1904 upos = buf;
1905 uend = buf + buf_len;
1906
1907 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1908 if (ret) {
1909 os_free(tmp);
1910 return ret;
1911 }
1912
1913 if (upos == buf) {
1914 upos[0] = '.';
1915 upos[1] = '\0';
1916 } else if (upos[-1] == '.')
1917 upos[-1] = '\0';
1918
1919 os_free(tmp);
1920 return 0;
1921}
1922
1923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924static struct p2p_srv_bonjour *
1925wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1926 const struct wpabuf *query)
1927{
1928 struct p2p_srv_bonjour *bsrv;
1929 size_t len;
1930
1931 len = wpabuf_len(query);
1932 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1933 struct p2p_srv_bonjour, list) {
1934 if (len == wpabuf_len(bsrv->query) &&
1935 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1936 len) == 0)
1937 return bsrv;
1938 }
1939 return NULL;
1940}
1941
1942
1943static struct p2p_srv_upnp *
1944wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1945 const char *service)
1946{
1947 struct p2p_srv_upnp *usrv;
1948
1949 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1950 struct p2p_srv_upnp, list) {
1951 if (version == usrv->version &&
1952 os_strcmp(service, usrv->service) == 0)
1953 return usrv;
1954 }
1955 return NULL;
1956}
1957
1958
1959static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1960 u8 srv_trans_id)
1961{
1962 u8 *len_pos;
1963
1964 if (wpabuf_tailroom(resp) < 5)
1965 return;
1966
1967 /* Length (to be filled) */
1968 len_pos = wpabuf_put(resp, 2);
1969 wpabuf_put_u8(resp, srv_proto);
1970 wpabuf_put_u8(resp, srv_trans_id);
1971 /* Status Code */
1972 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1973 /* Response Data: empty */
1974 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1975}
1976
1977
1978static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1979 struct wpabuf *resp, u8 srv_trans_id)
1980{
1981 struct p2p_srv_bonjour *bsrv;
1982 u8 *len_pos;
1983
1984 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1985
1986 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1987 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1988 return;
1989 }
1990
1991 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1992 struct p2p_srv_bonjour, list) {
1993 if (wpabuf_tailroom(resp) <
1994 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1995 return;
1996 /* Length (to be filled) */
1997 len_pos = wpabuf_put(resp, 2);
1998 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1999 wpabuf_put_u8(resp, srv_trans_id);
2000 /* Status Code */
2001 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2002 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2003 wpabuf_head(bsrv->resp),
2004 wpabuf_len(bsrv->resp));
2005 /* Response Data */
2006 wpabuf_put_buf(resp, bsrv->query); /* Key */
2007 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2008 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2009 2);
2010 }
2011}
2012
2013
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002014static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2015 size_t query_len)
2016{
2017 char str_rx[256], str_srv[256];
2018
2019 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2020 return 0; /* Too short to include DNS Type and Version */
2021 if (os_memcmp(query + query_len - 3,
2022 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2023 3) != 0)
2024 return 0; /* Mismatch in DNS Type or Version */
2025 if (query_len == wpabuf_len(bsrv->query) &&
2026 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2027 return 1; /* Binary match */
2028
2029 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2030 0))
2031 return 0; /* Failed to uncompress query */
2032 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2033 wpabuf_head(bsrv->query),
2034 wpabuf_len(bsrv->query) - 3, 0))
2035 return 0; /* Failed to uncompress service */
2036
2037 return os_strcmp(str_rx, str_srv) == 0;
2038}
2039
2040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2042 struct wpabuf *resp, u8 srv_trans_id,
2043 const u8 *query, size_t query_len)
2044{
2045 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002047 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048
2049 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2050 query, query_len);
2051 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2052 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2053 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2054 srv_trans_id);
2055 return;
2056 }
2057
2058 if (query_len == 0) {
2059 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2060 return;
2061 }
2062
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002063 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2064 struct p2p_srv_bonjour, list) {
2065 if (!match_bonjour_query(bsrv, query, query_len))
2066 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002068 if (wpabuf_tailroom(resp) <
2069 5 + query_len + wpabuf_len(bsrv->resp))
2070 return;
2071
2072 matches++;
2073
2074 /* Length (to be filled) */
2075 len_pos = wpabuf_put(resp, 2);
2076 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2077 wpabuf_put_u8(resp, srv_trans_id);
2078
2079 /* Status Code */
2080 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2081 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2082 wpabuf_head(bsrv->resp),
2083 wpabuf_len(bsrv->resp));
2084
2085 /* Response Data */
2086 wpabuf_put_data(resp, query, query_len); /* Key */
2087 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2088
2089 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2090 }
2091
2092 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2094 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002095 if (wpabuf_tailroom(resp) < 5)
2096 return;
2097
2098 /* Length (to be filled) */
2099 len_pos = wpabuf_put(resp, 2);
2100 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2101 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102
2103 /* Status Code */
2104 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2105 /* Response Data: empty */
2106 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2107 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109}
2110
2111
2112static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2113 struct wpabuf *resp, u8 srv_trans_id)
2114{
2115 struct p2p_srv_upnp *usrv;
2116 u8 *len_pos;
2117
2118 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2119
2120 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2121 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2122 return;
2123 }
2124
2125 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2126 struct p2p_srv_upnp, list) {
2127 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2128 return;
2129
2130 /* Length (to be filled) */
2131 len_pos = wpabuf_put(resp, 2);
2132 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2133 wpabuf_put_u8(resp, srv_trans_id);
2134
2135 /* Status Code */
2136 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2137 /* Response Data */
2138 wpabuf_put_u8(resp, usrv->version);
2139 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2140 usrv->service);
2141 wpabuf_put_str(resp, usrv->service);
2142 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2143 2);
2144 }
2145}
2146
2147
2148static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2149 struct wpabuf *resp, u8 srv_trans_id,
2150 const u8 *query, size_t query_len)
2151{
2152 struct p2p_srv_upnp *usrv;
2153 u8 *len_pos;
2154 u8 version;
2155 char *str;
2156 int count = 0;
2157
2158 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2159 query, query_len);
2160
2161 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2162 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2163 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2164 srv_trans_id);
2165 return;
2166 }
2167
2168 if (query_len == 0) {
2169 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2170 return;
2171 }
2172
2173 if (wpabuf_tailroom(resp) < 5)
2174 return;
2175
2176 /* Length (to be filled) */
2177 len_pos = wpabuf_put(resp, 2);
2178 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2179 wpabuf_put_u8(resp, srv_trans_id);
2180
2181 version = query[0];
2182 str = os_malloc(query_len);
2183 if (str == NULL)
2184 return;
2185 os_memcpy(str, query + 1, query_len - 1);
2186 str[query_len - 1] = '\0';
2187
2188 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2189 struct p2p_srv_upnp, list) {
2190 if (version != usrv->version)
2191 continue;
2192
2193 if (os_strcmp(str, "ssdp:all") != 0 &&
2194 os_strstr(usrv->service, str) == NULL)
2195 continue;
2196
2197 if (wpabuf_tailroom(resp) < 2)
2198 break;
2199 if (count == 0) {
2200 /* Status Code */
2201 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2202 /* Response Data */
2203 wpabuf_put_u8(resp, version);
2204 } else
2205 wpabuf_put_u8(resp, ',');
2206
2207 count++;
2208
2209 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2210 usrv->service);
2211 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2212 break;
2213 wpabuf_put_str(resp, usrv->service);
2214 }
2215 os_free(str);
2216
2217 if (count == 0) {
2218 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2219 "available");
2220 /* Status Code */
2221 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2222 /* Response Data: empty */
2223 }
2224
2225 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2226}
2227
2228
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002229#ifdef CONFIG_WIFI_DISPLAY
2230static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2231 struct wpabuf *resp, u8 srv_trans_id,
2232 const u8 *query, size_t query_len)
2233{
2234 const u8 *pos;
2235 u8 role;
2236 u8 *len_pos;
2237
2238 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2239
2240 if (!wpa_s->global->wifi_display) {
2241 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2242 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2243 srv_trans_id);
2244 return;
2245 }
2246
2247 if (query_len < 1) {
2248 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2249 "Role");
2250 return;
2251 }
2252
2253 if (wpabuf_tailroom(resp) < 5)
2254 return;
2255
2256 pos = query;
2257 role = *pos++;
2258 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2259
2260 /* TODO: role specific handling */
2261
2262 /* Length (to be filled) */
2263 len_pos = wpabuf_put(resp, 2);
2264 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2265 wpabuf_put_u8(resp, srv_trans_id);
2266 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2267
2268 while (pos < query + query_len) {
2269 if (*pos < MAX_WFD_SUBELEMS &&
2270 wpa_s->global->wfd_subelem[*pos] &&
2271 wpabuf_tailroom(resp) >=
2272 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2273 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2274 "subelement %u", *pos);
2275 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2276 }
2277 pos++;
2278 }
2279
2280 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2281}
2282#endif /* CONFIG_WIFI_DISPLAY */
2283
2284
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002285static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2286 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002287{
2288 struct wpa_supplicant *wpa_s = ctx;
2289 const u8 *pos = tlvs;
2290 const u8 *end = tlvs + tlvs_len;
2291 const u8 *tlv_end;
2292 u16 slen;
2293 struct wpabuf *resp;
2294 u8 srv_proto, srv_trans_id;
2295 size_t buf_len;
2296 char *buf;
2297
2298 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2299 tlvs, tlvs_len);
2300 buf_len = 2 * tlvs_len + 1;
2301 buf = os_malloc(buf_len);
2302 if (buf) {
2303 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2304 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2305 MACSTR " %u %u %s",
2306 freq, MAC2STR(sa), dialog_token, update_indic,
2307 buf);
2308 os_free(buf);
2309 }
2310
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002311 if (wpa_s->p2p_sd_over_ctrl_iface) {
2312 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2313 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002314 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002315 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316
2317 resp = wpabuf_alloc(10000);
2318 if (resp == NULL)
2319 return;
2320
2321 while (pos + 1 < end) {
2322 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2323 slen = WPA_GET_LE16(pos);
2324 pos += 2;
2325 if (pos + slen > end || slen < 2) {
2326 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2327 "length");
2328 wpabuf_free(resp);
2329 return;
2330 }
2331 tlv_end = pos + slen;
2332
2333 srv_proto = *pos++;
2334 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2335 srv_proto);
2336 srv_trans_id = *pos++;
2337 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2338 srv_trans_id);
2339
2340 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2341 pos, tlv_end - pos);
2342
2343
2344 if (wpa_s->force_long_sd) {
2345 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2346 "response");
2347 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2348 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2349 goto done;
2350 }
2351
2352 switch (srv_proto) {
2353 case P2P_SERV_ALL_SERVICES:
2354 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2355 "for all services");
2356 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2357 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2358 wpa_printf(MSG_DEBUG, "P2P: No service "
2359 "discovery protocols available");
2360 wpas_sd_add_proto_not_avail(
2361 resp, P2P_SERV_ALL_SERVICES,
2362 srv_trans_id);
2363 break;
2364 }
2365 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2366 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2367 break;
2368 case P2P_SERV_BONJOUR:
2369 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2370 pos, tlv_end - pos);
2371 break;
2372 case P2P_SERV_UPNP:
2373 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2374 pos, tlv_end - pos);
2375 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002376#ifdef CONFIG_WIFI_DISPLAY
2377 case P2P_SERV_WIFI_DISPLAY:
2378 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2379 pos, tlv_end - pos);
2380 break;
2381#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 default:
2383 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2384 "protocol %u", srv_proto);
2385 wpas_sd_add_proto_not_avail(resp, srv_proto,
2386 srv_trans_id);
2387 break;
2388 }
2389
2390 pos = tlv_end;
2391 }
2392
2393done:
2394 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2395 update_indic, tlvs, tlvs_len);
2396
2397 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2398
2399 wpabuf_free(resp);
2400}
2401
2402
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002403static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2404 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405{
2406 struct wpa_supplicant *wpa_s = ctx;
2407 const u8 *pos = tlvs;
2408 const u8 *end = tlvs + tlvs_len;
2409 const u8 *tlv_end;
2410 u16 slen;
2411 size_t buf_len;
2412 char *buf;
2413
2414 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2415 tlvs, tlvs_len);
2416 if (tlvs_len > 1500) {
2417 /* TODO: better way for handling this */
2418 wpa_msg_ctrl(wpa_s, MSG_INFO,
2419 P2P_EVENT_SERV_DISC_RESP MACSTR
2420 " %u <long response: %u bytes>",
2421 MAC2STR(sa), update_indic,
2422 (unsigned int) tlvs_len);
2423 } else {
2424 buf_len = 2 * tlvs_len + 1;
2425 buf = os_malloc(buf_len);
2426 if (buf) {
2427 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2428 wpa_msg_ctrl(wpa_s, MSG_INFO,
2429 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2430 MAC2STR(sa), update_indic, buf);
2431 os_free(buf);
2432 }
2433 }
2434
2435 while (pos < end) {
2436 u8 srv_proto, srv_trans_id, status;
2437
2438 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2439 slen = WPA_GET_LE16(pos);
2440 pos += 2;
2441 if (pos + slen > end || slen < 3) {
2442 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2443 "length");
2444 return;
2445 }
2446 tlv_end = pos + slen;
2447
2448 srv_proto = *pos++;
2449 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2450 srv_proto);
2451 srv_trans_id = *pos++;
2452 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2453 srv_trans_id);
2454 status = *pos++;
2455 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2456 status);
2457
2458 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2459 pos, tlv_end - pos);
2460
2461 pos = tlv_end;
2462 }
2463
2464 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2465}
2466
2467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002468u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2469 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002470{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002472 return 0;
2473 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474}
2475
2476
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002477u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2478 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002479{
2480 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002481 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002482
2483 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2484 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002485 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2487 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2488 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2489 wpabuf_put_u8(tlvs, version);
2490 wpabuf_put_str(tlvs, query);
2491 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2492 wpabuf_free(tlvs);
2493 return ret;
2494}
2495
2496
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002497#ifdef CONFIG_WIFI_DISPLAY
2498
2499static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2500 const struct wpabuf *tlvs)
2501{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002502 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2503 return 0;
2504 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2505}
2506
2507
2508#define MAX_WFD_SD_SUBELEMS 20
2509
2510static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2511 const char *subelems)
2512{
2513 u8 *len;
2514 const char *pos;
2515 int val;
2516 int count = 0;
2517
2518 len = wpabuf_put(tlvs, 2);
2519 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2520 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2521
2522 wpabuf_put_u8(tlvs, role);
2523
2524 pos = subelems;
2525 while (*pos) {
2526 val = atoi(pos);
2527 if (val >= 0 && val < 256) {
2528 wpabuf_put_u8(tlvs, val);
2529 count++;
2530 if (count == MAX_WFD_SD_SUBELEMS)
2531 break;
2532 }
2533 pos = os_strchr(pos + 1, ',');
2534 if (pos == NULL)
2535 break;
2536 pos++;
2537 }
2538
2539 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2540}
2541
2542
2543u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2544 const u8 *dst, const char *role)
2545{
2546 struct wpabuf *tlvs;
2547 u64 ret;
2548 const char *subelems;
2549 u8 id = 1;
2550
2551 subelems = os_strchr(role, ' ');
2552 if (subelems == NULL)
2553 return 0;
2554 subelems++;
2555
2556 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2557 if (tlvs == NULL)
2558 return 0;
2559
2560 if (os_strstr(role, "[source]"))
2561 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2562 if (os_strstr(role, "[pri-sink]"))
2563 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2564 if (os_strstr(role, "[sec-sink]"))
2565 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2566 if (os_strstr(role, "[source+sink]"))
2567 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2568
2569 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2570 wpabuf_free(tlvs);
2571 return ret;
2572}
2573
2574#endif /* CONFIG_WIFI_DISPLAY */
2575
2576
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2580 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002581 return p2p_sd_cancel_request(wpa_s->global->p2p,
2582 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583}
2584
2585
2586void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2587 const u8 *dst, u8 dialog_token,
2588 const struct wpabuf *resp_tlvs)
2589{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2591 return;
2592 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2593 resp_tlvs);
2594}
2595
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2598{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002599 if (wpa_s->global->p2p)
2600 p2p_sd_service_update(wpa_s->global->p2p);
2601}
2602
2603
2604static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2605{
2606 dl_list_del(&bsrv->list);
2607 wpabuf_free(bsrv->query);
2608 wpabuf_free(bsrv->resp);
2609 os_free(bsrv);
2610}
2611
2612
2613static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2614{
2615 dl_list_del(&usrv->list);
2616 os_free(usrv->service);
2617 os_free(usrv);
2618}
2619
2620
2621void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2622{
2623 struct p2p_srv_bonjour *bsrv, *bn;
2624 struct p2p_srv_upnp *usrv, *un;
2625
2626 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2627 struct p2p_srv_bonjour, list)
2628 wpas_p2p_srv_bonjour_free(bsrv);
2629
2630 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2631 struct p2p_srv_upnp, list)
2632 wpas_p2p_srv_upnp_free(usrv);
2633
2634 wpas_p2p_sd_service_update(wpa_s);
2635}
2636
2637
2638int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2639 struct wpabuf *query, struct wpabuf *resp)
2640{
2641 struct p2p_srv_bonjour *bsrv;
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 bsrv = os_zalloc(sizeof(*bsrv));
2644 if (bsrv == NULL)
2645 return -1;
2646 bsrv->query = query;
2647 bsrv->resp = resp;
2648 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2649
2650 wpas_p2p_sd_service_update(wpa_s);
2651 return 0;
2652}
2653
2654
2655int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2656 const struct wpabuf *query)
2657{
2658 struct p2p_srv_bonjour *bsrv;
2659
2660 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2661 if (bsrv == NULL)
2662 return -1;
2663 wpas_p2p_srv_bonjour_free(bsrv);
2664 wpas_p2p_sd_service_update(wpa_s);
2665 return 0;
2666}
2667
2668
2669int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2670 const char *service)
2671{
2672 struct p2p_srv_upnp *usrv;
2673
2674 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2675 return 0; /* Already listed */
2676 usrv = os_zalloc(sizeof(*usrv));
2677 if (usrv == NULL)
2678 return -1;
2679 usrv->version = version;
2680 usrv->service = os_strdup(service);
2681 if (usrv->service == NULL) {
2682 os_free(usrv);
2683 return -1;
2684 }
2685 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2686
2687 wpas_p2p_sd_service_update(wpa_s);
2688 return 0;
2689}
2690
2691
2692int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2693 const char *service)
2694{
2695 struct p2p_srv_upnp *usrv;
2696
2697 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2698 if (usrv == NULL)
2699 return -1;
2700 wpas_p2p_srv_upnp_free(usrv);
2701 wpas_p2p_sd_service_update(wpa_s);
2702 return 0;
2703}
2704
2705
2706static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2707 const u8 *peer, const char *params,
2708 unsigned int generated_pin)
2709{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002710 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2711 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712}
2713
2714
2715static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2716 const u8 *peer, const char *params)
2717{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002718 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2719 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720}
2721
2722
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002723static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2724 const u8 *dev_addr, const u8 *pri_dev_type,
2725 const char *dev_name, u16 supp_config_methods,
2726 u8 dev_capab, u8 group_capab, const u8 *group_id,
2727 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728{
2729 struct wpa_supplicant *wpa_s = ctx;
2730 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002731 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732 u8 empty_dev_type[8];
2733 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002734 struct wpa_supplicant *group = NULL;
2735
2736 if (group_id) {
2737 for (group = wpa_s->global->ifaces; group; group = group->next)
2738 {
2739 struct wpa_ssid *s = group->current_ssid;
2740 if (s != NULL &&
2741 s->mode == WPAS_MODE_P2P_GO &&
2742 group_id_len - ETH_ALEN == s->ssid_len &&
2743 os_memcmp(group_id + ETH_ALEN, s->ssid,
2744 s->ssid_len) == 0)
2745 break;
2746 }
2747 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748
2749 if (pri_dev_type == NULL) {
2750 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2751 pri_dev_type = empty_dev_type;
2752 }
2753 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2754 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002755 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002756 MAC2STR(dev_addr),
2757 wps_dev_type_bin2str(pri_dev_type, devtype,
2758 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002759 dev_name, supp_config_methods, dev_capab, group_capab,
2760 group ? " group=" : "",
2761 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 if (config_methods & WPS_CONFIG_DISPLAY) {
2765 generated_pin = wps_generate_pin();
2766 wpas_prov_disc_local_display(wpa_s, peer, params,
2767 generated_pin);
2768 } else if (config_methods & WPS_CONFIG_KEYPAD)
2769 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2770 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002771 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2772 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002773
2774 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2775 P2P_PROV_DISC_SUCCESS,
2776 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777}
2778
2779
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002780static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781{
2782 struct wpa_supplicant *wpa_s = ctx;
2783 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002784 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002785
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002786 if (wpa_s->pending_pd_before_join &&
2787 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2788 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2789 wpa_s->pending_pd_before_join = 0;
2790 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2791 "join-existing-group operation");
2792 wpas_p2p_join_start(wpa_s);
2793 return;
2794 }
2795
Dmitry Shmidt04949592012-07-19 12:16:46 -07002796 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2797 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2798 os_snprintf(params, sizeof(params), " peer_go=%d",
2799 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2800 else
2801 params[0] = '\0';
2802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002803 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002804 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002805 else if (config_methods & WPS_CONFIG_KEYPAD) {
2806 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002807 wpas_prov_disc_local_display(wpa_s, peer, params,
2808 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002810 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2811 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812
Jouni Malinen75ecf522011-06-27 15:19:46 -07002813 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2814 P2P_PROV_DISC_SUCCESS,
2815 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816}
2817
2818
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002819static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2820 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002821{
2822 struct wpa_supplicant *wpa_s = ctx;
2823
Dmitry Shmidt04949592012-07-19 12:16:46 -07002824 if (wpa_s->p2p_fallback_to_go_neg) {
2825 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2826 "failed - fall back to GO Negotiation");
2827 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2828 return;
2829 }
2830
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002831 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002832 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002833 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2834 "join-existing-group operation (no ACK for PD "
2835 "Req attempts)");
2836 wpas_p2p_join_start(wpa_s);
2837 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002838 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002839
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002840 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2841 " p2p_dev_addr=" MACSTR " status=%d",
2842 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002843
Jouni Malinen75ecf522011-06-27 15:19:46 -07002844 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2845 status, 0, 0);
2846}
2847
2848
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002849static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2850{
2851 if (channels == NULL)
2852 return 1; /* Assume no restrictions */
2853 return p2p_channels_includes_freq(channels, freq);
2854
2855}
2856
2857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2859 const u8 *go_dev_addr, const u8 *ssid,
2860 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002861 int *force_freq, int persistent_group,
2862 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863{
2864 struct wpa_supplicant *wpa_s = ctx;
2865 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 int res;
2867 struct wpa_supplicant *grp;
2868
2869 if (!persistent_group) {
2870 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2871 " to join an active group", MAC2STR(sa));
2872 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2873 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2874 == 0 ||
2875 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2876 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2877 "authorized invitation");
2878 goto accept_inv;
2879 }
2880 /*
2881 * Do not accept the invitation automatically; notify user and
2882 * request approval.
2883 */
2884 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2885 }
2886
2887 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2888 if (grp) {
2889 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2890 "running persistent group");
2891 if (*go)
2892 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2893 goto accept_inv;
2894 }
2895
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002896 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2897 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2898 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2899 "invitation to re-invoke a persistent group");
2900 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2902
2903 for (s = wpa_s->conf->ssid; s; s = s->next) {
2904 if (s->disabled == 2 &&
2905 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2906 s->ssid_len == ssid_len &&
2907 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2908 break;
2909 }
2910
2911 if (!s) {
2912 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2913 " requested reinvocation of an unknown group",
2914 MAC2STR(sa));
2915 return P2P_SC_FAIL_UNKNOWN_GROUP;
2916 }
2917
2918 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2919 *go = 1;
2920 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2921 wpa_printf(MSG_DEBUG, "P2P: The only available "
2922 "interface is already in use - reject "
2923 "invitation");
2924 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2925 }
2926 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2927 } else if (s->mode == WPAS_MODE_P2P_GO) {
2928 *go = 1;
2929 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2930 {
2931 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2932 "interface address for the group");
2933 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2934 }
2935 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2936 ETH_ALEN);
2937 }
2938
2939accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002940 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2941
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002942 /* Get one of the frequencies currently in use */
2943 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002944 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002945 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08002946
2947 if (wpa_s->num_multichan_concurrent < 2 ||
2948 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2949 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2950 *force_freq = res;
2951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952 }
2953
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002954 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2955 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002956 if (*go == 0) {
2957 /* We are the client */
2958 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2959 "running a GO but we are capable of MCC, "
2960 "figure out the best channel to use");
2961 *force_freq = 0;
2962 } else if (!freq_included(channels, *force_freq)) {
2963 /* We are the GO, and *force_freq is not in the
2964 * intersection */
2965 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2966 "in intersection but we are capable of MCC, "
2967 "figure out the best channel to use",
2968 *force_freq);
2969 *force_freq = 0;
2970 }
2971 }
2972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973 return P2P_SC_SUCCESS;
2974}
2975
2976
2977static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2978 const u8 *ssid, size_t ssid_len,
2979 const u8 *go_dev_addr, u8 status,
2980 int op_freq)
2981{
2982 struct wpa_supplicant *wpa_s = ctx;
2983 struct wpa_ssid *s;
2984
2985 for (s = wpa_s->conf->ssid; s; s = s->next) {
2986 if (s->disabled == 2 &&
2987 s->ssid_len == ssid_len &&
2988 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2989 break;
2990 }
2991
2992 if (status == P2P_SC_SUCCESS) {
2993 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2994 " was accepted; op_freq=%d MHz",
2995 MAC2STR(sa), op_freq);
2996 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002997 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002999 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003000 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003002 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003004 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 }
3006 return;
3007 }
3008
3009 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3010 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3011 " was rejected (status %u)", MAC2STR(sa), status);
3012 return;
3013 }
3014
3015 if (!s) {
3016 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003017 wpa_msg_global(wpa_s, MSG_INFO,
3018 P2P_EVENT_INVITATION_RECEIVED
3019 "sa=" MACSTR " go_dev_addr=" MACSTR
3020 " bssid=" MACSTR " unknown-network",
3021 MAC2STR(sa), MAC2STR(go_dev_addr),
3022 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003024 wpa_msg_global(wpa_s, MSG_INFO,
3025 P2P_EVENT_INVITATION_RECEIVED
3026 "sa=" MACSTR " go_dev_addr=" MACSTR
3027 " unknown-network",
3028 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 }
3030 return;
3031 }
3032
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003033 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003034 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3035 "sa=" MACSTR " persistent=%d freq=%d",
3036 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003037 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003038 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3039 "sa=" MACSTR " persistent=%d",
3040 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003041 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003042}
3043
3044
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003045static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3046 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003047 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003048{
3049 size_t i;
3050
3051 if (ssid == NULL)
3052 return;
3053
3054 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3055 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3056 ETH_ALEN) == 0)
3057 break;
3058 }
3059 if (i >= ssid->num_p2p_clients) {
3060 if (ssid->mode != WPAS_MODE_P2P_GO &&
3061 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3062 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3063 "due to invitation result", ssid->id);
3064 wpas_notify_network_removed(wpa_s, ssid);
3065 wpa_config_remove_network(wpa_s->conf, ssid->id);
3066 return;
3067 }
3068 return; /* Peer not found in client list */
3069 }
3070
3071 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003072 "group %d client list%s",
3073 MAC2STR(peer), ssid->id,
3074 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003075 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3076 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3077 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3078 ssid->num_p2p_clients--;
3079#ifndef CONFIG_NO_CONFIG_WRITE
3080 if (wpa_s->parent->conf->update_config &&
3081 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3082 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3083#endif /* CONFIG_NO_CONFIG_WRITE */
3084}
3085
3086
3087static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3088 const u8 *peer)
3089{
3090 struct wpa_ssid *ssid;
3091
3092 wpa_s = wpa_s->global->p2p_invite_group;
3093 if (wpa_s == NULL)
3094 return; /* No known invitation group */
3095 ssid = wpa_s->current_ssid;
3096 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3097 !ssid->p2p_persistent_group)
3098 return; /* Not operating as a GO in persistent group */
3099 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3100 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003101 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003102}
3103
3104
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003105static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003106 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003107 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108{
3109 struct wpa_supplicant *wpa_s = ctx;
3110 struct wpa_ssid *ssid;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003111 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112
3113 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003114 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3115 "status=%d " MACSTR,
3116 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003118 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3119 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 }
3121 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3122
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003123 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3124 status, MAC2STR(peer));
3125 if (wpa_s->pending_invite_ssid_id == -1) {
3126 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3127 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003128 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003131 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3132 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3133 "invitation exchange to indicate readiness for "
3134 "re-invocation");
3135 }
3136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003138 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3139 ssid = wpa_config_get_network(
3140 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003141 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003142 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 wpas_p2p_remove_pending_group_interface(wpa_s);
3144 return;
3145 }
3146
3147 ssid = wpa_config_get_network(wpa_s->conf,
3148 wpa_s->pending_invite_ssid_id);
3149 if (ssid == NULL) {
3150 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3151 "data matching with invitation");
3152 return;
3153 }
3154
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003155 /*
3156 * The peer could have missed our ctrl::ack frame for Invitation
3157 * Response and continue retransmitting the frame. To reduce the
3158 * likelihood of the peer not getting successful TX status for the
3159 * Invitation Response frame, wait a short time here before starting
3160 * the persistent group so that we will remain on the current channel to
3161 * acknowledge any possible retransmission from the peer.
3162 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07003163#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3165 "starting persistent group");
3166 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07003167#else
3168 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
3169 "starting persistent group");
3170 os_sleep(0, 100000);
3171#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003172
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003173 freq = wpa_s->p2p_persistent_go_freq;
3174 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3175 freq_included(channels, neg_freq)) {
3176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
3177 neg_freq);
3178 freq = neg_freq;
3179 }
3180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003181 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003182 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003183 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003184 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3185 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003186 ssid->mode == WPAS_MODE_P2P_GO ?
3187 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3188 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189}
3190
3191
Dmitry Shmidt04949592012-07-19 12:16:46 -07003192static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3193 unsigned int freq)
3194{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003195 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003196}
3197
3198
3199static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3200{
3201 reg->channel[reg->channels] = chan;
3202 reg->channels++;
3203}
3204
3205
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003207 struct p2p_channels *chan,
3208 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003209{
3210 int i, cla = 0;
3211
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003212 os_memset(cli_chan, 0, sizeof(*cli_chan));
3213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3215 "band");
3216
3217 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3218 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003219 chan->reg_class[cla].channels = 0;
3220 for (i = 0; i < 11; i++) {
3221 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3222 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3223 }
3224 if (chan->reg_class[cla].channels)
3225 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226
3227 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3228 "band");
3229
3230 /* Operating class 115 - 5 GHz, channels 36-48 */
3231 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003232 chan->reg_class[cla].channels = 0;
3233 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3234 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3235 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3236 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3237 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3238 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3239 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3240 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3241 if (chan->reg_class[cla].channels)
3242 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243
3244 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3245 "band");
3246
3247 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3248 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003249 chan->reg_class[cla].channels = 0;
3250 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3251 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3252 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3253 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3254 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3255 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3256 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3257 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3258 if (chan->reg_class[cla].channels)
3259 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003260
3261 chan->reg_classes = cla;
3262 return 0;
3263}
3264
3265
3266static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3267 u16 num_modes,
3268 enum hostapd_hw_mode mode)
3269{
3270 u16 i;
3271
3272 for (i = 0; i < num_modes; i++) {
3273 if (modes[i].mode == mode)
3274 return &modes[i];
3275 }
3276
3277 return NULL;
3278}
3279
3280
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003281enum chan_allowed {
3282 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3283};
3284
Dmitry Shmidt04949592012-07-19 12:16:46 -07003285static int has_channel(struct wpa_global *global,
3286 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287{
3288 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003289 unsigned int freq;
3290
3291 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3292 chan * 5;
3293 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003294 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295
3296 for (i = 0; i < mode->num_channels; i++) {
3297 if (mode->channels[i].chan == chan) {
3298 if (flags)
3299 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003300 if (mode->channels[i].flag &
3301 (HOSTAPD_CHAN_DISABLED |
3302 HOSTAPD_CHAN_RADAR))
3303 return NOT_ALLOWED;
3304 if (mode->channels[i].flag &
3305 (HOSTAPD_CHAN_PASSIVE_SCAN |
3306 HOSTAPD_CHAN_NO_IBSS))
3307 return PASSIVE_ONLY;
3308 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003309 }
3310 }
3311
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003312 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313}
3314
3315
3316struct p2p_oper_class_map {
3317 enum hostapd_hw_mode mode;
3318 u8 op_class;
3319 u8 min_chan;
3320 u8 max_chan;
3321 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003322 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323};
3324
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003325static struct p2p_oper_class_map op_class[] = {
3326 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003327#if 0 /* Do not enable HT40 on 2 GHz for now */
3328 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3329 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3330#endif
3331 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3332 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3333 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3334 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3335 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3336 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003337
3338 /*
3339 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3340 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3341 * 80 MHz, but currently use the following definition for simplicity
3342 * (these center frequencies are not actual channels, which makes
3343 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3344 * removing invalid channels.
3345 */
3346 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003347 { -1, 0, 0, 0, 0, BW20 }
3348};
3349
3350
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003351static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3352 struct hostapd_hw_modes *mode,
3353 u8 channel)
3354{
3355 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3356 unsigned int i;
3357
3358 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3359 return 0;
3360
3361 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3362 /*
3363 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3364 * so the center channel is 6 channels away from the start/end.
3365 */
3366 if (channel >= center_channels[i] - 6 &&
3367 channel <= center_channels[i] + 6)
3368 return center_channels[i];
3369
3370 return 0;
3371}
3372
3373
3374static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3375 struct hostapd_hw_modes *mode,
3376 u8 channel, u8 bw)
3377{
3378 u8 center_chan;
3379 int i, flags;
3380 enum chan_allowed res, ret = ALLOWED;
3381
3382 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3383 if (!center_chan)
3384 return NOT_ALLOWED;
3385 if (center_chan >= 58 && center_chan <= 138)
3386 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3387
3388 /* check all the channels are available */
3389 for (i = 0; i < 4; i++) {
3390 int adj_chan = center_chan - 6 + i * 4;
3391
3392 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3393 if (res == NOT_ALLOWED)
3394 return NOT_ALLOWED;
3395 if (res == PASSIVE_ONLY)
3396 ret = PASSIVE_ONLY;
3397
3398 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3399 return NOT_ALLOWED;
3400 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3401 return NOT_ALLOWED;
3402 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3403 return NOT_ALLOWED;
3404 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3405 return NOT_ALLOWED;
3406 }
3407
3408 return ret;
3409}
3410
3411
3412static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3413 struct hostapd_hw_modes *mode,
3414 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003415{
3416 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003417 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003418
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003419 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3420 if (bw == BW40MINUS) {
3421 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3422 return NOT_ALLOWED;
3423 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3424 } else if (bw == BW40PLUS) {
3425 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3426 return NOT_ALLOWED;
3427 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3428 } else if (bw == BW80) {
3429 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3430 }
3431
3432 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3433 return NOT_ALLOWED;
3434 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3435 return PASSIVE_ONLY;
3436 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003437}
3438
3439
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003440static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003441 struct p2p_channels *chan,
3442 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003444 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003445 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003447 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3449 "of all supported channels; assume dualband "
3450 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003451 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 }
3453
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003454 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455
3456 for (op = 0; op_class[op].op_class; op++) {
3457 struct p2p_oper_class_map *o = &op_class[op];
3458 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003459 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003461 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003462 if (mode == NULL)
3463 continue;
3464 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003465 enum chan_allowed res;
3466 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3467 if (res == ALLOWED) {
3468 if (reg == NULL) {
3469 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3470 o->op_class);
3471 reg = &chan->reg_class[cla];
3472 cla++;
3473 reg->reg_class = o->op_class;
3474 }
3475 reg->channel[reg->channels] = ch;
3476 reg->channels++;
3477 } else if (res == PASSIVE_ONLY &&
3478 wpa_s->conf->p2p_add_cli_chan) {
3479 if (cli_reg == NULL) {
3480 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3481 o->op_class);
3482 cli_reg = &cli_chan->reg_class[cli_cla];
3483 cli_cla++;
3484 cli_reg->reg_class = o->op_class;
3485 }
3486 cli_reg->channel[cli_reg->channels] = ch;
3487 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003488 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003489 }
3490 if (reg) {
3491 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3492 reg->channel, reg->channels);
3493 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003494 if (cli_reg) {
3495 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3496 cli_reg->channel, cli_reg->channels);
3497 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498 }
3499
3500 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003501 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003503 return 0;
3504}
3505
3506
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003507int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3508 struct hostapd_hw_modes *mode, u8 channel)
3509{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003510 int op;
3511 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003512
3513 for (op = 0; op_class[op].op_class; op++) {
3514 struct p2p_oper_class_map *o = &op_class[op];
3515 u8 ch;
3516
3517 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3518 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3519 o->bw == BW20 || ch != channel)
3520 continue;
3521 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003522 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003523 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003524 }
3525 }
3526 return 0;
3527}
3528
3529
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003530int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3531 struct hostapd_hw_modes *mode, u8 channel)
3532{
3533 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3534 return 0;
3535
3536 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3537}
3538
3539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3541 size_t buf_len)
3542{
3543 struct wpa_supplicant *wpa_s = ctx;
3544
3545 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3546 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3547 break;
3548 }
3549 if (wpa_s == NULL)
3550 return -1;
3551
3552 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3553}
3554
3555
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003556static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3557{
3558 struct wpa_supplicant *wpa_s = ctx;
3559
3560 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3561 struct wpa_ssid *ssid = wpa_s->current_ssid;
3562 if (ssid == NULL)
3563 continue;
3564 if (ssid->mode != WPAS_MODE_INFRA)
3565 continue;
3566 if (wpa_s->wpa_state != WPA_COMPLETED &&
3567 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3568 continue;
3569 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3570 return 1;
3571 }
3572
3573 return 0;
3574}
3575
3576
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003577static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3578{
3579 struct wpa_supplicant *wpa_s = ctx;
3580 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3581}
3582
3583
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003584int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3585{
3586 struct wpa_interface iface;
3587 struct wpa_supplicant *p2pdev_wpa_s;
3588 char ifname[100];
3589 char force_name[100];
3590 int ret;
3591
3592 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3593 wpa_s->ifname);
3594 force_name[0] = '\0';
3595 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3596 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3597 force_name, wpa_s->pending_interface_addr, NULL);
3598 if (ret < 0) {
3599 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3600 return ret;
3601 }
3602 os_strlcpy(wpa_s->pending_interface_name, ifname,
3603 sizeof(wpa_s->pending_interface_name));
3604
3605 os_memset(&iface, 0, sizeof(iface));
3606 iface.p2p_mgmt = 1;
3607 iface.ifname = wpa_s->pending_interface_name;
3608 iface.driver = wpa_s->driver->name;
3609 iface.driver_param = wpa_s->conf->driver_param;
3610 iface.confname = wpa_s->confname;
3611 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3612 if (!p2pdev_wpa_s) {
3613 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3614 return -1;
3615 }
3616 p2pdev_wpa_s->parent = wpa_s;
3617
3618 wpa_s->pending_interface_name[0] = '\0';
3619 return 0;
3620}
3621
3622
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003623static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3624 const u8 *noa, size_t noa_len)
3625{
3626 struct wpa_supplicant *wpa_s, *intf = ctx;
3627 char hex[100];
3628
3629 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3630 if (wpa_s->waiting_presence_resp)
3631 break;
3632 }
3633 if (!wpa_s) {
3634 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3635 return;
3636 }
3637 wpa_s->waiting_presence_resp = 0;
3638
3639 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3640 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3641 " status=%u noa=%s", MAC2STR(src), status, hex);
3642}
3643
3644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003645/**
3646 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3647 * @global: Pointer to global data from wpa_supplicant_init()
3648 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3649 * Returns: 0 on success, -1 on failure
3650 */
3651int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3652{
3653 struct p2p_config p2p;
3654 unsigned int r;
3655 int i;
3656
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003657 if (wpa_s->conf->p2p_disabled)
3658 return 0;
3659
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003660 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3661 return 0;
3662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663 if (global->p2p)
3664 return 0;
3665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003668 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 p2p.p2p_scan = wpas_p2p_scan;
3670 p2p.send_action = wpas_send_action;
3671 p2p.send_action_done = wpas_send_action_done;
3672 p2p.go_neg_completed = wpas_go_neg_completed;
3673 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3674 p2p.dev_found = wpas_dev_found;
3675 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003676 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003677 p2p.start_listen = wpas_start_listen;
3678 p2p.stop_listen = wpas_stop_listen;
3679 p2p.send_probe_resp = wpas_send_probe_resp;
3680 p2p.sd_request = wpas_sd_request;
3681 p2p.sd_response = wpas_sd_response;
3682 p2p.prov_disc_req = wpas_prov_disc_req;
3683 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003684 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003685 p2p.invitation_process = wpas_invitation_process;
3686 p2p.invitation_received = wpas_invitation_received;
3687 p2p.invitation_result = wpas_invitation_result;
3688 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003689 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003690 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691
3692 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003693 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 p2p.dev_name = wpa_s->conf->device_name;
3695 p2p.manufacturer = wpa_s->conf->manufacturer;
3696 p2p.model_name = wpa_s->conf->model_name;
3697 p2p.model_number = wpa_s->conf->model_number;
3698 p2p.serial_number = wpa_s->conf->serial_number;
3699 if (wpa_s->wps) {
3700 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3701 p2p.config_methods = wpa_s->wps->config_methods;
3702 }
3703
3704 if (wpa_s->conf->p2p_listen_reg_class &&
3705 wpa_s->conf->p2p_listen_channel) {
3706 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3707 p2p.channel = wpa_s->conf->p2p_listen_channel;
3708 } else {
3709 p2p.reg_class = 81;
3710 /*
3711 * Pick one of the social channels randomly as the listen
3712 * channel.
3713 */
3714 os_get_random((u8 *) &r, sizeof(r));
3715 p2p.channel = 1 + (r % 3) * 5;
3716 }
3717 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3718
3719 if (wpa_s->conf->p2p_oper_reg_class &&
3720 wpa_s->conf->p2p_oper_channel) {
3721 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3722 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3723 p2p.cfg_op_channel = 1;
3724 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3725 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3726
3727 } else {
3728 p2p.op_reg_class = 81;
3729 /*
3730 * Use random operation channel from (1, 6, 11) if no other
3731 * preference is indicated.
3732 */
3733 os_get_random((u8 *) &r, sizeof(r));
3734 p2p.op_channel = 1 + (r % 3) * 5;
3735 p2p.cfg_op_channel = 0;
3736 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3737 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3738 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003739
3740 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3741 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3742 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3743 }
3744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3746 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3747 p2p.country[2] = 0x04;
3748 } else
3749 os_memcpy(p2p.country, "XX\x04", 3);
3750
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003751 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003752 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3753 "channel list");
3754 return -1;
3755 }
3756
3757 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3758 WPS_DEV_TYPE_LEN);
3759
3760 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3761 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3762 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3763
3764 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3765 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3766
3767 p2p.max_peers = 100;
3768
3769 if (wpa_s->conf->p2p_ssid_postfix) {
3770 p2p.ssid_postfix_len =
3771 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3772 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3773 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3774 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3775 p2p.ssid_postfix_len);
3776 }
3777
3778 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3779
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003780 p2p.max_listen = wpa_s->max_remain_on_chan;
3781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 global->p2p = p2p_init(&p2p);
3783 if (global->p2p == NULL)
3784 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003785 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786
3787 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3788 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3789 continue;
3790 p2p_add_wps_vendor_extension(
3791 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3792 }
3793
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003794 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796 return 0;
3797}
3798
3799
3800/**
3801 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3802 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3803 *
3804 * This function deinitialize per-interface P2P data.
3805 */
3806void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3807{
3808 if (wpa_s->driver && wpa_s->drv_priv)
3809 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003810
3811 if (wpa_s->go_params) {
3812 /* Clear any stored provisioning info */
3813 p2p_clear_provisioning_info(
3814 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003815 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003816 }
3817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 os_free(wpa_s->go_params);
3819 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3821 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3822 wpa_s->p2p_long_listen = 0;
3823 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3824 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3825 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003826 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003827 wpas_p2p_listen_work_done(wpa_s);
3828 if (wpa_s->p2p_send_action_work) {
3829 os_free(wpa_s->p2p_send_action_work->ctx);
3830 radio_work_done(wpa_s->p2p_send_action_work);
3831 wpa_s->p2p_send_action_work = NULL;
3832 }
3833 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003834
3835 /* TODO: remove group interface from the driver if this wpa_s instance
3836 * is on top of a P2P group interface */
3837}
3838
3839
3840/**
3841 * wpas_p2p_deinit_global - Deinitialize global P2P module
3842 * @global: Pointer to global data from wpa_supplicant_init()
3843 *
3844 * This function deinitializes the global (per device) P2P module.
3845 */
3846void wpas_p2p_deinit_global(struct wpa_global *global)
3847{
3848 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003850 wpa_s = global->ifaces;
3851 if (wpa_s)
3852 wpas_p2p_service_flush(wpa_s);
3853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003854 if (global->p2p == NULL)
3855 return;
3856
3857 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003858 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3859 wpa_s = wpa_s->next;
3860 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003861 tmp = global->ifaces;
3862 while (tmp &&
3863 (tmp == wpa_s ||
3864 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3865 tmp = tmp->next;
3866 }
3867 if (tmp == NULL)
3868 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003869 /* Disconnect from the P2P group and deinit the interface */
3870 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871 }
3872
3873 /*
3874 * Deinit GO data on any possibly remaining interface (if main
3875 * interface is used as GO).
3876 */
3877 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3878 if (wpa_s->ap_iface)
3879 wpas_p2p_group_deinit(wpa_s);
3880 }
3881
3882 p2p_deinit(global->p2p);
3883 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003884 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885}
3886
3887
3888static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3889{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003890 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3891 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003892 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893 if (wpa_s->drv_flags &
3894 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3895 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3896 return 1; /* P2P group requires a new interface in every case
3897 */
3898 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3899 return 0; /* driver does not support concurrent operations */
3900 if (wpa_s->global->ifaces->next)
3901 return 1; /* more that one interface already in use */
3902 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3903 return 1; /* this interface is already in use */
3904 return 0;
3905}
3906
3907
3908static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3909 const u8 *peer_addr,
3910 enum p2p_wps_method wps_method,
3911 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003912 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003913 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003915 if (persistent_group && wpa_s->conf->persistent_reconnect)
3916 persistent_group = 2;
3917
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003918 /*
3919 * Increase GO config timeout if HT40 is used since it takes some time
3920 * to scan channels for coex purposes before the BSS can be started.
3921 */
3922 p2p_set_config_timeout(wpa_s->global->p2p,
3923 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3926 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003927 persistent_group, ssid ? ssid->ssid : NULL,
3928 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003929 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003930}
3931
3932
3933static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3934 const u8 *peer_addr,
3935 enum p2p_wps_method wps_method,
3936 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003937 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003938 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940 if (persistent_group && wpa_s->conf->persistent_reconnect)
3941 persistent_group = 2;
3942
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3944 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003945 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003946 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947}
3948
3949
3950static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3951{
3952 wpa_s->p2p_join_scan_count++;
3953 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3954 wpa_s->p2p_join_scan_count);
3955 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3956 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3957 " for join operationg - stop join attempt",
3958 MAC2STR(wpa_s->pending_join_iface_addr));
3959 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003960 if (wpa_s->p2p_auto_pd) {
3961 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003962 wpa_msg_global(wpa_s, MSG_INFO,
3963 P2P_EVENT_PROV_DISC_FAILURE
3964 " p2p_dev_addr=" MACSTR " status=N/A",
3965 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003966 return;
3967 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003968 wpa_msg_global(wpa_s->parent, MSG_INFO,
3969 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003970 }
3971}
3972
3973
Dmitry Shmidt04949592012-07-19 12:16:46 -07003974static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3975{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003976 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003977
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003978 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3979 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003980 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003981 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003982
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003983 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3984 if (!freqs)
3985 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003986
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003987 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3988 wpa_s->num_multichan_concurrent);
3989 if (num < 0) {
3990 res = 1;
3991 goto exit_free;
3992 }
3993
3994 for (i = 0; i < num; i++) {
3995 if (freqs[i] == freq) {
3996 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3997 freq);
3998 res = 0;
3999 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004000 }
4001 }
4002
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004003 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004004
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004005exit_free:
4006 os_free(freqs);
4007 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004008}
4009
4010
4011static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4012 const u8 *peer_dev_addr)
4013{
4014 struct wpa_bss *bss;
4015 int updated;
4016
4017 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4018 if (bss == NULL)
4019 return -1;
4020 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4021 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4022 "last scan");
4023 return 0;
4024 }
4025
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004026 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4027 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004028 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4029 "%ld.%06ld (%supdated in last scan)",
4030 bss->last_update.sec, bss->last_update.usec,
4031 updated ? "": "not ");
4032
4033 return updated;
4034}
4035
4036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004037static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4038 struct wpa_scan_results *scan_res)
4039{
4040 struct wpa_bss *bss;
4041 int freq;
4042 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4045
4046 if (wpa_s->global->p2p_disabled)
4047 return;
4048
Dmitry Shmidt04949592012-07-19 12:16:46 -07004049 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4050 scan_res ? (int) scan_res->num : -1,
4051 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004052
4053 if (scan_res)
4054 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4055
Dmitry Shmidt04949592012-07-19 12:16:46 -07004056 if (wpa_s->p2p_auto_pd) {
4057 int join = wpas_p2p_peer_go(wpa_s,
4058 wpa_s->pending_join_dev_addr);
4059 if (join == 0 &&
4060 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4061 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004062 bss = wpa_bss_get_bssid_latest(
4063 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004064 if (bss) {
4065 freq = bss->freq;
4066 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4067 "the peer " MACSTR " at %d MHz",
4068 wpa_s->auto_pd_scan_retry,
4069 MAC2STR(wpa_s->
4070 pending_join_dev_addr),
4071 freq);
4072 wpas_p2p_join_scan_req(wpa_s, freq);
4073 return;
4074 }
4075 }
4076
4077 if (join < 0)
4078 join = 0;
4079
4080 wpa_s->p2p_auto_pd = 0;
4081 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4082 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4083 MAC2STR(wpa_s->pending_join_dev_addr), join);
4084 if (p2p_prov_disc_req(wpa_s->global->p2p,
4085 wpa_s->pending_join_dev_addr,
4086 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004087 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004088 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004089 wpa_msg_global(wpa_s, MSG_INFO,
4090 P2P_EVENT_PROV_DISC_FAILURE
4091 " p2p_dev_addr=" MACSTR " status=N/A",
4092 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004093 }
4094 return;
4095 }
4096
4097 if (wpa_s->p2p_auto_join) {
4098 int join = wpas_p2p_peer_go(wpa_s,
4099 wpa_s->pending_join_dev_addr);
4100 if (join < 0) {
4101 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4102 "running a GO -> use GO Negotiation");
4103 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4104 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4105 wpa_s->p2p_persistent_group, 0, 0, 0,
4106 wpa_s->p2p_go_intent,
4107 wpa_s->p2p_connect_freq,
4108 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004109 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004110 wpa_s->p2p_go_ht40,
4111 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004112 return;
4113 }
4114
4115 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4116 "try to join the group", join ? "" :
4117 " in older scan");
4118 if (!join)
4119 wpa_s->p2p_fallback_to_go_neg = 1;
4120 }
4121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4123 wpa_s->pending_join_iface_addr);
4124 if (freq < 0 &&
4125 p2p_get_interface_addr(wpa_s->global->p2p,
4126 wpa_s->pending_join_dev_addr,
4127 iface_addr) == 0 &&
4128 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4129 {
4130 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4131 "address for join from " MACSTR " to " MACSTR
4132 " based on newly discovered P2P peer entry",
4133 MAC2STR(wpa_s->pending_join_iface_addr),
4134 MAC2STR(iface_addr));
4135 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4136 ETH_ALEN);
4137
4138 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4139 wpa_s->pending_join_iface_addr);
4140 }
4141 if (freq >= 0) {
4142 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4143 "from P2P peer table: %d MHz", freq);
4144 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004145 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004146 if (bss) {
4147 freq = bss->freq;
4148 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004149 "from BSS table: %d MHz (SSID %s)", freq,
4150 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 }
4152 if (freq > 0) {
4153 u16 method;
4154
Dmitry Shmidt04949592012-07-19 12:16:46 -07004155 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004156 wpa_msg_global(wpa_s->parent, MSG_INFO,
4157 P2P_EVENT_GROUP_FORMATION_FAILURE
4158 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004159 return;
4160 }
4161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4163 "prior to joining an existing group (GO " MACSTR
4164 " freq=%u MHz)",
4165 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4166 wpa_s->pending_pd_before_join = 1;
4167
4168 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 case WPS_PIN_DISPLAY:
4170 method = WPS_CONFIG_KEYPAD;
4171 break;
4172 case WPS_PIN_KEYPAD:
4173 method = WPS_CONFIG_DISPLAY;
4174 break;
4175 case WPS_PBC:
4176 method = WPS_CONFIG_PUSHBUTTON;
4177 break;
4178 default:
4179 method = 0;
4180 break;
4181 }
4182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004183 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4184 wpa_s->pending_join_dev_addr) ==
4185 method)) {
4186 /*
4187 * We have already performed provision discovery for
4188 * joining the group. Proceed directly to join
4189 * operation without duplicated provision discovery. */
4190 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4191 "with " MACSTR " already done - proceed to "
4192 "join",
4193 MAC2STR(wpa_s->pending_join_dev_addr));
4194 wpa_s->pending_pd_before_join = 0;
4195 goto start;
4196 }
4197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004199 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004200 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004201 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4202 "Discovery Request before joining an "
4203 "existing group");
4204 wpa_s->pending_pd_before_join = 0;
4205 goto start;
4206 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 return;
4208 }
4209
4210 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4211 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4212 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4213 wpas_p2p_check_join_scan_limit(wpa_s);
4214 return;
4215
4216start:
4217 /* Start join operation immediately */
4218 wpas_p2p_join_start(wpa_s);
4219}
4220
4221
Dmitry Shmidt04949592012-07-19 12:16:46 -07004222static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004223{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004224 int ret;
4225 struct wpa_driver_scan_params params;
4226 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004227 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004228 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004229#ifdef ANDROID_P2P
4230 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004231
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07004232 /* If freq is not provided, check the operating freq of the GO and do a
4233 * a directed scan to save time
4234 */
4235 if(!freq) {
4236 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4237 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
4238 }
4239#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240 os_memset(&params, 0, sizeof(params));
4241
4242 /* P2P Wildcard SSID */
4243 params.num_ssids = 1;
4244 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4245 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4246
4247 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004248 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4249 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4250 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004251 if (wps_ie == NULL) {
4252 wpas_p2p_scan_res_join(wpa_s, NULL);
4253 return;
4254 }
4255
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004256 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4257 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004258 if (ies == NULL) {
4259 wpabuf_free(wps_ie);
4260 wpas_p2p_scan_res_join(wpa_s, NULL);
4261 return;
4262 }
4263 wpabuf_put_buf(ies, wps_ie);
4264 wpabuf_free(wps_ie);
4265
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004266 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004267
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004268 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004269 params.extra_ies = wpabuf_head(ies);
4270 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004271 if (freq > 0) {
4272 freqs[0] = freq;
4273 params.freqs = freqs;
4274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275
4276 /*
4277 * Run a scan to update BSS table and start Provision Discovery once
4278 * the new scan results become available.
4279 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004280 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004281 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004282 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004283 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004284 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004285 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004286
4287 wpabuf_free(ies);
4288
4289 if (ret) {
4290 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4291 "try again later");
4292 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4293 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4294 wpas_p2p_check_join_scan_limit(wpa_s);
4295 }
4296}
4297
4298
Dmitry Shmidt04949592012-07-19 12:16:46 -07004299static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4300{
4301 struct wpa_supplicant *wpa_s = eloop_ctx;
4302 wpas_p2p_join_scan_req(wpa_s, 0);
4303}
4304
4305
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004307 const u8 *dev_addr, enum p2p_wps_method wps_method,
4308 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309{
4310 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004311 MACSTR " dev " MACSTR ")%s",
4312 MAC2STR(iface_addr), MAC2STR(dev_addr),
4313 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314
Dmitry Shmidt04949592012-07-19 12:16:46 -07004315 wpa_s->p2p_auto_pd = 0;
4316 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004317 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4318 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4319 wpa_s->pending_join_wps_method = wps_method;
4320
4321 /* Make sure we are not running find during connection establishment */
4322 wpas_p2p_stop_find(wpa_s);
4323
4324 wpa_s->p2p_join_scan_count = 0;
4325 wpas_p2p_join_scan(wpa_s, NULL);
4326 return 0;
4327}
4328
4329
4330static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4331{
4332 struct wpa_supplicant *group;
4333 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004334 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004335
4336 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4337 if (group == NULL)
4338 return -1;
4339 if (group != wpa_s) {
4340 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4341 sizeof(group->p2p_pin));
4342 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004343 } else {
4344 /*
4345 * Need to mark the current interface for p2p_group_formation
4346 * when a separate group interface is not used. This is needed
4347 * to allow p2p_cancel stop a pending p2p_connect-join.
4348 * wpas_p2p_init_group_interface() addresses this for the case
4349 * where a separate group interface is used.
4350 */
4351 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 }
4353
4354 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004355 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004356
4357 os_memset(&res, 0, sizeof(res));
4358 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4359 ETH_ALEN);
4360 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004361 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004362 if (bss) {
4363 res.freq = bss->freq;
4364 res.ssid_len = bss->ssid_len;
4365 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004366 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4367 "from BSS table: %d MHz (SSID %s)", bss->freq,
4368 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004369 }
4370
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004371 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4372 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4373 "starting client");
4374 wpa_drv_cancel_remain_on_channel(wpa_s);
4375 wpa_s->off_channel_freq = 0;
4376 wpa_s->roc_waiting_drv_freq = 0;
4377 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 wpas_start_wps_enrollee(group, &res);
4379
4380 /*
4381 * Allow a longer timeout for join-a-running-group than normal 15
4382 * second group formation timeout since the GO may not have authorized
4383 * our connection yet.
4384 */
4385 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4386 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4387 wpa_s, NULL);
4388
4389 return 0;
4390}
4391
4392
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004393static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004394 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004395{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004396 int *freqs, res;
4397 unsigned int freq_in_use = 0, num, i;
4398
4399 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4400 if (!freqs)
4401 return -1;
4402
4403 num = get_shared_radio_freqs(wpa_s, freqs,
4404 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004405 wpa_printf(MSG_DEBUG,
4406 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4407 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004408
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004409 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004410 int ret;
4411 if (go)
4412 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4413 else
4414 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4415 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004416 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4417 "(%u MHz) is not supported for P2P uses",
4418 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004419 res = -3;
4420 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004421 }
4422
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004423 for (i = 0; i < num; i++) {
4424 if (freqs[i] == freq)
4425 freq_in_use = 1;
4426 }
4427
4428 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4429 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4430 freq);
4431 res = -2;
4432 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004433 }
4434 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4435 "requested channel (%u MHz)", freq);
4436 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004437 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004438 }
4439
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004440 for (i = 0; i < num; i++) {
4441 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4442 continue;
4443
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004444 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004445 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4446 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004447 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004448 } else {
4449 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 -08004450 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004451 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004452 }
4453 break;
4454 }
4455
4456 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004457 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004458 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4459 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004460 } else if (num < wpa_s->num_multichan_concurrent) {
4461 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4462 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004463 } else {
4464 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4465 res = -2;
4466 goto exit_free;
4467 }
4468 }
4469
4470exit_ok:
4471 res = 0;
4472exit_free:
4473 os_free(freqs);
4474 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004475}
4476
4477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478/**
4479 * wpas_p2p_connect - Request P2P Group Formation to be started
4480 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4481 * @peer_addr: Address of the peer P2P Device
4482 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4483 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004484 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004485 * @join: Whether to join an existing group (as a client) instead of starting
4486 * Group Owner negotiation; @peer_addr is BSSID in that case
4487 * @auth: Whether to only authorize the connection instead of doing that and
4488 * initiating Group Owner negotiation
4489 * @go_intent: GO Intent or -1 to use default
4490 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004491 * @persistent_id: Persistent group credentials to use for forcing GO
4492 * parameters or -1 to generate new values (SSID/passphrase)
4493 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4494 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004495 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004496 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4498 * failure, -2 on failure due to channel not currently available,
4499 * -3 if forced channel is not supported
4500 */
4501int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4502 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004503 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004504 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004505 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004506{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004507 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004508 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004509 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004510 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004511 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512
4513 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4514 return -1;
4515
Dmitry Shmidt04949592012-07-19 12:16:46 -07004516 if (persistent_id >= 0) {
4517 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4518 if (ssid == NULL || ssid->disabled != 2 ||
4519 ssid->mode != WPAS_MODE_P2P_GO)
4520 return -1;
4521 }
4522
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004523 os_free(wpa_s->global->add_psk);
4524 wpa_s->global->add_psk = NULL;
4525
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004526 wpa_s->global->p2p_fail_on_wps_complete = 0;
4527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004528 if (go_intent < 0)
4529 go_intent = wpa_s->conf->p2p_go_intent;
4530
4531 if (!auth)
4532 wpa_s->p2p_long_listen = 0;
4533
4534 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004535 wpa_s->p2p_persistent_group = !!persistent_group;
4536 wpa_s->p2p_persistent_id = persistent_id;
4537 wpa_s->p2p_go_intent = go_intent;
4538 wpa_s->p2p_connect_freq = freq;
4539 wpa_s->p2p_fallback_to_go_neg = 0;
4540 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004541 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004542 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004543
4544 if (pin)
4545 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4546 else if (wps_method == WPS_PIN_DISPLAY) {
4547 ret = wps_generate_pin();
4548 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4549 ret);
4550 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4551 wpa_s->p2p_pin);
4552 } else
4553 wpa_s->p2p_pin[0] = '\0';
4554
Dmitry Shmidt04949592012-07-19 12:16:46 -07004555 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004556 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4557 if (auth) {
4558 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4559 "connect a running group from " MACSTR,
4560 MAC2STR(peer_addr));
4561 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4562 return ret;
4563 }
4564 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4565 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4566 iface_addr) < 0) {
4567 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4568 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4569 dev_addr);
4570 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004571 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004572 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004573 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4574 "%ld.%06ld",
4575 wpa_s->p2p_auto_started.sec,
4576 wpa_s->p2p_auto_started.usec);
4577 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004578 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004579 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4580 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004581 return -1;
4582 return ret;
4583 }
4584
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004585 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4586 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004587 if (res)
4588 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004589 wpas_p2p_set_own_freq_preference(wpa_s,
4590 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004591
4592 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4593
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004594 if (wpa_s->create_p2p_iface) {
4595 /* Prepare to add a new interface for the group */
4596 iftype = WPA_IF_P2P_GROUP;
4597 if (go_intent == 15)
4598 iftype = WPA_IF_P2P_GO;
4599 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4600 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4601 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004602 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004604
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004605 if_addr = wpa_s->pending_interface_addr;
4606 } else
4607 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004608
4609 if (auth) {
4610 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004611 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004612 force_freq, persistent_group, ssid,
4613 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004614 return -1;
4615 return ret;
4616 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004617
4618 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4619 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004620 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004621 if (wpa_s->create_p2p_iface)
4622 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004623 return -1;
4624 }
4625 return ret;
4626}
4627
4628
4629/**
4630 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4631 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4632 * @freq: Frequency of the channel in MHz
4633 * @duration: Duration of the stay on the channel in milliseconds
4634 *
4635 * This callback is called when the driver indicates that it has started the
4636 * requested remain-on-channel duration.
4637 */
4638void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4639 unsigned int freq, unsigned int duration)
4640{
4641 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4642 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004643 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4644 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4645 wpa_s->pending_listen_duration);
4646 wpa_s->pending_listen_freq = 0;
4647 }
4648}
4649
4650
4651static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4652 unsigned int timeout)
4653{
4654 /* Limit maximum Listen state time based on driver limitation. */
4655 if (timeout > wpa_s->max_remain_on_chan)
4656 timeout = wpa_s->max_remain_on_chan;
4657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004658 return p2p_listen(wpa_s->global->p2p, timeout);
4659}
4660
4661
4662/**
4663 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4664 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4665 * @freq: Frequency of the channel in MHz
4666 *
4667 * This callback is called when the driver indicates that a remain-on-channel
4668 * operation has been completed, i.e., the duration on the requested channel
4669 * has timed out.
4670 */
4671void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4672 unsigned int freq)
4673{
4674 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4675 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004676 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004677 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004678 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4679 return;
4680 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4681 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004682 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 return;
4684 if (wpa_s->p2p_long_listen > 0)
4685 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4686 if (wpa_s->p2p_long_listen > 0) {
4687 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4688 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004689 } else {
4690 /*
4691 * When listen duration is over, stop listen & update p2p_state
4692 * to IDLE.
4693 */
4694 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004695 }
4696}
4697
4698
4699/**
4700 * wpas_p2p_group_remove - Remove a P2P group
4701 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4702 * @ifname: Network interface name of the group interface or "*" to remove all
4703 * groups
4704 * Returns: 0 on success, -1 on failure
4705 *
4706 * This function is used to remove a P2P group. This can be used to disconnect
4707 * from a group in which the local end is a P2P Client or to end a P2P Group in
4708 * case the local end is the Group Owner. If a virtual network interface was
4709 * created for this group, that interface will be removed. Otherwise, only the
4710 * configured P2P group network will be removed from the interface.
4711 */
4712int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4713{
4714 struct wpa_global *global = wpa_s->global;
4715
4716 if (os_strcmp(ifname, "*") == 0) {
4717 struct wpa_supplicant *prev;
4718 wpa_s = global->ifaces;
4719 while (wpa_s) {
4720 prev = wpa_s;
4721 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004722 if (prev->p2p_group_interface !=
4723 NOT_P2P_GROUP_INTERFACE ||
4724 (prev->current_ssid &&
4725 prev->current_ssid->p2p_group))
4726 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004727 }
4728 return 0;
4729 }
4730
4731 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4732 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4733 break;
4734 }
4735
4736 return wpas_p2p_disconnect(wpa_s);
4737}
4738
4739
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004740static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4741{
4742 unsigned int r;
4743
4744 if (freq == 2) {
4745 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4746 "band");
4747 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004748 p2p_supported_freq_go(wpa_s->global->p2p,
4749 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004750 freq = wpa_s->best_24_freq;
4751 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4752 "channel: %d MHz", freq);
4753 } else {
4754 os_get_random((u8 *) &r, sizeof(r));
4755 freq = 2412 + (r % 3) * 25;
4756 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4757 "channel: %d MHz", freq);
4758 }
4759 }
4760
4761 if (freq == 5) {
4762 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4763 "band");
4764 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004765 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004766 wpa_s->best_5_freq)) {
4767 freq = wpa_s->best_5_freq;
4768 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4769 "channel: %d MHz", freq);
4770 } else {
4771 os_get_random((u8 *) &r, sizeof(r));
4772 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004773 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004774 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4775 "5 GHz channel for P2P group");
4776 return -1;
4777 }
4778 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4779 "channel: %d MHz", freq);
4780 }
4781 }
4782
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004783 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004784 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4785 "(%u MHz) is not supported for P2P uses",
4786 freq);
4787 return -1;
4788 }
4789
4790 return freq;
4791}
4792
4793
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004794static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4795 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004796 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004797 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004799 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004800 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004801 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004802
4803 os_memset(params, 0, sizeof(*params));
4804 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004805 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004806 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004807 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004808 if (!freq_included(channels, freq)) {
4809 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4810 "accepted", freq);
4811 return -1;
4812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004813 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4814 "frequency %d MHz", freq);
4815 params->freq = freq;
4816 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4817 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004818 wpa_s->conf->p2p_oper_channel <= 11 &&
4819 freq_included(channels,
4820 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004821 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4822 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4823 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004824 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4825 wpa_s->conf->p2p_oper_reg_class == 116 ||
4826 wpa_s->conf->p2p_oper_reg_class == 117 ||
4827 wpa_s->conf->p2p_oper_reg_class == 124 ||
4828 wpa_s->conf->p2p_oper_reg_class == 126 ||
4829 wpa_s->conf->p2p_oper_reg_class == 127) &&
4830 freq_included(channels,
4831 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004832 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4833 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4834 "frequency %d MHz", params->freq);
4835 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4836 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004837 p2p_supported_freq_go(wpa_s->global->p2p,
4838 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004839 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004840 params->freq = wpa_s->best_overall_freq;
4841 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4842 "channel %d MHz", params->freq);
4843 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4844 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004845 p2p_supported_freq_go(wpa_s->global->p2p,
4846 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004847 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 params->freq = wpa_s->best_24_freq;
4849 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4850 "channel %d MHz", params->freq);
4851 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4852 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004853 p2p_supported_freq_go(wpa_s->global->p2p,
4854 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004855 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004856 params->freq = wpa_s->best_5_freq;
4857 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4858 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004859 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4860 channels))) {
4861 params->freq = pref_freq;
4862 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4863 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004864 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004865 int chan;
4866 for (chan = 0; chan < 11; chan++) {
4867 params->freq = 2412 + chan * 5;
4868 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004869 params->freq) &&
4870 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004871 break;
4872 }
4873 if (chan == 11) {
4874 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4875 "allowed");
4876 return -1;
4877 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4879 "known)", params->freq);
4880 }
4881
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004882 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4883 if (!freqs)
4884 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004886 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4887 wpa_s->num_multichan_concurrent);
4888 if (res < 0) {
4889 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004890 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004891 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004892 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004893
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004894 for (i = 0; i < num; i++) {
4895 if (freq && freqs[i] == freq)
4896 break;
4897 if (!freq && freq_included(channels, freqs[i])) {
4898 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4899 freqs[i]);
4900 params->freq = freqs[i];
4901 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004902 }
4903 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004904
4905 if (i == num) {
4906 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4907 if (freq)
4908 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4909 else
4910 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4911 os_free(freqs);
4912 return -1;
4913 } else if (num == 0) {
4914 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4915 } else {
4916 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4917 }
4918 }
4919
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004920 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004921 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004922}
4923
4924
4925static struct wpa_supplicant *
4926wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4927 int go)
4928{
4929 struct wpa_supplicant *group_wpa_s;
4930
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004931 if (!wpas_p2p_create_iface(wpa_s)) {
4932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4933 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004934 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004936 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004937
4938 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004939 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004940 wpa_msg_global(wpa_s, MSG_ERROR,
4941 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004942 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4945 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004946 wpa_msg_global(wpa_s, MSG_ERROR,
4947 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948 wpas_p2p_remove_pending_group_interface(wpa_s);
4949 return NULL;
4950 }
4951
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004952 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4953 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004954 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004955 return group_wpa_s;
4956}
4957
4958
4959/**
4960 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4961 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4962 * @persistent_group: Whether to create a persistent group
4963 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004964 * @ht40: Start GO with 40 MHz channel width
4965 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004966 * Returns: 0 on success, -1 on failure
4967 *
4968 * This function creates a new P2P group with the local end as the Group Owner,
4969 * i.e., without using Group Owner Negotiation.
4970 */
4971int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004972 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973{
4974 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004975
4976 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4977 return -1;
4978
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004979 os_free(wpa_s->global->add_psk);
4980 wpa_s->global->add_psk = NULL;
4981
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004982 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004983 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004984 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004985
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004986 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4987 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004988 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004989
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004990 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004991 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004992 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004993 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004994 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4995 "(%u MHz) is not supported for P2P uses",
4996 params.freq);
4997 return -1;
4998 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004999 p2p_go_params(wpa_s->global->p2p, &params);
5000 params.persistent_group = persistent_group;
5001
5002 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5003 if (wpa_s == NULL)
5004 return -1;
5005 wpas_start_wps_go(wpa_s, &params, 0);
5006
5007 return 0;
5008}
5009
5010
5011static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5012 struct wpa_ssid *params, int addr_allocated)
5013{
5014 struct wpa_ssid *ssid;
5015
5016 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5017 if (wpa_s == NULL)
5018 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005019 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005020
5021 wpa_supplicant_ap_deinit(wpa_s);
5022
5023 ssid = wpa_config_add_network(wpa_s->conf);
5024 if (ssid == NULL)
5025 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 wpa_config_set_network_defaults(ssid);
5027 ssid->temporary = 1;
5028 ssid->proto = WPA_PROTO_RSN;
5029 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5030 ssid->group_cipher = WPA_CIPHER_CCMP;
5031 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5032 ssid->ssid = os_malloc(params->ssid_len);
5033 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005034 wpa_config_remove_network(wpa_s->conf, ssid->id);
5035 return -1;
5036 }
5037 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5038 ssid->ssid_len = params->ssid_len;
5039 ssid->p2p_group = 1;
5040 ssid->export_keys = 1;
5041 if (params->psk_set) {
5042 os_memcpy(ssid->psk, params->psk, 32);
5043 ssid->psk_set = 1;
5044 }
5045 if (params->passphrase)
5046 ssid->passphrase = os_strdup(params->passphrase);
5047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005048 wpa_s->show_group_started = 1;
5049
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005050 wpa_supplicant_select_network(wpa_s, ssid);
5051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005052 return 0;
5053}
5054
5055
5056int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5057 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005058 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005059 const struct p2p_channels *channels,
5060 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005061{
5062 struct p2p_go_neg_results params;
5063 int go = 0;
5064
5065 if (ssid->disabled != 2 || ssid->ssid == NULL)
5066 return -1;
5067
5068 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5069 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5070 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5071 "already running");
5072 return 0;
5073 }
5074
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005075 os_free(wpa_s->global->add_psk);
5076 wpa_s->global->add_psk = NULL;
5077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005078 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005079 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080
Dmitry Shmidt04949592012-07-19 12:16:46 -07005081 wpa_s->p2p_fallback_to_go_neg = 0;
5082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005083 if (ssid->mode == WPAS_MODE_INFRA)
5084 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5085
5086 if (ssid->mode != WPAS_MODE_P2P_GO)
5087 return -1;
5088
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005089 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5090 if (freq < 0)
5091 return -1;
5092
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005093 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005094 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095
5096 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005097 params.psk_set = ssid->psk_set;
5098 if (params.psk_set)
5099 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005100 if (ssid->passphrase) {
5101 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5102 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5103 "persistent group");
5104 return -1;
5105 }
5106 os_strlcpy(params.passphrase, ssid->passphrase,
5107 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005108 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005109 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5110 params.ssid_len = ssid->ssid_len;
5111 params.persistent_group = 1;
5112
5113 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5114 if (wpa_s == NULL)
5115 return -1;
5116
Dmitry Shmidt56052862013-10-04 10:23:25 -07005117 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005118 wpas_start_wps_go(wpa_s, &params, 0);
5119
5120 return 0;
5121}
5122
5123
5124static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5125 struct wpabuf *proberesp_ies)
5126{
5127 struct wpa_supplicant *wpa_s = ctx;
5128 if (wpa_s->ap_iface) {
5129 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005130 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5131 wpabuf_free(beacon_ies);
5132 wpabuf_free(proberesp_ies);
5133 return;
5134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005135 if (beacon_ies) {
5136 wpabuf_free(hapd->p2p_beacon_ie);
5137 hapd->p2p_beacon_ie = beacon_ies;
5138 }
5139 wpabuf_free(hapd->p2p_probe_resp_ie);
5140 hapd->p2p_probe_resp_ie = proberesp_ies;
5141 } else {
5142 wpabuf_free(beacon_ies);
5143 wpabuf_free(proberesp_ies);
5144 }
5145 wpa_supplicant_ap_update_beacon(wpa_s);
5146}
5147
5148
5149static void wpas_p2p_idle_update(void *ctx, int idle)
5150{
5151 struct wpa_supplicant *wpa_s = ctx;
5152 if (!wpa_s->ap_iface)
5153 return;
5154 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005155 if (idle) {
5156 if (wpa_s->global->p2p_fail_on_wps_complete &&
5157 wpa_s->p2p_in_provisioning) {
5158 wpas_p2p_grpform_fail_after_wps(wpa_s);
5159 return;
5160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005161 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005162 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005163 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5164}
5165
5166
5167struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005168 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005169{
5170 struct p2p_group *group;
5171 struct p2p_group_config *cfg;
5172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005173 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5174 return NULL;
5175
5176 cfg = os_zalloc(sizeof(*cfg));
5177 if (cfg == NULL)
5178 return NULL;
5179
Dmitry Shmidt04949592012-07-19 12:16:46 -07005180 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005181 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005182 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005183 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005184 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5185 if (wpa_s->max_stations &&
5186 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5187 cfg->max_clients = wpa_s->max_stations;
5188 else
5189 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005190 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5191 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005192 cfg->cb_ctx = wpa_s;
5193 cfg->ie_update = wpas_p2p_ie_update;
5194 cfg->idle_update = wpas_p2p_idle_update;
5195
5196 group = p2p_group_init(wpa_s->global->p2p, cfg);
5197 if (group == NULL)
5198 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005199 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005200 p2p_group_notif_formation_done(group);
5201 wpa_s->p2p_group = group;
5202 return group;
5203}
5204
5205
5206void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5207 int registrar)
5208{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005209 struct wpa_ssid *ssid = wpa_s->current_ssid;
5210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211 if (!wpa_s->p2p_in_provisioning) {
5212 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5213 "provisioning not in progress");
5214 return;
5215 }
5216
Dmitry Shmidt04949592012-07-19 12:16:46 -07005217 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5218 u8 go_dev_addr[ETH_ALEN];
5219 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5220 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5221 ssid->ssid_len);
5222 /* Clear any stored provisioning info */
5223 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5224 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005226 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5227 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005228 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005229 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5230 /*
5231 * Use a separate timeout for initial data connection to
5232 * complete to allow the group to be removed automatically if
5233 * something goes wrong in this step before the P2P group idle
5234 * timeout mechanism is taken into use.
5235 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005236 wpa_dbg(wpa_s, MSG_DEBUG,
5237 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5238 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005239 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5240 wpas_p2p_group_formation_timeout,
5241 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005242 } else if (ssid) {
5243 /*
5244 * Use a separate timeout for initial data connection to
5245 * complete to allow the group to be removed automatically if
5246 * the client does not complete data connection successfully.
5247 */
5248 wpa_dbg(wpa_s, MSG_DEBUG,
5249 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5250 P2P_MAX_INITIAL_CONN_WAIT_GO);
5251 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5252 wpas_p2p_group_formation_timeout,
5253 wpa_s->parent, NULL);
5254 /*
5255 * Complete group formation on first successful data connection
5256 */
5257 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005258 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005259 if (wpa_s->global->p2p)
5260 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 wpas_group_formation_completed(wpa_s, 1);
5262}
5263
5264
Jouni Malinen75ecf522011-06-27 15:19:46 -07005265void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5266 struct wps_event_fail *fail)
5267{
5268 if (!wpa_s->p2p_in_provisioning) {
5269 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5270 "provisioning not in progress");
5271 return;
5272 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005273
5274 if (wpa_s->go_params) {
5275 p2p_clear_provisioning_info(
5276 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005277 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005278 }
5279
Jouni Malinen75ecf522011-06-27 15:19:46 -07005280 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005281
5282 if (wpa_s == wpa_s->global->p2p_group_formation) {
5283 /*
5284 * Allow some time for the failed WPS negotiation exchange to
5285 * complete, but remove the group since group formation cannot
5286 * succeed after provisioning failure.
5287 */
5288 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5289 wpa_s->global->p2p_fail_on_wps_complete = 1;
5290 eloop_deplete_timeout(0, 50000,
5291 wpas_p2p_group_formation_timeout,
5292 wpa_s->parent, NULL);
5293 }
5294}
5295
5296
5297int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5298{
5299 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5300 !wpa_s->p2p_in_provisioning)
5301 return 0;
5302
5303 wpas_p2p_grpform_fail_after_wps(wpa_s);
5304
5305 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005306}
5307
5308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005310 const char *config_method,
5311 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005312{
5313 u16 config_methods;
5314
Dmitry Shmidt04949592012-07-19 12:16:46 -07005315 wpa_s->p2p_fallback_to_go_neg = 0;
5316 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005317 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005318 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005319 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005320 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005321 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5322 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005324 else {
5325 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005326 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005327 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005328
Dmitry Shmidt04949592012-07-19 12:16:46 -07005329 if (use == WPAS_P2P_PD_AUTO) {
5330 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5331 wpa_s->pending_pd_config_methods = config_methods;
5332 wpa_s->p2p_auto_pd = 1;
5333 wpa_s->p2p_auto_join = 0;
5334 wpa_s->pending_pd_before_join = 0;
5335 wpa_s->auto_pd_scan_retry = 0;
5336 wpas_p2p_stop_find(wpa_s);
5337 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005338 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005339 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5340 wpa_s->p2p_auto_started.sec,
5341 wpa_s->p2p_auto_started.usec);
5342 wpas_p2p_join_scan(wpa_s, NULL);
5343 return 0;
5344 }
5345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005346 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5347 return -1;
5348
5349 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005350 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005351 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005352}
5353
5354
5355int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5356 char *end)
5357{
5358 return p2p_scan_result_text(ies, ies_len, buf, end);
5359}
5360
5361
5362static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5363{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005364 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 return;
5366
5367 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5368 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005369 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005370}
5371
5372
5373int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5374 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005375 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005376 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377{
5378 wpas_p2p_clear_pending_action_tx(wpa_s);
5379 wpa_s->p2p_long_listen = 0;
5380
Dmitry Shmidt04949592012-07-19 12:16:46 -07005381 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5382 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005383 return -1;
5384
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005385 wpa_supplicant_cancel_sched_scan(wpa_s);
5386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005387 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005388 num_req_dev_types, req_dev_types, dev_id,
5389 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005390}
5391
5392
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005393static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005394{
5395 wpas_p2p_clear_pending_action_tx(wpa_s);
5396 wpa_s->p2p_long_listen = 0;
5397 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5398 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399
5400 if (wpa_s->global->p2p)
5401 p2p_stop_find(wpa_s->global->p2p);
5402
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005403 return 0;
5404}
5405
5406
5407void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5408{
5409 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5410 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005411 wpas_p2p_remove_pending_group_interface(wpa_s);
5412}
5413
5414
5415static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5416{
5417 struct wpa_supplicant *wpa_s = eloop_ctx;
5418 wpa_s->p2p_long_listen = 0;
5419}
5420
5421
5422int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5423{
5424 int res;
5425
5426 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5427 return -1;
5428
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005429 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005430 wpas_p2p_clear_pending_action_tx(wpa_s);
5431
5432 if (timeout == 0) {
5433 /*
5434 * This is a request for unlimited Listen state. However, at
5435 * least for now, this is mapped to a Listen state for one
5436 * hour.
5437 */
5438 timeout = 3600;
5439 }
5440 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5441 wpa_s->p2p_long_listen = 0;
5442
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005443 /*
5444 * Stop previous find/listen operation to avoid trying to request a new
5445 * remain-on-channel operation while the driver is still running the
5446 * previous one.
5447 */
5448 if (wpa_s->global->p2p)
5449 p2p_stop_find(wpa_s->global->p2p);
5450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005451 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5452 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5453 wpa_s->p2p_long_listen = timeout * 1000;
5454 eloop_register_timeout(timeout, 0,
5455 wpas_p2p_long_listen_timeout,
5456 wpa_s, NULL);
5457 }
5458
5459 return res;
5460}
5461
5462
5463int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5464 u8 *buf, size_t len, int p2p_group)
5465{
5466 struct wpabuf *p2p_ie;
5467 int ret;
5468
5469 if (wpa_s->global->p2p_disabled)
5470 return -1;
5471 if (wpa_s->global->p2p == NULL)
5472 return -1;
5473 if (bss == NULL)
5474 return -1;
5475
5476 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5477 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5478 p2p_group, p2p_ie);
5479 wpabuf_free(p2p_ie);
5480
5481 return ret;
5482}
5483
5484
5485int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005486 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005487 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005488{
5489 if (wpa_s->global->p2p_disabled)
5490 return 0;
5491 if (wpa_s->global->p2p == NULL)
5492 return 0;
5493
Dmitry Shmidt04949592012-07-19 12:16:46 -07005494 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5495 ie, ie_len)) {
5496 case P2P_PREQ_NOT_P2P:
5497 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5498 ssi_signal);
5499 /* fall through */
5500 case P2P_PREQ_MALFORMED:
5501 case P2P_PREQ_NOT_LISTEN:
5502 case P2P_PREQ_NOT_PROCESSED:
5503 default: /* make gcc happy */
5504 return 0;
5505 case P2P_PREQ_PROCESSED:
5506 return 1;
5507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508}
5509
5510
5511void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5512 const u8 *sa, const u8 *bssid,
5513 u8 category, const u8 *data, size_t len, int freq)
5514{
5515 if (wpa_s->global->p2p_disabled)
5516 return;
5517 if (wpa_s->global->p2p == NULL)
5518 return;
5519
5520 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5521 freq);
5522}
5523
5524
5525void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5526{
5527 if (wpa_s->global->p2p_disabled)
5528 return;
5529 if (wpa_s->global->p2p == NULL)
5530 return;
5531
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005532 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533}
5534
5535
5536void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5537{
5538 p2p_group_deinit(wpa_s->p2p_group);
5539 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005540
5541 wpa_s->ap_configured_cb = NULL;
5542 wpa_s->ap_configured_cb_ctx = NULL;
5543 wpa_s->ap_configured_cb_data = NULL;
5544 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005545}
5546
5547
5548int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5549{
5550 wpa_s->p2p_long_listen = 0;
5551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005552 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5553 return -1;
5554
5555 return p2p_reject(wpa_s->global->p2p, addr);
5556}
5557
5558
5559/* Invite to reinvoke a persistent group */
5560int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005561 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005562 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005563{
5564 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005565 u8 *bssid = NULL;
5566 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005567 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005568 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005569
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005570 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005571 if (peer_addr)
5572 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5573 else
5574 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005575
Jouni Malinen31be0a42012-08-31 21:20:51 +03005576 wpa_s->p2p_persistent_go_freq = freq;
5577 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005578 if (ssid->mode == WPAS_MODE_P2P_GO) {
5579 role = P2P_INVITE_ROLE_GO;
5580 if (peer_addr == NULL) {
5581 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5582 "address in invitation command");
5583 return -1;
5584 }
5585 if (wpas_p2p_create_iface(wpa_s)) {
5586 if (wpas_p2p_add_group_interface(wpa_s,
5587 WPA_IF_P2P_GO) < 0) {
5588 wpa_printf(MSG_ERROR, "P2P: Failed to "
5589 "allocate a new interface for the "
5590 "group");
5591 return -1;
5592 }
5593 bssid = wpa_s->pending_interface_addr;
5594 } else
5595 bssid = wpa_s->own_addr;
5596 } else {
5597 role = P2P_INVITE_ROLE_CLIENT;
5598 peer_addr = ssid->bssid;
5599 }
5600 wpa_s->pending_invite_ssid_id = ssid->id;
5601
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005602 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5603 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005604 if (res)
5605 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005606
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005607 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5608 return -1;
5609
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005610 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5611 no_pref_freq_given && pref_freq > 0 &&
5612 wpa_s->num_multichan_concurrent > 1 &&
5613 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5614 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5615 pref_freq);
5616 pref_freq = 0;
5617 }
5618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005620 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5621 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005622}
5623
5624
5625/* Invite to join an active group */
5626int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5627 const u8 *peer_addr, const u8 *go_dev_addr)
5628{
5629 struct wpa_global *global = wpa_s->global;
5630 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005631 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005632 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005633 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005634 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005635 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636
Jouni Malinen31be0a42012-08-31 21:20:51 +03005637 wpa_s->p2p_persistent_go_freq = 0;
5638 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005639 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005641 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5642 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5643 break;
5644 }
5645 if (wpa_s == NULL) {
5646 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5647 return -1;
5648 }
5649
5650 ssid = wpa_s->current_ssid;
5651 if (ssid == NULL) {
5652 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5653 "invitation");
5654 return -1;
5655 }
5656
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005657 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005658 persistent = ssid->p2p_persistent_group &&
5659 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5660 ssid->ssid, ssid->ssid_len);
5661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005662 if (ssid->mode == WPAS_MODE_P2P_GO) {
5663 role = P2P_INVITE_ROLE_ACTIVE_GO;
5664 bssid = wpa_s->own_addr;
5665 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005666 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005667 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005668 } else {
5669 role = P2P_INVITE_ROLE_CLIENT;
5670 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5671 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5672 "invite to current group");
5673 return -1;
5674 }
5675 bssid = wpa_s->bssid;
5676 if (go_dev_addr == NULL &&
5677 !is_zero_ether_addr(wpa_s->go_dev_addr))
5678 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005679 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5680 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 }
5682 wpa_s->parent->pending_invite_ssid_id = -1;
5683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005684 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5685 return -1;
5686
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005687 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5688 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005689 if (res)
5690 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005691 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005694 ssid->ssid, ssid->ssid_len, force_freq,
5695 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005696}
5697
5698
5699void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5700{
5701 struct wpa_ssid *ssid = wpa_s->current_ssid;
5702 const char *ssid_txt;
5703 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005704 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005705 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005706 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005707
Dmitry Shmidt04949592012-07-19 12:16:46 -07005708 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5709 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5710 wpa_s->parent, NULL);
5711 }
5712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005713 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005714 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005715
5716 wpa_s->show_group_started = 0;
5717
5718 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5719 os_memset(go_dev_addr, 0, ETH_ALEN);
5720 if (ssid->bssid_set)
5721 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5722 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5723 ssid->ssid_len);
5724 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5725
5726 if (wpa_s->global->p2p_group_formation == wpa_s)
5727 wpa_s->global->p2p_group_formation = NULL;
5728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005729 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5730 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731 if (ssid->passphrase == NULL && ssid->psk_set) {
5732 char psk[65];
5733 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005734 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5735 "%s client ssid=\"%s\" freq=%d psk=%s "
5736 "go_dev_addr=" MACSTR "%s",
5737 wpa_s->ifname, ssid_txt, freq, psk,
5738 MAC2STR(go_dev_addr),
5739 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005741 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5742 "%s client ssid=\"%s\" freq=%d "
5743 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5744 wpa_s->ifname, ssid_txt, freq,
5745 ssid->passphrase ? ssid->passphrase : "",
5746 MAC2STR(go_dev_addr),
5747 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748 }
5749
5750 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005751 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5752 ssid, go_dev_addr);
5753 if (network_id < 0)
5754 network_id = ssid->id;
5755 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005756}
5757
5758
5759int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5760 u32 interval1, u32 duration2, u32 interval2)
5761{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005762 int ret;
5763
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005764 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5765 return -1;
5766
5767 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5768 wpa_s->current_ssid == NULL ||
5769 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5770 return -1;
5771
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005772 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5773 wpa_s->own_addr, wpa_s->assoc_freq,
5774 duration1, interval1, duration2, interval2);
5775 if (ret == 0)
5776 wpa_s->waiting_presence_resp = 1;
5777
5778 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005779}
5780
5781
5782int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5783 unsigned int interval)
5784{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005785 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5786 return -1;
5787
5788 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5789}
5790
5791
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005792static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5793{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005794 if (wpa_s->current_ssid == NULL) {
5795 /*
5796 * current_ssid can be cleared when P2P client interface gets
5797 * disconnected, so assume this interface was used as P2P
5798 * client.
5799 */
5800 return 1;
5801 }
5802 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005803 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5804}
5805
5806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005807static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5808{
5809 struct wpa_supplicant *wpa_s = eloop_ctx;
5810
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005811 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005812 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5813 "disabled");
5814 return;
5815 }
5816
Dmitry Shmidt04949592012-07-19 12:16:46 -07005817 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5818 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005819 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005820}
5821
5822
5823static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5824{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005825 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005826
Dmitry Shmidt04949592012-07-19 12:16:46 -07005827 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5828 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5829
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005830 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5831 return;
5832
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005833 timeout = wpa_s->conf->p2p_group_idle;
5834 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5835 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5836 timeout = P2P_MAX_CLIENT_IDLE;
5837
5838 if (timeout == 0)
5839 return;
5840
Dmitry Shmidt04949592012-07-19 12:16:46 -07005841 if (timeout < 0) {
5842 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5843 timeout = 0; /* special client mode no-timeout */
5844 else
5845 return;
5846 }
5847
5848 if (wpa_s->p2p_in_provisioning) {
5849 /*
5850 * Use the normal group formation timeout during the
5851 * provisioning phase to avoid terminating this process too
5852 * early due to group idle timeout.
5853 */
5854 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5855 "during provisioning");
5856 return;
5857 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305858
Dmitry Shmidt04949592012-07-19 12:16:46 -07005859 if (wpa_s->show_group_started) {
5860 /*
5861 * Use the normal group formation timeout between the end of
5862 * the provisioning phase and completion of 4-way handshake to
5863 * avoid terminating this process too early due to group idle
5864 * timeout.
5865 */
5866 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5867 "while waiting for initial 4-way handshake to "
5868 "complete");
5869 return;
5870 }
5871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005872 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005873 timeout);
5874 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5875 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005876}
5877
5878
Jouni Malinen2b89da82012-08-31 22:04:41 +03005879/* Returns 1 if the interface was removed */
5880int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5881 u16 reason_code, const u8 *ie, size_t ie_len,
5882 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005883{
5884 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005885 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005886
Dmitry Shmidt04949592012-07-19 12:16:46 -07005887 if (!locally_generated)
5888 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5889 ie_len);
5890
5891 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5892 wpa_s->current_ssid &&
5893 wpa_s->current_ssid->p2p_group &&
5894 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5895 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5896 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005897 if (wpas_p2p_group_delete(wpa_s,
5898 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5899 > 0)
5900 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005901 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005902
5903 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005904}
5905
5906
5907void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005908 u16 reason_code, const u8 *ie, size_t ie_len,
5909 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005910{
5911 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5912 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005913
Dmitry Shmidt04949592012-07-19 12:16:46 -07005914 if (!locally_generated)
5915 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5916 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005917}
5918
5919
5920void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5921{
5922 struct p2p_data *p2p = wpa_s->global->p2p;
5923
5924 if (p2p == NULL)
5925 return;
5926
5927 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5928 return;
5929
5930 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5931 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5932
5933 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5934 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5935
5936 if (wpa_s->wps &&
5937 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5938 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5939
5940 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5941 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5942
5943 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5944 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5945 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5946 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5947 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5948 }
5949
5950 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5951 p2p_set_sec_dev_types(p2p,
5952 (void *) wpa_s->conf->sec_device_type,
5953 wpa_s->conf->num_sec_device_types);
5954
5955 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5956 int i;
5957 p2p_remove_wps_vendor_extensions(p2p);
5958 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5959 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5960 continue;
5961 p2p_add_wps_vendor_extension(
5962 p2p, wpa_s->conf->wps_vendor_ext[i]);
5963 }
5964 }
5965
5966 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5967 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5968 char country[3];
5969 country[0] = wpa_s->conf->country[0];
5970 country[1] = wpa_s->conf->country[1];
5971 country[2] = 0x04;
5972 p2p_set_country(p2p, country);
5973 }
5974
5975 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5976 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5977 wpa_s->conf->p2p_ssid_postfix ?
5978 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5979 0);
5980 }
5981
5982 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5983 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005984
5985 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5986 u8 reg_class, channel;
5987 int ret;
5988 unsigned int r;
5989 if (wpa_s->conf->p2p_listen_reg_class &&
5990 wpa_s->conf->p2p_listen_channel) {
5991 reg_class = wpa_s->conf->p2p_listen_reg_class;
5992 channel = wpa_s->conf->p2p_listen_channel;
5993 } else {
5994 reg_class = 81;
5995 /*
5996 * Pick one of the social channels randomly as the
5997 * listen channel.
5998 */
5999 os_get_random((u8 *) &r, sizeof(r));
6000 channel = 1 + (r % 3) * 5;
6001 }
6002 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6003 if (ret)
6004 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6005 "failed: %d", ret);
6006 }
6007 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6008 u8 op_reg_class, op_channel, cfg_op_channel;
6009 int ret = 0;
6010 unsigned int r;
6011 if (wpa_s->conf->p2p_oper_reg_class &&
6012 wpa_s->conf->p2p_oper_channel) {
6013 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6014 op_channel = wpa_s->conf->p2p_oper_channel;
6015 cfg_op_channel = 1;
6016 } else {
6017 op_reg_class = 81;
6018 /*
6019 * Use random operation channel from (1, 6, 11)
6020 *if no other preference is indicated.
6021 */
6022 os_get_random((u8 *) &r, sizeof(r));
6023 op_channel = 1 + (r % 3) * 5;
6024 cfg_op_channel = 0;
6025 }
6026 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6027 cfg_op_channel);
6028 if (ret)
6029 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6030 "failed: %d", ret);
6031 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006032
6033 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6034 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6035 wpa_s->conf->p2p_pref_chan) < 0) {
6036 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6037 "update failed");
6038 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006039
6040 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6041 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6042 "update failed");
6043 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006044 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006045}
6046
6047
6048int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6049 int duration)
6050{
6051 if (!wpa_s->ap_iface)
6052 return -1;
6053 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6054 duration);
6055}
6056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006057
6058int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6059{
6060 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6061 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006062
6063 wpa_s->global->cross_connection = enabled;
6064 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6065
6066 if (!enabled) {
6067 struct wpa_supplicant *iface;
6068
6069 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6070 {
6071 if (iface->cross_connect_enabled == 0)
6072 continue;
6073
6074 iface->cross_connect_enabled = 0;
6075 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006076 wpa_msg_global(iface->parent, MSG_INFO,
6077 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6078 iface->ifname,
6079 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006080 }
6081 }
6082
6083 return 0;
6084}
6085
6086
6087static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6088{
6089 struct wpa_supplicant *iface;
6090
6091 if (!uplink->global->cross_connection)
6092 return;
6093
6094 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6095 if (!iface->cross_connect_enabled)
6096 continue;
6097 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6098 0)
6099 continue;
6100 if (iface->ap_iface == NULL)
6101 continue;
6102 if (iface->cross_connect_in_use)
6103 continue;
6104
6105 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006106 wpa_msg_global(iface->parent, MSG_INFO,
6107 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6108 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006109 }
6110}
6111
6112
6113static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6114{
6115 struct wpa_supplicant *iface;
6116
6117 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6118 if (!iface->cross_connect_enabled)
6119 continue;
6120 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6121 0)
6122 continue;
6123 if (!iface->cross_connect_in_use)
6124 continue;
6125
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006126 wpa_msg_global(iface->parent, MSG_INFO,
6127 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6128 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129 iface->cross_connect_in_use = 0;
6130 }
6131}
6132
6133
6134void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6135{
6136 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6137 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6138 wpa_s->cross_connect_disallowed)
6139 wpas_p2p_disable_cross_connect(wpa_s);
6140 else
6141 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006142 if (!wpa_s->ap_iface &&
6143 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6144 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145}
6146
6147
6148void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6149{
6150 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006151 if (!wpa_s->ap_iface &&
6152 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6153 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 wpas_p2p_set_group_idle_timeout(wpa_s);
6155}
6156
6157
6158static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6159{
6160 struct wpa_supplicant *iface;
6161
6162 if (!wpa_s->global->cross_connection)
6163 return;
6164
6165 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6166 if (iface == wpa_s)
6167 continue;
6168 if (iface->drv_flags &
6169 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6170 continue;
6171 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6172 continue;
6173
6174 wpa_s->cross_connect_enabled = 1;
6175 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6176 sizeof(wpa_s->cross_connect_uplink));
6177 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6178 "%s to %s whenever uplink is available",
6179 wpa_s->ifname, wpa_s->cross_connect_uplink);
6180
6181 if (iface->ap_iface || iface->current_ssid == NULL ||
6182 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6183 iface->cross_connect_disallowed ||
6184 iface->wpa_state != WPA_COMPLETED)
6185 break;
6186
6187 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006188 wpa_msg_global(wpa_s->parent, MSG_INFO,
6189 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6190 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191 break;
6192 }
6193}
6194
6195
6196int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6197{
6198 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6199 !wpa_s->p2p_in_provisioning)
6200 return 0; /* not P2P client operation */
6201
6202 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6203 "session overlap");
6204 if (wpa_s != wpa_s->parent)
6205 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006206 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006207 return 1;
6208}
6209
6210
6211void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6212{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006213 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006214
6215 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6216 return;
6217
6218 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006219 os_memset(&cli_chan, 0, sizeof(cli_chan));
6220 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006221 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6222 "channel list");
6223 return;
6224 }
6225
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006226 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006227}
6228
6229
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006230static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6231 struct wpa_scan_results *scan_res)
6232{
6233 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6234}
6235
6236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006237int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6238{
6239 struct wpa_global *global = wpa_s->global;
6240 int found = 0;
6241 const u8 *peer;
6242
6243 if (global->p2p == NULL)
6244 return -1;
6245
6246 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6247
6248 if (wpa_s->pending_interface_name[0] &&
6249 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6250 found = 1;
6251
6252 peer = p2p_get_go_neg_peer(global->p2p);
6253 if (peer) {
6254 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6255 MACSTR, MAC2STR(peer));
6256 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006257 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006258 }
6259
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006260 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6261 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6262 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6263 found = 1;
6264 }
6265
6266 if (wpa_s->pending_pd_before_join) {
6267 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6268 wpa_s->pending_pd_before_join = 0;
6269 found = 1;
6270 }
6271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006272 wpas_p2p_stop_find(wpa_s);
6273
6274 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6275 if (wpa_s == global->p2p_group_formation &&
6276 (wpa_s->p2p_in_provisioning ||
6277 wpa_s->parent->pending_interface_type ==
6278 WPA_IF_P2P_CLIENT)) {
6279 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6280 "formation found - cancelling",
6281 wpa_s->ifname);
6282 found = 1;
6283 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6284 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006285 if (wpa_s->p2p_in_provisioning) {
6286 wpas_group_formation_completed(wpa_s, 0);
6287 break;
6288 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006289 wpas_p2p_group_delete(wpa_s,
6290 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006291 break;
6292 }
6293 }
6294
6295 if (!found) {
6296 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6297 return -1;
6298 }
6299
6300 return 0;
6301}
6302
6303
6304void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6305{
6306 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6307 return;
6308
6309 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6310 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006311 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006312}
6313
6314
6315void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6316 int freq_24, int freq_5, int freq_overall)
6317{
6318 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006319 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006320 return;
6321 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6322}
6323
6324
6325int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6326{
6327 u8 peer[ETH_ALEN];
6328 struct p2p_data *p2p = wpa_s->global->p2p;
6329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006330 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006331 return -1;
6332
6333 if (hwaddr_aton(addr, peer))
6334 return -1;
6335
6336 return p2p_unauthorize(p2p, peer);
6337}
6338
6339
6340/**
6341 * wpas_p2p_disconnect - Disconnect from a P2P Group
6342 * @wpa_s: Pointer to wpa_supplicant data
6343 * Returns: 0 on success, -1 on failure
6344 *
6345 * This can be used to disconnect from a group in which the local end is a P2P
6346 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6347 * virtual network interface was created for this group, that interface will be
6348 * removed. Otherwise, only the configured P2P group network will be removed
6349 * from the interface.
6350 */
6351int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6352{
6353
6354 if (wpa_s == NULL)
6355 return -1;
6356
Jouni Malinen2b89da82012-08-31 22:04:41 +03006357 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6358 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006359}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006360
6361
6362int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6363{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006364 int ret;
6365
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006366 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6367 return 0;
6368
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006369 ret = p2p_in_progress(wpa_s->global->p2p);
6370 if (ret == 0) {
6371 /*
6372 * Check whether there is an ongoing WPS provisioning step (or
6373 * other parts of group formation) on another interface since
6374 * p2p_in_progress() does not report this to avoid issues for
6375 * scans during such provisioning step.
6376 */
6377 if (wpa_s->global->p2p_group_formation &&
6378 wpa_s->global->p2p_group_formation != wpa_s) {
6379 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6380 "in group formation",
6381 wpa_s->global->p2p_group_formation->ifname);
6382 ret = 1;
6383 }
6384 }
6385
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006386 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006387 struct os_reltime now;
6388 os_get_reltime(&now);
6389 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6390 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006391 /* Wait for the first client has expired */
6392 wpa_s->global->p2p_go_wait_client.sec = 0;
6393 } else {
6394 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6395 ret = 1;
6396 }
6397 }
6398
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006399 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006400}
6401
6402
6403void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6404 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006405{
6406 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6407 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6408 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006409 /**
6410 * Remove the network by scheduling the group formation
6411 * timeout to happen immediately. The teardown code
6412 * needs to be scheduled to run asynch later so that we
6413 * don't delete data from under ourselves unexpectedly.
6414 * Calling wpas_p2p_group_formation_timeout directly
6415 * causes a series of crashes in WPS failure scenarios.
6416 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006417 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6418 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006419 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6420 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006421 }
6422}
6423
6424
6425struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006426 const u8 *addr, const u8 *ssid,
6427 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006428{
6429 struct wpa_ssid *s;
6430 size_t i;
6431
6432 for (s = wpa_s->conf->ssid; s; s = s->next) {
6433 if (s->disabled != 2)
6434 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006435 if (ssid &&
6436 (ssid_len != s->ssid_len ||
6437 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6438 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006439 if (addr == NULL) {
6440 if (s->mode == WPAS_MODE_P2P_GO)
6441 return s;
6442 continue;
6443 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006444 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6445 return s; /* peer is GO in the persistent group */
6446 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6447 continue;
6448 for (i = 0; i < s->num_p2p_clients; i++) {
6449 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6450 addr, ETH_ALEN) == 0)
6451 return s; /* peer is P2P client in persistent
6452 * group */
6453 }
6454 }
6455
6456 return NULL;
6457}
6458
6459
6460void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6461 const u8 *addr)
6462{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006463 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6464 wpa_s->parent, NULL) > 0) {
6465 /*
6466 * This can happen if WPS provisioning step is not terminated
6467 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6468 * peer was able to connect, there is no need to time out group
6469 * formation after this, though. In addition, this is used with
6470 * the initial connection wait on the GO as a separate formation
6471 * timeout and as such, expected to be hit after the initial WPS
6472 * provisioning step.
6473 */
6474 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6475 }
6476 if (!wpa_s->p2p_go_group_formation_completed) {
6477 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6478 wpa_s->p2p_go_group_formation_completed = 1;
6479 wpa_s->global->p2p_group_formation = NULL;
6480 wpa_s->p2p_in_provisioning = 0;
6481 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006482 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006483 if (addr == NULL)
6484 return;
6485 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6486}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006487
Dmitry Shmidt04949592012-07-19 12:16:46 -07006488
6489static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6490 int group_added)
6491{
6492 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006493 if (wpa_s->global->p2p_group_formation)
6494 group = wpa_s->global->p2p_group_formation;
6495 wpa_s = wpa_s->parent;
6496 offchannel_send_action_done(wpa_s);
6497 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006498 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006499 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6500 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6501 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6502 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6503 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006504 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006505 wpa_s->p2p_go_ht40,
6506 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006507}
6508
6509
6510int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6511{
6512 if (!wpa_s->p2p_fallback_to_go_neg ||
6513 wpa_s->p2p_in_provisioning <= 5)
6514 return 0;
6515
6516 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6517 return 0; /* peer operating as a GO */
6518
6519 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6520 "fallback to GO Negotiation");
6521 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6522
6523 return 1;
6524}
6525
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006526
6527unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6528{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006529 struct wpa_supplicant *ifs;
6530
6531 if (wpa_s->wpa_state > WPA_SCANNING) {
6532 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6533 "concurrent operation",
6534 P2P_CONCURRENT_SEARCH_DELAY);
6535 return P2P_CONCURRENT_SEARCH_DELAY;
6536 }
6537
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006538 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6539 radio_list) {
6540 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006541 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6542 "delay due to concurrent operation on "
6543 "interface %s",
6544 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6545 return P2P_CONCURRENT_SEARCH_DELAY;
6546 }
6547 }
6548
6549 return 0;
6550}
6551
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006552
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006553static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6554 struct wpa_ssid *s, const u8 *addr,
6555 int iface_addr)
6556{
6557 struct psk_list_entry *psk, *tmp;
6558 int changed = 0;
6559
6560 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6561 list) {
6562 if ((iface_addr && !psk->p2p &&
6563 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6564 (!iface_addr && psk->p2p &&
6565 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6566 wpa_dbg(wpa_s, MSG_DEBUG,
6567 "P2P: Remove persistent group PSK list entry for "
6568 MACSTR " p2p=%u",
6569 MAC2STR(psk->addr), psk->p2p);
6570 dl_list_del(&psk->list);
6571 os_free(psk);
6572 changed++;
6573 }
6574 }
6575
6576 return changed;
6577}
6578
6579
6580void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6581 const u8 *p2p_dev_addr,
6582 const u8 *psk, size_t psk_len)
6583{
6584 struct wpa_ssid *ssid = wpa_s->current_ssid;
6585 struct wpa_ssid *persistent;
6586 struct psk_list_entry *p;
6587
6588 if (psk_len != sizeof(p->psk))
6589 return;
6590
6591 if (p2p_dev_addr) {
6592 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6593 " p2p_dev_addr=" MACSTR,
6594 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6595 if (is_zero_ether_addr(p2p_dev_addr))
6596 p2p_dev_addr = NULL;
6597 } else {
6598 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6599 MAC2STR(mac_addr));
6600 }
6601
6602 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6603 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6604 /* To be added to persistent group once created */
6605 if (wpa_s->global->add_psk == NULL) {
6606 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6607 if (wpa_s->global->add_psk == NULL)
6608 return;
6609 }
6610 p = wpa_s->global->add_psk;
6611 if (p2p_dev_addr) {
6612 p->p2p = 1;
6613 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6614 } else {
6615 p->p2p = 0;
6616 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6617 }
6618 os_memcpy(p->psk, psk, psk_len);
6619 return;
6620 }
6621
6622 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6623 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6624 return;
6625 }
6626
6627 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6628 ssid->ssid_len);
6629 if (!persistent) {
6630 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6631 return;
6632 }
6633
6634 p = os_zalloc(sizeof(*p));
6635 if (p == NULL)
6636 return;
6637 if (p2p_dev_addr) {
6638 p->p2p = 1;
6639 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6640 } else {
6641 p->p2p = 0;
6642 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6643 }
6644 os_memcpy(p->psk, psk, psk_len);
6645
6646 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6647 struct psk_list_entry *last;
6648 last = dl_list_last(&persistent->psk_list,
6649 struct psk_list_entry, list);
6650 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6651 MACSTR " (p2p=%u) to make room for a new one",
6652 MAC2STR(last->addr), last->p2p);
6653 dl_list_del(&last->list);
6654 os_free(last);
6655 }
6656
6657 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6658 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6659 p2p_dev_addr == NULL);
6660 if (p2p_dev_addr) {
6661 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6662 MACSTR, MAC2STR(p2p_dev_addr));
6663 } else {
6664 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6665 MAC2STR(mac_addr));
6666 }
6667 dl_list_add(&persistent->psk_list, &p->list);
6668
6669#ifndef CONFIG_NO_CONFIG_WRITE
6670 if (wpa_s->parent->conf->update_config &&
6671 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6672 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6673#endif /* CONFIG_NO_CONFIG_WRITE */
6674}
6675
6676
6677static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6678 struct wpa_ssid *s, const u8 *addr,
6679 int iface_addr)
6680{
6681 int res;
6682
6683 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6684 if (res > 0) {
6685#ifndef CONFIG_NO_CONFIG_WRITE
6686 if (wpa_s->conf->update_config &&
6687 wpa_config_write(wpa_s->confname, wpa_s->conf))
6688 wpa_dbg(wpa_s, MSG_DEBUG,
6689 "P2P: Failed to update configuration");
6690#endif /* CONFIG_NO_CONFIG_WRITE */
6691 }
6692}
6693
6694
6695static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6696 const u8 *peer, int iface_addr)
6697{
6698 struct hostapd_data *hapd;
6699 struct hostapd_wpa_psk *psk, *prev, *rem;
6700 struct sta_info *sta;
6701
6702 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6703 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6704 return;
6705
6706 /* Remove per-station PSK entry */
6707 hapd = wpa_s->ap_iface->bss[0];
6708 prev = NULL;
6709 psk = hapd->conf->ssid.wpa_psk;
6710 while (psk) {
6711 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6712 (!iface_addr &&
6713 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6714 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6715 MACSTR " iface_addr=%d",
6716 MAC2STR(peer), iface_addr);
6717 if (prev)
6718 prev->next = psk->next;
6719 else
6720 hapd->conf->ssid.wpa_psk = psk->next;
6721 rem = psk;
6722 psk = psk->next;
6723 os_free(rem);
6724 } else {
6725 prev = psk;
6726 psk = psk->next;
6727 }
6728 }
6729
6730 /* Disconnect from group */
6731 if (iface_addr)
6732 sta = ap_get_sta(hapd, peer);
6733 else
6734 sta = ap_get_sta_p2p(hapd, peer);
6735 if (sta) {
6736 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6737 " (iface_addr=%d) from group",
6738 MAC2STR(peer), iface_addr);
6739 hostapd_drv_sta_deauth(hapd, sta->addr,
6740 WLAN_REASON_DEAUTH_LEAVING);
6741 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6742 }
6743}
6744
6745
6746void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6747 int iface_addr)
6748{
6749 struct wpa_ssid *s;
6750 struct wpa_supplicant *w;
6751
6752 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6753
6754 /* Remove from any persistent group */
6755 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6756 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6757 continue;
6758 if (!iface_addr)
6759 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6760 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6761 }
6762
6763 /* Remove from any operating group */
6764 for (w = wpa_s->global->ifaces; w; w = w->next)
6765 wpas_p2p_remove_client_go(w, peer, iface_addr);
6766}
6767
6768
6769static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6770{
6771 struct wpa_supplicant *wpa_s = eloop_ctx;
6772 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6773}
6774
6775
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006776static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6777{
6778 struct wpa_supplicant *wpa_s = eloop_ctx;
6779
6780 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6781 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6782}
6783
6784
6785int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6786 struct wpa_ssid *ssid)
6787{
6788 struct wpa_supplicant *iface;
6789
6790 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6791 if (!iface->current_ssid ||
6792 iface->current_ssid->frequency == freq ||
6793 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6794 !iface->current_ssid->p2p_group))
6795 continue;
6796
6797 /* Remove the connection with least priority */
6798 if (!wpas_is_p2p_prioritized(iface)) {
6799 /* STA connection has priority over existing
6800 * P2P connection, so remove the interface. */
6801 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6802 eloop_register_timeout(0, 0,
6803 wpas_p2p_group_freq_conflict,
6804 iface, NULL);
6805 /* If connection in progress is P2P connection, do not
6806 * proceed for the connection. */
6807 if (wpa_s == iface)
6808 return -1;
6809 else
6810 return 0;
6811 } else {
6812 /* P2P connection has priority, disable the STA network
6813 */
6814 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6815 ssid);
6816 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6817 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6818 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6819 ETH_ALEN);
6820 /* If P2P connection is in progress, continue
6821 * connecting...*/
6822 if (wpa_s == iface)
6823 return 0;
6824 else
6825 return -1;
6826 }
6827 }
6828
6829 return 0;
6830}
6831
6832
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006833int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6834{
6835 struct wpa_ssid *ssid = wpa_s->current_ssid;
6836
6837 if (ssid == NULL || !ssid->p2p_group)
6838 return 0;
6839
6840 if (wpa_s->p2p_last_4way_hs_fail &&
6841 wpa_s->p2p_last_4way_hs_fail == ssid) {
6842 u8 go_dev_addr[ETH_ALEN];
6843 struct wpa_ssid *persistent;
6844
6845 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6846 ssid->ssid,
6847 ssid->ssid_len) <= 0) {
6848 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6849 goto disconnect;
6850 }
6851
6852 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6853 MACSTR, MAC2STR(go_dev_addr));
6854 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6855 ssid->ssid,
6856 ssid->ssid_len);
6857 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6858 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6859 goto disconnect;
6860 }
6861 wpa_msg_global(wpa_s->parent, MSG_INFO,
6862 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6863 persistent->id);
6864 disconnect:
6865 wpa_s->p2p_last_4way_hs_fail = NULL;
6866 /*
6867 * Remove the group from a timeout to avoid issues with caller
6868 * continuing to use the interface if this is on a P2P group
6869 * interface.
6870 */
6871 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6872 wpa_s, NULL);
6873 return 1;
6874 }
6875
6876 wpa_s->p2p_last_4way_hs_fail = ssid;
6877 return 0;
6878}