blob: fa75fa5d061aa09aa51d74c6cba925b0eea0e45f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070025#include "eapol_supp/eapol_supp_sm.h"
26#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ap.h"
30#include "config_ssid.h"
31#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "notify.h"
33#include "scan.h"
34#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps_supplicant.h"
37#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080038#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
40
41/*
42 * How many times to try to scan to find the GO before giving up on join
43 * request.
44 */
45#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46
Dmitry Shmidt04949592012-07-19 12:16:46 -070047#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049#ifndef P2P_MAX_CLIENT_IDLE
50/*
51 * How many seconds to try to reconnect to the GO when connection in P2P client
52 * role has been lost.
53 */
54#define P2P_MAX_CLIENT_IDLE 10
55#endif /* P2P_MAX_CLIENT_IDLE */
56
Dmitry Shmidt04949592012-07-19 12:16:46 -070057#ifndef P2P_MAX_INITIAL_CONN_WAIT
58/*
59 * How many seconds to wait for initial 4-way handshake to get completed after
60 * WPS provisioning step.
61 */
62#define P2P_MAX_INITIAL_CONN_WAIT 10
63#endif /* P2P_MAX_INITIAL_CONN_WAIT */
64
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070065#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
66/*
67 * How many seconds to wait for initial 4-way handshake to get completed after
68 * WPS provisioning step on the GO. This controls the extra time the P2P
69 * operation is considered to be in progress (e.g., to delay other scans) after
70 * WPS provisioning has been completed on the GO during group formation.
71 */
72#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
73#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
74
Dmitry Shmidt56052862013-10-04 10:23:25 -070075#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
76/*
77 * How many seconds to wait for initial 4-way handshake to get completed after
78 * re-invocation of a persistent group on the GO when the client is expected
79 * to connect automatically (no user interaction).
80 */
81#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
82#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
83
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070084#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070085#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086#endif /* P2P_CONCURRENT_SEARCH_DELAY */
87
Dmitry Shmidt34af3062013-07-11 10:46:32 -070088#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
89
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070090enum p2p_group_removal_reason {
91 P2P_GROUP_REMOVAL_UNKNOWN,
92 P2P_GROUP_REMOVAL_SILENT,
93 P2P_GROUP_REMOVAL_FORMATION_FAILED,
94 P2P_GROUP_REMOVAL_REQUESTED,
95 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
96 P2P_GROUP_REMOVAL_UNAVAILABLE,
97 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080098 P2P_GROUP_REMOVAL_PSK_FAILURE,
99 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100};
101
Jouni Malinendc7b7132012-09-14 12:53:47 -0700102
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
104static struct wpa_supplicant *
105wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
106 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800107static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800109static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
110 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
112static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700113 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800114 int auto_join, int freq,
115 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800120static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800122static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700123static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
124 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800125static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800126static void wpas_stop_listen(void *ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700127
128
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700129/*
130 * Get the number of concurrent channels that the HW can operate, but that are
131 * currently not in use by any of the wpa_supplicant interfaces.
132 */
133static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
134{
135 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800136 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700137
138 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
139 if (!freqs)
140 return -1;
141
142 num = get_shared_radio_freqs(wpa_s, freqs,
143 wpa_s->num_multichan_concurrent);
144 os_free(freqs);
145
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800146 unused = wpa_s->num_multichan_concurrent - num;
147 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
148 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700149}
150
151
152/*
153 * Get the frequencies that are currently in use by one or more of the virtual
154 * interfaces, and that are also valid for P2P operation.
155 */
156static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 int *p2p_freqs, unsigned int len)
158{
159 int *freqs;
160 unsigned int num, i, j;
161
162 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
163 if (!freqs)
164 return -1;
165
166 num = get_shared_radio_freqs(wpa_s, freqs,
167 wpa_s->num_multichan_concurrent);
168
169 os_memset(p2p_freqs, 0, sizeof(int) * len);
170
171 for (i = 0, j = 0; i < num && j < len; i++) {
172 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
173 p2p_freqs[j++] = freqs[i];
174 }
175
176 os_free(freqs);
177
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800178 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
179
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700180 return j;
181}
182
183
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700184static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
185 int freq)
186{
187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
188 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800189 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
190 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
191 wpas_p2p_num_unused_channels(wpa_s) > 0) {
192 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
193 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700194 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800195 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700196 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
197}
198
199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
201 struct wpa_scan_results *scan_res)
202{
203 size_t i;
204
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800205 if (wpa_s->p2p_scan_work) {
206 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
207 wpa_s->p2p_scan_work = NULL;
208 radio_work_done(work);
209 }
210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
212 return;
213
214 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
215 (int) scan_res->num);
216
217 for (i = 0; i < scan_res->num; i++) {
218 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800219 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700220 const u8 *ies;
221 size_t ies_len;
222
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800223 time_tmp_age.sec = bss->age / 1000;
224 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800225 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700226
227 ies = (const u8 *) (bss + 1);
228 ies_len = bss->ie_len;
229 if (bss->beacon_ie_len > 0 &&
230 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
231 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
232 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
233 MACSTR, MAC2STR(bss->bssid));
234 ies = ies + ies_len;
235 ies_len = bss->beacon_ie_len;
236 }
237
238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800240 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700241 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 break;
243 }
244
245 p2p_scan_res_handled(wpa_s->global->p2p);
246}
247
248
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800249static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
250{
251 struct wpa_supplicant *wpa_s = work->wpa_s;
252 struct wpa_driver_scan_params *params = work->ctx;
253 int ret;
254
255 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800256 if (!work->started) {
257 wpa_scan_free_params(params);
258 return;
259 }
260
261 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800262 return;
263 }
264
265 ret = wpa_drv_scan(wpa_s, params);
266 wpa_scan_free_params(params);
267 work->ctx = NULL;
268 if (ret) {
269 radio_work_done(work);
270 return;
271 }
272
273 os_get_reltime(&wpa_s->scan_trigger_time);
274 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
275 wpa_s->own_scan_requested = 1;
276 wpa_s->p2p_scan_work = work;
277}
278
279
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
281 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700282 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283{
284 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800285 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800288 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
290 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
291 return -1;
292
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800293 if (wpa_s->p2p_scan_work) {
294 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
295 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700296 }
297
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800298 params = os_zalloc(sizeof(*params));
299 if (params == NULL)
300 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301
302 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800303 params->num_ssids = 1;
304 n = os_malloc(P2P_WILDCARD_SSID_LEN);
305 if (n == NULL)
306 goto fail;
307 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
308 params->ssids[0].ssid = n;
309 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310
311 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700312 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
313 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 num_req_dev_types, req_dev_types);
315 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800316 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
319 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700320 if (ies == NULL) {
321 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 }
324 wpabuf_put_buf(ies, wps_ie);
325 wpabuf_free(wps_ie);
326
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800327 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800329 params->p2p_probe = 1;
330 n = os_malloc(wpabuf_len(ies));
331 if (n == NULL) {
332 wpabuf_free(ies);
333 goto fail;
334 }
335 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
336 params->extra_ies = n;
337 params->extra_ies_len = wpabuf_len(ies);
338 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
340 switch (type) {
341 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800342 params->freqs = os_malloc(4 * sizeof(int));
343 if (params->freqs == NULL)
344 goto fail;
345 params->freqs[0] = 2412;
346 params->freqs[1] = 2437;
347 params->freqs[2] = 2462;
348 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700349 break;
350 case P2P_SCAN_FULL:
351 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800353 params->freqs = os_malloc(5 * sizeof(int));
354 if (params->freqs == NULL)
355 goto fail;
356 params->freqs[0] = 2412;
357 params->freqs[1] = 2437;
358 params->freqs[2] = 2462;
359 params->freqs[3] = freq;
360 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361 break;
362 }
363
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800364 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800365 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
366 params) < 0)
367 goto fail;
368 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800370fail:
371 wpa_scan_free_params(params);
372 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373}
374
375
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
377{
378 switch (p2p_group_interface) {
379 case P2P_GROUP_INTERFACE_PENDING:
380 return WPA_IF_P2P_GROUP;
381 case P2P_GROUP_INTERFACE_GO:
382 return WPA_IF_P2P_GO;
383 case P2P_GROUP_INTERFACE_CLIENT:
384 return WPA_IF_P2P_CLIENT;
385 }
386
387 return WPA_IF_P2P_GROUP;
388}
389
390
391static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
392 const u8 *ssid,
393 size_t ssid_len, int *go)
394{
395 struct wpa_ssid *s;
396
397 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
398 for (s = wpa_s->conf->ssid; s; s = s->next) {
399 if (s->disabled != 0 || !s->p2p_group ||
400 s->ssid_len != ssid_len ||
401 os_memcmp(ssid, s->ssid, ssid_len) != 0)
402 continue;
403 if (s->mode == WPAS_MODE_P2P_GO &&
404 s != wpa_s->current_ssid)
405 continue;
406 if (go)
407 *go = s->mode == WPAS_MODE_P2P_GO;
408 return wpa_s;
409 }
410 }
411
412 return NULL;
413}
414
415
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
417 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700418{
419 struct wpa_ssid *ssid;
420 char *gtype;
421 const char *reason;
422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 ssid = wpa_s->current_ssid;
424 if (ssid == NULL) {
425 /*
426 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700427 * pending P2P group interface waiting for provisioning or a
428 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 */
430 ssid = wpa_s->conf->ssid;
431 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700432 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433 break;
434 ssid = ssid->next;
435 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300436 if (ssid == NULL &&
437 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
438 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700439 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
440 "not found");
441 return -1;
442 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 }
444 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
445 gtype = "GO";
446 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
447 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
448 wpa_s->reassociate = 0;
449 wpa_s->disconnected = 1;
450 wpa_supplicant_deauthenticate(wpa_s,
451 WLAN_REASON_DEAUTH_LEAVING);
452 gtype = "client";
453 } else
454 gtype = "GO";
455 if (wpa_s->cross_connect_in_use) {
456 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700457 wpa_msg_global(wpa_s->parent, MSG_INFO,
458 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
459 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700461 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 case P2P_GROUP_REMOVAL_REQUESTED:
463 reason = " reason=REQUESTED";
464 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
466 reason = " reason=FORMATION_FAILED";
467 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
469 reason = " reason=IDLE";
470 break;
471 case P2P_GROUP_REMOVAL_UNAVAILABLE:
472 reason = " reason=UNAVAILABLE";
473 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700474 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
475 reason = " reason=GO_ENDING_SESSION";
476 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700477 case P2P_GROUP_REMOVAL_PSK_FAILURE:
478 reason = " reason=PSK_FAILURE";
479 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800480 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
481 reason = " reason=FREQ_CONFLICT";
482 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 default:
484 reason = "";
485 break;
486 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700487 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700488 wpa_msg_global(wpa_s->parent, MSG_INFO,
489 P2P_EVENT_GROUP_REMOVED "%s %s%s",
490 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800493 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
494 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700495 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800497 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700498 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800499 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
500 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700501 wpa_s->p2p_in_provisioning = 0;
502 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700503
Dmitry Shmidt96571392013-10-14 12:54:46 -0700504 /*
505 * Make sure wait for the first client does not remain active after the
506 * group has been removed.
507 */
508 wpa_s->global->p2p_go_wait_client.sec = 0;
509
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700510 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
512
513 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
514 struct wpa_global *global;
515 char *ifname;
516 enum wpa_driver_if_type type;
517 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
518 wpa_s->ifname);
519 global = wpa_s->global;
520 ifname = os_strdup(wpa_s->ifname);
521 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700522 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 wpa_s = global->ifaces;
524 if (wpa_s && ifname)
525 wpa_drv_if_remove(wpa_s, type, ifname);
526 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300527 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 }
529
Dmitry Shmidt96571392013-10-14 12:54:46 -0700530 if (!wpa_s->p2p_go_group_formation_completed) {
531 wpa_s->global->p2p_group_formation = NULL;
532 wpa_s->p2p_in_provisioning = 0;
533 }
534
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800535 wpa_s->show_group_started = 0;
536 os_free(wpa_s->go_params);
537 wpa_s->go_params = NULL;
538
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800539 wpa_s->waiting_presence_resp = 0;
540
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
542 if (ssid && (ssid->p2p_group ||
543 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
544 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
545 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700546 if (ssid == wpa_s->current_ssid) {
547 wpa_sm_set_config(wpa_s->wpa, NULL);
548 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700550 }
551 /*
552 * Networks objects created during any P2P activities are not
553 * exposed out as they might/will confuse certain non-P2P aware
554 * applications since these network objects won't behave like
555 * regular ones.
556 *
557 * Likewise, we don't send out network removed signals for such
558 * network objects.
559 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560 wpa_config_remove_network(wpa_s->conf, id);
561 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800562 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 } else {
564 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
565 "found");
566 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700567 if (wpa_s->ap_iface)
568 wpa_supplicant_ap_deinit(wpa_s);
569 else
570 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700571
572 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573}
574
575
576static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
577 u8 *go_dev_addr,
578 const u8 *ssid, size_t ssid_len)
579{
580 struct wpa_bss *bss;
581 const u8 *bssid;
582 struct wpabuf *p2p;
583 u8 group_capab;
584 const u8 *addr;
585
586 if (wpa_s->go_params)
587 bssid = wpa_s->go_params->peer_interface_addr;
588 else
589 bssid = wpa_s->bssid;
590
591 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800592 if (bss == NULL && wpa_s->go_params &&
593 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
594 bss = wpa_bss_get_p2p_dev_addr(
595 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 if (bss == NULL) {
597 u8 iface_addr[ETH_ALEN];
598 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
599 iface_addr) == 0)
600 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
601 }
602 if (bss == NULL) {
603 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
604 "group is persistent - BSS " MACSTR " not found",
605 MAC2STR(bssid));
606 return 0;
607 }
608
609 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700610 if (p2p == NULL)
611 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
612 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 if (p2p == NULL) {
614 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
615 "group is persistent - BSS " MACSTR
616 " did not include P2P IE", MAC2STR(bssid));
617 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
618 (u8 *) (bss + 1), bss->ie_len);
619 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
620 ((u8 *) bss + 1) + bss->ie_len,
621 bss->beacon_ie_len);
622 return 0;
623 }
624
625 group_capab = p2p_get_group_capab(p2p);
626 addr = p2p_get_go_dev_addr(p2p);
627 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
628 "group_capab=0x%x", group_capab);
629 if (addr) {
630 os_memcpy(go_dev_addr, addr, ETH_ALEN);
631 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
632 MAC2STR(addr));
633 } else
634 os_memset(go_dev_addr, 0, ETH_ALEN);
635 wpabuf_free(p2p);
636
637 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
638 "go_dev_addr=" MACSTR,
639 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
640
641 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
642}
643
644
Jouni Malinen75ecf522011-06-27 15:19:46 -0700645static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
646 struct wpa_ssid *ssid,
647 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648{
649 struct wpa_ssid *s;
650 int changed = 0;
651
652 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
653 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
654 for (s = wpa_s->conf->ssid; s; s = s->next) {
655 if (s->disabled == 2 &&
656 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
657 s->ssid_len == ssid->ssid_len &&
658 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
659 break;
660 }
661
662 if (s) {
663 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
664 "entry");
665 if (ssid->passphrase && !s->passphrase)
666 changed = 1;
667 else if (ssid->passphrase && s->passphrase &&
668 os_strcmp(ssid->passphrase, s->passphrase) != 0)
669 changed = 1;
670 } else {
671 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
672 "entry");
673 changed = 1;
674 s = wpa_config_add_network(wpa_s->conf);
675 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700676 return -1;
677
678 /*
679 * Instead of network_added we emit persistent_group_added
680 * notification. Also to keep the defense checks in
681 * persistent_group obj registration method, we set the
682 * relevant flags in s to designate it as a persistent group.
683 */
684 s->p2p_group = 1;
685 s->p2p_persistent_group = 1;
686 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 wpa_config_set_network_defaults(s);
688 }
689
690 s->p2p_group = 1;
691 s->p2p_persistent_group = 1;
692 s->disabled = 2;
693 s->bssid_set = 1;
694 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
695 s->mode = ssid->mode;
696 s->auth_alg = WPA_AUTH_ALG_OPEN;
697 s->key_mgmt = WPA_KEY_MGMT_PSK;
698 s->proto = WPA_PROTO_RSN;
699 s->pairwise_cipher = WPA_CIPHER_CCMP;
700 s->export_keys = 1;
701 if (ssid->passphrase) {
702 os_free(s->passphrase);
703 s->passphrase = os_strdup(ssid->passphrase);
704 }
705 if (ssid->psk_set) {
706 s->psk_set = 1;
707 os_memcpy(s->psk, ssid->psk, 32);
708 }
709 if (s->passphrase && !s->psk_set)
710 wpa_config_update_psk(s);
711 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
712 os_free(s->ssid);
713 s->ssid = os_malloc(ssid->ssid_len);
714 }
715 if (s->ssid) {
716 s->ssid_len = ssid->ssid_len;
717 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
718 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700719 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
720 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
721 wpa_s->global->add_psk = NULL;
722 changed = 1;
723 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724
725#ifndef CONFIG_NO_CONFIG_WRITE
726 if (changed && wpa_s->conf->update_config &&
727 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
728 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
729 }
730#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700731
732 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733}
734
735
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800736static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
737 const u8 *addr)
738{
739 struct wpa_ssid *ssid, *s;
740 u8 *n;
741 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700742 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743
744 ssid = wpa_s->current_ssid;
745 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
746 !ssid->p2p_persistent_group)
747 return;
748
749 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
750 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
751 continue;
752
753 if (s->ssid_len == ssid->ssid_len &&
754 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
755 break;
756 }
757
758 if (s == NULL)
759 return;
760
761 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
762 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700763 ETH_ALEN) != 0)
764 continue;
765
766 if (i == s->num_p2p_clients - 1)
767 return; /* already the most recent entry */
768
769 /* move the entry to mark it most recent */
770 os_memmove(s->p2p_client_list + i * ETH_ALEN,
771 s->p2p_client_list + (i + 1) * ETH_ALEN,
772 (s->num_p2p_clients - i - 1) * ETH_ALEN);
773 os_memcpy(s->p2p_client_list +
774 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
775 found = 1;
776 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800777 }
778
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700779 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
780 n = os_realloc_array(s->p2p_client_list,
781 s->num_p2p_clients + 1, ETH_ALEN);
782 if (n == NULL)
783 return;
784 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
785 s->p2p_client_list = n;
786 s->num_p2p_clients++;
787 } else if (!found) {
788 /* Not enough room for an additional entry - drop the oldest
789 * entry */
790 os_memmove(s->p2p_client_list,
791 s->p2p_client_list + ETH_ALEN,
792 (s->num_p2p_clients - 1) * ETH_ALEN);
793 os_memcpy(s->p2p_client_list +
794 (s->num_p2p_clients - 1) * ETH_ALEN,
795 addr, ETH_ALEN);
796 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800797
798#ifndef CONFIG_NO_CONFIG_WRITE
799 if (wpa_s->parent->conf->update_config &&
800 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
801 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
802#endif /* CONFIG_NO_CONFIG_WRITE */
803}
804
805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
807 int success)
808{
809 struct wpa_ssid *ssid;
810 const char *ssid_txt;
811 int client;
812 int persistent;
813 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700814 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815
816 /*
817 * This callback is likely called for the main interface. Update wpa_s
818 * to use the group interface if a new interface was created for the
819 * group.
820 */
821 if (wpa_s->global->p2p_group_formation)
822 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700823 if (wpa_s->p2p_go_group_formation_completed) {
824 wpa_s->global->p2p_group_formation = NULL;
825 wpa_s->p2p_in_provisioning = 0;
826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
828 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700829 wpa_msg_global(wpa_s->parent, MSG_INFO,
830 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700831 wpas_p2p_group_delete(wpa_s,
832 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 return;
834 }
835
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700836 wpa_msg_global(wpa_s->parent, MSG_INFO,
837 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838
839 ssid = wpa_s->current_ssid;
840 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
841 ssid->mode = WPAS_MODE_P2P_GO;
842 p2p_group_notif_formation_done(wpa_s->p2p_group);
843 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
844 }
845
846 persistent = 0;
847 if (ssid) {
848 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
849 client = ssid->mode == WPAS_MODE_INFRA;
850 if (ssid->mode == WPAS_MODE_P2P_GO) {
851 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700852 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
853 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 } else
855 persistent = wpas_p2p_persistent_group(wpa_s,
856 go_dev_addr,
857 ssid->ssid,
858 ssid->ssid_len);
859 } else {
860 ssid_txt = "";
861 client = wpa_s->p2p_group_interface ==
862 P2P_GROUP_INTERFACE_CLIENT;
863 os_memset(go_dev_addr, 0, ETH_ALEN);
864 }
865
866 wpa_s->show_group_started = 0;
867 if (client) {
868 /*
869 * Indicate event only after successfully completed 4-way
870 * handshake, i.e., when the interface is ready for data
871 * packets.
872 */
873 wpa_s->show_group_started = 1;
874 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
875 char psk[65];
876 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700877 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
878 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
879 MACSTR "%s",
880 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
881 MAC2STR(go_dev_addr),
882 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 wpas_p2p_cross_connect_setup(wpa_s);
884 wpas_p2p_set_group_idle_timeout(wpa_s);
885 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700886 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
887 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
888 "go_dev_addr=" MACSTR "%s",
889 wpa_s->ifname, ssid_txt,
890 ssid ? ssid->frequency : 0,
891 ssid && ssid->passphrase ? ssid->passphrase : "",
892 MAC2STR(go_dev_addr),
893 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 wpas_p2p_cross_connect_setup(wpa_s);
895 wpas_p2p_set_group_idle_timeout(wpa_s);
896 }
897
898 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700899 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
900 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700901 else {
902 os_free(wpa_s->global->add_psk);
903 wpa_s->global->add_psk = NULL;
904 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700906 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700907 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700908 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800909 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911}
912
913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800914struct send_action_work {
915 unsigned int freq;
916 u8 dst[ETH_ALEN];
917 u8 src[ETH_ALEN];
918 u8 bssid[ETH_ALEN];
919 size_t len;
920 unsigned int wait_time;
921 u8 buf[0];
922};
923
924
925static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
926 void *timeout_ctx)
927{
928 struct wpa_supplicant *wpa_s = eloop_ctx;
929
930 if (!wpa_s->p2p_send_action_work)
931 return;
932
933 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
934 os_free(wpa_s->p2p_send_action_work->ctx);
935 radio_work_done(wpa_s->p2p_send_action_work);
936 wpa_s->p2p_send_action_work = NULL;
937}
938
939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
941 unsigned int freq,
942 const u8 *dst, const u8 *src,
943 const u8 *bssid,
944 const u8 *data, size_t data_len,
945 enum offchannel_send_action_result
946 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800950 if (wpa_s->p2p_send_action_work) {
951 struct send_action_work *awork;
952 awork = wpa_s->p2p_send_action_work->ctx;
953 if (awork->wait_time == 0) {
954 os_free(awork);
955 radio_work_done(wpa_s->p2p_send_action_work);
956 wpa_s->p2p_send_action_work = NULL;
957 } else {
958 /*
959 * In theory, this should not be needed, but number of
960 * places in the P2P code is still using non-zero wait
961 * time for the last Action frame in the sequence and
962 * some of these do not call send_action_done().
963 */
964 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
965 wpa_s, NULL);
966 eloop_register_timeout(
967 0, awork->wait_time * 1000,
968 wpas_p2p_send_action_work_timeout,
969 wpa_s, NULL);
970 }
971 }
972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
974 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800976 switch (result) {
977 case OFFCHANNEL_SEND_ACTION_SUCCESS:
978 res = P2P_SEND_ACTION_SUCCESS;
979 break;
980 case OFFCHANNEL_SEND_ACTION_NO_ACK:
981 res = P2P_SEND_ACTION_NO_ACK;
982 break;
983 case OFFCHANNEL_SEND_ACTION_FAILED:
984 res = P2P_SEND_ACTION_FAILED;
985 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 }
987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800990 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
991 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800993 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
994 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
997 "during p2p_connect-auto");
998 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
999 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 }
1001}
1002
1003
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001004static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1005{
1006 struct wpa_supplicant *wpa_s = work->wpa_s;
1007 struct send_action_work *awork = work->ctx;
1008
1009 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001010 if (work->started) {
1011 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1012 wpa_s, NULL);
1013 wpa_s->p2p_send_action_work = NULL;
1014 offchannel_send_action_done(wpa_s);
1015 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001016 os_free(awork);
1017 return;
1018 }
1019
1020 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1021 awork->bssid, awork->buf, awork->len,
1022 awork->wait_time,
1023 wpas_p2p_send_action_tx_status, 1) < 0) {
1024 os_free(awork);
1025 radio_work_done(work);
1026 return;
1027 }
1028 wpa_s->p2p_send_action_work = work;
1029}
1030
1031
1032static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1033 unsigned int freq, const u8 *dst,
1034 const u8 *src, const u8 *bssid, const u8 *buf,
1035 size_t len, unsigned int wait_time)
1036{
1037 struct send_action_work *awork;
1038
1039 if (wpa_s->p2p_send_action_work) {
1040 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1041 return -1;
1042 }
1043
1044 awork = os_zalloc(sizeof(*awork) + len);
1045 if (awork == NULL)
1046 return -1;
1047
1048 awork->freq = freq;
1049 os_memcpy(awork->dst, dst, ETH_ALEN);
1050 os_memcpy(awork->src, src, ETH_ALEN);
1051 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1052 awork->len = len;
1053 awork->wait_time = wait_time;
1054 os_memcpy(awork->buf, buf, len);
1055
1056 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1057 wpas_send_action_cb, awork) < 0) {
1058 os_free(awork);
1059 return -1;
1060 }
1061
1062 return 0;
1063}
1064
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1067 const u8 *src, const u8 *bssid, const u8 *buf,
1068 size_t len, unsigned int wait_time)
1069{
1070 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001071 int listen_freq = -1, send_freq = -1;
1072
1073 if (wpa_s->p2p_listen_work)
1074 listen_freq = wpa_s->p2p_listen_work->freq;
1075 if (wpa_s->p2p_send_action_work)
1076 send_freq = wpa_s->p2p_send_action_work->freq;
1077 if (listen_freq != (int) freq && send_freq != (int) freq) {
1078 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1079 listen_freq, send_freq);
1080 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1081 len, wait_time);
1082 }
1083
1084 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1086 wait_time,
1087 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088}
1089
1090
1091static void wpas_send_action_done(void *ctx)
1092{
1093 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094
1095 if (wpa_s->p2p_send_action_work) {
1096 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1097 wpa_s, NULL);
1098 os_free(wpa_s->p2p_send_action_work->ctx);
1099 radio_work_done(wpa_s->p2p_send_action_work);
1100 wpa_s->p2p_send_action_work = NULL;
1101 }
1102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001103 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104}
1105
1106
1107static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1108 struct p2p_go_neg_results *params)
1109{
1110 if (wpa_s->go_params == NULL) {
1111 wpa_s->go_params = os_malloc(sizeof(*params));
1112 if (wpa_s->go_params == NULL)
1113 return -1;
1114 }
1115 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1116 return 0;
1117}
1118
1119
1120static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1121 struct p2p_go_neg_results *res)
1122{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001123 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1124 " dev_addr " MACSTR " wps_method %d",
1125 MAC2STR(res->peer_interface_addr),
1126 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1128 res->ssid, res->ssid_len);
1129 wpa_supplicant_ap_deinit(wpa_s);
1130 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001131 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001133#ifdef CONFIG_WPS_NFC
1134 } else if (res->wps_method == WPS_NFC) {
1135 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1136 res->peer_interface_addr,
1137 wpa_s->parent->p2p_oob_dev_pw,
1138 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1139 wpa_s->parent->p2p_oob_dev_pw_id ==
1140 DEV_PW_NFC_CONNECTION_HANDOVER ?
1141 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1142 NULL,
1143 NULL, 0, 0);
1144#endif /* CONFIG_WPS_NFC */
1145 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 u16 dev_pw_id = DEV_PW_DEFAULT;
1147 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1148 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1149 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1150 wpa_s->p2p_pin, 1, dev_pw_id);
1151 }
1152}
1153
1154
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001155static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid)
1157{
1158 struct wpa_ssid *persistent;
1159 struct psk_list_entry *psk;
1160 struct hostapd_data *hapd;
1161
1162 if (!wpa_s->ap_iface)
1163 return;
1164
1165 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1166 ssid->ssid_len);
1167 if (persistent == NULL)
1168 return;
1169
1170 hapd = wpa_s->ap_iface->bss[0];
1171
1172 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1173 list) {
1174 struct hostapd_wpa_psk *hpsk;
1175
1176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1177 MACSTR " psk=%d",
1178 MAC2STR(psk->addr), psk->p2p);
1179 hpsk = os_zalloc(sizeof(*hpsk));
1180 if (hpsk == NULL)
1181 break;
1182 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1183 if (psk->p2p)
1184 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1185 else
1186 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1187 hpsk->next = hapd->conf->ssid.wpa_psk;
1188 hapd->conf->ssid.wpa_psk = hpsk;
1189 }
1190}
1191
1192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193static void p2p_go_configured(void *ctx, void *data)
1194{
1195 struct wpa_supplicant *wpa_s = ctx;
1196 struct p2p_go_neg_results *params = data;
1197 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001198 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
1200 ssid = wpa_s->current_ssid;
1201 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1202 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1203 if (wpa_s->global->p2p_group_formation == wpa_s)
1204 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001205 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001206 wpa_msg_global(wpa_s->parent, MSG_INFO,
1207 P2P_EVENT_GROUP_STARTED
1208 "%s GO ssid=\"%s\" freq=%d "
1209 "passphrase=\"%s\" go_dev_addr=" MACSTR
1210 "%s", wpa_s->ifname,
1211 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1212 ssid->frequency, params->passphrase,
1213 MAC2STR(wpa_s->global->p2p_dev_addr),
1214 params->persistent_group ?
1215 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001216 } else {
1217 char psk[65];
1218 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1219 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001220 wpa_msg_global(wpa_s->parent, MSG_INFO,
1221 P2P_EVENT_GROUP_STARTED
1222 "%s GO ssid=\"%s\" freq=%d psk=%s "
1223 "go_dev_addr=" MACSTR "%s",
1224 wpa_s->ifname,
1225 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1226 ssid->frequency, psk,
1227 MAC2STR(wpa_s->global->p2p_dev_addr),
1228 params->persistent_group ?
1229 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001230 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001231
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001232 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001233 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001234 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001236 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001237 wpas_p2p_add_psk_list(wpa_s, ssid);
1238 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001239 if (network_id < 0)
1240 network_id = ssid->id;
1241 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 wpas_p2p_cross_connect_setup(wpa_s);
1243 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001244
1245 if (wpa_s->p2p_first_connection_timeout) {
1246 wpa_dbg(wpa_s, MSG_DEBUG,
1247 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1248 wpa_s->p2p_first_connection_timeout);
1249 wpa_s->p2p_go_group_formation_completed = 0;
1250 wpa_s->global->p2p_group_formation = wpa_s;
1251 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1252 wpa_s->parent, NULL);
1253 eloop_register_timeout(
1254 wpa_s->p2p_first_connection_timeout, 0,
1255 wpas_p2p_group_formation_timeout,
1256 wpa_s->parent, NULL);
1257 }
1258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 return;
1260 }
1261
1262 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1263 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1264 params->peer_interface_addr)) {
1265 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1266 "filtering");
1267 return;
1268 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001269 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001272#ifdef CONFIG_WPS_NFC
1273 } else if (params->wps_method == WPS_NFC) {
1274 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1275 DEV_PW_NFC_CONNECTION_HANDOVER &&
1276 !wpa_s->parent->p2p_oob_dev_pw) {
1277 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1278 return;
1279 }
1280 wpas_ap_wps_add_nfc_pw(
1281 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1282 wpa_s->parent->p2p_oob_dev_pw,
1283 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1284 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1285#endif /* CONFIG_WPS_NFC */
1286 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001288 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 os_free(wpa_s->go_params);
1290 wpa_s->go_params = NULL;
1291}
1292
1293
1294static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1295 struct p2p_go_neg_results *params,
1296 int group_formation)
1297{
1298 struct wpa_ssid *ssid;
1299
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1301 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1303 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001305 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306
1307 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001308 if (ssid == NULL) {
1309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001313 wpa_s->show_group_started = 0;
1314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 wpa_config_set_network_defaults(ssid);
1316 ssid->temporary = 1;
1317 ssid->p2p_group = 1;
1318 ssid->p2p_persistent_group = params->persistent_group;
1319 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1320 WPAS_MODE_P2P_GO;
1321 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001322 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001323 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 ssid->ssid = os_zalloc(params->ssid_len + 1);
1325 if (ssid->ssid) {
1326 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1327 ssid->ssid_len = params->ssid_len;
1328 }
1329 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1330 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1331 ssid->proto = WPA_PROTO_RSN;
1332 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001333 if (os_strlen(params->passphrase) > 0) {
1334 ssid->passphrase = os_strdup(params->passphrase);
1335 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001336 wpa_msg_global(wpa_s, MSG_ERROR,
1337 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338 wpa_config_remove_network(wpa_s->conf, ssid->id);
1339 return;
1340 }
1341 } else
1342 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001343 ssid->psk_set = params->psk_set;
1344 if (ssid->psk_set)
1345 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001346 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001347 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001348 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349
1350 wpa_s->ap_configured_cb = p2p_go_configured;
1351 wpa_s->ap_configured_cb_ctx = wpa_s;
1352 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001353 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 wpa_s->reassociate = 1;
1355 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1357 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 wpa_supplicant_req_scan(wpa_s, 0, 0);
1359}
1360
1361
1362static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1363 const struct wpa_supplicant *src)
1364{
1365 struct wpa_config *d;
1366 const struct wpa_config *s;
1367
1368 d = dst->conf;
1369 s = src->conf;
1370
1371#define C(n) if (s->n) d->n = os_strdup(s->n)
1372 C(device_name);
1373 C(manufacturer);
1374 C(model_name);
1375 C(model_number);
1376 C(serial_number);
1377 C(config_methods);
1378#undef C
1379
1380 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1381 os_memcpy(d->sec_device_type, s->sec_device_type,
1382 sizeof(d->sec_device_type));
1383 d->num_sec_device_types = s->num_sec_device_types;
1384
1385 d->p2p_group_idle = s->p2p_group_idle;
1386 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001387 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001388 d->max_num_sta = s->max_num_sta;
1389 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001390 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001391 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001392 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001393 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001394 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001395
1396 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1397 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1398 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400}
1401
1402
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001403static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1404 char *ifname, size_t len)
1405{
1406 char *ifname_ptr = wpa_s->ifname;
1407
1408 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1409 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1410 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1411 }
1412
1413 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1414 if (os_strlen(ifname) >= IFNAMSIZ &&
1415 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1416 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001417 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001418 }
1419}
1420
1421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1423 enum wpa_driver_if_type type)
1424{
1425 char ifname[120], force_ifname[120];
1426
1427 if (wpa_s->pending_interface_name[0]) {
1428 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1429 "- skip creation of a new one");
1430 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1431 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1432 "unknown?! ifname='%s'",
1433 wpa_s->pending_interface_name);
1434 return -1;
1435 }
1436 return 0;
1437 }
1438
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001439 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 force_ifname[0] = '\0';
1441
1442 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1443 ifname);
1444 wpa_s->p2p_group_idx++;
1445
1446 wpa_s->pending_interface_type = type;
1447 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1448 wpa_s->pending_interface_addr, NULL) < 0) {
1449 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1450 "interface");
1451 return -1;
1452 }
1453
1454 if (force_ifname[0]) {
1455 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1456 force_ifname);
1457 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1458 sizeof(wpa_s->pending_interface_name));
1459 } else
1460 os_strlcpy(wpa_s->pending_interface_name, ifname,
1461 sizeof(wpa_s->pending_interface_name));
1462 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1463 MACSTR, wpa_s->pending_interface_name,
1464 MAC2STR(wpa_s->pending_interface_addr));
1465
1466 return 0;
1467}
1468
1469
1470static void wpas_p2p_remove_pending_group_interface(
1471 struct wpa_supplicant *wpa_s)
1472{
1473 if (!wpa_s->pending_interface_name[0] ||
1474 is_zero_ether_addr(wpa_s->pending_interface_addr))
1475 return; /* No pending virtual interface */
1476
1477 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1478 wpa_s->pending_interface_name);
1479 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1480 wpa_s->pending_interface_name);
1481 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1482 wpa_s->pending_interface_name[0] = '\0';
1483}
1484
1485
1486static struct wpa_supplicant *
1487wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1488{
1489 struct wpa_interface iface;
1490 struct wpa_supplicant *group_wpa_s;
1491
1492 if (!wpa_s->pending_interface_name[0]) {
1493 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1494 if (!wpas_p2p_create_iface(wpa_s))
1495 return NULL;
1496 /*
1497 * Something has forced us to remove the pending interface; try
1498 * to create a new one and hope for the best that we will get
1499 * the same local address.
1500 */
1501 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1502 WPA_IF_P2P_CLIENT) < 0)
1503 return NULL;
1504 }
1505
1506 os_memset(&iface, 0, sizeof(iface));
1507 iface.ifname = wpa_s->pending_interface_name;
1508 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001509 if (wpa_s->conf->ctrl_interface == NULL &&
1510 wpa_s->parent != wpa_s &&
1511 wpa_s->p2p_mgmt &&
1512 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1513 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1514 else
1515 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 iface.driver_param = wpa_s->conf->driver_param;
1517 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1518 if (group_wpa_s == NULL) {
1519 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1520 "wpa_supplicant interface");
1521 return NULL;
1522 }
1523 wpa_s->pending_interface_name[0] = '\0';
1524 group_wpa_s->parent = wpa_s;
1525 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1526 P2P_GROUP_INTERFACE_CLIENT;
1527 wpa_s->global->p2p_group_formation = group_wpa_s;
1528
1529 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1530
1531 return group_wpa_s;
1532}
1533
1534
1535static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1536 void *timeout_ctx)
1537{
1538 struct wpa_supplicant *wpa_s = eloop_ctx;
1539 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001540 wpas_p2p_group_formation_failed(wpa_s);
1541}
1542
1543
1544void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1545{
1546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1547 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 if (wpa_s->global->p2p)
1549 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpas_group_formation_completed(wpa_s, 0);
1551}
1552
1553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001554static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1555{
1556 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1557 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1558 wpa_s->parent, NULL);
1559 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1560 wpa_s->parent, NULL);
1561 wpa_s->global->p2p_fail_on_wps_complete = 0;
1562}
1563
1564
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001565void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1566{
1567 if (wpa_s->global->p2p_group_formation != wpa_s)
1568 return;
1569 /* Speed up group formation timeout since this cannot succeed */
1570 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1571 wpa_s->parent, NULL);
1572 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1573 wpa_s->parent, NULL);
1574}
1575
1576
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578{
1579 struct wpa_supplicant *wpa_s = ctx;
1580
1581 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1582 wpa_drv_cancel_remain_on_channel(wpa_s);
1583 wpa_s->off_channel_freq = 0;
1584 wpa_s->roc_waiting_drv_freq = 0;
1585 }
1586
1587 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001588 wpa_msg_global(wpa_s, MSG_INFO,
1589 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1590 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001591 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpas_p2p_remove_pending_group_interface(wpa_s);
1593 return;
1594 }
1595
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001596 if (wpa_s->p2p_go_ht40)
1597 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001598 if (wpa_s->p2p_go_vht)
1599 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001600
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001601 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1602 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1603 " wps_method=%s",
1604 res->role_go ? "GO" : "client", res->freq, res->ht40,
1605 MAC2STR(res->peer_device_addr),
1606 MAC2STR(res->peer_interface_addr),
1607 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609
Dmitry Shmidt04949592012-07-19 12:16:46 -07001610 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1611 struct wpa_ssid *ssid;
1612 ssid = wpa_config_get_network(wpa_s->conf,
1613 wpa_s->p2p_persistent_id);
1614 if (ssid && ssid->disabled == 2 &&
1615 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1616 size_t len = os_strlen(ssid->passphrase);
1617 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1618 "on requested persistent group");
1619 os_memcpy(res->passphrase, ssid->passphrase, len);
1620 res->passphrase[len] = '\0';
1621 }
1622 }
1623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (wpa_s->create_p2p_iface) {
1625 struct wpa_supplicant *group_wpa_s =
1626 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1627 if (group_wpa_s == NULL) {
1628 wpas_p2p_remove_pending_group_interface(wpa_s);
1629 return;
1630 }
1631 if (group_wpa_s != wpa_s) {
1632 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1633 sizeof(group_wpa_s->p2p_pin));
1634 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1635 }
1636 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1637 wpa_s->pending_interface_name[0] = '\0';
1638 group_wpa_s->p2p_in_provisioning = 1;
1639
1640 if (res->role_go)
1641 wpas_start_wps_go(group_wpa_s, res, 1);
1642 else
1643 wpas_start_wps_enrollee(group_wpa_s, res);
1644 } else {
1645 wpa_s->p2p_in_provisioning = 1;
1646 wpa_s->global->p2p_group_formation = wpa_s;
1647
1648 if (res->role_go)
1649 wpas_start_wps_go(wpa_s, res, 1);
1650 else
1651 wpas_start_wps_enrollee(ctx, res);
1652 }
1653
1654 wpa_s->p2p_long_listen = 0;
1655 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1656
1657 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1658 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1659 (res->peer_config_timeout % 100) * 10000,
1660 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1661}
1662
1663
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001664static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665{
1666 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001667 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1668 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669
1670 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1671}
1672
1673
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001674static void wpas_dev_found(void *ctx, const u8 *addr,
1675 const struct p2p_peer_info *info,
1676 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001678#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 struct wpa_supplicant *wpa_s = ctx;
1680 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001681 char *wfd_dev_info_hex = NULL;
1682
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001683#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001684 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1685 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001686#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001687
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001688 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1689 " p2p_dev_addr=" MACSTR
1690 " pri_dev_type=%s name='%s' config_methods=0x%x "
1691 "dev_capab=0x%x group_capab=0x%x%s%s",
1692 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1693 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1694 sizeof(devtype)),
1695 info->device_name, info->config_methods,
1696 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001697 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1698 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001700 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001701#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1704}
1705
1706
1707static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1708{
1709 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001710
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001711 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1712 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1715}
1716
1717
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001718static void wpas_find_stopped(void *ctx)
1719{
1720 struct wpa_supplicant *wpa_s = ctx;
1721 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1722}
1723
1724
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001725struct wpas_p2p_listen_work {
1726 unsigned int freq;
1727 unsigned int duration;
1728 struct wpabuf *probe_resp_ie;
1729};
1730
1731
1732static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1733{
1734 if (lwork == NULL)
1735 return;
1736 wpabuf_free(lwork->probe_resp_ie);
1737 os_free(lwork);
1738}
1739
1740
1741static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1742{
1743 struct wpas_p2p_listen_work *lwork;
1744
1745 if (!wpa_s->p2p_listen_work)
1746 return;
1747
1748 lwork = wpa_s->p2p_listen_work->ctx;
1749 wpas_p2p_listen_work_free(lwork);
1750 radio_work_done(wpa_s->p2p_listen_work);
1751 wpa_s->p2p_listen_work = NULL;
1752}
1753
1754
1755static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1756{
1757 struct wpa_supplicant *wpa_s = work->wpa_s;
1758 struct wpas_p2p_listen_work *lwork = work->ctx;
1759
1760 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001761 if (work->started) {
1762 wpa_s->p2p_listen_work = NULL;
1763 wpas_stop_listen(wpa_s);
1764 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001765 wpas_p2p_listen_work_free(lwork);
1766 return;
1767 }
1768
1769 wpa_s->p2p_listen_work = work;
1770
1771 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1772
1773 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1774 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1775 "report received Probe Request frames");
1776 wpas_p2p_listen_work_done(wpa_s);
1777 return;
1778 }
1779
1780 wpa_s->pending_listen_freq = lwork->freq;
1781 wpa_s->pending_listen_duration = lwork->duration;
1782
1783 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1784 {
1785 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1786 "to remain on channel (%u MHz) for Listen "
1787 "state", lwork->freq);
1788 wpas_p2p_listen_work_done(wpa_s);
1789 wpa_s->pending_listen_freq = 0;
1790 return;
1791 }
1792 wpa_s->off_channel_freq = 0;
1793 wpa_s->roc_waiting_drv_freq = lwork->freq;
1794}
1795
1796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797static int wpas_start_listen(void *ctx, unsigned int freq,
1798 unsigned int duration,
1799 const struct wpabuf *probe_resp_ie)
1800{
1801 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001802 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001804 if (wpa_s->p2p_listen_work) {
1805 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 return -1;
1807 }
1808
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001809 lwork = os_zalloc(sizeof(*lwork));
1810 if (lwork == NULL)
1811 return -1;
1812 lwork->freq = freq;
1813 lwork->duration = duration;
1814 if (probe_resp_ie) {
1815 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1816 if (lwork->probe_resp_ie == NULL) {
1817 wpas_p2p_listen_work_free(lwork);
1818 return -1;
1819 }
1820 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001822 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1823 lwork) < 0) {
1824 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001825 return -1;
1826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827
1828 return 0;
1829}
1830
1831
1832static void wpas_stop_listen(void *ctx)
1833{
1834 struct wpa_supplicant *wpa_s = ctx;
1835 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1836 wpa_drv_cancel_remain_on_channel(wpa_s);
1837 wpa_s->off_channel_freq = 0;
1838 wpa_s->roc_waiting_drv_freq = 0;
1839 }
1840 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1841 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001842 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843}
1844
1845
1846static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1847{
1848 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001849 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850}
1851
1852
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001853/*
1854 * DNS Header section is used only to calculate compression pointers, so the
1855 * contents of this data does not matter, but the length needs to be reserved
1856 * in the virtual packet.
1857 */
1858#define DNS_HEADER_LEN 12
1859
1860/*
1861 * 27-octet in-memory packet from P2P specification containing two implied
1862 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1863 */
1864#define P2P_SD_IN_MEMORY_LEN 27
1865
1866static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1867 u8 **spos, const u8 *end)
1868{
1869 while (*spos < end) {
1870 u8 val = ((*spos)[0] & 0xc0) >> 6;
1871 int len;
1872
1873 if (val == 1 || val == 2) {
1874 /* These are reserved values in RFC 1035 */
1875 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1876 "sequence starting with 0x%x", val);
1877 return -1;
1878 }
1879
1880 if (val == 3) {
1881 u16 offset;
1882 u8 *spos_tmp;
1883
1884 /* Offset */
1885 if (*spos + 2 > end) {
1886 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1887 "DNS offset field");
1888 return -1;
1889 }
1890
1891 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1892 if (offset >= *spos - start) {
1893 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1894 "pointer offset %u", offset);
1895 return -1;
1896 }
1897
1898 (*spos) += 2;
1899 spos_tmp = start + offset;
1900 return p2p_sd_dns_uncompress_label(upos, uend, start,
1901 &spos_tmp,
1902 *spos - 2);
1903 }
1904
1905 /* Label */
1906 len = (*spos)[0] & 0x3f;
1907 if (len == 0)
1908 return 0;
1909
1910 (*spos)++;
1911 if (*spos + len > end) {
1912 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1913 "sequence - no room for label with length "
1914 "%u", len);
1915 return -1;
1916 }
1917
1918 if (*upos + len + 2 > uend)
1919 return -2;
1920
1921 os_memcpy(*upos, *spos, len);
1922 *spos += len;
1923 *upos += len;
1924 (*upos)[0] = '.';
1925 (*upos)++;
1926 (*upos)[0] = '\0';
1927 }
1928
1929 return 0;
1930}
1931
1932
1933/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1934 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1935 * not large enough */
1936static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1937 size_t msg_len, size_t offset)
1938{
1939 /* 27-octet in-memory packet from P2P specification */
1940 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1941 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1942 u8 *tmp, *end, *spos;
1943 char *upos, *uend;
1944 int ret = 0;
1945
1946 if (buf_len < 2)
1947 return -1;
1948 if (offset > msg_len)
1949 return -1;
1950
1951 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1952 if (tmp == NULL)
1953 return -1;
1954 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1955 end = spos + msg_len;
1956 spos += offset;
1957
1958 os_memset(tmp, 0, DNS_HEADER_LEN);
1959 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1960 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1961
1962 upos = buf;
1963 uend = buf + buf_len;
1964
1965 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1966 if (ret) {
1967 os_free(tmp);
1968 return ret;
1969 }
1970
1971 if (upos == buf) {
1972 upos[0] = '.';
1973 upos[1] = '\0';
1974 } else if (upos[-1] == '.')
1975 upos[-1] = '\0';
1976
1977 os_free(tmp);
1978 return 0;
1979}
1980
1981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982static struct p2p_srv_bonjour *
1983wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1984 const struct wpabuf *query)
1985{
1986 struct p2p_srv_bonjour *bsrv;
1987 size_t len;
1988
1989 len = wpabuf_len(query);
1990 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1991 struct p2p_srv_bonjour, list) {
1992 if (len == wpabuf_len(bsrv->query) &&
1993 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1994 len) == 0)
1995 return bsrv;
1996 }
1997 return NULL;
1998}
1999
2000
2001static struct p2p_srv_upnp *
2002wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2003 const char *service)
2004{
2005 struct p2p_srv_upnp *usrv;
2006
2007 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2008 struct p2p_srv_upnp, list) {
2009 if (version == usrv->version &&
2010 os_strcmp(service, usrv->service) == 0)
2011 return usrv;
2012 }
2013 return NULL;
2014}
2015
2016
2017static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2018 u8 srv_trans_id)
2019{
2020 u8 *len_pos;
2021
2022 if (wpabuf_tailroom(resp) < 5)
2023 return;
2024
2025 /* Length (to be filled) */
2026 len_pos = wpabuf_put(resp, 2);
2027 wpabuf_put_u8(resp, srv_proto);
2028 wpabuf_put_u8(resp, srv_trans_id);
2029 /* Status Code */
2030 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2031 /* Response Data: empty */
2032 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2033}
2034
2035
2036static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2037 struct wpabuf *resp, u8 srv_trans_id)
2038{
2039 struct p2p_srv_bonjour *bsrv;
2040 u8 *len_pos;
2041
2042 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2043
2044 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2045 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2046 return;
2047 }
2048
2049 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2050 struct p2p_srv_bonjour, list) {
2051 if (wpabuf_tailroom(resp) <
2052 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2053 return;
2054 /* Length (to be filled) */
2055 len_pos = wpabuf_put(resp, 2);
2056 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2057 wpabuf_put_u8(resp, srv_trans_id);
2058 /* Status Code */
2059 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2060 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2061 wpabuf_head(bsrv->resp),
2062 wpabuf_len(bsrv->resp));
2063 /* Response Data */
2064 wpabuf_put_buf(resp, bsrv->query); /* Key */
2065 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2066 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2067 2);
2068 }
2069}
2070
2071
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002072static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2073 size_t query_len)
2074{
2075 char str_rx[256], str_srv[256];
2076
2077 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2078 return 0; /* Too short to include DNS Type and Version */
2079 if (os_memcmp(query + query_len - 3,
2080 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2081 3) != 0)
2082 return 0; /* Mismatch in DNS Type or Version */
2083 if (query_len == wpabuf_len(bsrv->query) &&
2084 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2085 return 1; /* Binary match */
2086
2087 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2088 0))
2089 return 0; /* Failed to uncompress query */
2090 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2091 wpabuf_head(bsrv->query),
2092 wpabuf_len(bsrv->query) - 3, 0))
2093 return 0; /* Failed to uncompress service */
2094
2095 return os_strcmp(str_rx, str_srv) == 0;
2096}
2097
2098
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2100 struct wpabuf *resp, u8 srv_trans_id,
2101 const u8 *query, size_t query_len)
2102{
2103 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002105 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106
2107 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2108 query, query_len);
2109 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2110 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2111 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2112 srv_trans_id);
2113 return;
2114 }
2115
2116 if (query_len == 0) {
2117 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2118 return;
2119 }
2120
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002121 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2122 struct p2p_srv_bonjour, list) {
2123 if (!match_bonjour_query(bsrv, query, query_len))
2124 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002126 if (wpabuf_tailroom(resp) <
2127 5 + query_len + wpabuf_len(bsrv->resp))
2128 return;
2129
2130 matches++;
2131
2132 /* Length (to be filled) */
2133 len_pos = wpabuf_put(resp, 2);
2134 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2135 wpabuf_put_u8(resp, srv_trans_id);
2136
2137 /* Status Code */
2138 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2139 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2140 wpabuf_head(bsrv->resp),
2141 wpabuf_len(bsrv->resp));
2142
2143 /* Response Data */
2144 wpabuf_put_data(resp, query, query_len); /* Key */
2145 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2146
2147 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2148 }
2149
2150 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2152 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002153 if (wpabuf_tailroom(resp) < 5)
2154 return;
2155
2156 /* Length (to be filled) */
2157 len_pos = wpabuf_put(resp, 2);
2158 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2159 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160
2161 /* Status Code */
2162 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2163 /* Response Data: empty */
2164 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2165 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167}
2168
2169
2170static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2171 struct wpabuf *resp, u8 srv_trans_id)
2172{
2173 struct p2p_srv_upnp *usrv;
2174 u8 *len_pos;
2175
2176 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2177
2178 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2179 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2180 return;
2181 }
2182
2183 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2184 struct p2p_srv_upnp, list) {
2185 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2186 return;
2187
2188 /* Length (to be filled) */
2189 len_pos = wpabuf_put(resp, 2);
2190 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2191 wpabuf_put_u8(resp, srv_trans_id);
2192
2193 /* Status Code */
2194 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2195 /* Response Data */
2196 wpabuf_put_u8(resp, usrv->version);
2197 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2198 usrv->service);
2199 wpabuf_put_str(resp, usrv->service);
2200 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2201 2);
2202 }
2203}
2204
2205
2206static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2207 struct wpabuf *resp, u8 srv_trans_id,
2208 const u8 *query, size_t query_len)
2209{
2210 struct p2p_srv_upnp *usrv;
2211 u8 *len_pos;
2212 u8 version;
2213 char *str;
2214 int count = 0;
2215
2216 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2217 query, query_len);
2218
2219 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2220 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2221 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2222 srv_trans_id);
2223 return;
2224 }
2225
2226 if (query_len == 0) {
2227 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2228 return;
2229 }
2230
2231 if (wpabuf_tailroom(resp) < 5)
2232 return;
2233
2234 /* Length (to be filled) */
2235 len_pos = wpabuf_put(resp, 2);
2236 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2237 wpabuf_put_u8(resp, srv_trans_id);
2238
2239 version = query[0];
2240 str = os_malloc(query_len);
2241 if (str == NULL)
2242 return;
2243 os_memcpy(str, query + 1, query_len - 1);
2244 str[query_len - 1] = '\0';
2245
2246 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2247 struct p2p_srv_upnp, list) {
2248 if (version != usrv->version)
2249 continue;
2250
2251 if (os_strcmp(str, "ssdp:all") != 0 &&
2252 os_strstr(usrv->service, str) == NULL)
2253 continue;
2254
2255 if (wpabuf_tailroom(resp) < 2)
2256 break;
2257 if (count == 0) {
2258 /* Status Code */
2259 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2260 /* Response Data */
2261 wpabuf_put_u8(resp, version);
2262 } else
2263 wpabuf_put_u8(resp, ',');
2264
2265 count++;
2266
2267 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2268 usrv->service);
2269 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2270 break;
2271 wpabuf_put_str(resp, usrv->service);
2272 }
2273 os_free(str);
2274
2275 if (count == 0) {
2276 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2277 "available");
2278 /* Status Code */
2279 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2280 /* Response Data: empty */
2281 }
2282
2283 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2284}
2285
2286
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002287#ifdef CONFIG_WIFI_DISPLAY
2288static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2289 struct wpabuf *resp, u8 srv_trans_id,
2290 const u8 *query, size_t query_len)
2291{
2292 const u8 *pos;
2293 u8 role;
2294 u8 *len_pos;
2295
2296 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2297
2298 if (!wpa_s->global->wifi_display) {
2299 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2300 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2301 srv_trans_id);
2302 return;
2303 }
2304
2305 if (query_len < 1) {
2306 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2307 "Role");
2308 return;
2309 }
2310
2311 if (wpabuf_tailroom(resp) < 5)
2312 return;
2313
2314 pos = query;
2315 role = *pos++;
2316 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2317
2318 /* TODO: role specific handling */
2319
2320 /* Length (to be filled) */
2321 len_pos = wpabuf_put(resp, 2);
2322 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2323 wpabuf_put_u8(resp, srv_trans_id);
2324 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2325
2326 while (pos < query + query_len) {
2327 if (*pos < MAX_WFD_SUBELEMS &&
2328 wpa_s->global->wfd_subelem[*pos] &&
2329 wpabuf_tailroom(resp) >=
2330 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2331 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2332 "subelement %u", *pos);
2333 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2334 }
2335 pos++;
2336 }
2337
2338 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2339}
2340#endif /* CONFIG_WIFI_DISPLAY */
2341
2342
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002343static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2344 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345{
2346 struct wpa_supplicant *wpa_s = ctx;
2347 const u8 *pos = tlvs;
2348 const u8 *end = tlvs + tlvs_len;
2349 const u8 *tlv_end;
2350 u16 slen;
2351 struct wpabuf *resp;
2352 u8 srv_proto, srv_trans_id;
2353 size_t buf_len;
2354 char *buf;
2355
2356 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2357 tlvs, tlvs_len);
2358 buf_len = 2 * tlvs_len + 1;
2359 buf = os_malloc(buf_len);
2360 if (buf) {
2361 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2362 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2363 MACSTR " %u %u %s",
2364 freq, MAC2STR(sa), dialog_token, update_indic,
2365 buf);
2366 os_free(buf);
2367 }
2368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002369 if (wpa_s->p2p_sd_over_ctrl_iface) {
2370 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2371 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002373 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374
2375 resp = wpabuf_alloc(10000);
2376 if (resp == NULL)
2377 return;
2378
2379 while (pos + 1 < end) {
2380 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2381 slen = WPA_GET_LE16(pos);
2382 pos += 2;
2383 if (pos + slen > end || slen < 2) {
2384 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2385 "length");
2386 wpabuf_free(resp);
2387 return;
2388 }
2389 tlv_end = pos + slen;
2390
2391 srv_proto = *pos++;
2392 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2393 srv_proto);
2394 srv_trans_id = *pos++;
2395 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2396 srv_trans_id);
2397
2398 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2399 pos, tlv_end - pos);
2400
2401
2402 if (wpa_s->force_long_sd) {
2403 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2404 "response");
2405 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2406 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2407 goto done;
2408 }
2409
2410 switch (srv_proto) {
2411 case P2P_SERV_ALL_SERVICES:
2412 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2413 "for all services");
2414 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2415 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2416 wpa_printf(MSG_DEBUG, "P2P: No service "
2417 "discovery protocols available");
2418 wpas_sd_add_proto_not_avail(
2419 resp, P2P_SERV_ALL_SERVICES,
2420 srv_trans_id);
2421 break;
2422 }
2423 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2424 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2425 break;
2426 case P2P_SERV_BONJOUR:
2427 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2428 pos, tlv_end - pos);
2429 break;
2430 case P2P_SERV_UPNP:
2431 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2432 pos, tlv_end - pos);
2433 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002434#ifdef CONFIG_WIFI_DISPLAY
2435 case P2P_SERV_WIFI_DISPLAY:
2436 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2437 pos, tlv_end - pos);
2438 break;
2439#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 default:
2441 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2442 "protocol %u", srv_proto);
2443 wpas_sd_add_proto_not_avail(resp, srv_proto,
2444 srv_trans_id);
2445 break;
2446 }
2447
2448 pos = tlv_end;
2449 }
2450
2451done:
2452 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2453 update_indic, tlvs, tlvs_len);
2454
2455 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2456
2457 wpabuf_free(resp);
2458}
2459
2460
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002461static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2462 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463{
2464 struct wpa_supplicant *wpa_s = ctx;
2465 const u8 *pos = tlvs;
2466 const u8 *end = tlvs + tlvs_len;
2467 const u8 *tlv_end;
2468 u16 slen;
2469 size_t buf_len;
2470 char *buf;
2471
2472 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2473 tlvs, tlvs_len);
2474 if (tlvs_len > 1500) {
2475 /* TODO: better way for handling this */
2476 wpa_msg_ctrl(wpa_s, MSG_INFO,
2477 P2P_EVENT_SERV_DISC_RESP MACSTR
2478 " %u <long response: %u bytes>",
2479 MAC2STR(sa), update_indic,
2480 (unsigned int) tlvs_len);
2481 } else {
2482 buf_len = 2 * tlvs_len + 1;
2483 buf = os_malloc(buf_len);
2484 if (buf) {
2485 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2486 wpa_msg_ctrl(wpa_s, MSG_INFO,
2487 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2488 MAC2STR(sa), update_indic, buf);
2489 os_free(buf);
2490 }
2491 }
2492
2493 while (pos < end) {
2494 u8 srv_proto, srv_trans_id, status;
2495
2496 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2497 slen = WPA_GET_LE16(pos);
2498 pos += 2;
2499 if (pos + slen > end || slen < 3) {
2500 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2501 "length");
2502 return;
2503 }
2504 tlv_end = pos + slen;
2505
2506 srv_proto = *pos++;
2507 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2508 srv_proto);
2509 srv_trans_id = *pos++;
2510 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2511 srv_trans_id);
2512 status = *pos++;
2513 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2514 status);
2515
2516 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2517 pos, tlv_end - pos);
2518
2519 pos = tlv_end;
2520 }
2521
2522 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2523}
2524
2525
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002526u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2527 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002530 return 0;
2531 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532}
2533
2534
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002535u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2536 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537{
2538 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002539 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540
2541 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2542 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002543 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2545 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2546 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2547 wpabuf_put_u8(tlvs, version);
2548 wpabuf_put_str(tlvs, query);
2549 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2550 wpabuf_free(tlvs);
2551 return ret;
2552}
2553
2554
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002555#ifdef CONFIG_WIFI_DISPLAY
2556
2557static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2558 const struct wpabuf *tlvs)
2559{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002560 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2561 return 0;
2562 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2563}
2564
2565
2566#define MAX_WFD_SD_SUBELEMS 20
2567
2568static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2569 const char *subelems)
2570{
2571 u8 *len;
2572 const char *pos;
2573 int val;
2574 int count = 0;
2575
2576 len = wpabuf_put(tlvs, 2);
2577 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2578 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2579
2580 wpabuf_put_u8(tlvs, role);
2581
2582 pos = subelems;
2583 while (*pos) {
2584 val = atoi(pos);
2585 if (val >= 0 && val < 256) {
2586 wpabuf_put_u8(tlvs, val);
2587 count++;
2588 if (count == MAX_WFD_SD_SUBELEMS)
2589 break;
2590 }
2591 pos = os_strchr(pos + 1, ',');
2592 if (pos == NULL)
2593 break;
2594 pos++;
2595 }
2596
2597 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2598}
2599
2600
2601u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2602 const u8 *dst, const char *role)
2603{
2604 struct wpabuf *tlvs;
2605 u64 ret;
2606 const char *subelems;
2607 u8 id = 1;
2608
2609 subelems = os_strchr(role, ' ');
2610 if (subelems == NULL)
2611 return 0;
2612 subelems++;
2613
2614 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2615 if (tlvs == NULL)
2616 return 0;
2617
2618 if (os_strstr(role, "[source]"))
2619 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2620 if (os_strstr(role, "[pri-sink]"))
2621 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2622 if (os_strstr(role, "[sec-sink]"))
2623 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2624 if (os_strstr(role, "[source+sink]"))
2625 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2626
2627 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2628 wpabuf_free(tlvs);
2629 return ret;
2630}
2631
2632#endif /* CONFIG_WIFI_DISPLAY */
2633
2634
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002635int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2638 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002639 return p2p_sd_cancel_request(wpa_s->global->p2p,
2640 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002641}
2642
2643
2644void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2645 const u8 *dst, u8 dialog_token,
2646 const struct wpabuf *resp_tlvs)
2647{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2649 return;
2650 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2651 resp_tlvs);
2652}
2653
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2656{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657 if (wpa_s->global->p2p)
2658 p2p_sd_service_update(wpa_s->global->p2p);
2659}
2660
2661
2662static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2663{
2664 dl_list_del(&bsrv->list);
2665 wpabuf_free(bsrv->query);
2666 wpabuf_free(bsrv->resp);
2667 os_free(bsrv);
2668}
2669
2670
2671static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2672{
2673 dl_list_del(&usrv->list);
2674 os_free(usrv->service);
2675 os_free(usrv);
2676}
2677
2678
2679void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2680{
2681 struct p2p_srv_bonjour *bsrv, *bn;
2682 struct p2p_srv_upnp *usrv, *un;
2683
2684 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2685 struct p2p_srv_bonjour, list)
2686 wpas_p2p_srv_bonjour_free(bsrv);
2687
2688 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2689 struct p2p_srv_upnp, list)
2690 wpas_p2p_srv_upnp_free(usrv);
2691
2692 wpas_p2p_sd_service_update(wpa_s);
2693}
2694
2695
2696int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2697 struct wpabuf *query, struct wpabuf *resp)
2698{
2699 struct p2p_srv_bonjour *bsrv;
2700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 bsrv = os_zalloc(sizeof(*bsrv));
2702 if (bsrv == NULL)
2703 return -1;
2704 bsrv->query = query;
2705 bsrv->resp = resp;
2706 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2707
2708 wpas_p2p_sd_service_update(wpa_s);
2709 return 0;
2710}
2711
2712
2713int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2714 const struct wpabuf *query)
2715{
2716 struct p2p_srv_bonjour *bsrv;
2717
2718 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2719 if (bsrv == NULL)
2720 return -1;
2721 wpas_p2p_srv_bonjour_free(bsrv);
2722 wpas_p2p_sd_service_update(wpa_s);
2723 return 0;
2724}
2725
2726
2727int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2728 const char *service)
2729{
2730 struct p2p_srv_upnp *usrv;
2731
2732 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2733 return 0; /* Already listed */
2734 usrv = os_zalloc(sizeof(*usrv));
2735 if (usrv == NULL)
2736 return -1;
2737 usrv->version = version;
2738 usrv->service = os_strdup(service);
2739 if (usrv->service == NULL) {
2740 os_free(usrv);
2741 return -1;
2742 }
2743 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2744
2745 wpas_p2p_sd_service_update(wpa_s);
2746 return 0;
2747}
2748
2749
2750int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2751 const char *service)
2752{
2753 struct p2p_srv_upnp *usrv;
2754
2755 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2756 if (usrv == NULL)
2757 return -1;
2758 wpas_p2p_srv_upnp_free(usrv);
2759 wpas_p2p_sd_service_update(wpa_s);
2760 return 0;
2761}
2762
2763
2764static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2765 const u8 *peer, const char *params,
2766 unsigned int generated_pin)
2767{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002768 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2769 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770}
2771
2772
2773static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2774 const u8 *peer, const char *params)
2775{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002776 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2777 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002778}
2779
2780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002781static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2782 const u8 *dev_addr, const u8 *pri_dev_type,
2783 const char *dev_name, u16 supp_config_methods,
2784 u8 dev_capab, u8 group_capab, const u8 *group_id,
2785 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786{
2787 struct wpa_supplicant *wpa_s = ctx;
2788 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002789 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 u8 empty_dev_type[8];
2791 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002792 struct wpa_supplicant *group = NULL;
2793
2794 if (group_id) {
2795 for (group = wpa_s->global->ifaces; group; group = group->next)
2796 {
2797 struct wpa_ssid *s = group->current_ssid;
2798 if (s != NULL &&
2799 s->mode == WPAS_MODE_P2P_GO &&
2800 group_id_len - ETH_ALEN == s->ssid_len &&
2801 os_memcmp(group_id + ETH_ALEN, s->ssid,
2802 s->ssid_len) == 0)
2803 break;
2804 }
2805 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806
2807 if (pri_dev_type == NULL) {
2808 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2809 pri_dev_type = empty_dev_type;
2810 }
2811 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2812 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002813 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002814 MAC2STR(dev_addr),
2815 wps_dev_type_bin2str(pri_dev_type, devtype,
2816 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002817 dev_name, supp_config_methods, dev_capab, group_capab,
2818 group ? " group=" : "",
2819 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 if (config_methods & WPS_CONFIG_DISPLAY) {
2823 generated_pin = wps_generate_pin();
2824 wpas_prov_disc_local_display(wpa_s, peer, params,
2825 generated_pin);
2826 } else if (config_methods & WPS_CONFIG_KEYPAD)
2827 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2828 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002829 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2830 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002831
2832 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2833 P2P_PROV_DISC_SUCCESS,
2834 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835}
2836
2837
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002838static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839{
2840 struct wpa_supplicant *wpa_s = ctx;
2841 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002842 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002844 if (wpa_s->pending_pd_before_join &&
2845 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2846 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2847 wpa_s->pending_pd_before_join = 0;
2848 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2849 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002850 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002851 return;
2852 }
2853
Dmitry Shmidt04949592012-07-19 12:16:46 -07002854 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2855 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2856 os_snprintf(params, sizeof(params), " peer_go=%d",
2857 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2858 else
2859 params[0] = '\0';
2860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002862 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 else if (config_methods & WPS_CONFIG_KEYPAD) {
2864 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002865 wpas_prov_disc_local_display(wpa_s, peer, params,
2866 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002868 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2869 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870
Jouni Malinen75ecf522011-06-27 15:19:46 -07002871 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2872 P2P_PROV_DISC_SUCCESS,
2873 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002874}
2875
2876
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2878 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002879{
2880 struct wpa_supplicant *wpa_s = ctx;
2881
Dmitry Shmidt04949592012-07-19 12:16:46 -07002882 if (wpa_s->p2p_fallback_to_go_neg) {
2883 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2884 "failed - fall back to GO Negotiation");
2885 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2886 return;
2887 }
2888
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002889 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002890 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002891 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2892 "join-existing-group operation (no ACK for PD "
2893 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002894 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002895 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002896 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002897
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002898 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2899 " p2p_dev_addr=" MACSTR " status=%d",
2900 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002901
Jouni Malinen75ecf522011-06-27 15:19:46 -07002902 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2903 status, 0, 0);
2904}
2905
2906
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002907static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2908{
2909 if (channels == NULL)
2910 return 1; /* Assume no restrictions */
2911 return p2p_channels_includes_freq(channels, freq);
2912
2913}
2914
2915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2917 const u8 *go_dev_addr, const u8 *ssid,
2918 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002919 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002920 const struct p2p_channels *channels,
2921 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922{
2923 struct wpa_supplicant *wpa_s = ctx;
2924 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925 int res;
2926 struct wpa_supplicant *grp;
2927
2928 if (!persistent_group) {
2929 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002930 " to join an active group (SSID: %s)",
2931 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2933 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2934 == 0 ||
2935 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2936 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2937 "authorized invitation");
2938 goto accept_inv;
2939 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002940
2941#ifdef CONFIG_WPS_NFC
2942 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2943 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2944 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2945 wpa_s->parent->p2p_wps_method = WPS_NFC;
2946 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2947 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2948 go_dev_addr, ETH_ALEN);
2949 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2950 bssid, ETH_ALEN);
2951 goto accept_inv;
2952 }
2953#endif /* CONFIG_WPS_NFC */
2954
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955 /*
2956 * Do not accept the invitation automatically; notify user and
2957 * request approval.
2958 */
2959 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2960 }
2961
2962 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2963 if (grp) {
2964 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2965 "running persistent group");
2966 if (*go)
2967 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2968 goto accept_inv;
2969 }
2970
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002971 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2972 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2973 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2974 "invitation to re-invoke a persistent group");
2975 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2977
2978 for (s = wpa_s->conf->ssid; s; s = s->next) {
2979 if (s->disabled == 2 &&
2980 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2981 s->ssid_len == ssid_len &&
2982 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2983 break;
2984 }
2985
2986 if (!s) {
2987 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2988 " requested reinvocation of an unknown group",
2989 MAC2STR(sa));
2990 return P2P_SC_FAIL_UNKNOWN_GROUP;
2991 }
2992
2993 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2994 *go = 1;
2995 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2996 wpa_printf(MSG_DEBUG, "P2P: The only available "
2997 "interface is already in use - reject "
2998 "invitation");
2999 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3000 }
3001 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3002 } else if (s->mode == WPAS_MODE_P2P_GO) {
3003 *go = 1;
3004 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3005 {
3006 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3007 "interface address for the group");
3008 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3009 }
3010 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3011 ETH_ALEN);
3012 }
3013
3014accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003015 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3016
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003017 /* Get one of the frequencies currently in use */
3018 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003019 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003020 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003021
3022 if (wpa_s->num_multichan_concurrent < 2 ||
3023 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3024 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3025 *force_freq = res;
3026 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003027 }
3028
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003029 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3030 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003031 if (*go == 0) {
3032 /* We are the client */
3033 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3034 "running a GO but we are capable of MCC, "
3035 "figure out the best channel to use");
3036 *force_freq = 0;
3037 } else if (!freq_included(channels, *force_freq)) {
3038 /* We are the GO, and *force_freq is not in the
3039 * intersection */
3040 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3041 "in intersection but we are capable of MCC, "
3042 "figure out the best channel to use",
3043 *force_freq);
3044 *force_freq = 0;
3045 }
3046 }
3047
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048 return P2P_SC_SUCCESS;
3049}
3050
3051
3052static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3053 const u8 *ssid, size_t ssid_len,
3054 const u8 *go_dev_addr, u8 status,
3055 int op_freq)
3056{
3057 struct wpa_supplicant *wpa_s = ctx;
3058 struct wpa_ssid *s;
3059
3060 for (s = wpa_s->conf->ssid; s; s = s->next) {
3061 if (s->disabled == 2 &&
3062 s->ssid_len == ssid_len &&
3063 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3064 break;
3065 }
3066
3067 if (status == P2P_SC_SUCCESS) {
3068 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003069 " was accepted; op_freq=%d MHz, SSID=%s",
3070 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003071 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003072 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 wpas_p2p_group_add_persistent(
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003074 wpa_s, s, go, 0, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003075 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003077 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003079 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003080 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 }
3082 return;
3083 }
3084
3085 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3086 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3087 " was rejected (status %u)", MAC2STR(sa), status);
3088 return;
3089 }
3090
3091 if (!s) {
3092 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003093 wpa_msg_global(wpa_s, MSG_INFO,
3094 P2P_EVENT_INVITATION_RECEIVED
3095 "sa=" MACSTR " go_dev_addr=" MACSTR
3096 " bssid=" MACSTR " unknown-network",
3097 MAC2STR(sa), MAC2STR(go_dev_addr),
3098 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003099 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003100 wpa_msg_global(wpa_s, MSG_INFO,
3101 P2P_EVENT_INVITATION_RECEIVED
3102 "sa=" MACSTR " go_dev_addr=" MACSTR
3103 " unknown-network",
3104 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 }
3106 return;
3107 }
3108
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003109 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003110 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3111 "sa=" MACSTR " persistent=%d freq=%d",
3112 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003113 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003114 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3115 "sa=" MACSTR " persistent=%d",
3116 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003117 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118}
3119
3120
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003121static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3122 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003123 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003124{
3125 size_t i;
3126
3127 if (ssid == NULL)
3128 return;
3129
3130 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3131 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3132 ETH_ALEN) == 0)
3133 break;
3134 }
3135 if (i >= ssid->num_p2p_clients) {
3136 if (ssid->mode != WPAS_MODE_P2P_GO &&
3137 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3138 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3139 "due to invitation result", ssid->id);
3140 wpas_notify_network_removed(wpa_s, ssid);
3141 wpa_config_remove_network(wpa_s->conf, ssid->id);
3142 return;
3143 }
3144 return; /* Peer not found in client list */
3145 }
3146
3147 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003148 "group %d client list%s",
3149 MAC2STR(peer), ssid->id,
3150 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003151 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3152 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3153 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3154 ssid->num_p2p_clients--;
3155#ifndef CONFIG_NO_CONFIG_WRITE
3156 if (wpa_s->parent->conf->update_config &&
3157 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3158 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3159#endif /* CONFIG_NO_CONFIG_WRITE */
3160}
3161
3162
3163static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3164 const u8 *peer)
3165{
3166 struct wpa_ssid *ssid;
3167
3168 wpa_s = wpa_s->global->p2p_invite_group;
3169 if (wpa_s == NULL)
3170 return; /* No known invitation group */
3171 ssid = wpa_s->current_ssid;
3172 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3173 !ssid->p2p_persistent_group)
3174 return; /* Not operating as a GO in persistent group */
3175 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3176 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003177 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003178}
3179
3180
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003181static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003182 const struct p2p_channels *channels,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003183 const u8 *peer, int neg_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184{
3185 struct wpa_supplicant *wpa_s = ctx;
3186 struct wpa_ssid *ssid;
3187
3188 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003189 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3190 "status=%d " MACSTR,
3191 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003193 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3194 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195 }
3196 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3197
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003198 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3199 status, MAC2STR(peer));
3200 if (wpa_s->pending_invite_ssid_id == -1) {
3201 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3202 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003204 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003205
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003206 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3207 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3208 "invitation exchange to indicate readiness for "
3209 "re-invocation");
3210 }
3211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003213 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3214 ssid = wpa_config_get_network(
3215 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003216 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003217 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003218 wpas_p2p_remove_pending_group_interface(wpa_s);
3219 return;
3220 }
3221
3222 ssid = wpa_config_get_network(wpa_s->conf,
3223 wpa_s->pending_invite_ssid_id);
3224 if (ssid == NULL) {
3225 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3226 "data matching with invitation");
3227 return;
3228 }
3229
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003230 /*
3231 * The peer could have missed our ctrl::ack frame for Invitation
3232 * Response and continue retransmitting the frame. To reduce the
3233 * likelihood of the peer not getting successful TX status for the
3234 * Invitation Response frame, wait a short time here before starting
3235 * the persistent group so that we will remain on the current channel to
3236 * acknowledge any possible retransmission from the peer.
3237 */
3238 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3239 "starting persistent group");
3240 os_sleep(0, 50000);
3241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003243 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003244 wpa_s->p2p_persistent_go_freq,
3245 neg_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003246 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3247 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003248 ssid->mode == WPAS_MODE_P2P_GO ?
3249 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3250 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251}
3252
3253
Dmitry Shmidt04949592012-07-19 12:16:46 -07003254static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3255 unsigned int freq)
3256{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003257 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3258 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003259 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003260}
3261
3262
3263static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3264{
3265 reg->channel[reg->channels] = chan;
3266 reg->channels++;
3267}
3268
3269
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003270static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003271 struct p2p_channels *chan,
3272 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003273{
3274 int i, cla = 0;
3275
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003276 os_memset(cli_chan, 0, sizeof(*cli_chan));
3277
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3279 "band");
3280
3281 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3282 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003283 chan->reg_class[cla].channels = 0;
3284 for (i = 0; i < 11; i++) {
3285 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3286 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3287 }
3288 if (chan->reg_class[cla].channels)
3289 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290
3291 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3292 "band");
3293
3294 /* Operating class 115 - 5 GHz, channels 36-48 */
3295 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003296 chan->reg_class[cla].channels = 0;
3297 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3298 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3299 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3300 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3301 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3302 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3303 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3304 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3305 if (chan->reg_class[cla].channels)
3306 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307
3308 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3309 "band");
3310
3311 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3312 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 chan->reg_class[cla].channels = 0;
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3316 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3317 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3318 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3319 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3320 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3321 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3322 if (chan->reg_class[cla].channels)
3323 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324
3325 chan->reg_classes = cla;
3326 return 0;
3327}
3328
3329
3330static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3331 u16 num_modes,
3332 enum hostapd_hw_mode mode)
3333{
3334 u16 i;
3335
3336 for (i = 0; i < num_modes; i++) {
3337 if (modes[i].mode == mode)
3338 return &modes[i];
3339 }
3340
3341 return NULL;
3342}
3343
3344
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003345enum chan_allowed {
3346 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3347};
3348
Dmitry Shmidt04949592012-07-19 12:16:46 -07003349static int has_channel(struct wpa_global *global,
3350 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003351{
3352 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003353 unsigned int freq;
3354
3355 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3356 chan * 5;
3357 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003358 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003359
3360 for (i = 0; i < mode->num_channels; i++) {
3361 if (mode->channels[i].chan == chan) {
3362 if (flags)
3363 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003364 if (mode->channels[i].flag &
3365 (HOSTAPD_CHAN_DISABLED |
3366 HOSTAPD_CHAN_RADAR))
3367 return NOT_ALLOWED;
3368 if (mode->channels[i].flag &
3369 (HOSTAPD_CHAN_PASSIVE_SCAN |
3370 HOSTAPD_CHAN_NO_IBSS))
3371 return PASSIVE_ONLY;
3372 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373 }
3374 }
3375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003376 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377}
3378
3379
3380struct p2p_oper_class_map {
3381 enum hostapd_hw_mode mode;
3382 u8 op_class;
3383 u8 min_chan;
3384 u8 max_chan;
3385 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003386 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387};
3388
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003389static struct p2p_oper_class_map op_class[] = {
3390 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003391#if 0 /* Do not enable HT40 on 2 GHz for now */
3392 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3393 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3394#endif
3395 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3396 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3397 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3398 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3399 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3400 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003401
3402 /*
3403 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3404 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3405 * 80 MHz, but currently use the following definition for simplicity
3406 * (these center frequencies are not actual channels, which makes
3407 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3408 * removing invalid channels.
3409 */
3410 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003411 { -1, 0, 0, 0, 0, BW20 }
3412};
3413
3414
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003415static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3416 struct hostapd_hw_modes *mode,
3417 u8 channel)
3418{
3419 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3420 unsigned int i;
3421
3422 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3423 return 0;
3424
3425 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3426 /*
3427 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3428 * so the center channel is 6 channels away from the start/end.
3429 */
3430 if (channel >= center_channels[i] - 6 &&
3431 channel <= center_channels[i] + 6)
3432 return center_channels[i];
3433
3434 return 0;
3435}
3436
3437
3438static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3439 struct hostapd_hw_modes *mode,
3440 u8 channel, u8 bw)
3441{
3442 u8 center_chan;
3443 int i, flags;
3444 enum chan_allowed res, ret = ALLOWED;
3445
3446 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3447 if (!center_chan)
3448 return NOT_ALLOWED;
3449 if (center_chan >= 58 && center_chan <= 138)
3450 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3451
3452 /* check all the channels are available */
3453 for (i = 0; i < 4; i++) {
3454 int adj_chan = center_chan - 6 + i * 4;
3455
3456 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3457 if (res == NOT_ALLOWED)
3458 return NOT_ALLOWED;
3459 if (res == PASSIVE_ONLY)
3460 ret = PASSIVE_ONLY;
3461
3462 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3463 return NOT_ALLOWED;
3464 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3465 return NOT_ALLOWED;
3466 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3467 return NOT_ALLOWED;
3468 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3469 return NOT_ALLOWED;
3470 }
3471
3472 return ret;
3473}
3474
3475
3476static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3477 struct hostapd_hw_modes *mode,
3478 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003479{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003480 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003481 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003482
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003483 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3484 if (bw == BW40MINUS) {
3485 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3486 return NOT_ALLOWED;
3487 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3488 } else if (bw == BW40PLUS) {
3489 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3490 return NOT_ALLOWED;
3491 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3492 } else if (bw == BW80) {
3493 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3494 }
3495
3496 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3497 return NOT_ALLOWED;
3498 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3499 return PASSIVE_ONLY;
3500 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003501}
3502
3503
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003504static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003505 struct p2p_channels *chan,
3506 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003508 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003509 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003511 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3513 "of all supported channels; assume dualband "
3514 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003515 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003516 }
3517
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003518 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003519
3520 for (op = 0; op_class[op].op_class; op++) {
3521 struct p2p_oper_class_map *o = &op_class[op];
3522 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003523 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003525 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 if (mode == NULL)
3527 continue;
3528 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003529 enum chan_allowed res;
3530 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3531 if (res == ALLOWED) {
3532 if (reg == NULL) {
3533 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3534 o->op_class);
3535 reg = &chan->reg_class[cla];
3536 cla++;
3537 reg->reg_class = o->op_class;
3538 }
3539 reg->channel[reg->channels] = ch;
3540 reg->channels++;
3541 } else if (res == PASSIVE_ONLY &&
3542 wpa_s->conf->p2p_add_cli_chan) {
3543 if (cli_reg == NULL) {
3544 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3545 o->op_class);
3546 cli_reg = &cli_chan->reg_class[cli_cla];
3547 cli_cla++;
3548 cli_reg->reg_class = o->op_class;
3549 }
3550 cli_reg->channel[cli_reg->channels] = ch;
3551 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003553 }
3554 if (reg) {
3555 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3556 reg->channel, reg->channels);
3557 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003558 if (cli_reg) {
3559 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3560 cli_reg->channel, cli_reg->channels);
3561 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003562 }
3563
3564 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003565 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 return 0;
3568}
3569
3570
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003571int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3572 struct hostapd_hw_modes *mode, u8 channel)
3573{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003574 int op;
3575 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003576
3577 for (op = 0; op_class[op].op_class; op++) {
3578 struct p2p_oper_class_map *o = &op_class[op];
3579 u8 ch;
3580
3581 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3582 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3583 o->bw == BW20 || ch != channel)
3584 continue;
3585 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003586 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003587 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003588 }
3589 }
3590 return 0;
3591}
3592
3593
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003594int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3595 struct hostapd_hw_modes *mode, u8 channel)
3596{
3597 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3598 return 0;
3599
3600 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3601}
3602
3603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3605 size_t buf_len)
3606{
3607 struct wpa_supplicant *wpa_s = ctx;
3608
3609 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3610 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3611 break;
3612 }
3613 if (wpa_s == NULL)
3614 return -1;
3615
3616 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3617}
3618
3619
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003620static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3621{
3622 struct wpa_supplicant *wpa_s = ctx;
3623
3624 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3625 struct wpa_ssid *ssid = wpa_s->current_ssid;
3626 if (ssid == NULL)
3627 continue;
3628 if (ssid->mode != WPAS_MODE_INFRA)
3629 continue;
3630 if (wpa_s->wpa_state != WPA_COMPLETED &&
3631 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3632 continue;
3633 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3634 return 1;
3635 }
3636
3637 return 0;
3638}
3639
3640
Dmitry Shmidt18463232014-01-24 12:29:41 -08003641static int wpas_is_concurrent_session_active(void *ctx)
3642{
3643 struct wpa_supplicant *wpa_s = ctx;
3644 struct wpa_supplicant *ifs;
3645
3646 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3647 if (ifs == wpa_s)
3648 continue;
3649 if (ifs->wpa_state > WPA_ASSOCIATED)
3650 return 1;
3651 }
3652 return 0;
3653}
3654
3655
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003656static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3657{
3658 struct wpa_supplicant *wpa_s = ctx;
3659 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3660}
3661
3662
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003663int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3664{
3665 struct wpa_interface iface;
3666 struct wpa_supplicant *p2pdev_wpa_s;
3667 char ifname[100];
3668 char force_name[100];
3669 int ret;
3670
3671 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3672 wpa_s->ifname);
3673 force_name[0] = '\0';
3674 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3675 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3676 force_name, wpa_s->pending_interface_addr, NULL);
3677 if (ret < 0) {
3678 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3679 return ret;
3680 }
3681 os_strlcpy(wpa_s->pending_interface_name, ifname,
3682 sizeof(wpa_s->pending_interface_name));
3683
3684 os_memset(&iface, 0, sizeof(iface));
3685 iface.p2p_mgmt = 1;
3686 iface.ifname = wpa_s->pending_interface_name;
3687 iface.driver = wpa_s->driver->name;
3688 iface.driver_param = wpa_s->conf->driver_param;
3689 iface.confname = wpa_s->confname;
3690 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3691 if (!p2pdev_wpa_s) {
3692 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3693 return -1;
3694 }
3695 p2pdev_wpa_s->parent = wpa_s;
3696
3697 wpa_s->pending_interface_name[0] = '\0';
3698 return 0;
3699}
3700
3701
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003702static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3703 const u8 *noa, size_t noa_len)
3704{
3705 struct wpa_supplicant *wpa_s, *intf = ctx;
3706 char hex[100];
3707
3708 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3709 if (wpa_s->waiting_presence_resp)
3710 break;
3711 }
3712 if (!wpa_s) {
3713 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3714 return;
3715 }
3716 wpa_s->waiting_presence_resp = 0;
3717
3718 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3719 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3720 " status=%u noa=%s", MAC2STR(src), status, hex);
3721}
3722
3723
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003724/**
3725 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3726 * @global: Pointer to global data from wpa_supplicant_init()
3727 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3728 * Returns: 0 on success, -1 on failure
3729 */
3730int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3731{
3732 struct p2p_config p2p;
3733 unsigned int r;
3734 int i;
3735
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003736 if (wpa_s->conf->p2p_disabled)
3737 return 0;
3738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3740 return 0;
3741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742 if (global->p2p)
3743 return 0;
3744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003746 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003747 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 p2p.p2p_scan = wpas_p2p_scan;
3749 p2p.send_action = wpas_send_action;
3750 p2p.send_action_done = wpas_send_action_done;
3751 p2p.go_neg_completed = wpas_go_neg_completed;
3752 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3753 p2p.dev_found = wpas_dev_found;
3754 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003755 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003756 p2p.start_listen = wpas_start_listen;
3757 p2p.stop_listen = wpas_stop_listen;
3758 p2p.send_probe_resp = wpas_send_probe_resp;
3759 p2p.sd_request = wpas_sd_request;
3760 p2p.sd_response = wpas_sd_response;
3761 p2p.prov_disc_req = wpas_prov_disc_req;
3762 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003763 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 p2p.invitation_process = wpas_invitation_process;
3765 p2p.invitation_received = wpas_invitation_received;
3766 p2p.invitation_result = wpas_invitation_result;
3767 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003768 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003769 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003770 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771
3772 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003773 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 p2p.dev_name = wpa_s->conf->device_name;
3775 p2p.manufacturer = wpa_s->conf->manufacturer;
3776 p2p.model_name = wpa_s->conf->model_name;
3777 p2p.model_number = wpa_s->conf->model_number;
3778 p2p.serial_number = wpa_s->conf->serial_number;
3779 if (wpa_s->wps) {
3780 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3781 p2p.config_methods = wpa_s->wps->config_methods;
3782 }
3783
3784 if (wpa_s->conf->p2p_listen_reg_class &&
3785 wpa_s->conf->p2p_listen_channel) {
3786 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3787 p2p.channel = wpa_s->conf->p2p_listen_channel;
3788 } else {
3789 p2p.reg_class = 81;
3790 /*
3791 * Pick one of the social channels randomly as the listen
3792 * channel.
3793 */
3794 os_get_random((u8 *) &r, sizeof(r));
3795 p2p.channel = 1 + (r % 3) * 5;
3796 }
3797 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3798
3799 if (wpa_s->conf->p2p_oper_reg_class &&
3800 wpa_s->conf->p2p_oper_channel) {
3801 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3802 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3803 p2p.cfg_op_channel = 1;
3804 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3805 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3806
3807 } else {
3808 p2p.op_reg_class = 81;
3809 /*
3810 * Use random operation channel from (1, 6, 11) if no other
3811 * preference is indicated.
3812 */
3813 os_get_random((u8 *) &r, sizeof(r));
3814 p2p.op_channel = 1 + (r % 3) * 5;
3815 p2p.cfg_op_channel = 0;
3816 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3817 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3818 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003819
3820 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3821 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3822 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3823 }
3824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003825 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3826 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3827 p2p.country[2] = 0x04;
3828 } else
3829 os_memcpy(p2p.country, "XX\x04", 3);
3830
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003831 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3833 "channel list");
3834 return -1;
3835 }
3836
3837 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3838 WPS_DEV_TYPE_LEN);
3839
3840 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3841 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3842 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3843
3844 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3845 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3846
3847 p2p.max_peers = 100;
3848
3849 if (wpa_s->conf->p2p_ssid_postfix) {
3850 p2p.ssid_postfix_len =
3851 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3852 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3853 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3854 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3855 p2p.ssid_postfix_len);
3856 }
3857
3858 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3859
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003860 p2p.max_listen = wpa_s->max_remain_on_chan;
3861
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003862 global->p2p = p2p_init(&p2p);
3863 if (global->p2p == NULL)
3864 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003865 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003866
3867 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3868 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3869 continue;
3870 p2p_add_wps_vendor_extension(
3871 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3872 }
3873
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003874 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003876 return 0;
3877}
3878
3879
3880/**
3881 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3882 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3883 *
3884 * This function deinitialize per-interface P2P data.
3885 */
3886void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3887{
3888 if (wpa_s->driver && wpa_s->drv_priv)
3889 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003890
3891 if (wpa_s->go_params) {
3892 /* Clear any stored provisioning info */
3893 p2p_clear_provisioning_info(
3894 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003895 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003896 }
3897
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003898 os_free(wpa_s->go_params);
3899 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003900 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3901 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3902 wpa_s->p2p_long_listen = 0;
3903 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3904 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3905 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003906 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003907 wpas_p2p_listen_work_done(wpa_s);
3908 if (wpa_s->p2p_send_action_work) {
3909 os_free(wpa_s->p2p_send_action_work->ctx);
3910 radio_work_done(wpa_s->p2p_send_action_work);
3911 wpa_s->p2p_send_action_work = NULL;
3912 }
3913 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003914
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003915 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3916 wpa_s->p2p_oob_dev_pw = NULL;
3917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918 /* TODO: remove group interface from the driver if this wpa_s instance
3919 * is on top of a P2P group interface */
3920}
3921
3922
3923/**
3924 * wpas_p2p_deinit_global - Deinitialize global P2P module
3925 * @global: Pointer to global data from wpa_supplicant_init()
3926 *
3927 * This function deinitializes the global (per device) P2P module.
3928 */
3929void wpas_p2p_deinit_global(struct wpa_global *global)
3930{
3931 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003932
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003933 wpa_s = global->ifaces;
3934 if (wpa_s)
3935 wpas_p2p_service_flush(wpa_s);
3936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 if (global->p2p == NULL)
3938 return;
3939
3940 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3942 wpa_s = wpa_s->next;
3943 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003944 tmp = global->ifaces;
3945 while (tmp &&
3946 (tmp == wpa_s ||
3947 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3948 tmp = tmp->next;
3949 }
3950 if (tmp == NULL)
3951 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003952 /* Disconnect from the P2P group and deinit the interface */
3953 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 }
3955
3956 /*
3957 * Deinit GO data on any possibly remaining interface (if main
3958 * interface is used as GO).
3959 */
3960 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3961 if (wpa_s->ap_iface)
3962 wpas_p2p_group_deinit(wpa_s);
3963 }
3964
3965 p2p_deinit(global->p2p);
3966 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003967 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968}
3969
3970
3971static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3972{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003973 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3974 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003975 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003976 if (wpa_s->drv_flags &
3977 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3978 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3979 return 1; /* P2P group requires a new interface in every case
3980 */
3981 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3982 return 0; /* driver does not support concurrent operations */
3983 if (wpa_s->global->ifaces->next)
3984 return 1; /* more that one interface already in use */
3985 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3986 return 1; /* this interface is already in use */
3987 return 0;
3988}
3989
3990
3991static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3992 const u8 *peer_addr,
3993 enum p2p_wps_method wps_method,
3994 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003995 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003996 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003998 if (persistent_group && wpa_s->conf->persistent_reconnect)
3999 persistent_group = 2;
4000
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004001 /*
4002 * Increase GO config timeout if HT40 is used since it takes some time
4003 * to scan channels for coex purposes before the BSS can be started.
4004 */
4005 p2p_set_config_timeout(wpa_s->global->p2p,
4006 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4007
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4009 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004010 persistent_group, ssid ? ssid->ssid : NULL,
4011 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004012 wpa_s->p2p_pd_before_go_neg, pref_freq,
4013 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4014 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015}
4016
4017
4018static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4019 const u8 *peer_addr,
4020 enum p2p_wps_method wps_method,
4021 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004022 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004023 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004025 if (persistent_group && wpa_s->conf->persistent_reconnect)
4026 persistent_group = 2;
4027
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4029 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004030 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004031 ssid ? ssid->ssid_len : 0, pref_freq,
4032 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4033 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034}
4035
4036
4037static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4038{
4039 wpa_s->p2p_join_scan_count++;
4040 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4041 wpa_s->p2p_join_scan_count);
4042 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4043 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4044 " for join operationg - stop join attempt",
4045 MAC2STR(wpa_s->pending_join_iface_addr));
4046 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004047 if (wpa_s->p2p_auto_pd) {
4048 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004049 wpa_msg_global(wpa_s, MSG_INFO,
4050 P2P_EVENT_PROV_DISC_FAILURE
4051 " p2p_dev_addr=" MACSTR " status=N/A",
4052 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004053 return;
4054 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004055 wpa_msg_global(wpa_s->parent, MSG_INFO,
4056 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004057 }
4058}
4059
4060
Dmitry Shmidt04949592012-07-19 12:16:46 -07004061static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4062{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004063 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004064
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004065 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4066 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004067 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004068 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004069
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004070 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4071 if (!freqs)
4072 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004073
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004074 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4075 wpa_s->num_multichan_concurrent);
4076 if (num < 0) {
4077 res = 1;
4078 goto exit_free;
4079 }
4080
4081 for (i = 0; i < num; i++) {
4082 if (freqs[i] == freq) {
4083 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4084 freq);
4085 res = 0;
4086 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004087 }
4088 }
4089
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004090 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004091
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004092exit_free:
4093 os_free(freqs);
4094 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004095}
4096
4097
4098static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4099 const u8 *peer_dev_addr)
4100{
4101 struct wpa_bss *bss;
4102 int updated;
4103
4104 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4105 if (bss == NULL)
4106 return -1;
4107 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4108 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4109 "last scan");
4110 return 0;
4111 }
4112
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004113 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4114 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004115 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4116 "%ld.%06ld (%supdated in last scan)",
4117 bss->last_update.sec, bss->last_update.usec,
4118 updated ? "": "not ");
4119
4120 return updated;
4121}
4122
4123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004124static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4125 struct wpa_scan_results *scan_res)
4126{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004127 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 int freq;
4129 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004130
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4132
4133 if (wpa_s->global->p2p_disabled)
4134 return;
4135
Dmitry Shmidt04949592012-07-19 12:16:46 -07004136 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4137 scan_res ? (int) scan_res->num : -1,
4138 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004139
4140 if (scan_res)
4141 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4142
Dmitry Shmidt04949592012-07-19 12:16:46 -07004143 if (wpa_s->p2p_auto_pd) {
4144 int join = wpas_p2p_peer_go(wpa_s,
4145 wpa_s->pending_join_dev_addr);
4146 if (join == 0 &&
4147 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4148 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004149 bss = wpa_bss_get_bssid_latest(
4150 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004151 if (bss) {
4152 freq = bss->freq;
4153 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4154 "the peer " MACSTR " at %d MHz",
4155 wpa_s->auto_pd_scan_retry,
4156 MAC2STR(wpa_s->
4157 pending_join_dev_addr),
4158 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004159 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004160 return;
4161 }
4162 }
4163
4164 if (join < 0)
4165 join = 0;
4166
4167 wpa_s->p2p_auto_pd = 0;
4168 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4169 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4170 MAC2STR(wpa_s->pending_join_dev_addr), join);
4171 if (p2p_prov_disc_req(wpa_s->global->p2p,
4172 wpa_s->pending_join_dev_addr,
4173 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004174 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004175 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004176 wpa_msg_global(wpa_s, MSG_INFO,
4177 P2P_EVENT_PROV_DISC_FAILURE
4178 " p2p_dev_addr=" MACSTR " status=N/A",
4179 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004180 }
4181 return;
4182 }
4183
4184 if (wpa_s->p2p_auto_join) {
4185 int join = wpas_p2p_peer_go(wpa_s,
4186 wpa_s->pending_join_dev_addr);
4187 if (join < 0) {
4188 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4189 "running a GO -> use GO Negotiation");
4190 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4191 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4192 wpa_s->p2p_persistent_group, 0, 0, 0,
4193 wpa_s->p2p_go_intent,
4194 wpa_s->p2p_connect_freq,
4195 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004196 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004197 wpa_s->p2p_go_ht40,
4198 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004199 return;
4200 }
4201
4202 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4203 "try to join the group", join ? "" :
4204 " in older scan");
4205 if (!join)
4206 wpa_s->p2p_fallback_to_go_neg = 1;
4207 }
4208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4210 wpa_s->pending_join_iface_addr);
4211 if (freq < 0 &&
4212 p2p_get_interface_addr(wpa_s->global->p2p,
4213 wpa_s->pending_join_dev_addr,
4214 iface_addr) == 0 &&
4215 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4216 {
4217 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4218 "address for join from " MACSTR " to " MACSTR
4219 " based on newly discovered P2P peer entry",
4220 MAC2STR(wpa_s->pending_join_iface_addr),
4221 MAC2STR(iface_addr));
4222 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4223 ETH_ALEN);
4224
4225 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4226 wpa_s->pending_join_iface_addr);
4227 }
4228 if (freq >= 0) {
4229 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4230 "from P2P peer table: %d MHz", freq);
4231 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004232 if (wpa_s->p2p_join_ssid_len) {
4233 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4234 MACSTR " and SSID %s",
4235 MAC2STR(wpa_s->pending_join_iface_addr),
4236 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4237 wpa_s->p2p_join_ssid_len));
4238 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4239 wpa_s->p2p_join_ssid,
4240 wpa_s->p2p_join_ssid_len);
4241 }
4242 if (!bss) {
4243 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4244 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4245 bss = wpa_bss_get_bssid_latest(wpa_s,
4246 wpa_s->pending_join_iface_addr);
4247 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004248 if (bss) {
4249 freq = bss->freq;
4250 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004251 "from BSS table: %d MHz (SSID %s)", freq,
4252 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 }
4254 if (freq > 0) {
4255 u16 method;
4256
Dmitry Shmidt04949592012-07-19 12:16:46 -07004257 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004258 wpa_msg_global(wpa_s->parent, MSG_INFO,
4259 P2P_EVENT_GROUP_FORMATION_FAILURE
4260 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004261 return;
4262 }
4263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004264 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4265 "prior to joining an existing group (GO " MACSTR
4266 " freq=%u MHz)",
4267 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4268 wpa_s->pending_pd_before_join = 1;
4269
4270 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 case WPS_PIN_DISPLAY:
4272 method = WPS_CONFIG_KEYPAD;
4273 break;
4274 case WPS_PIN_KEYPAD:
4275 method = WPS_CONFIG_DISPLAY;
4276 break;
4277 case WPS_PBC:
4278 method = WPS_CONFIG_PUSHBUTTON;
4279 break;
4280 default:
4281 method = 0;
4282 break;
4283 }
4284
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004285 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4286 wpa_s->pending_join_dev_addr) ==
4287 method)) {
4288 /*
4289 * We have already performed provision discovery for
4290 * joining the group. Proceed directly to join
4291 * operation without duplicated provision discovery. */
4292 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4293 "with " MACSTR " already done - proceed to "
4294 "join",
4295 MAC2STR(wpa_s->pending_join_dev_addr));
4296 wpa_s->pending_pd_before_join = 0;
4297 goto start;
4298 }
4299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004301 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004302 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4304 "Discovery Request before joining an "
4305 "existing group");
4306 wpa_s->pending_pd_before_join = 0;
4307 goto start;
4308 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 return;
4310 }
4311
4312 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4313 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4314 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4315 wpas_p2p_check_join_scan_limit(wpa_s);
4316 return;
4317
4318start:
4319 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004320 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004321}
4322
4323
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004324static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4325 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004326{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327 int ret;
4328 struct wpa_driver_scan_params params;
4329 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004330 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004331 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332
4333 os_memset(&params, 0, sizeof(params));
4334
4335 /* P2P Wildcard SSID */
4336 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004337 if (ssid && ssid_len) {
4338 params.ssids[0].ssid = ssid;
4339 params.ssids[0].ssid_len = ssid_len;
4340 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4341 wpa_s->p2p_join_ssid_len = ssid_len;
4342 } else {
4343 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4344 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4345 wpa_s->p2p_join_ssid_len = 0;
4346 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347
4348 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004349 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4350 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4351 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004352 if (wps_ie == NULL) {
4353 wpas_p2p_scan_res_join(wpa_s, NULL);
4354 return;
4355 }
4356
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4358 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359 if (ies == NULL) {
4360 wpabuf_free(wps_ie);
4361 wpas_p2p_scan_res_join(wpa_s, NULL);
4362 return;
4363 }
4364 wpabuf_put_buf(ies, wps_ie);
4365 wpabuf_free(wps_ie);
4366
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004367 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004369 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 params.extra_ies = wpabuf_head(ies);
4371 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004372
4373 if (!freq) {
4374 int oper_freq;
4375 /*
4376 * If freq is not provided, check the operating freq of the GO
4377 * and use a single channel scan on if possible.
4378 */
4379 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4380 wpa_s->pending_join_iface_addr);
4381 if (oper_freq > 0)
4382 freq = oper_freq;
4383 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004384 if (freq > 0) {
4385 freqs[0] = freq;
4386 params.freqs = freqs;
4387 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004388
4389 /*
4390 * Run a scan to update BSS table and start Provision Discovery once
4391 * the new scan results become available.
4392 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004393 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004394 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004395 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004396 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004397 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004398 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004399
4400 wpabuf_free(ies);
4401
4402 if (ret) {
4403 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4404 "try again later");
4405 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4406 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4407 wpas_p2p_check_join_scan_limit(wpa_s);
4408 }
4409}
4410
4411
Dmitry Shmidt04949592012-07-19 12:16:46 -07004412static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4413{
4414 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004415 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004416}
4417
4418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004419static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004420 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004421 int auto_join, int op_freq,
4422 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004423{
4424 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004425 MACSTR " dev " MACSTR " op_freq=%d)%s",
4426 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004427 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004428 if (ssid && ssid_len) {
4429 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4430 wpa_ssid_txt(ssid, ssid_len));
4431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432
Dmitry Shmidt04949592012-07-19 12:16:46 -07004433 wpa_s->p2p_auto_pd = 0;
4434 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4436 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4437 wpa_s->pending_join_wps_method = wps_method;
4438
4439 /* Make sure we are not running find during connection establishment */
4440 wpas_p2p_stop_find(wpa_s);
4441
4442 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004443 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 return 0;
4445}
4446
4447
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004448static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4449 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
4451 struct wpa_supplicant *group;
4452 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004453 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454
4455 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4456 if (group == NULL)
4457 return -1;
4458 if (group != wpa_s) {
4459 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4460 sizeof(group->p2p_pin));
4461 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004462 } else {
4463 /*
4464 * Need to mark the current interface for p2p_group_formation
4465 * when a separate group interface is not used. This is needed
4466 * to allow p2p_cancel stop a pending p2p_connect-join.
4467 * wpas_p2p_init_group_interface() addresses this for the case
4468 * where a separate group interface is used.
4469 */
4470 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 }
4472
4473 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004474 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004475
4476 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004477 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004478 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4479 ETH_ALEN);
4480 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004481 if (freq && ssid && ssid_len) {
4482 res.freq = freq;
4483 res.ssid_len = ssid_len;
4484 os_memcpy(res.ssid, ssid, ssid_len);
4485 } else {
4486 bss = wpa_bss_get_bssid_latest(wpa_s,
4487 wpa_s->pending_join_iface_addr);
4488 if (bss) {
4489 res.freq = bss->freq;
4490 res.ssid_len = bss->ssid_len;
4491 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4492 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4493 bss->freq,
4494 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4495 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004496 }
4497
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004498 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4499 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4500 "starting client");
4501 wpa_drv_cancel_remain_on_channel(wpa_s);
4502 wpa_s->off_channel_freq = 0;
4503 wpa_s->roc_waiting_drv_freq = 0;
4504 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 wpas_start_wps_enrollee(group, &res);
4506
4507 /*
4508 * Allow a longer timeout for join-a-running-group than normal 15
4509 * second group formation timeout since the GO may not have authorized
4510 * our connection yet.
4511 */
4512 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4513 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4514 wpa_s, NULL);
4515
4516 return 0;
4517}
4518
4519
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004520static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004521 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004522{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004523 int *freqs, res;
4524 unsigned int freq_in_use = 0, num, i;
4525
4526 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4527 if (!freqs)
4528 return -1;
4529
4530 num = get_shared_radio_freqs(wpa_s, freqs,
4531 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004532 wpa_printf(MSG_DEBUG,
4533 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4534 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004535
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004536 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004537 int ret;
4538 if (go)
4539 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4540 else
4541 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4542 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004543 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4544 "(%u MHz) is not supported for P2P uses",
4545 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004546 res = -3;
4547 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004548 }
4549
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004550 for (i = 0; i < num; i++) {
4551 if (freqs[i] == freq)
4552 freq_in_use = 1;
4553 }
4554
4555 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4556 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4557 freq);
4558 res = -2;
4559 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004560 }
4561 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4562 "requested channel (%u MHz)", freq);
4563 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004564 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004565 }
4566
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004567 for (i = 0; i < num; i++) {
4568 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4569 continue;
4570
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004571 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004572 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4573 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004574 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004575 } else {
4576 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 -08004577 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004578 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004579 }
4580 break;
4581 }
4582
4583 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004584 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004585 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4586 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004587 } else if (num < wpa_s->num_multichan_concurrent) {
4588 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4589 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004590 } else {
4591 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4592 res = -2;
4593 goto exit_free;
4594 }
4595 }
4596
4597exit_ok:
4598 res = 0;
4599exit_free:
4600 os_free(freqs);
4601 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004602}
4603
4604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605/**
4606 * wpas_p2p_connect - Request P2P Group Formation to be started
4607 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4608 * @peer_addr: Address of the peer P2P Device
4609 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4610 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004611 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004612 * @join: Whether to join an existing group (as a client) instead of starting
4613 * Group Owner negotiation; @peer_addr is BSSID in that case
4614 * @auth: Whether to only authorize the connection instead of doing that and
4615 * initiating Group Owner negotiation
4616 * @go_intent: GO Intent or -1 to use default
4617 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004618 * @persistent_id: Persistent group credentials to use for forcing GO
4619 * parameters or -1 to generate new values (SSID/passphrase)
4620 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4621 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004622 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004623 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004624 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4625 * failure, -2 on failure due to channel not currently available,
4626 * -3 if forced channel is not supported
4627 */
4628int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4629 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004630 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004631 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004632 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004633{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004634 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004635 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004637 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639
4640 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4641 return -1;
4642
Dmitry Shmidt04949592012-07-19 12:16:46 -07004643 if (persistent_id >= 0) {
4644 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4645 if (ssid == NULL || ssid->disabled != 2 ||
4646 ssid->mode != WPAS_MODE_P2P_GO)
4647 return -1;
4648 }
4649
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004650 os_free(wpa_s->global->add_psk);
4651 wpa_s->global->add_psk = NULL;
4652
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004653 wpa_s->global->p2p_fail_on_wps_complete = 0;
4654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004655 if (go_intent < 0)
4656 go_intent = wpa_s->conf->p2p_go_intent;
4657
4658 if (!auth)
4659 wpa_s->p2p_long_listen = 0;
4660
4661 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004662 wpa_s->p2p_persistent_group = !!persistent_group;
4663 wpa_s->p2p_persistent_id = persistent_id;
4664 wpa_s->p2p_go_intent = go_intent;
4665 wpa_s->p2p_connect_freq = freq;
4666 wpa_s->p2p_fallback_to_go_neg = 0;
4667 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004668 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004669 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004670
4671 if (pin)
4672 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4673 else if (wps_method == WPS_PIN_DISPLAY) {
4674 ret = wps_generate_pin();
4675 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4676 ret);
4677 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4678 wpa_s->p2p_pin);
4679 } else
4680 wpa_s->p2p_pin[0] = '\0';
4681
Dmitry Shmidt04949592012-07-19 12:16:46 -07004682 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004683 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4684 if (auth) {
4685 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4686 "connect a running group from " MACSTR,
4687 MAC2STR(peer_addr));
4688 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4689 return ret;
4690 }
4691 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4692 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4693 iface_addr) < 0) {
4694 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4695 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4696 dev_addr);
4697 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004698 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004699 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004700 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4701 "%ld.%06ld",
4702 wpa_s->p2p_auto_started.sec,
4703 wpa_s->p2p_auto_started.usec);
4704 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004705 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004706 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004707 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004708 return -1;
4709 return ret;
4710 }
4711
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004712 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4713 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004714 if (res)
4715 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004716 wpas_p2p_set_own_freq_preference(wpa_s,
4717 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718
4719 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4720
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004721 if (wpa_s->create_p2p_iface) {
4722 /* Prepare to add a new interface for the group */
4723 iftype = WPA_IF_P2P_GROUP;
4724 if (go_intent == 15)
4725 iftype = WPA_IF_P2P_GO;
4726 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4727 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4728 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004729 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004730 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004731
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004732 if_addr = wpa_s->pending_interface_addr;
4733 } else
4734 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735
4736 if (auth) {
4737 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004738 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004739 force_freq, persistent_group, ssid,
4740 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004741 return -1;
4742 return ret;
4743 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004744
4745 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4746 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004747 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004748 if (wpa_s->create_p2p_iface)
4749 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004750 return -1;
4751 }
4752 return ret;
4753}
4754
4755
4756/**
4757 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4758 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4759 * @freq: Frequency of the channel in MHz
4760 * @duration: Duration of the stay on the channel in milliseconds
4761 *
4762 * This callback is called when the driver indicates that it has started the
4763 * requested remain-on-channel duration.
4764 */
4765void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4766 unsigned int freq, unsigned int duration)
4767{
4768 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4769 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4771 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4772 wpa_s->pending_listen_duration);
4773 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004774 } else {
4775 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4776 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4777 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 }
4779}
4780
4781
4782static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4783 unsigned int timeout)
4784{
4785 /* Limit maximum Listen state time based on driver limitation. */
4786 if (timeout > wpa_s->max_remain_on_chan)
4787 timeout = wpa_s->max_remain_on_chan;
4788
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789 return p2p_listen(wpa_s->global->p2p, timeout);
4790}
4791
4792
4793/**
4794 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4795 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4796 * @freq: Frequency of the channel in MHz
4797 *
4798 * This callback is called when the driver indicates that a remain-on-channel
4799 * operation has been completed, i.e., the duration on the requested channel
4800 * has timed out.
4801 */
4802void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4803 unsigned int freq)
4804{
4805 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4806 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004807 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004808 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004809 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4810 return;
4811 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4812 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004813 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004814 return;
4815 if (wpa_s->p2p_long_listen > 0)
4816 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4817 if (wpa_s->p2p_long_listen > 0) {
4818 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4819 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004820 } else {
4821 /*
4822 * When listen duration is over, stop listen & update p2p_state
4823 * to IDLE.
4824 */
4825 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004826 }
4827}
4828
4829
4830/**
4831 * wpas_p2p_group_remove - Remove a P2P group
4832 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4833 * @ifname: Network interface name of the group interface or "*" to remove all
4834 * groups
4835 * Returns: 0 on success, -1 on failure
4836 *
4837 * This function is used to remove a P2P group. This can be used to disconnect
4838 * from a group in which the local end is a P2P Client or to end a P2P Group in
4839 * case the local end is the Group Owner. If a virtual network interface was
4840 * created for this group, that interface will be removed. Otherwise, only the
4841 * configured P2P group network will be removed from the interface.
4842 */
4843int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4844{
4845 struct wpa_global *global = wpa_s->global;
4846
4847 if (os_strcmp(ifname, "*") == 0) {
4848 struct wpa_supplicant *prev;
4849 wpa_s = global->ifaces;
4850 while (wpa_s) {
4851 prev = wpa_s;
4852 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004853 if (prev->p2p_group_interface !=
4854 NOT_P2P_GROUP_INTERFACE ||
4855 (prev->current_ssid &&
4856 prev->current_ssid->p2p_group))
4857 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004858 }
4859 return 0;
4860 }
4861
4862 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4863 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4864 break;
4865 }
4866
4867 return wpas_p2p_disconnect(wpa_s);
4868}
4869
4870
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004871static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4872{
4873 unsigned int r;
4874
4875 if (freq == 2) {
4876 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4877 "band");
4878 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004879 p2p_supported_freq_go(wpa_s->global->p2p,
4880 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004881 freq = wpa_s->best_24_freq;
4882 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4883 "channel: %d MHz", freq);
4884 } else {
4885 os_get_random((u8 *) &r, sizeof(r));
4886 freq = 2412 + (r % 3) * 25;
4887 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4888 "channel: %d MHz", freq);
4889 }
4890 }
4891
4892 if (freq == 5) {
4893 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4894 "band");
4895 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004896 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004897 wpa_s->best_5_freq)) {
4898 freq = wpa_s->best_5_freq;
4899 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4900 "channel: %d MHz", freq);
4901 } else {
4902 os_get_random((u8 *) &r, sizeof(r));
4903 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004904 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004905 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4906 "5 GHz channel for P2P group");
4907 return -1;
4908 }
4909 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4910 "channel: %d MHz", freq);
4911 }
4912 }
4913
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004914 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004915 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4916 "(%u MHz) is not supported for P2P uses",
4917 freq);
4918 return -1;
4919 }
4920
4921 return freq;
4922}
4923
4924
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004925static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4926 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004927 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004928 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004929{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004930 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004931 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004932 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004933
4934 os_memset(params, 0, sizeof(*params));
4935 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004936 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004937 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004938 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004939 if (!freq_included(channels, freq)) {
4940 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4941 "accepted", freq);
4942 return -1;
4943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4945 "frequency %d MHz", freq);
4946 params->freq = freq;
4947 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4948 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004949 wpa_s->conf->p2p_oper_channel <= 11 &&
4950 freq_included(channels,
4951 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004952 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4953 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4954 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004955 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4956 wpa_s->conf->p2p_oper_reg_class == 116 ||
4957 wpa_s->conf->p2p_oper_reg_class == 117 ||
4958 wpa_s->conf->p2p_oper_reg_class == 124 ||
4959 wpa_s->conf->p2p_oper_reg_class == 126 ||
4960 wpa_s->conf->p2p_oper_reg_class == 127) &&
4961 freq_included(channels,
4962 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004963 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4964 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4965 "frequency %d MHz", params->freq);
4966 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4967 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004968 p2p_supported_freq_go(wpa_s->global->p2p,
4969 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004970 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 params->freq = wpa_s->best_overall_freq;
4972 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4973 "channel %d MHz", params->freq);
4974 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4975 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004976 p2p_supported_freq_go(wpa_s->global->p2p,
4977 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004978 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 params->freq = wpa_s->best_24_freq;
4980 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4981 "channel %d MHz", params->freq);
4982 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4983 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004984 p2p_supported_freq_go(wpa_s->global->p2p,
4985 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004986 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004987 params->freq = wpa_s->best_5_freq;
4988 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4989 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004990 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4991 channels))) {
4992 params->freq = pref_freq;
4993 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4994 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004995 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004996 int chan;
4997 for (chan = 0; chan < 11; chan++) {
4998 params->freq = 2412 + chan * 5;
4999 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005000 params->freq) &&
5001 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07005002 break;
5003 }
5004 if (chan == 11) {
5005 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5006 "allowed");
5007 return -1;
5008 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005009 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5010 "known)", params->freq);
5011 }
5012
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005013 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5014 if (!freqs)
5015 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005016
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005017 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5018 wpa_s->num_multichan_concurrent);
5019 if (res < 0) {
5020 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005021 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005023 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005024
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005025 for (i = 0; i < num; i++) {
5026 if (freq && freqs[i] == freq)
5027 break;
5028 if (!freq && freq_included(channels, freqs[i])) {
5029 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5030 freqs[i]);
5031 params->freq = freqs[i];
5032 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005033 }
5034 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005035
5036 if (i == num) {
5037 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5038 if (freq)
5039 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5040 else
5041 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5042 os_free(freqs);
5043 return -1;
5044 } else if (num == 0) {
5045 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5046 } else {
5047 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5048 }
5049 }
5050
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005051 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005052 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053}
5054
5055
5056static struct wpa_supplicant *
5057wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5058 int go)
5059{
5060 struct wpa_supplicant *group_wpa_s;
5061
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005062 if (!wpas_p2p_create_iface(wpa_s)) {
5063 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5064 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005065 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005066 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005067 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005068
5069 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005070 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005071 wpa_msg_global(wpa_s, MSG_ERROR,
5072 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005073 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005075 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5076 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005077 wpa_msg_global(wpa_s, MSG_ERROR,
5078 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 wpas_p2p_remove_pending_group_interface(wpa_s);
5080 return NULL;
5081 }
5082
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005083 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5084 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005085 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 return group_wpa_s;
5087}
5088
5089
5090/**
5091 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5092 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5093 * @persistent_group: Whether to create a persistent group
5094 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005095 * @ht40: Start GO with 40 MHz channel width
5096 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005097 * Returns: 0 on success, -1 on failure
5098 *
5099 * This function creates a new P2P group with the local end as the Group Owner,
5100 * i.e., without using Group Owner Negotiation.
5101 */
5102int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005103 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104{
5105 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106
5107 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5108 return -1;
5109
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005110 os_free(wpa_s->global->add_psk);
5111 wpa_s->global->add_psk = NULL;
5112
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005113 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005114 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005115 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005116
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005117 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5118 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005119 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005121 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005122 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005123 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005124 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005125 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5126 "(%u MHz) is not supported for P2P uses",
5127 params.freq);
5128 return -1;
5129 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005130 p2p_go_params(wpa_s->global->p2p, &params);
5131 params.persistent_group = persistent_group;
5132
5133 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5134 if (wpa_s == NULL)
5135 return -1;
5136 wpas_start_wps_go(wpa_s, &params, 0);
5137
5138 return 0;
5139}
5140
5141
5142static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5143 struct wpa_ssid *params, int addr_allocated)
5144{
5145 struct wpa_ssid *ssid;
5146
5147 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5148 if (wpa_s == NULL)
5149 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005150 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151
5152 wpa_supplicant_ap_deinit(wpa_s);
5153
5154 ssid = wpa_config_add_network(wpa_s->conf);
5155 if (ssid == NULL)
5156 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157 wpa_config_set_network_defaults(ssid);
5158 ssid->temporary = 1;
5159 ssid->proto = WPA_PROTO_RSN;
5160 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5161 ssid->group_cipher = WPA_CIPHER_CCMP;
5162 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5163 ssid->ssid = os_malloc(params->ssid_len);
5164 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005165 wpa_config_remove_network(wpa_s->conf, ssid->id);
5166 return -1;
5167 }
5168 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5169 ssid->ssid_len = params->ssid_len;
5170 ssid->p2p_group = 1;
5171 ssid->export_keys = 1;
5172 if (params->psk_set) {
5173 os_memcpy(ssid->psk, params->psk, 32);
5174 ssid->psk_set = 1;
5175 }
5176 if (params->passphrase)
5177 ssid->passphrase = os_strdup(params->passphrase);
5178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005179 wpa_s->show_group_started = 1;
5180
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005181 wpa_supplicant_select_network(wpa_s, ssid);
5182
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005183 return 0;
5184}
5185
5186
5187int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5188 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005189 int force_freq, int neg_freq, int ht40,
5190 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005191 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005192{
5193 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005194 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005195
5196 if (ssid->disabled != 2 || ssid->ssid == NULL)
5197 return -1;
5198
5199 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5200 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5201 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5202 "already running");
5203 return 0;
5204 }
5205
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005206 os_free(wpa_s->global->add_psk);
5207 wpa_s->global->add_psk = NULL;
5208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005209 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005210 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005211
Dmitry Shmidt04949592012-07-19 12:16:46 -07005212 wpa_s->p2p_fallback_to_go_neg = 0;
5213
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005214 if (ssid->mode == WPAS_MODE_INFRA)
5215 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5216
5217 if (ssid->mode != WPAS_MODE_P2P_GO)
5218 return -1;
5219
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005220 if (force_freq > 0) {
5221 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5222 if (freq < 0)
5223 return -1;
5224 } else {
5225 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5226 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5227 freq = 0;
5228 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005229
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005230 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005231 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005232
5233 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005234 params.psk_set = ssid->psk_set;
5235 if (params.psk_set)
5236 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005237 if (ssid->passphrase) {
5238 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5239 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5240 "persistent group");
5241 return -1;
5242 }
5243 os_strlcpy(params.passphrase, ssid->passphrase,
5244 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005245 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5247 params.ssid_len = ssid->ssid_len;
5248 params.persistent_group = 1;
5249
5250 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5251 if (wpa_s == NULL)
5252 return -1;
5253
Dmitry Shmidt56052862013-10-04 10:23:25 -07005254 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005255 wpas_start_wps_go(wpa_s, &params, 0);
5256
5257 return 0;
5258}
5259
5260
5261static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5262 struct wpabuf *proberesp_ies)
5263{
5264 struct wpa_supplicant *wpa_s = ctx;
5265 if (wpa_s->ap_iface) {
5266 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005267 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5268 wpabuf_free(beacon_ies);
5269 wpabuf_free(proberesp_ies);
5270 return;
5271 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272 if (beacon_ies) {
5273 wpabuf_free(hapd->p2p_beacon_ie);
5274 hapd->p2p_beacon_ie = beacon_ies;
5275 }
5276 wpabuf_free(hapd->p2p_probe_resp_ie);
5277 hapd->p2p_probe_resp_ie = proberesp_ies;
5278 } else {
5279 wpabuf_free(beacon_ies);
5280 wpabuf_free(proberesp_ies);
5281 }
5282 wpa_supplicant_ap_update_beacon(wpa_s);
5283}
5284
5285
5286static void wpas_p2p_idle_update(void *ctx, int idle)
5287{
5288 struct wpa_supplicant *wpa_s = ctx;
5289 if (!wpa_s->ap_iface)
5290 return;
5291 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005292 if (idle) {
5293 if (wpa_s->global->p2p_fail_on_wps_complete &&
5294 wpa_s->p2p_in_provisioning) {
5295 wpas_p2p_grpform_fail_after_wps(wpa_s);
5296 return;
5297 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005299 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5301}
5302
5303
5304struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005305 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306{
5307 struct p2p_group *group;
5308 struct p2p_group_config *cfg;
5309
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005310 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5311 return NULL;
5312
5313 cfg = os_zalloc(sizeof(*cfg));
5314 if (cfg == NULL)
5315 return NULL;
5316
Dmitry Shmidt04949592012-07-19 12:16:46 -07005317 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005318 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005319 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005320 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005321 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5322 if (wpa_s->max_stations &&
5323 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5324 cfg->max_clients = wpa_s->max_stations;
5325 else
5326 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005327 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5328 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005329 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005330 cfg->cb_ctx = wpa_s;
5331 cfg->ie_update = wpas_p2p_ie_update;
5332 cfg->idle_update = wpas_p2p_idle_update;
5333
5334 group = p2p_group_init(wpa_s->global->p2p, cfg);
5335 if (group == NULL)
5336 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005337 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 p2p_group_notif_formation_done(group);
5339 wpa_s->p2p_group = group;
5340 return group;
5341}
5342
5343
5344void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5345 int registrar)
5346{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005347 struct wpa_ssid *ssid = wpa_s->current_ssid;
5348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349 if (!wpa_s->p2p_in_provisioning) {
5350 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5351 "provisioning not in progress");
5352 return;
5353 }
5354
Dmitry Shmidt04949592012-07-19 12:16:46 -07005355 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5356 u8 go_dev_addr[ETH_ALEN];
5357 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5358 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5359 ssid->ssid_len);
5360 /* Clear any stored provisioning info */
5361 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5362 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5365 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005366 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005367 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5368 /*
5369 * Use a separate timeout for initial data connection to
5370 * complete to allow the group to be removed automatically if
5371 * something goes wrong in this step before the P2P group idle
5372 * timeout mechanism is taken into use.
5373 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005374 wpa_dbg(wpa_s, MSG_DEBUG,
5375 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5376 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005377 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5378 wpas_p2p_group_formation_timeout,
5379 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005380 } else if (ssid) {
5381 /*
5382 * Use a separate timeout for initial data connection to
5383 * complete to allow the group to be removed automatically if
5384 * the client does not complete data connection successfully.
5385 */
5386 wpa_dbg(wpa_s, MSG_DEBUG,
5387 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5388 P2P_MAX_INITIAL_CONN_WAIT_GO);
5389 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5390 wpas_p2p_group_formation_timeout,
5391 wpa_s->parent, NULL);
5392 /*
5393 * Complete group formation on first successful data connection
5394 */
5395 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005396 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005397 if (wpa_s->global->p2p)
5398 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399 wpas_group_formation_completed(wpa_s, 1);
5400}
5401
5402
Jouni Malinen75ecf522011-06-27 15:19:46 -07005403void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5404 struct wps_event_fail *fail)
5405{
5406 if (!wpa_s->p2p_in_provisioning) {
5407 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5408 "provisioning not in progress");
5409 return;
5410 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005411
5412 if (wpa_s->go_params) {
5413 p2p_clear_provisioning_info(
5414 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005415 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005416 }
5417
Jouni Malinen75ecf522011-06-27 15:19:46 -07005418 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005419
5420 if (wpa_s == wpa_s->global->p2p_group_formation) {
5421 /*
5422 * Allow some time for the failed WPS negotiation exchange to
5423 * complete, but remove the group since group formation cannot
5424 * succeed after provisioning failure.
5425 */
5426 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5427 wpa_s->global->p2p_fail_on_wps_complete = 1;
5428 eloop_deplete_timeout(0, 50000,
5429 wpas_p2p_group_formation_timeout,
5430 wpa_s->parent, NULL);
5431 }
5432}
5433
5434
5435int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5436{
5437 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5438 !wpa_s->p2p_in_provisioning)
5439 return 0;
5440
5441 wpas_p2p_grpform_fail_after_wps(wpa_s);
5442
5443 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005444}
5445
5446
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005447int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005448 const char *config_method,
5449 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005450{
5451 u16 config_methods;
5452
Dmitry Shmidt04949592012-07-19 12:16:46 -07005453 wpa_s->p2p_fallback_to_go_neg = 0;
5454 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005455 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005456 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005457 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005459 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5460 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005461 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005462 else {
5463 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005464 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005466
Dmitry Shmidt04949592012-07-19 12:16:46 -07005467 if (use == WPAS_P2P_PD_AUTO) {
5468 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5469 wpa_s->pending_pd_config_methods = config_methods;
5470 wpa_s->p2p_auto_pd = 1;
5471 wpa_s->p2p_auto_join = 0;
5472 wpa_s->pending_pd_before_join = 0;
5473 wpa_s->auto_pd_scan_retry = 0;
5474 wpas_p2p_stop_find(wpa_s);
5475 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005476 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005477 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5478 wpa_s->p2p_auto_started.sec,
5479 wpa_s->p2p_auto_started.usec);
5480 wpas_p2p_join_scan(wpa_s, NULL);
5481 return 0;
5482 }
5483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005484 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5485 return -1;
5486
5487 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005488 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005489 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005490}
5491
5492
5493int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5494 char *end)
5495{
5496 return p2p_scan_result_text(ies, ies_len, buf, end);
5497}
5498
5499
5500static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5501{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005502 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005503 return;
5504
5505 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5506 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005507 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005508}
5509
5510
5511int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5512 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005513 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005514 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005515{
5516 wpas_p2p_clear_pending_action_tx(wpa_s);
5517 wpa_s->p2p_long_listen = 0;
5518
Dmitry Shmidt04949592012-07-19 12:16:46 -07005519 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5520 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521 return -1;
5522
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005523 wpa_supplicant_cancel_sched_scan(wpa_s);
5524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005526 num_req_dev_types, req_dev_types, dev_id,
5527 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005528}
5529
5530
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005531static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005532{
5533 wpas_p2p_clear_pending_action_tx(wpa_s);
5534 wpa_s->p2p_long_listen = 0;
5535 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5536 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537
5538 if (wpa_s->global->p2p)
5539 p2p_stop_find(wpa_s->global->p2p);
5540
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005541 return 0;
5542}
5543
5544
5545void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5546{
5547 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5548 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005549 wpas_p2p_remove_pending_group_interface(wpa_s);
5550}
5551
5552
5553static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5554{
5555 struct wpa_supplicant *wpa_s = eloop_ctx;
5556 wpa_s->p2p_long_listen = 0;
5557}
5558
5559
5560int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5561{
5562 int res;
5563
5564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5565 return -1;
5566
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005567 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005568 wpas_p2p_clear_pending_action_tx(wpa_s);
5569
5570 if (timeout == 0) {
5571 /*
5572 * This is a request for unlimited Listen state. However, at
5573 * least for now, this is mapped to a Listen state for one
5574 * hour.
5575 */
5576 timeout = 3600;
5577 }
5578 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5579 wpa_s->p2p_long_listen = 0;
5580
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005581 /*
5582 * Stop previous find/listen operation to avoid trying to request a new
5583 * remain-on-channel operation while the driver is still running the
5584 * previous one.
5585 */
5586 if (wpa_s->global->p2p)
5587 p2p_stop_find(wpa_s->global->p2p);
5588
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005589 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5590 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5591 wpa_s->p2p_long_listen = timeout * 1000;
5592 eloop_register_timeout(timeout, 0,
5593 wpas_p2p_long_listen_timeout,
5594 wpa_s, NULL);
5595 }
5596
5597 return res;
5598}
5599
5600
5601int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5602 u8 *buf, size_t len, int p2p_group)
5603{
5604 struct wpabuf *p2p_ie;
5605 int ret;
5606
5607 if (wpa_s->global->p2p_disabled)
5608 return -1;
5609 if (wpa_s->global->p2p == NULL)
5610 return -1;
5611 if (bss == NULL)
5612 return -1;
5613
5614 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5615 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5616 p2p_group, p2p_ie);
5617 wpabuf_free(p2p_ie);
5618
5619 return ret;
5620}
5621
5622
5623int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005624 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005625 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626{
5627 if (wpa_s->global->p2p_disabled)
5628 return 0;
5629 if (wpa_s->global->p2p == NULL)
5630 return 0;
5631
Dmitry Shmidt04949592012-07-19 12:16:46 -07005632 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5633 ie, ie_len)) {
5634 case P2P_PREQ_NOT_P2P:
5635 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5636 ssi_signal);
5637 /* fall through */
5638 case P2P_PREQ_MALFORMED:
5639 case P2P_PREQ_NOT_LISTEN:
5640 case P2P_PREQ_NOT_PROCESSED:
5641 default: /* make gcc happy */
5642 return 0;
5643 case P2P_PREQ_PROCESSED:
5644 return 1;
5645 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005646}
5647
5648
5649void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5650 const u8 *sa, const u8 *bssid,
5651 u8 category, const u8 *data, size_t len, int freq)
5652{
5653 if (wpa_s->global->p2p_disabled)
5654 return;
5655 if (wpa_s->global->p2p == NULL)
5656 return;
5657
5658 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5659 freq);
5660}
5661
5662
5663void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5664{
5665 if (wpa_s->global->p2p_disabled)
5666 return;
5667 if (wpa_s->global->p2p == NULL)
5668 return;
5669
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005670 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671}
5672
5673
5674void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5675{
5676 p2p_group_deinit(wpa_s->p2p_group);
5677 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005678
5679 wpa_s->ap_configured_cb = NULL;
5680 wpa_s->ap_configured_cb_ctx = NULL;
5681 wpa_s->ap_configured_cb_data = NULL;
5682 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683}
5684
5685
5686int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5687{
5688 wpa_s->p2p_long_listen = 0;
5689
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005690 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5691 return -1;
5692
5693 return p2p_reject(wpa_s->global->p2p, addr);
5694}
5695
5696
5697/* Invite to reinvoke a persistent group */
5698int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005699 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005700 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005701{
5702 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005703 u8 *bssid = NULL;
5704 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005705 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005706 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005707
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005708 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005709 if (peer_addr)
5710 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5711 else
5712 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005713
Jouni Malinen31be0a42012-08-31 21:20:51 +03005714 wpa_s->p2p_persistent_go_freq = freq;
5715 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005716 if (ssid->mode == WPAS_MODE_P2P_GO) {
5717 role = P2P_INVITE_ROLE_GO;
5718 if (peer_addr == NULL) {
5719 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5720 "address in invitation command");
5721 return -1;
5722 }
5723 if (wpas_p2p_create_iface(wpa_s)) {
5724 if (wpas_p2p_add_group_interface(wpa_s,
5725 WPA_IF_P2P_GO) < 0) {
5726 wpa_printf(MSG_ERROR, "P2P: Failed to "
5727 "allocate a new interface for the "
5728 "group");
5729 return -1;
5730 }
5731 bssid = wpa_s->pending_interface_addr;
5732 } else
5733 bssid = wpa_s->own_addr;
5734 } else {
5735 role = P2P_INVITE_ROLE_CLIENT;
5736 peer_addr = ssid->bssid;
5737 }
5738 wpa_s->pending_invite_ssid_id = ssid->id;
5739
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005740 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5741 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005742 if (res)
5743 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005744
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005745 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5746 return -1;
5747
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005748 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5749 no_pref_freq_given && pref_freq > 0 &&
5750 wpa_s->num_multichan_concurrent > 1 &&
5751 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5752 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5753 pref_freq);
5754 pref_freq = 0;
5755 }
5756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005758 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005759 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005760}
5761
5762
5763/* Invite to join an active group */
5764int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5765 const u8 *peer_addr, const u8 *go_dev_addr)
5766{
5767 struct wpa_global *global = wpa_s->global;
5768 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005769 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005770 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005771 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005772 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005773 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005774
Jouni Malinen31be0a42012-08-31 21:20:51 +03005775 wpa_s->p2p_persistent_go_freq = 0;
5776 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005777 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005779 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5780 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5781 break;
5782 }
5783 if (wpa_s == NULL) {
5784 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5785 return -1;
5786 }
5787
5788 ssid = wpa_s->current_ssid;
5789 if (ssid == NULL) {
5790 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5791 "invitation");
5792 return -1;
5793 }
5794
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005795 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005796 persistent = ssid->p2p_persistent_group &&
5797 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5798 ssid->ssid, ssid->ssid_len);
5799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005800 if (ssid->mode == WPAS_MODE_P2P_GO) {
5801 role = P2P_INVITE_ROLE_ACTIVE_GO;
5802 bssid = wpa_s->own_addr;
5803 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005804 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005805 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005806 } else {
5807 role = P2P_INVITE_ROLE_CLIENT;
5808 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5809 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5810 "invite to current group");
5811 return -1;
5812 }
5813 bssid = wpa_s->bssid;
5814 if (go_dev_addr == NULL &&
5815 !is_zero_ether_addr(wpa_s->go_dev_addr))
5816 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005817 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5818 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005819 }
5820 wpa_s->parent->pending_invite_ssid_id = -1;
5821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5823 return -1;
5824
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005825 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5826 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005827 if (res)
5828 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005829 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005831 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005832 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005833 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005834}
5835
5836
5837void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5838{
5839 struct wpa_ssid *ssid = wpa_s->current_ssid;
5840 const char *ssid_txt;
5841 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005842 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005844 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005845 u8 ip[3 * 4];
5846 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847
Dmitry Shmidt04949592012-07-19 12:16:46 -07005848 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5849 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5850 wpa_s->parent, NULL);
5851 }
5852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005853 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005854 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005855
5856 wpa_s->show_group_started = 0;
5857
5858 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5859 os_memset(go_dev_addr, 0, ETH_ALEN);
5860 if (ssid->bssid_set)
5861 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5862 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5863 ssid->ssid_len);
5864 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5865
5866 if (wpa_s->global->p2p_group_formation == wpa_s)
5867 wpa_s->global->p2p_group_formation = NULL;
5868
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005869 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5870 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005871
5872 ip_addr[0] = '\0';
5873 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5874 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5875 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5876 ip[0], ip[1], ip[2], ip[3],
5877 ip[4], ip[5], ip[6], ip[7],
5878 ip[8], ip[9], ip[10], ip[11]);
5879 }
5880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005881 if (ssid->passphrase == NULL && ssid->psk_set) {
5882 char psk[65];
5883 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005884 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5885 "%s client ssid=\"%s\" freq=%d psk=%s "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005886 "go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005887 wpa_s->ifname, ssid_txt, freq, psk,
5888 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005889 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005891 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5892 "%s client ssid=\"%s\" freq=%d "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005893 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005894 wpa_s->ifname, ssid_txt, freq,
5895 ssid->passphrase ? ssid->passphrase : "",
5896 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005897 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005898 }
5899
5900 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005901 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5902 ssid, go_dev_addr);
5903 if (network_id < 0)
5904 network_id = ssid->id;
5905 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906}
5907
5908
5909int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5910 u32 interval1, u32 duration2, u32 interval2)
5911{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005912 int ret;
5913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5915 return -1;
5916
5917 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5918 wpa_s->current_ssid == NULL ||
5919 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5920 return -1;
5921
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005922 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5923 wpa_s->own_addr, wpa_s->assoc_freq,
5924 duration1, interval1, duration2, interval2);
5925 if (ret == 0)
5926 wpa_s->waiting_presence_resp = 1;
5927
5928 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005929}
5930
5931
5932int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5933 unsigned int interval)
5934{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5936 return -1;
5937
5938 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5939}
5940
5941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005942static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5943{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005944 if (wpa_s->current_ssid == NULL) {
5945 /*
5946 * current_ssid can be cleared when P2P client interface gets
5947 * disconnected, so assume this interface was used as P2P
5948 * client.
5949 */
5950 return 1;
5951 }
5952 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005953 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5954}
5955
5956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005957static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5958{
5959 struct wpa_supplicant *wpa_s = eloop_ctx;
5960
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005961 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005962 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5963 "disabled");
5964 return;
5965 }
5966
Dmitry Shmidt04949592012-07-19 12:16:46 -07005967 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5968 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005969 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970}
5971
5972
5973static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5974{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005975 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976
Dmitry Shmidt04949592012-07-19 12:16:46 -07005977 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5978 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5979
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005980 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5981 return;
5982
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005983 timeout = wpa_s->conf->p2p_group_idle;
5984 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5985 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5986 timeout = P2P_MAX_CLIENT_IDLE;
5987
5988 if (timeout == 0)
5989 return;
5990
Dmitry Shmidt04949592012-07-19 12:16:46 -07005991 if (timeout < 0) {
5992 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5993 timeout = 0; /* special client mode no-timeout */
5994 else
5995 return;
5996 }
5997
5998 if (wpa_s->p2p_in_provisioning) {
5999 /*
6000 * Use the normal group formation timeout during the
6001 * provisioning phase to avoid terminating this process too
6002 * early due to group idle timeout.
6003 */
6004 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6005 "during provisioning");
6006 return;
6007 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306008
Dmitry Shmidt04949592012-07-19 12:16:46 -07006009 if (wpa_s->show_group_started) {
6010 /*
6011 * Use the normal group formation timeout between the end of
6012 * the provisioning phase and completion of 4-way handshake to
6013 * avoid terminating this process too early due to group idle
6014 * timeout.
6015 */
6016 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6017 "while waiting for initial 4-way handshake to "
6018 "complete");
6019 return;
6020 }
6021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006022 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006023 timeout);
6024 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6025 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006026}
6027
6028
Jouni Malinen2b89da82012-08-31 22:04:41 +03006029/* Returns 1 if the interface was removed */
6030int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6031 u16 reason_code, const u8 *ie, size_t ie_len,
6032 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006033{
6034 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006035 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006036
Dmitry Shmidt04949592012-07-19 12:16:46 -07006037 if (!locally_generated)
6038 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6039 ie_len);
6040
6041 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6042 wpa_s->current_ssid &&
6043 wpa_s->current_ssid->p2p_group &&
6044 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6045 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6046 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006047 if (wpas_p2p_group_delete(wpa_s,
6048 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6049 > 0)
6050 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006051 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006052
6053 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006054}
6055
6056
6057void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006058 u16 reason_code, const u8 *ie, size_t ie_len,
6059 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006060{
6061 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6062 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006063
Dmitry Shmidt04949592012-07-19 12:16:46 -07006064 if (!locally_generated)
6065 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6066 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006067}
6068
6069
6070void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6071{
6072 struct p2p_data *p2p = wpa_s->global->p2p;
6073
6074 if (p2p == NULL)
6075 return;
6076
6077 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6078 return;
6079
6080 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6081 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6082
6083 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6084 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6085
6086 if (wpa_s->wps &&
6087 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6088 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6089
6090 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6091 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6092
6093 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6094 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6095 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6096 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6097 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6098 }
6099
6100 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6101 p2p_set_sec_dev_types(p2p,
6102 (void *) wpa_s->conf->sec_device_type,
6103 wpa_s->conf->num_sec_device_types);
6104
6105 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6106 int i;
6107 p2p_remove_wps_vendor_extensions(p2p);
6108 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6109 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6110 continue;
6111 p2p_add_wps_vendor_extension(
6112 p2p, wpa_s->conf->wps_vendor_ext[i]);
6113 }
6114 }
6115
6116 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6117 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6118 char country[3];
6119 country[0] = wpa_s->conf->country[0];
6120 country[1] = wpa_s->conf->country[1];
6121 country[2] = 0x04;
6122 p2p_set_country(p2p, country);
6123 }
6124
6125 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6126 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6127 wpa_s->conf->p2p_ssid_postfix ?
6128 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6129 0);
6130 }
6131
6132 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6133 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006134
6135 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6136 u8 reg_class, channel;
6137 int ret;
6138 unsigned int r;
6139 if (wpa_s->conf->p2p_listen_reg_class &&
6140 wpa_s->conf->p2p_listen_channel) {
6141 reg_class = wpa_s->conf->p2p_listen_reg_class;
6142 channel = wpa_s->conf->p2p_listen_channel;
6143 } else {
6144 reg_class = 81;
6145 /*
6146 * Pick one of the social channels randomly as the
6147 * listen channel.
6148 */
6149 os_get_random((u8 *) &r, sizeof(r));
6150 channel = 1 + (r % 3) * 5;
6151 }
6152 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6153 if (ret)
6154 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6155 "failed: %d", ret);
6156 }
6157 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6158 u8 op_reg_class, op_channel, cfg_op_channel;
6159 int ret = 0;
6160 unsigned int r;
6161 if (wpa_s->conf->p2p_oper_reg_class &&
6162 wpa_s->conf->p2p_oper_channel) {
6163 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6164 op_channel = wpa_s->conf->p2p_oper_channel;
6165 cfg_op_channel = 1;
6166 } else {
6167 op_reg_class = 81;
6168 /*
6169 * Use random operation channel from (1, 6, 11)
6170 *if no other preference is indicated.
6171 */
6172 os_get_random((u8 *) &r, sizeof(r));
6173 op_channel = 1 + (r % 3) * 5;
6174 cfg_op_channel = 0;
6175 }
6176 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6177 cfg_op_channel);
6178 if (ret)
6179 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6180 "failed: %d", ret);
6181 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006182
6183 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6184 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6185 wpa_s->conf->p2p_pref_chan) < 0) {
6186 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6187 "update failed");
6188 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006189
6190 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6191 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6192 "update failed");
6193 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006195}
6196
6197
6198int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6199 int duration)
6200{
6201 if (!wpa_s->ap_iface)
6202 return -1;
6203 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6204 duration);
6205}
6206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006207
6208int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6209{
6210 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6211 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006212
6213 wpa_s->global->cross_connection = enabled;
6214 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6215
6216 if (!enabled) {
6217 struct wpa_supplicant *iface;
6218
6219 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6220 {
6221 if (iface->cross_connect_enabled == 0)
6222 continue;
6223
6224 iface->cross_connect_enabled = 0;
6225 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006226 wpa_msg_global(iface->parent, MSG_INFO,
6227 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6228 iface->ifname,
6229 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230 }
6231 }
6232
6233 return 0;
6234}
6235
6236
6237static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6238{
6239 struct wpa_supplicant *iface;
6240
6241 if (!uplink->global->cross_connection)
6242 return;
6243
6244 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6245 if (!iface->cross_connect_enabled)
6246 continue;
6247 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6248 0)
6249 continue;
6250 if (iface->ap_iface == NULL)
6251 continue;
6252 if (iface->cross_connect_in_use)
6253 continue;
6254
6255 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006256 wpa_msg_global(iface->parent, MSG_INFO,
6257 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6258 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006259 }
6260}
6261
6262
6263static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6264{
6265 struct wpa_supplicant *iface;
6266
6267 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6268 if (!iface->cross_connect_enabled)
6269 continue;
6270 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6271 0)
6272 continue;
6273 if (!iface->cross_connect_in_use)
6274 continue;
6275
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006276 wpa_msg_global(iface->parent, MSG_INFO,
6277 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6278 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279 iface->cross_connect_in_use = 0;
6280 }
6281}
6282
6283
6284void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6285{
6286 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6287 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6288 wpa_s->cross_connect_disallowed)
6289 wpas_p2p_disable_cross_connect(wpa_s);
6290 else
6291 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006292 if (!wpa_s->ap_iface &&
6293 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6294 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006295}
6296
6297
6298void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6299{
6300 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006301 if (!wpa_s->ap_iface &&
6302 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6303 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006304 wpas_p2p_set_group_idle_timeout(wpa_s);
6305}
6306
6307
6308static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6309{
6310 struct wpa_supplicant *iface;
6311
6312 if (!wpa_s->global->cross_connection)
6313 return;
6314
6315 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6316 if (iface == wpa_s)
6317 continue;
6318 if (iface->drv_flags &
6319 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6320 continue;
6321 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6322 continue;
6323
6324 wpa_s->cross_connect_enabled = 1;
6325 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6326 sizeof(wpa_s->cross_connect_uplink));
6327 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6328 "%s to %s whenever uplink is available",
6329 wpa_s->ifname, wpa_s->cross_connect_uplink);
6330
6331 if (iface->ap_iface || iface->current_ssid == NULL ||
6332 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6333 iface->cross_connect_disallowed ||
6334 iface->wpa_state != WPA_COMPLETED)
6335 break;
6336
6337 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006338 wpa_msg_global(wpa_s->parent, MSG_INFO,
6339 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6340 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006341 break;
6342 }
6343}
6344
6345
6346int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6347{
6348 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6349 !wpa_s->p2p_in_provisioning)
6350 return 0; /* not P2P client operation */
6351
6352 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6353 "session overlap");
6354 if (wpa_s != wpa_s->parent)
6355 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006356 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006357 return 1;
6358}
6359
6360
6361void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6362{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006363 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006364
6365 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6366 return;
6367
6368 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006369 os_memset(&cli_chan, 0, sizeof(cli_chan));
6370 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006371 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6372 "channel list");
6373 return;
6374 }
6375
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006376 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006377}
6378
6379
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006380static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6381 struct wpa_scan_results *scan_res)
6382{
6383 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6384}
6385
6386
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006387int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6388{
6389 struct wpa_global *global = wpa_s->global;
6390 int found = 0;
6391 const u8 *peer;
6392
6393 if (global->p2p == NULL)
6394 return -1;
6395
6396 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6397
6398 if (wpa_s->pending_interface_name[0] &&
6399 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6400 found = 1;
6401
6402 peer = p2p_get_go_neg_peer(global->p2p);
6403 if (peer) {
6404 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6405 MACSTR, MAC2STR(peer));
6406 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006407 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408 }
6409
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006410 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6411 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6412 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6413 found = 1;
6414 }
6415
6416 if (wpa_s->pending_pd_before_join) {
6417 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6418 wpa_s->pending_pd_before_join = 0;
6419 found = 1;
6420 }
6421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006422 wpas_p2p_stop_find(wpa_s);
6423
6424 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6425 if (wpa_s == global->p2p_group_formation &&
6426 (wpa_s->p2p_in_provisioning ||
6427 wpa_s->parent->pending_interface_type ==
6428 WPA_IF_P2P_CLIENT)) {
6429 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6430 "formation found - cancelling",
6431 wpa_s->ifname);
6432 found = 1;
6433 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6434 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006435 if (wpa_s->p2p_in_provisioning) {
6436 wpas_group_formation_completed(wpa_s, 0);
6437 break;
6438 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006439 wpas_p2p_group_delete(wpa_s,
6440 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006441 break;
6442 }
6443 }
6444
6445 if (!found) {
6446 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6447 return -1;
6448 }
6449
6450 return 0;
6451}
6452
6453
6454void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6455{
6456 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6457 return;
6458
6459 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6460 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006461 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006462}
6463
6464
6465void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6466 int freq_24, int freq_5, int freq_overall)
6467{
6468 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006469 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006470 return;
6471 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6472}
6473
6474
6475int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6476{
6477 u8 peer[ETH_ALEN];
6478 struct p2p_data *p2p = wpa_s->global->p2p;
6479
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006480 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006481 return -1;
6482
6483 if (hwaddr_aton(addr, peer))
6484 return -1;
6485
6486 return p2p_unauthorize(p2p, peer);
6487}
6488
6489
6490/**
6491 * wpas_p2p_disconnect - Disconnect from a P2P Group
6492 * @wpa_s: Pointer to wpa_supplicant data
6493 * Returns: 0 on success, -1 on failure
6494 *
6495 * This can be used to disconnect from a group in which the local end is a P2P
6496 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6497 * virtual network interface was created for this group, that interface will be
6498 * removed. Otherwise, only the configured P2P group network will be removed
6499 * from the interface.
6500 */
6501int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6502{
6503
6504 if (wpa_s == NULL)
6505 return -1;
6506
Jouni Malinen2b89da82012-08-31 22:04:41 +03006507 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6508 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006510
6511
6512int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6513{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006514 int ret;
6515
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006516 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6517 return 0;
6518
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006519 ret = p2p_in_progress(wpa_s->global->p2p);
6520 if (ret == 0) {
6521 /*
6522 * Check whether there is an ongoing WPS provisioning step (or
6523 * other parts of group formation) on another interface since
6524 * p2p_in_progress() does not report this to avoid issues for
6525 * scans during such provisioning step.
6526 */
6527 if (wpa_s->global->p2p_group_formation &&
6528 wpa_s->global->p2p_group_formation != wpa_s) {
6529 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6530 "in group formation",
6531 wpa_s->global->p2p_group_formation->ifname);
6532 ret = 1;
6533 }
6534 }
6535
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006536 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006537 struct os_reltime now;
6538 os_get_reltime(&now);
6539 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6540 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006541 /* Wait for the first client has expired */
6542 wpa_s->global->p2p_go_wait_client.sec = 0;
6543 } else {
6544 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6545 ret = 1;
6546 }
6547 }
6548
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006549 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006550}
6551
6552
6553void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6554 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006555{
6556 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6557 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6558 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006559 /**
6560 * Remove the network by scheduling the group formation
6561 * timeout to happen immediately. The teardown code
6562 * needs to be scheduled to run asynch later so that we
6563 * don't delete data from under ourselves unexpectedly.
6564 * Calling wpas_p2p_group_formation_timeout directly
6565 * causes a series of crashes in WPS failure scenarios.
6566 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006567 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6568 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006569 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6570 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006571 }
6572}
6573
6574
6575struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006576 const u8 *addr, const u8 *ssid,
6577 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006578{
6579 struct wpa_ssid *s;
6580 size_t i;
6581
6582 for (s = wpa_s->conf->ssid; s; s = s->next) {
6583 if (s->disabled != 2)
6584 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006585 if (ssid &&
6586 (ssid_len != s->ssid_len ||
6587 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6588 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006589 if (addr == NULL) {
6590 if (s->mode == WPAS_MODE_P2P_GO)
6591 return s;
6592 continue;
6593 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006594 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6595 return s; /* peer is GO in the persistent group */
6596 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6597 continue;
6598 for (i = 0; i < s->num_p2p_clients; i++) {
6599 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6600 addr, ETH_ALEN) == 0)
6601 return s; /* peer is P2P client in persistent
6602 * group */
6603 }
6604 }
6605
6606 return NULL;
6607}
6608
6609
6610void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6611 const u8 *addr)
6612{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006613 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6614 wpa_s->parent, NULL) > 0) {
6615 /*
6616 * This can happen if WPS provisioning step is not terminated
6617 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6618 * peer was able to connect, there is no need to time out group
6619 * formation after this, though. In addition, this is used with
6620 * the initial connection wait on the GO as a separate formation
6621 * timeout and as such, expected to be hit after the initial WPS
6622 * provisioning step.
6623 */
6624 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6625 }
6626 if (!wpa_s->p2p_go_group_formation_completed) {
6627 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6628 wpa_s->p2p_go_group_formation_completed = 1;
6629 wpa_s->global->p2p_group_formation = NULL;
6630 wpa_s->p2p_in_provisioning = 0;
6631 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006632 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006633 if (addr == NULL)
6634 return;
6635 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6636}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006637
Dmitry Shmidt04949592012-07-19 12:16:46 -07006638
6639static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6640 int group_added)
6641{
6642 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006643 if (wpa_s->global->p2p_group_formation)
6644 group = wpa_s->global->p2p_group_formation;
6645 wpa_s = wpa_s->parent;
6646 offchannel_send_action_done(wpa_s);
6647 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006648 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006649 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6650 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6651 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6652 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6653 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006654 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006655 wpa_s->p2p_go_ht40,
6656 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006657}
6658
6659
6660int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6661{
6662 if (!wpa_s->p2p_fallback_to_go_neg ||
6663 wpa_s->p2p_in_provisioning <= 5)
6664 return 0;
6665
6666 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6667 return 0; /* peer operating as a GO */
6668
6669 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6670 "fallback to GO Negotiation");
6671 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6672
6673 return 1;
6674}
6675
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006676
6677unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6678{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006679 struct wpa_supplicant *ifs;
6680
6681 if (wpa_s->wpa_state > WPA_SCANNING) {
6682 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6683 "concurrent operation",
6684 P2P_CONCURRENT_SEARCH_DELAY);
6685 return P2P_CONCURRENT_SEARCH_DELAY;
6686 }
6687
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006688 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6689 radio_list) {
6690 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006691 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6692 "delay due to concurrent operation on "
6693 "interface %s",
6694 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6695 return P2P_CONCURRENT_SEARCH_DELAY;
6696 }
6697 }
6698
6699 return 0;
6700}
6701
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006702
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006703static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6704 struct wpa_ssid *s, const u8 *addr,
6705 int iface_addr)
6706{
6707 struct psk_list_entry *psk, *tmp;
6708 int changed = 0;
6709
6710 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6711 list) {
6712 if ((iface_addr && !psk->p2p &&
6713 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6714 (!iface_addr && psk->p2p &&
6715 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6716 wpa_dbg(wpa_s, MSG_DEBUG,
6717 "P2P: Remove persistent group PSK list entry for "
6718 MACSTR " p2p=%u",
6719 MAC2STR(psk->addr), psk->p2p);
6720 dl_list_del(&psk->list);
6721 os_free(psk);
6722 changed++;
6723 }
6724 }
6725
6726 return changed;
6727}
6728
6729
6730void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6731 const u8 *p2p_dev_addr,
6732 const u8 *psk, size_t psk_len)
6733{
6734 struct wpa_ssid *ssid = wpa_s->current_ssid;
6735 struct wpa_ssid *persistent;
6736 struct psk_list_entry *p;
6737
6738 if (psk_len != sizeof(p->psk))
6739 return;
6740
6741 if (p2p_dev_addr) {
6742 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6743 " p2p_dev_addr=" MACSTR,
6744 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6745 if (is_zero_ether_addr(p2p_dev_addr))
6746 p2p_dev_addr = NULL;
6747 } else {
6748 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6749 MAC2STR(mac_addr));
6750 }
6751
6752 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6753 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6754 /* To be added to persistent group once created */
6755 if (wpa_s->global->add_psk == NULL) {
6756 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6757 if (wpa_s->global->add_psk == NULL)
6758 return;
6759 }
6760 p = wpa_s->global->add_psk;
6761 if (p2p_dev_addr) {
6762 p->p2p = 1;
6763 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6764 } else {
6765 p->p2p = 0;
6766 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6767 }
6768 os_memcpy(p->psk, psk, psk_len);
6769 return;
6770 }
6771
6772 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6773 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6774 return;
6775 }
6776
6777 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6778 ssid->ssid_len);
6779 if (!persistent) {
6780 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6781 return;
6782 }
6783
6784 p = os_zalloc(sizeof(*p));
6785 if (p == NULL)
6786 return;
6787 if (p2p_dev_addr) {
6788 p->p2p = 1;
6789 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6790 } else {
6791 p->p2p = 0;
6792 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6793 }
6794 os_memcpy(p->psk, psk, psk_len);
6795
6796 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6797 struct psk_list_entry *last;
6798 last = dl_list_last(&persistent->psk_list,
6799 struct psk_list_entry, list);
6800 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6801 MACSTR " (p2p=%u) to make room for a new one",
6802 MAC2STR(last->addr), last->p2p);
6803 dl_list_del(&last->list);
6804 os_free(last);
6805 }
6806
6807 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6808 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6809 p2p_dev_addr == NULL);
6810 if (p2p_dev_addr) {
6811 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6812 MACSTR, MAC2STR(p2p_dev_addr));
6813 } else {
6814 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6815 MAC2STR(mac_addr));
6816 }
6817 dl_list_add(&persistent->psk_list, &p->list);
6818
6819#ifndef CONFIG_NO_CONFIG_WRITE
6820 if (wpa_s->parent->conf->update_config &&
6821 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6822 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6823#endif /* CONFIG_NO_CONFIG_WRITE */
6824}
6825
6826
6827static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6828 struct wpa_ssid *s, const u8 *addr,
6829 int iface_addr)
6830{
6831 int res;
6832
6833 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6834 if (res > 0) {
6835#ifndef CONFIG_NO_CONFIG_WRITE
6836 if (wpa_s->conf->update_config &&
6837 wpa_config_write(wpa_s->confname, wpa_s->conf))
6838 wpa_dbg(wpa_s, MSG_DEBUG,
6839 "P2P: Failed to update configuration");
6840#endif /* CONFIG_NO_CONFIG_WRITE */
6841 }
6842}
6843
6844
6845static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6846 const u8 *peer, int iface_addr)
6847{
6848 struct hostapd_data *hapd;
6849 struct hostapd_wpa_psk *psk, *prev, *rem;
6850 struct sta_info *sta;
6851
6852 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6853 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6854 return;
6855
6856 /* Remove per-station PSK entry */
6857 hapd = wpa_s->ap_iface->bss[0];
6858 prev = NULL;
6859 psk = hapd->conf->ssid.wpa_psk;
6860 while (psk) {
6861 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6862 (!iface_addr &&
6863 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6864 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6865 MACSTR " iface_addr=%d",
6866 MAC2STR(peer), iface_addr);
6867 if (prev)
6868 prev->next = psk->next;
6869 else
6870 hapd->conf->ssid.wpa_psk = psk->next;
6871 rem = psk;
6872 psk = psk->next;
6873 os_free(rem);
6874 } else {
6875 prev = psk;
6876 psk = psk->next;
6877 }
6878 }
6879
6880 /* Disconnect from group */
6881 if (iface_addr)
6882 sta = ap_get_sta(hapd, peer);
6883 else
6884 sta = ap_get_sta_p2p(hapd, peer);
6885 if (sta) {
6886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6887 " (iface_addr=%d) from group",
6888 MAC2STR(peer), iface_addr);
6889 hostapd_drv_sta_deauth(hapd, sta->addr,
6890 WLAN_REASON_DEAUTH_LEAVING);
6891 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6892 }
6893}
6894
6895
6896void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6897 int iface_addr)
6898{
6899 struct wpa_ssid *s;
6900 struct wpa_supplicant *w;
6901
6902 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6903
6904 /* Remove from any persistent group */
6905 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6906 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6907 continue;
6908 if (!iface_addr)
6909 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6910 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6911 }
6912
6913 /* Remove from any operating group */
6914 for (w = wpa_s->global->ifaces; w; w = w->next)
6915 wpas_p2p_remove_client_go(w, peer, iface_addr);
6916}
6917
6918
6919static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6920{
6921 struct wpa_supplicant *wpa_s = eloop_ctx;
6922 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6923}
6924
6925
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006926static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6927{
6928 struct wpa_supplicant *wpa_s = eloop_ctx;
6929
6930 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6931 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6932}
6933
6934
6935int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6936 struct wpa_ssid *ssid)
6937{
6938 struct wpa_supplicant *iface;
6939
6940 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6941 if (!iface->current_ssid ||
6942 iface->current_ssid->frequency == freq ||
6943 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6944 !iface->current_ssid->p2p_group))
6945 continue;
6946
6947 /* Remove the connection with least priority */
6948 if (!wpas_is_p2p_prioritized(iface)) {
6949 /* STA connection has priority over existing
6950 * P2P connection, so remove the interface. */
6951 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6952 eloop_register_timeout(0, 0,
6953 wpas_p2p_group_freq_conflict,
6954 iface, NULL);
6955 /* If connection in progress is P2P connection, do not
6956 * proceed for the connection. */
6957 if (wpa_s == iface)
6958 return -1;
6959 else
6960 return 0;
6961 } else {
6962 /* P2P connection has priority, disable the STA network
6963 */
6964 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6965 ssid);
6966 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6967 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6968 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6969 ETH_ALEN);
6970 /* If P2P connection is in progress, continue
6971 * connecting...*/
6972 if (wpa_s == iface)
6973 return 0;
6974 else
6975 return -1;
6976 }
6977 }
6978
6979 return 0;
6980}
6981
6982
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006983int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6984{
6985 struct wpa_ssid *ssid = wpa_s->current_ssid;
6986
6987 if (ssid == NULL || !ssid->p2p_group)
6988 return 0;
6989
6990 if (wpa_s->p2p_last_4way_hs_fail &&
6991 wpa_s->p2p_last_4way_hs_fail == ssid) {
6992 u8 go_dev_addr[ETH_ALEN];
6993 struct wpa_ssid *persistent;
6994
6995 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6996 ssid->ssid,
6997 ssid->ssid_len) <= 0) {
6998 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6999 goto disconnect;
7000 }
7001
7002 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7003 MACSTR, MAC2STR(go_dev_addr));
7004 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7005 ssid->ssid,
7006 ssid->ssid_len);
7007 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7008 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7009 goto disconnect;
7010 }
7011 wpa_msg_global(wpa_s->parent, MSG_INFO,
7012 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7013 persistent->id);
7014 disconnect:
7015 wpa_s->p2p_last_4way_hs_fail = NULL;
7016 /*
7017 * Remove the group from a timeout to avoid issues with caller
7018 * continuing to use the interface if this is on a P2P group
7019 * interface.
7020 */
7021 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7022 wpa_s, NULL);
7023 return 1;
7024 }
7025
7026 wpa_s->p2p_last_4way_hs_fail = ssid;
7027 return 0;
7028}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007029
7030
7031#ifdef CONFIG_WPS_NFC
7032
7033static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7034 struct wpabuf *p2p)
7035{
7036 struct wpabuf *ret;
7037 size_t wsc_len;
7038
7039 if (p2p == NULL) {
7040 wpabuf_free(wsc);
7041 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7042 return NULL;
7043 }
7044
7045 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7046 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7047 if (ret == NULL) {
7048 wpabuf_free(wsc);
7049 wpabuf_free(p2p);
7050 return NULL;
7051 }
7052
7053 wpabuf_put_be16(ret, wsc_len);
7054 if (wsc)
7055 wpabuf_put_buf(ret, wsc);
7056 wpabuf_put_be16(ret, wpabuf_len(p2p));
7057 wpabuf_put_buf(ret, p2p);
7058
7059 wpabuf_free(wsc);
7060 wpabuf_free(p2p);
7061 wpa_hexdump_buf(MSG_DEBUG,
7062 "P2P: Generated NFC connection handover message", ret);
7063
7064 if (ndef && ret) {
7065 struct wpabuf *tmp;
7066 tmp = ndef_build_p2p(ret);
7067 wpabuf_free(ret);
7068 if (tmp == NULL) {
7069 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7070 return NULL;
7071 }
7072 ret = tmp;
7073 }
7074
7075 return ret;
7076}
7077
7078
7079static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7080 struct wpa_ssid **ssid, u8 *go_dev_addr)
7081{
7082 struct wpa_supplicant *iface;
7083
7084 if (go_dev_addr)
7085 os_memset(go_dev_addr, 0, ETH_ALEN);
7086 if (ssid)
7087 *ssid = NULL;
7088 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7089 if (iface->wpa_state < WPA_ASSOCIATING ||
7090 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7091 !iface->current_ssid->p2p_group ||
7092 iface->current_ssid->mode != WPAS_MODE_INFRA)
7093 continue;
7094 if (ssid)
7095 *ssid = iface->current_ssid;
7096 if (go_dev_addr)
7097 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7098 return iface->assoc_freq;
7099 }
7100 return 0;
7101}
7102
7103
7104struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7105 int ndef)
7106{
7107 struct wpabuf *wsc, *p2p;
7108 struct wpa_ssid *ssid;
7109 u8 go_dev_addr[ETH_ALEN];
7110 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7111
7112 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7113 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7114 return NULL;
7115 }
7116
7117 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7118 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7119 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7120 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7121 return NULL;
7122 }
7123
7124 if (cli_freq == 0) {
7125 wsc = wps_build_nfc_handover_req_p2p(
7126 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7127 } else
7128 wsc = NULL;
7129 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7130 go_dev_addr, ssid ? ssid->ssid : NULL,
7131 ssid ? ssid->ssid_len : 0);
7132
7133 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7134}
7135
7136
7137struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7138 int ndef, int tag)
7139{
7140 struct wpabuf *wsc, *p2p;
7141 struct wpa_ssid *ssid;
7142 u8 go_dev_addr[ETH_ALEN];
7143 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7144
7145 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7146 return NULL;
7147
7148 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7149 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7150 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7151 return NULL;
7152
7153 if (cli_freq == 0) {
7154 wsc = wps_build_nfc_handover_sel_p2p(
7155 wpa_s->parent->wps,
7156 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7157 DEV_PW_NFC_CONNECTION_HANDOVER,
7158 wpa_s->conf->wps_nfc_dh_pubkey,
7159 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7160 } else
7161 wsc = NULL;
7162 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7163 go_dev_addr, ssid ? ssid->ssid : NULL,
7164 ssid ? ssid->ssid_len : 0);
7165
7166 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7167}
7168
7169
7170static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7171 struct p2p_nfc_params *params)
7172{
7173 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7174 "connection handover (freq=%d)",
7175 params->go_freq);
7176
7177 if (params->go_freq && params->go_ssid_len) {
7178 wpa_s->p2p_wps_method = WPS_NFC;
7179 wpa_s->pending_join_wps_method = WPS_NFC;
7180 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7181 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7182 ETH_ALEN);
7183 return wpas_p2p_join_start(wpa_s, params->go_freq,
7184 params->go_ssid,
7185 params->go_ssid_len);
7186 }
7187
7188 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7189 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7190 params->go_freq, -1, 0, 1, 1);
7191}
7192
7193
7194static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7195 struct p2p_nfc_params *params, int tag)
7196{
7197 int res, persistent;
7198 struct wpa_ssid *ssid;
7199
7200 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7201 "connection handover");
7202 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7203 ssid = wpa_s->current_ssid;
7204 if (ssid == NULL)
7205 continue;
7206 if (ssid->mode != WPAS_MODE_P2P_GO)
7207 continue;
7208 if (wpa_s->ap_iface == NULL)
7209 continue;
7210 break;
7211 }
7212 if (wpa_s == NULL) {
7213 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7214 return -1;
7215 }
7216
7217 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7218 DEV_PW_NFC_CONNECTION_HANDOVER &&
7219 !wpa_s->parent->p2p_oob_dev_pw) {
7220 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7221 return -1;
7222 }
7223 res = wpas_ap_wps_add_nfc_pw(
7224 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7225 wpa_s->parent->p2p_oob_dev_pw,
7226 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7227 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7228 if (res)
7229 return res;
7230
7231 if (!tag) {
7232 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7233 return 0;
7234 }
7235
7236 if (!params->peer ||
7237 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7238 return 0;
7239
7240 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7241 " to join", MAC2STR(params->peer->p2p_device_addr));
7242
7243 wpa_s->global->p2p_invite_group = wpa_s;
7244 persistent = ssid->p2p_persistent_group &&
7245 wpas_p2p_get_persistent(wpa_s->parent,
7246 params->peer->p2p_device_addr,
7247 ssid->ssid, ssid->ssid_len);
7248 wpa_s->parent->pending_invite_ssid_id = -1;
7249
7250 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7251 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7252 ssid->ssid, ssid->ssid_len, ssid->frequency,
7253 wpa_s->global->p2p_dev_addr, persistent, 0,
7254 wpa_s->parent->p2p_oob_dev_pw_id);
7255}
7256
7257
7258static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7259 struct p2p_nfc_params *params,
7260 int forced_freq)
7261{
7262 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7263 "connection handover");
7264 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7265 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7266 forced_freq, -1, 0, 1, 1);
7267}
7268
7269
7270static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7271 struct p2p_nfc_params *params,
7272 int forced_freq)
7273{
7274 int res;
7275
7276 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7277 "connection handover");
7278 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7279 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7280 forced_freq, -1, 0, 1, 1);
7281 if (res)
7282 return res;
7283
7284 res = wpas_p2p_listen(wpa_s, 60);
7285 if (res) {
7286 p2p_unauthorize(wpa_s->global->p2p,
7287 params->peer->p2p_device_addr);
7288 }
7289
7290 return res;
7291}
7292
7293
7294static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7295 const struct wpabuf *data,
7296 int sel, int tag, int forced_freq)
7297{
7298 const u8 *pos, *end;
7299 u16 len, id;
7300 struct p2p_nfc_params params;
7301 int res;
7302
7303 os_memset(&params, 0, sizeof(params));
7304 params.sel = sel;
7305
7306 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7307
7308 pos = wpabuf_head(data);
7309 end = pos + wpabuf_len(data);
7310
7311 if (end - pos < 2) {
7312 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7313 "attributes");
7314 return -1;
7315 }
7316 len = WPA_GET_BE16(pos);
7317 pos += 2;
7318 if (pos + len > end) {
7319 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7320 "attributes");
7321 return -1;
7322 }
7323 params.wsc_attr = pos;
7324 params.wsc_len = len;
7325 pos += len;
7326
7327 if (end - pos < 2) {
7328 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7329 "attributes");
7330 return -1;
7331 }
7332 len = WPA_GET_BE16(pos);
7333 pos += 2;
7334 if (pos + len > end) {
7335 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7336 "attributes");
7337 return -1;
7338 }
7339 params.p2p_attr = pos;
7340 params.p2p_len = len;
7341 pos += len;
7342
7343 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7344 params.wsc_attr, params.wsc_len);
7345 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7346 params.p2p_attr, params.p2p_len);
7347 if (pos < end) {
7348 wpa_hexdump(MSG_DEBUG,
7349 "P2P: Ignored extra data after P2P attributes",
7350 pos, end - pos);
7351 }
7352
7353 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7354 if (res)
7355 return res;
7356
7357 if (params.next_step == NO_ACTION)
7358 return 0;
7359
7360 if (params.next_step == BOTH_GO) {
7361 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7362 MAC2STR(params.peer->p2p_device_addr));
7363 return 0;
7364 }
7365
7366 if (params.next_step == PEER_CLIENT) {
7367 if (!is_zero_ether_addr(params.go_dev_addr)) {
7368 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7369 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7370 " ssid=\"%s\"",
7371 MAC2STR(params.peer->p2p_device_addr),
7372 params.go_freq,
7373 MAC2STR(params.go_dev_addr),
7374 wpa_ssid_txt(params.go_ssid,
7375 params.go_ssid_len));
7376 } else {
7377 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7378 "peer=" MACSTR " freq=%d",
7379 MAC2STR(params.peer->p2p_device_addr),
7380 params.go_freq);
7381 }
7382 return 0;
7383 }
7384
7385 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7386 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7387 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7388 return 0;
7389 }
7390
7391 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7392 wpa_s->p2p_oob_dev_pw = NULL;
7393
7394 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7395 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7396 "received");
7397 return -1;
7398 }
7399
7400 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7401 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7402 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7403 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7404 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7405 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7406 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7407
7408 if (tag) {
7409 if (id < 0x10) {
7410 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7411 "peer OOB Device Password Id %u", id);
7412 return -1;
7413 }
7414 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7415 "Device Password Id %u", id);
7416 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7417 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7418 params.oob_dev_pw_len -
7419 WPS_OOB_PUBKEY_HASH_LEN - 2);
7420 wpa_s->p2p_oob_dev_pw_id = id;
7421 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7422 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7423 params.oob_dev_pw_len -
7424 WPS_OOB_PUBKEY_HASH_LEN - 2);
7425 if (wpa_s->p2p_oob_dev_pw == NULL)
7426 return -1;
7427
7428 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7429 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7430 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7431 return -1;
7432 } else {
7433 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7434 "without Device Password");
7435 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7436 }
7437
7438 switch (params.next_step) {
7439 case NO_ACTION:
7440 case BOTH_GO:
7441 case PEER_CLIENT:
7442 /* already covered above */
7443 return 0;
7444 case JOIN_GROUP:
7445 return wpas_p2p_nfc_join_group(wpa_s, &params);
7446 case AUTH_JOIN:
7447 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7448 case INIT_GO_NEG:
7449 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7450 case RESP_GO_NEG:
7451 /* TODO: use own OOB Dev Pw */
7452 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7453 }
7454
7455 return -1;
7456}
7457
7458
7459int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7460 const struct wpabuf *data, int forced_freq)
7461{
7462 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7463 return -1;
7464
7465 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7466}
7467
7468
7469int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7470 const struct wpabuf *req,
7471 const struct wpabuf *sel, int forced_freq)
7472{
7473 struct wpabuf *tmp;
7474 int ret;
7475
7476 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7477 return -1;
7478
7479 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7480
7481 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7482 wpabuf_head(req), wpabuf_len(req));
7483 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7484 wpabuf_head(sel), wpabuf_len(sel));
7485 if (forced_freq)
7486 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7487 tmp = ndef_parse_p2p(init ? sel : req);
7488 if (tmp == NULL) {
7489 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7490 return -1;
7491 }
7492
7493 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7494 forced_freq);
7495 wpabuf_free(tmp);
7496
7497 return ret;
7498}
7499
7500
7501int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7502{
7503 const u8 *if_addr;
7504 int go_intent = wpa_s->conf->p2p_go_intent;
7505 struct wpa_supplicant *iface;
7506
7507 if (wpa_s->global->p2p == NULL)
7508 return -1;
7509
7510 if (!enabled) {
7511 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7512 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7513 {
7514 if (!iface->ap_iface)
7515 continue;
7516 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7517 }
7518 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7519 0, NULL);
7520 if (wpa_s->p2p_nfc_tag_enabled)
7521 wpas_p2p_remove_pending_group_interface(wpa_s);
7522 wpa_s->p2p_nfc_tag_enabled = 0;
7523 return 0;
7524 }
7525
7526 if (wpa_s->global->p2p_disabled)
7527 return -1;
7528
7529 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7530 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7531 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7532 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7533 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7534 "to allow static handover cases");
7535 return -1;
7536 }
7537
7538 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7539
7540 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7541 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7542 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7543 if (wpa_s->p2p_oob_dev_pw == NULL)
7544 return -1;
7545 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7546
7547 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7548
7549 if (wpa_s->create_p2p_iface) {
7550 enum wpa_driver_if_type iftype;
7551 /* Prepare to add a new interface for the group */
7552 iftype = WPA_IF_P2P_GROUP;
7553 if (go_intent == 15)
7554 iftype = WPA_IF_P2P_GO;
7555 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7556 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7557 "interface for the group");
7558 return -1;
7559 }
7560
7561 if_addr = wpa_s->pending_interface_addr;
7562 } else
7563 if_addr = wpa_s->own_addr;
7564
7565 wpa_s->p2p_nfc_tag_enabled = enabled;
7566
7567 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7568 struct hostapd_data *hapd;
7569 if (iface->ap_iface == NULL)
7570 continue;
7571 hapd = iface->ap_iface->bss[0];
7572 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7573 hapd->conf->wps_nfc_dh_pubkey =
7574 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7575 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7576 hapd->conf->wps_nfc_dh_privkey =
7577 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7578 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7579 hapd->conf->wps_nfc_dev_pw =
7580 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7581 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7582
7583 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7584 wpa_dbg(iface, MSG_DEBUG,
7585 "P2P: Failed to enable NFC Tag for GO");
7586 }
7587 }
7588 p2p_set_authorized_oob_dev_pw_id(
7589 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7590 if_addr);
7591
7592 return 0;
7593}
7594
7595#endif /* CONFIG_WPS_NFC */