blob: 5e6646ed0e0890d815bde05e4df0b9176321cf1f [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "eloop.h"
14#include "common/ieee802_11_common.h"
15#include "common/ieee802_11_defs.h"
16#include "common/wpa_ctrl.h"
17#include "wps/wps_i.h"
18#include "p2p/p2p.h"
19#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080020#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070021#include "ap/sta_info.h"
22#include "ap/ap_drv_ops.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080023#include "ap/wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070025#include "eapol_supp/eapol_supp_sm.h"
26#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa_supplicant_i.h"
28#include "driver_i.h"
29#include "ap.h"
30#include "config_ssid.h"
31#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "notify.h"
33#include "scan.h"
34#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "wps_supplicant.h"
37#include "p2p_supplicant.h"
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080038#include "wifi_display.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039
40
41/*
42 * How many times to try to scan to find the GO before giving up on join
43 * request.
44 */
45#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46
Dmitry Shmidt04949592012-07-19 12:16:46 -070047#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049#ifndef P2P_MAX_CLIENT_IDLE
50/*
51 * How many seconds to try to reconnect to the GO when connection in P2P client
52 * role has been lost.
53 */
54#define P2P_MAX_CLIENT_IDLE 10
55#endif /* P2P_MAX_CLIENT_IDLE */
56
Dmitry Shmidt04949592012-07-19 12:16:46 -070057#ifndef P2P_MAX_INITIAL_CONN_WAIT
58/*
59 * How many seconds to wait for initial 4-way handshake to get completed after
Dmitry Shmidt15907092014-03-25 10:42:57 -070060 * WPS provisioning step or after the re-invocation of a persistent group on a
61 * P2P Client.
Dmitry Shmidt04949592012-07-19 12:16:46 -070062 */
63#define P2P_MAX_INITIAL_CONN_WAIT 10
64#endif /* P2P_MAX_INITIAL_CONN_WAIT */
65
Dmitry Shmidt92c368d2013-08-29 12:37:21 -070066#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67/*
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
72 */
73#define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75
Dmitry Shmidt56052862013-10-04 10:23:25 -070076#ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77/*
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
81 */
82#define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83#endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84
Dmitry Shmidt34af3062013-07-11 10:46:32 -070085#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
86
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080095 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070097};
98
Jouni Malinendc7b7132012-09-14 12:53:47 -070099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101static struct wpa_supplicant *
102wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
103 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800104static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800106static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700110 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800117static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
118 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800119static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800120static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
121 int group_added);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800122static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800123static void wpas_stop_listen(void *ctx);
Dmitry Shmidt684785c2014-05-12 13:34:29 -0700124static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700125static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800126static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
127 enum wpa_driver_if_type type);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128
129
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700130/*
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
133 */
134static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
135{
136 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800137 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145 os_free(freqs);
146
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800147 unused = wpa_s->num_multichan_concurrent - num;
148 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
149 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700150}
151
152
153/*
154 * Get the frequencies that are currently in use by one or more of the virtual
155 * interfaces, and that are also valid for P2P operation.
156 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700157static unsigned int
158wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
159 struct wpa_used_freq_data *p2p_freqs,
160 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700161{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700162 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700163 unsigned int num, i, j;
164
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700165 freqs = os_calloc(wpa_s->num_multichan_concurrent,
166 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700168 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700169
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700170 num = get_shared_radio_freqs_data(wpa_s, freqs,
171 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700172
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700173 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700174
175 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700176 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700177 p2p_freqs[j++] = freqs[i];
178 }
179
180 os_free(freqs);
181
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700182 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800183
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700184 return j;
185}
186
187
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
189 int freq)
190{
191 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
192 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800193 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
194 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
195 wpas_p2p_num_unused_channels(wpa_s) > 0) {
196 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
197 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700198 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800199 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700200 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
201}
202
203
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
205 struct wpa_scan_results *scan_res)
206{
207 size_t i;
208
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800209 if (wpa_s->p2p_scan_work) {
210 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
211 wpa_s->p2p_scan_work = NULL;
212 radio_work_done(work);
213 }
214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
216 return;
217
218 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
219 (int) scan_res->num);
220
221 for (i = 0; i < scan_res->num; i++) {
222 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800223 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700224 const u8 *ies;
225 size_t ies_len;
226
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800227 time_tmp_age.sec = bss->age / 1000;
228 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800229 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700230
231 ies = (const u8 *) (bss + 1);
232 ies_len = bss->ie_len;
233 if (bss->beacon_ie_len > 0 &&
234 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
235 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
236 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
237 MACSTR, MAC2STR(bss->bssid));
238 ies = ies + ies_len;
239 ies_len = bss->beacon_ie_len;
240 }
241
242
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800244 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700245 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246 break;
247 }
248
249 p2p_scan_res_handled(wpa_s->global->p2p);
250}
251
252
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800253static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
254{
255 struct wpa_supplicant *wpa_s = work->wpa_s;
256 struct wpa_driver_scan_params *params = work->ctx;
257 int ret;
258
259 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800260 if (!work->started) {
261 wpa_scan_free_params(params);
262 return;
263 }
264
265 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800266 return;
267 }
268
269 ret = wpa_drv_scan(wpa_s, params);
270 wpa_scan_free_params(params);
271 work->ctx = NULL;
272 if (ret) {
273 radio_work_done(work);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800274 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800275 return;
276 }
277
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800278 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800279 os_get_reltime(&wpa_s->scan_trigger_time);
280 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
281 wpa_s->own_scan_requested = 1;
282 wpa_s->p2p_scan_work = work;
283}
284
285
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800286static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
287 int freq)
288{
289 if (wpa_s->global->p2p_24ghz_social_channels &&
290 (freq == 2412 || freq == 2437 || freq == 2462)) {
291 /*
292 * Search all social channels regardless of whether these have
293 * been disabled for P2P operating channel use to avoid missing
294 * peers.
295 */
296 return 1;
297 }
298 return p2p_supported_freq(wpa_s->global->p2p, freq);
299}
300
301
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
303 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700304 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305{
306 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700309 unsigned int num_channels = 0;
310 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700312 u8 *n, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313
314 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
315 return -1;
316
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800317 if (wpa_s->p2p_scan_work) {
318 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
319 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700320 }
321
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800322 params = os_zalloc(sizeof(*params));
323 if (params == NULL)
324 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325
326 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800327 params->num_ssids = 1;
328 n = os_malloc(P2P_WILDCARD_SSID_LEN);
329 if (n == NULL)
330 goto fail;
331 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
332 params->ssids[0].ssid = n;
333 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700334
335 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700336 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
337 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 num_req_dev_types, req_dev_types);
339 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800340 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800342 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
343 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700344 if (ies == NULL) {
345 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800346 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 }
348 wpabuf_put_buf(ies, wps_ie);
349 wpabuf_free(wps_ie);
350
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800351 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800353 params->p2p_probe = 1;
354 n = os_malloc(wpabuf_len(ies));
355 if (n == NULL) {
356 wpabuf_free(ies);
357 goto fail;
358 }
359 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
360 params->extra_ies = n;
361 params->extra_ies_len = wpabuf_len(ies);
362 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700363
364 switch (type) {
365 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700366 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
367 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800368 if (params->freqs == NULL)
369 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700370 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800371 if (wpas_p2p_search_social_channel(
372 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700373 params->freqs[num_channels++] =
374 social_channels_freq[i];
375 }
376 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377 break;
378 case P2P_SCAN_FULL:
379 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700381 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
382 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800383 if (params->freqs == NULL)
384 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700385 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800386 if (wpas_p2p_search_social_channel(
387 wpa_s, social_channels_freq[i]))
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700388 params->freqs[num_channels++] =
389 social_channels_freq[i];
390 }
391 if (p2p_supported_freq(wpa_s->global->p2p, freq))
392 params->freqs[num_channels++] = freq;
393 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 break;
395 }
396
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800397 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800398 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
399 params) < 0)
400 goto fail;
401 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800403fail:
404 wpa_scan_free_params(params);
405 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406}
407
408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
410{
411 switch (p2p_group_interface) {
412 case P2P_GROUP_INTERFACE_PENDING:
413 return WPA_IF_P2P_GROUP;
414 case P2P_GROUP_INTERFACE_GO:
415 return WPA_IF_P2P_GO;
416 case P2P_GROUP_INTERFACE_CLIENT:
417 return WPA_IF_P2P_CLIENT;
418 }
419
420 return WPA_IF_P2P_GROUP;
421}
422
423
424static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
425 const u8 *ssid,
426 size_t ssid_len, int *go)
427{
428 struct wpa_ssid *s;
429
430 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
431 for (s = wpa_s->conf->ssid; s; s = s->next) {
432 if (s->disabled != 0 || !s->p2p_group ||
433 s->ssid_len != ssid_len ||
434 os_memcmp(ssid, s->ssid, ssid_len) != 0)
435 continue;
436 if (s->mode == WPAS_MODE_P2P_GO &&
437 s != wpa_s->current_ssid)
438 continue;
439 if (go)
440 *go = s->mode == WPAS_MODE_P2P_GO;
441 return wpa_s;
442 }
443 }
444
445 return NULL;
446}
447
448
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800449static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
450{
451 struct wpa_supplicant *wpa_s = eloop_ctx;
452 wpa_printf(MSG_DEBUG,
453 "P2P: Complete previously requested removal of %s",
454 wpa_s->ifname);
455 wpas_p2p_disconnect(wpa_s);
456}
457
458
459static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
460 struct wpa_supplicant *calling_wpa_s)
461{
462 if (calling_wpa_s == wpa_s && wpa_s &&
463 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
464 /*
465 * The calling wpa_s instance is going to be removed. Do that
466 * from an eloop callback to keep the instance available until
467 * the caller has returned. This my be needed, e.g., to provide
468 * control interface responses on the per-interface socket.
469 */
470 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
471 wpa_s, NULL) < 0)
472 return -1;
473 return 0;
474 }
475
476 return wpas_p2p_disconnect(wpa_s);
477}
478
479
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800480/* Determine total number of clients in active groups where we are the GO */
481static unsigned int p2p_group_go_member_count(struct wpa_supplicant *wpa_s)
482{
483 unsigned int count = 0;
484 struct wpa_ssid *s;
485
486 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
487 for (s = wpa_s->conf->ssid; s; s = s->next) {
488 wpa_printf(MSG_DEBUG,
489 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
490 wpa_s, s, s->disabled, s->p2p_group,
491 s->mode);
492 if (!s->disabled && s->p2p_group &&
493 s->mode == WPAS_MODE_P2P_GO) {
494 count += p2p_get_group_num_members(
495 wpa_s->p2p_group);
496 }
497 }
498 }
499
500 return count;
501}
502
503
504/* Find an interface for a P2P group where we are the GO */
505static struct wpa_supplicant *
506wpas_p2p_get_go_group(struct wpa_supplicant *wpa_s)
507{
508 struct wpa_supplicant *save = NULL;
509 struct wpa_ssid *s;
510
511 if (!wpa_s)
512 return NULL;
513
514 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
515 for (s = wpa_s->conf->ssid; s; s = s->next) {
516 if (s->disabled || !s->p2p_group ||
517 s->mode != WPAS_MODE_P2P_GO)
518 continue;
519
520 /* Prefer a group with connected clients */
521 if (p2p_get_group_num_members(wpa_s->p2p_group))
522 return wpa_s;
523 save = wpa_s;
524 }
525 }
526
527 /* No group with connected clients, so pick the one without (if any) */
528 return save;
529}
530
531
532/* Find an active P2P group where we are the GO */
533static struct wpa_ssid * wpas_p2p_group_go_ssid(struct wpa_supplicant *wpa_s,
534 u8 *bssid)
535{
536 struct wpa_ssid *s, *empty = NULL;
537
538 if (!wpa_s)
539 return 0;
540
541 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
542 for (s = wpa_s->conf->ssid; s; s = s->next) {
543 if (s->disabled || !s->p2p_group ||
544 s->mode != WPAS_MODE_P2P_GO)
545 continue;
546
547 os_memcpy(bssid, wpa_s->own_addr, ETH_ALEN);
548 if (p2p_get_group_num_members(wpa_s->p2p_group))
549 return s;
550 empty = s;
551 }
552 }
553
554 return empty;
555}
556
557
558/* Find a persistent group where we are the GO */
559static struct wpa_ssid *
560wpas_p2p_get_persistent_go(struct wpa_supplicant *wpa_s)
561{
562 struct wpa_ssid *s;
563
564 for (s = wpa_s->conf->ssid; s; s = s->next) {
565 if (s->disabled == 2 && s->mode == WPAS_MODE_P2P_GO)
566 return s;
567 }
568
569 return NULL;
570}
571
572
573static u8 p2ps_group_capability(void *ctx, u8 incoming, u8 role)
574{
575 struct wpa_supplicant *wpa_s = ctx, *tmp_wpa_s;
576 struct wpa_ssid *s;
577 u8 conncap = P2PS_SETUP_NONE;
578 unsigned int owned_members = 0;
579 unsigned int owner = 0;
580 unsigned int client = 0;
581 struct wpa_supplicant *go_wpa_s;
582 struct wpa_ssid *persistent_go;
583 int p2p_no_group_iface;
584
585 wpa_printf(MSG_DEBUG, "P2P: Conncap - in:%d role:%d", incoming, role);
586
587 /*
588 * For non-concurrent capable devices:
589 * If persistent_go, then no new.
590 * If GO, then no client.
591 * If client, then no GO.
592 */
593 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
594 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
595 p2p_no_group_iface = wpa_s->conf->p2p_no_group_iface;
596
597 wpa_printf(MSG_DEBUG, "P2P: GO(iface)=%p persistent(ssid)=%p",
598 go_wpa_s, persistent_go);
599
600 for (tmp_wpa_s = wpa_s->global->ifaces; tmp_wpa_s;
601 tmp_wpa_s = tmp_wpa_s->next) {
602 for (s = tmp_wpa_s->conf->ssid; s; s = s->next) {
603 wpa_printf(MSG_DEBUG,
604 "P2P: sup:%p ssid:%p disabled:%d p2p:%d mode:%d",
605 tmp_wpa_s, s, s->disabled,
606 s->p2p_group, s->mode);
607 if (!s->disabled && s->p2p_group) {
608 if (s->mode == WPAS_MODE_P2P_GO) {
609 owned_members +=
610 p2p_get_group_num_members(
611 tmp_wpa_s->p2p_group);
612 owner++;
613 } else
614 client++;
615 }
616 }
617 }
618
619 /* If not concurrent, restrict our choices */
620 if (p2p_no_group_iface) {
621 wpa_printf(MSG_DEBUG, "P2P: p2p_no_group_iface");
622
623 if (client)
624 return P2PS_SETUP_NONE;
625
626 if (go_wpa_s) {
627 if (role == P2PS_SETUP_CLIENT ||
628 incoming == P2PS_SETUP_GROUP_OWNER ||
629 p2p_client_limit_reached(go_wpa_s->p2p_group))
630 return P2PS_SETUP_NONE;
631
632 return P2PS_SETUP_GROUP_OWNER;
633 }
634
635 if (persistent_go) {
636 if (role == P2PS_SETUP_NONE || role == P2PS_SETUP_NEW) {
637 if (!incoming)
638 return P2PS_SETUP_GROUP_OWNER |
639 P2PS_SETUP_CLIENT;
640 if (incoming == P2PS_SETUP_NEW) {
641 u8 r;
642
643 if (os_get_random(&r, sizeof(r)) < 0 ||
644 (r & 1))
645 return P2PS_SETUP_CLIENT;
646 return P2PS_SETUP_GROUP_OWNER;
647 }
648 }
649 }
650 }
651
652 /* If a required role has been specified, handle it here */
653 if (role && role != P2PS_SETUP_NEW) {
654 switch (incoming) {
655 case P2PS_SETUP_NONE:
656 case P2PS_SETUP_NEW:
657 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
658 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
659 conncap = role;
660 goto grp_owner;
661
662 case P2PS_SETUP_GROUP_OWNER:
663 /*
664 * Must be a complimentary role - cannot be a client to
665 * more than one peer.
666 */
667 if (incoming == role || client)
668 return P2PS_SETUP_NONE;
669
670 return P2PS_SETUP_CLIENT;
671
672 case P2PS_SETUP_CLIENT:
673 /* Must be a complimentary role */
674 if (incoming != role) {
675 conncap = P2PS_SETUP_GROUP_OWNER;
676 goto grp_owner;
677 }
678
679 default:
680 return P2PS_SETUP_NONE;
681 }
682 }
683
684 /*
685 * For now, we only will support ownership of one group, and being a
686 * client of one group. Therefore, if we have either an existing GO
687 * group, or an existing client group, we will not do a new GO
688 * negotiation, but rather try to re-use the existing groups.
689 */
690 switch (incoming) {
691 case P2PS_SETUP_NONE:
692 case P2PS_SETUP_NEW:
693 if (client)
694 conncap = P2PS_SETUP_GROUP_OWNER;
695 else if (!owned_members)
696 conncap = P2PS_SETUP_NEW;
697 else if (incoming == P2PS_SETUP_NONE)
698 conncap = P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT;
699 else
700 conncap = P2PS_SETUP_CLIENT;
701 break;
702
703 case P2PS_SETUP_CLIENT:
704 conncap = P2PS_SETUP_GROUP_OWNER;
705 break;
706
707 case P2PS_SETUP_GROUP_OWNER:
708 if (!client)
709 conncap = P2PS_SETUP_CLIENT;
710 break;
711
712 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_NEW:
713 case P2PS_SETUP_GROUP_OWNER | P2PS_SETUP_CLIENT:
714 if (client)
715 conncap = P2PS_SETUP_GROUP_OWNER;
716 else {
717 u8 r;
718
719 if (os_get_random(&r, sizeof(r)) < 0 ||
720 (r & 1))
721 conncap = P2PS_SETUP_CLIENT;
722 else
723 conncap = P2PS_SETUP_GROUP_OWNER;
724 }
725 break;
726
727 default:
728 return P2PS_SETUP_NONE;
729 }
730
731grp_owner:
732 if ((conncap & P2PS_SETUP_GROUP_OWNER) ||
733 (!incoming && (conncap & P2PS_SETUP_NEW))) {
734 if (go_wpa_s && p2p_client_limit_reached(go_wpa_s->p2p_group))
735 conncap &= ~P2PS_SETUP_GROUP_OWNER;
736 wpa_printf(MSG_DEBUG, "P2P: GOs:%d members:%d conncap:%d",
737 owner, owned_members, conncap);
738
739 s = wpas_p2p_get_persistent_go(wpa_s);
740
741 if (!s && !owner && p2p_no_group_iface) {
742 p2p_set_intended_addr(wpa_s->global->p2p,
743 wpa_s->own_addr);
744 } else if (!s && !owner) {
745 if (wpas_p2p_add_group_interface(wpa_s,
746 WPA_IF_P2P_GO) < 0) {
747 wpa_printf(MSG_ERROR,
748 "P2P: Failed to allocate a new interface for the group");
749 return P2PS_SETUP_NONE;
750 }
751 wpa_s->global->pending_group_iface_for_p2ps = 1;
752 p2p_set_intended_addr(wpa_s->global->p2p,
753 wpa_s->pending_interface_addr);
754 }
755 }
756
757 return conncap;
758}
759
760
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700761static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
762 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763{
764 struct wpa_ssid *ssid;
765 char *gtype;
766 const char *reason;
767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 ssid = wpa_s->current_ssid;
769 if (ssid == NULL) {
770 /*
771 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700772 * pending P2P group interface waiting for provisioning or a
773 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 */
775 ssid = wpa_s->conf->ssid;
776 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700777 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778 break;
779 ssid = ssid->next;
780 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300781 if (ssid == NULL &&
782 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
783 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700784 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
785 "not found");
786 return -1;
787 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 }
789 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
790 gtype = "GO";
791 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
792 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
793 wpa_s->reassociate = 0;
794 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 gtype = "client";
796 } else
797 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700798
799 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
800 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
801
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800802 if (os_strcmp(gtype, "client") == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700803 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800804 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
805 wpa_s, NULL)) {
806 wpa_printf(MSG_DEBUG,
807 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
808 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
809 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
810 wpa_s, NULL);
811 }
812 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814 if (wpa_s->cross_connect_in_use) {
815 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700816 wpa_msg_global(wpa_s->parent, MSG_INFO,
817 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
818 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700820 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 case P2P_GROUP_REMOVAL_REQUESTED:
822 reason = " reason=REQUESTED";
823 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700824 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
825 reason = " reason=FORMATION_FAILED";
826 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
828 reason = " reason=IDLE";
829 break;
830 case P2P_GROUP_REMOVAL_UNAVAILABLE:
831 reason = " reason=UNAVAILABLE";
832 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700833 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
834 reason = " reason=GO_ENDING_SESSION";
835 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700836 case P2P_GROUP_REMOVAL_PSK_FAILURE:
837 reason = " reason=PSK_FAILURE";
838 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800839 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
840 reason = " reason=FREQ_CONFLICT";
841 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 default:
843 reason = "";
844 break;
845 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700846 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700847 wpa_msg_global(wpa_s->parent, MSG_INFO,
848 P2P_EVENT_GROUP_REMOVED "%s %s%s",
849 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700850 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800852 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
853 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700854 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
855 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800856 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700857 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800858 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
859 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700860 wpa_s->p2p_in_provisioning = 0;
861 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700862
Dmitry Shmidt15907092014-03-25 10:42:57 -0700863 wpa_s->p2p_in_invitation = 0;
864
Dmitry Shmidt96571392013-10-14 12:54:46 -0700865 /*
866 * Make sure wait for the first client does not remain active after the
867 * group has been removed.
868 */
869 wpa_s->global->p2p_go_wait_client.sec = 0;
870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
872 struct wpa_global *global;
873 char *ifname;
874 enum wpa_driver_if_type type;
875 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
876 wpa_s->ifname);
877 global = wpa_s->global;
878 ifname = os_strdup(wpa_s->ifname);
879 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800880 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700881 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 wpa_s = global->ifaces;
883 if (wpa_s && ifname)
884 wpa_drv_if_remove(wpa_s, type, ifname);
885 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300886 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 }
888
Dmitry Shmidt96571392013-10-14 12:54:46 -0700889 if (!wpa_s->p2p_go_group_formation_completed) {
890 wpa_s->global->p2p_group_formation = NULL;
891 wpa_s->p2p_in_provisioning = 0;
892 }
893
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800894 wpa_s->show_group_started = 0;
895 os_free(wpa_s->go_params);
896 wpa_s->go_params = NULL;
897
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800898 os_free(wpa_s->p2p_group_common_freqs);
899 wpa_s->p2p_group_common_freqs = NULL;
900 wpa_s->p2p_group_common_freqs_num = 0;
901
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800902 wpa_s->waiting_presence_resp = 0;
903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
905 if (ssid && (ssid->p2p_group ||
906 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
907 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
908 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700909 if (ssid == wpa_s->current_ssid) {
910 wpa_sm_set_config(wpa_s->wpa, NULL);
911 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700913 }
914 /*
915 * Networks objects created during any P2P activities are not
916 * exposed out as they might/will confuse certain non-P2P aware
917 * applications since these network objects won't behave like
918 * regular ones.
919 *
920 * Likewise, we don't send out network removed signals for such
921 * network objects.
922 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 wpa_config_remove_network(wpa_s->conf, id);
924 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800925 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 } else {
927 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
928 "found");
929 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700930 if (wpa_s->ap_iface)
931 wpa_supplicant_ap_deinit(wpa_s);
932 else
933 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700934
935 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936}
937
938
939static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
940 u8 *go_dev_addr,
941 const u8 *ssid, size_t ssid_len)
942{
943 struct wpa_bss *bss;
944 const u8 *bssid;
945 struct wpabuf *p2p;
946 u8 group_capab;
947 const u8 *addr;
948
949 if (wpa_s->go_params)
950 bssid = wpa_s->go_params->peer_interface_addr;
951 else
952 bssid = wpa_s->bssid;
953
954 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800955 if (bss == NULL && wpa_s->go_params &&
956 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
957 bss = wpa_bss_get_p2p_dev_addr(
958 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 if (bss == NULL) {
960 u8 iface_addr[ETH_ALEN];
961 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
962 iface_addr) == 0)
963 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
964 }
965 if (bss == NULL) {
966 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
967 "group is persistent - BSS " MACSTR " not found",
968 MAC2STR(bssid));
969 return 0;
970 }
971
972 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700973 if (p2p == NULL)
974 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
975 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 if (p2p == NULL) {
977 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
978 "group is persistent - BSS " MACSTR
979 " did not include P2P IE", MAC2STR(bssid));
980 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
981 (u8 *) (bss + 1), bss->ie_len);
982 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
983 ((u8 *) bss + 1) + bss->ie_len,
984 bss->beacon_ie_len);
985 return 0;
986 }
987
988 group_capab = p2p_get_group_capab(p2p);
989 addr = p2p_get_go_dev_addr(p2p);
990 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
991 "group_capab=0x%x", group_capab);
992 if (addr) {
993 os_memcpy(go_dev_addr, addr, ETH_ALEN);
994 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
995 MAC2STR(addr));
996 } else
997 os_memset(go_dev_addr, 0, ETH_ALEN);
998 wpabuf_free(p2p);
999
1000 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
1001 "go_dev_addr=" MACSTR,
1002 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
1003
1004 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
1005}
1006
1007
Jouni Malinen75ecf522011-06-27 15:19:46 -07001008static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
1009 struct wpa_ssid *ssid,
1010 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011{
1012 struct wpa_ssid *s;
1013 int changed = 0;
1014
1015 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
1016 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
1017 for (s = wpa_s->conf->ssid; s; s = s->next) {
1018 if (s->disabled == 2 &&
1019 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
1020 s->ssid_len == ssid->ssid_len &&
1021 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
1022 break;
1023 }
1024
1025 if (s) {
1026 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
1027 "entry");
1028 if (ssid->passphrase && !s->passphrase)
1029 changed = 1;
1030 else if (ssid->passphrase && s->passphrase &&
1031 os_strcmp(ssid->passphrase, s->passphrase) != 0)
1032 changed = 1;
1033 } else {
1034 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
1035 "entry");
1036 changed = 1;
1037 s = wpa_config_add_network(wpa_s->conf);
1038 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001039 return -1;
1040
1041 /*
1042 * Instead of network_added we emit persistent_group_added
1043 * notification. Also to keep the defense checks in
1044 * persistent_group obj registration method, we set the
1045 * relevant flags in s to designate it as a persistent group.
1046 */
1047 s->p2p_group = 1;
1048 s->p2p_persistent_group = 1;
1049 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 wpa_config_set_network_defaults(s);
1051 }
1052
1053 s->p2p_group = 1;
1054 s->p2p_persistent_group = 1;
1055 s->disabled = 2;
1056 s->bssid_set = 1;
1057 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
1058 s->mode = ssid->mode;
1059 s->auth_alg = WPA_AUTH_ALG_OPEN;
1060 s->key_mgmt = WPA_KEY_MGMT_PSK;
1061 s->proto = WPA_PROTO_RSN;
1062 s->pairwise_cipher = WPA_CIPHER_CCMP;
1063 s->export_keys = 1;
1064 if (ssid->passphrase) {
1065 os_free(s->passphrase);
1066 s->passphrase = os_strdup(ssid->passphrase);
1067 }
1068 if (ssid->psk_set) {
1069 s->psk_set = 1;
1070 os_memcpy(s->psk, ssid->psk, 32);
1071 }
1072 if (s->passphrase && !s->psk_set)
1073 wpa_config_update_psk(s);
1074 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
1075 os_free(s->ssid);
1076 s->ssid = os_malloc(ssid->ssid_len);
1077 }
1078 if (s->ssid) {
1079 s->ssid_len = ssid->ssid_len;
1080 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
1081 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001082 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
1083 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
1084 wpa_s->global->add_psk = NULL;
1085 changed = 1;
1086 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 if (changed && wpa_s->conf->update_config &&
1089 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
1090 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1091 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001092
1093 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094}
1095
1096
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001097static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
1098 const u8 *addr)
1099{
1100 struct wpa_ssid *ssid, *s;
1101 u8 *n;
1102 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001103 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001104
1105 ssid = wpa_s->current_ssid;
1106 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
1107 !ssid->p2p_persistent_group)
1108 return;
1109
1110 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
1111 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
1112 continue;
1113
1114 if (s->ssid_len == ssid->ssid_len &&
1115 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
1116 break;
1117 }
1118
1119 if (s == NULL)
1120 return;
1121
1122 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001123 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001124 ETH_ALEN) != 0)
1125 continue;
1126
1127 if (i == s->num_p2p_clients - 1)
1128 return; /* already the most recent entry */
1129
1130 /* move the entry to mark it most recent */
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001131 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
1132 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
1133 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001134 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001135 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
1136 ETH_ALEN);
1137 os_memset(s->p2p_client_list +
1138 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1139 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001140 found = 1;
1141 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001142 }
1143
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001144 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
1145 n = os_realloc_array(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001146 s->num_p2p_clients + 1, 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001147 if (n == NULL)
1148 return;
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001149 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
1150 ETH_ALEN);
1151 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
1152 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001153 s->p2p_client_list = n;
1154 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001155 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001156 /* Not enough room for an additional entry - drop the oldest
1157 * entry */
1158 os_memmove(s->p2p_client_list,
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001159 s->p2p_client_list + 2 * ETH_ALEN,
1160 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001161 os_memcpy(s->p2p_client_list +
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001162 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001163 addr, ETH_ALEN);
Dmitry Shmidtff787d52015-01-12 13:01:47 -08001164 os_memset(s->p2p_client_list +
1165 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
1166 0xff, ETH_ALEN);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001167 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001168
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001169 if (wpa_s->parent->conf->update_config &&
1170 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1171 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172}
1173
1174
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001175static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
1176 int go, struct wpa_ssid *ssid, int freq,
1177 const u8 *psk, const char *passphrase,
1178 const u8 *go_dev_addr, int persistent,
1179 const char *extra)
1180{
1181 const char *ssid_txt;
1182 char psk_txt[65];
1183
1184 if (psk)
1185 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
1186 else
1187 psk_txt[0] = '\0';
1188
1189 if (ssid)
1190 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
1191 else
1192 ssid_txt = "";
1193
1194 if (passphrase && passphrase[0] == '\0')
1195 passphrase = NULL;
1196
1197 /*
1198 * Include PSK/passphrase only in the control interface message and
1199 * leave it out from the debug log entry.
1200 */
1201 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
1202 P2P_EVENT_GROUP_STARTED
1203 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
1204 MACSTR "%s%s",
1205 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1206 psk ? " psk=" : "", psk_txt,
1207 passphrase ? " passphrase=\"" : "",
1208 passphrase ? passphrase : "",
1209 passphrase ? "\"" : "",
1210 MAC2STR(go_dev_addr),
1211 persistent ? " [PERSISTENT]" : "", extra);
1212 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
1213 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
1214 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
1215 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
1216 extra);
1217}
1218
1219
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
1221 int success)
1222{
1223 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224 int client;
1225 int persistent;
1226 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07001227 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001228
1229 /*
1230 * This callback is likely called for the main interface. Update wpa_s
1231 * to use the group interface if a new interface was created for the
1232 * group.
1233 */
1234 if (wpa_s->global->p2p_group_formation)
1235 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -07001236 if (wpa_s->p2p_go_group_formation_completed) {
1237 wpa_s->global->p2p_group_formation = NULL;
1238 wpa_s->p2p_in_provisioning = 0;
1239 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001240 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001241 wpa_s->group_formation_reported = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242
1243 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001244 wpa_msg_global(wpa_s->parent, MSG_INFO,
1245 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001246 wpas_p2p_group_delete(wpa_s,
1247 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248 return;
1249 }
1250
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001251 wpa_msg_global(wpa_s->parent, MSG_INFO,
1252 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253
1254 ssid = wpa_s->current_ssid;
1255 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1256 ssid->mode = WPAS_MODE_P2P_GO;
1257 p2p_group_notif_formation_done(wpa_s->p2p_group);
1258 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
1259 }
1260
1261 persistent = 0;
1262 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001263 client = ssid->mode == WPAS_MODE_INFRA;
1264 if (ssid->mode == WPAS_MODE_P2P_GO) {
1265 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001266 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
1267 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268 } else
1269 persistent = wpas_p2p_persistent_group(wpa_s,
1270 go_dev_addr,
1271 ssid->ssid,
1272 ssid->ssid_len);
1273 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001274 client = wpa_s->p2p_group_interface ==
1275 P2P_GROUP_INTERFACE_CLIENT;
1276 os_memset(go_dev_addr, 0, ETH_ALEN);
1277 }
1278
1279 wpa_s->show_group_started = 0;
1280 if (client) {
1281 /*
1282 * Indicate event only after successfully completed 4-way
1283 * handshake, i.e., when the interface is ready for data
1284 * packets.
1285 */
1286 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001288 wpas_p2p_group_started(wpa_s, 1, ssid,
1289 ssid ? ssid->frequency : 0,
1290 ssid && ssid->passphrase == NULL &&
1291 ssid->psk_set ? ssid->psk : NULL,
1292 ssid ? ssid->passphrase : NULL,
1293 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001294 wpas_p2p_cross_connect_setup(wpa_s);
1295 wpas_p2p_set_group_idle_timeout(wpa_s);
1296 }
1297
1298 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001299 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1300 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001301 else {
1302 os_free(wpa_s->global->add_psk);
1303 wpa_s->global->add_psk = NULL;
1304 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001305 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -07001306 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001307 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001308 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001309 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07001310 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001311}
1312
1313
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001314struct send_action_work {
1315 unsigned int freq;
1316 u8 dst[ETH_ALEN];
1317 u8 src[ETH_ALEN];
1318 u8 bssid[ETH_ALEN];
1319 size_t len;
1320 unsigned int wait_time;
1321 u8 buf[0];
1322};
1323
1324
1325static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1326 void *timeout_ctx)
1327{
1328 struct wpa_supplicant *wpa_s = eloop_ctx;
1329
1330 if (!wpa_s->p2p_send_action_work)
1331 return;
1332
1333 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1334 os_free(wpa_s->p2p_send_action_work->ctx);
1335 radio_work_done(wpa_s->p2p_send_action_work);
1336 wpa_s->p2p_send_action_work = NULL;
1337}
1338
1339
Dmitry Shmidt03658832014-08-13 11:03:49 -07001340static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001342 if (wpa_s->p2p_send_action_work) {
1343 struct send_action_work *awork;
1344 awork = wpa_s->p2p_send_action_work->ctx;
1345 if (awork->wait_time == 0) {
1346 os_free(awork);
1347 radio_work_done(wpa_s->p2p_send_action_work);
1348 wpa_s->p2p_send_action_work = NULL;
1349 } else {
1350 /*
1351 * In theory, this should not be needed, but number of
1352 * places in the P2P code is still using non-zero wait
1353 * time for the last Action frame in the sequence and
1354 * some of these do not call send_action_done().
1355 */
1356 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1357 wpa_s, NULL);
1358 eloop_register_timeout(
1359 0, awork->wait_time * 1000,
1360 wpas_p2p_send_action_work_timeout,
1361 wpa_s, NULL);
1362 }
1363 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001364}
1365
1366
1367static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1368 unsigned int freq,
1369 const u8 *dst, const u8 *src,
1370 const u8 *bssid,
1371 const u8 *data, size_t data_len,
1372 enum offchannel_send_action_result
1373 result)
1374{
1375 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1376
1377 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1380 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001381
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001382 switch (result) {
1383 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1384 res = P2P_SEND_ACTION_SUCCESS;
1385 break;
1386 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1387 res = P2P_SEND_ACTION_NO_ACK;
1388 break;
1389 case OFFCHANNEL_SEND_ACTION_FAILED:
1390 res = P2P_SEND_ACTION_FAILED;
1391 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 }
1393
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001394 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001396 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1397 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001398 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001399 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1400 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1403 "during p2p_connect-auto");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001404 wpa_msg_global(wpa_s->parent, MSG_INFO,
1405 P2P_EVENT_FALLBACK_TO_GO_NEG
1406 "reason=no-ACK-to-PD-Req");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001407 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1408 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 }
1410}
1411
1412
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001413static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1414{
1415 struct wpa_supplicant *wpa_s = work->wpa_s;
1416 struct send_action_work *awork = work->ctx;
1417
1418 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001419 if (work->started) {
1420 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1421 wpa_s, NULL);
1422 wpa_s->p2p_send_action_work = NULL;
1423 offchannel_send_action_done(wpa_s);
1424 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001425 os_free(awork);
1426 return;
1427 }
1428
1429 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1430 awork->bssid, awork->buf, awork->len,
1431 awork->wait_time,
1432 wpas_p2p_send_action_tx_status, 1) < 0) {
1433 os_free(awork);
1434 radio_work_done(work);
1435 return;
1436 }
1437 wpa_s->p2p_send_action_work = work;
1438}
1439
1440
1441static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1442 unsigned int freq, const u8 *dst,
1443 const u8 *src, const u8 *bssid, const u8 *buf,
1444 size_t len, unsigned int wait_time)
1445{
1446 struct send_action_work *awork;
1447
1448 if (wpa_s->p2p_send_action_work) {
1449 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1450 return -1;
1451 }
1452
1453 awork = os_zalloc(sizeof(*awork) + len);
1454 if (awork == NULL)
1455 return -1;
1456
1457 awork->freq = freq;
1458 os_memcpy(awork->dst, dst, ETH_ALEN);
1459 os_memcpy(awork->src, src, ETH_ALEN);
1460 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1461 awork->len = len;
1462 awork->wait_time = wait_time;
1463 os_memcpy(awork->buf, buf, len);
1464
1465 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1466 wpas_send_action_cb, awork) < 0) {
1467 os_free(awork);
1468 return -1;
1469 }
1470
1471 return 0;
1472}
1473
1474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001475static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1476 const u8 *src, const u8 *bssid, const u8 *buf,
1477 size_t len, unsigned int wait_time)
1478{
1479 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001480 int listen_freq = -1, send_freq = -1;
1481
1482 if (wpa_s->p2p_listen_work)
1483 listen_freq = wpa_s->p2p_listen_work->freq;
1484 if (wpa_s->p2p_send_action_work)
1485 send_freq = wpa_s->p2p_send_action_work->freq;
1486 if (listen_freq != (int) freq && send_freq != (int) freq) {
1487 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1488 listen_freq, send_freq);
1489 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1490 len, wait_time);
1491 }
1492
1493 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001494 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1495 wait_time,
1496 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497}
1498
1499
1500static void wpas_send_action_done(void *ctx)
1501{
1502 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001503
1504 if (wpa_s->p2p_send_action_work) {
1505 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1506 wpa_s, NULL);
1507 os_free(wpa_s->p2p_send_action_work->ctx);
1508 radio_work_done(wpa_s->p2p_send_action_work);
1509 wpa_s->p2p_send_action_work = NULL;
1510 }
1511
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001512 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513}
1514
1515
1516static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1517 struct p2p_go_neg_results *params)
1518{
1519 if (wpa_s->go_params == NULL) {
1520 wpa_s->go_params = os_malloc(sizeof(*params));
1521 if (wpa_s->go_params == NULL)
1522 return -1;
1523 }
1524 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1525 return 0;
1526}
1527
1528
1529static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1530 struct p2p_go_neg_results *res)
1531{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001532 wpa_s->group_formation_reported = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001533 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1534 " dev_addr " MACSTR " wps_method %d",
1535 MAC2STR(res->peer_interface_addr),
1536 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1538 res->ssid, res->ssid_len);
1539 wpa_supplicant_ap_deinit(wpa_s);
1540 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001541 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001543#ifdef CONFIG_WPS_NFC
1544 } else if (res->wps_method == WPS_NFC) {
1545 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1546 res->peer_interface_addr,
1547 wpa_s->parent->p2p_oob_dev_pw,
1548 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1549 wpa_s->parent->p2p_oob_dev_pw_id ==
1550 DEV_PW_NFC_CONNECTION_HANDOVER ?
1551 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1552 NULL,
1553 NULL, 0, 0);
1554#endif /* CONFIG_WPS_NFC */
1555 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 u16 dev_pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001557 if (wpa_s->p2p_wps_method == WPS_P2PS)
1558 dev_pw_id = DEV_PW_P2PS_DEFAULT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1560 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1561 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1562 wpa_s->p2p_pin, 1, dev_pw_id);
1563 }
1564}
1565
1566
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001567static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1568 struct wpa_ssid *ssid)
1569{
1570 struct wpa_ssid *persistent;
1571 struct psk_list_entry *psk;
1572 struct hostapd_data *hapd;
1573
1574 if (!wpa_s->ap_iface)
1575 return;
1576
1577 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1578 ssid->ssid_len);
1579 if (persistent == NULL)
1580 return;
1581
1582 hapd = wpa_s->ap_iface->bss[0];
1583
1584 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1585 list) {
1586 struct hostapd_wpa_psk *hpsk;
1587
1588 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1589 MACSTR " psk=%d",
1590 MAC2STR(psk->addr), psk->p2p);
1591 hpsk = os_zalloc(sizeof(*hpsk));
1592 if (hpsk == NULL)
1593 break;
1594 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1595 if (psk->p2p)
1596 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1597 else
1598 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1599 hpsk->next = hapd->conf->ssid.wpa_psk;
1600 hapd->conf->ssid.wpa_psk = hpsk;
1601 }
1602}
1603
1604
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001605static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1606{
1607 unsigned int i;
1608
1609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1610 wpa_s->p2p_group_common_freqs_num);
1611
1612 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1613 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1614 i, wpa_s->p2p_group_common_freqs[i]);
1615}
1616
1617
1618static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1619 struct p2p_go_neg_results *params)
1620{
1621 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1622
1623 wpa_s->p2p_group_common_freqs_num = 0;
1624 os_free(wpa_s->p2p_group_common_freqs);
1625 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1626 if (!wpa_s->p2p_group_common_freqs)
1627 return;
1628
1629 for (i = 0; i < len; i++) {
1630 if (!wpa_s->go_params->freq_list[i])
1631 break;
1632 wpa_s->p2p_group_common_freqs[i] =
1633 wpa_s->go_params->freq_list[i];
1634 }
1635 wpa_s->p2p_group_common_freqs_num = i;
1636}
1637
1638
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001639static void p2p_config_write(struct wpa_supplicant *wpa_s)
1640{
1641#ifndef CONFIG_NO_CONFIG_WRITE
1642 if (wpa_s->parent->conf->update_config &&
1643 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
1644 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
1645#endif /* CONFIG_NO_CONFIG_WRITE */
1646}
1647
1648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649static void p2p_go_configured(void *ctx, void *data)
1650{
1651 struct wpa_supplicant *wpa_s = ctx;
1652 struct p2p_go_neg_results *params = data;
1653 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001654 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001656 p2p_go_save_group_common_freqs(wpa_s, params);
1657 p2p_go_dump_common_freqs(wpa_s);
1658
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 ssid = wpa_s->current_ssid;
1660 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1661 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1662 if (wpa_s->global->p2p_group_formation == wpa_s)
1663 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001664 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1665 params->passphrase[0] == '\0' ?
1666 params->psk : NULL,
1667 params->passphrase,
1668 wpa_s->global->p2p_dev_addr,
1669 params->persistent_group, "");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001670 wpa_s->group_formation_reported = 1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001671
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001672 if (wpa_s->parent->p2ps_join_addr_valid) {
1673 wpa_dbg(wpa_s, MSG_DEBUG,
1674 "P2PS: Setting default PIN for " MACSTR,
1675 MAC2STR(wpa_s->parent->p2ps_join_addr));
1676 wpa_supplicant_ap_wps_pin(wpa_s,
1677 wpa_s->parent->p2ps_join_addr,
1678 "12345670", NULL, 0, 0);
1679 wpa_s->parent->p2ps_join_addr_valid = 0;
1680 }
1681
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001682 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001683 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001684 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001686 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001687 wpas_p2p_add_psk_list(wpa_s, ssid);
1688 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001689 if (network_id < 0)
1690 network_id = ssid->id;
1691 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 wpas_p2p_cross_connect_setup(wpa_s);
1693 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001694
1695 if (wpa_s->p2p_first_connection_timeout) {
1696 wpa_dbg(wpa_s, MSG_DEBUG,
1697 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1698 wpa_s->p2p_first_connection_timeout);
1699 wpa_s->p2p_go_group_formation_completed = 0;
1700 wpa_s->global->p2p_group_formation = wpa_s;
1701 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1702 wpa_s->parent, NULL);
1703 eloop_register_timeout(
1704 wpa_s->p2p_first_connection_timeout, 0,
1705 wpas_p2p_group_formation_timeout,
1706 wpa_s->parent, NULL);
1707 }
1708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709 return;
1710 }
1711
1712 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1713 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1714 params->peer_interface_addr)) {
1715 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1716 "filtering");
1717 return;
1718 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001719 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001722#ifdef CONFIG_WPS_NFC
1723 } else if (params->wps_method == WPS_NFC) {
1724 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1725 DEV_PW_NFC_CONNECTION_HANDOVER &&
1726 !wpa_s->parent->p2p_oob_dev_pw) {
1727 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1728 return;
1729 }
1730 wpas_ap_wps_add_nfc_pw(
1731 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1732 wpa_s->parent->p2p_oob_dev_pw,
1733 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1734 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1735#endif /* CONFIG_WPS_NFC */
1736 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001738 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 os_free(wpa_s->go_params);
1740 wpa_s->go_params = NULL;
1741}
1742
1743
1744static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1745 struct p2p_go_neg_results *params,
1746 int group_formation)
1747{
1748 struct wpa_ssid *ssid;
1749
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001750 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1751 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1752 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1753 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001755 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756
1757 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001758 if (ssid == NULL) {
1759 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001761 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001763 wpa_s->show_group_started = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001764 wpa_s->p2p_go_group_formation_completed = 0;
1765 wpa_s->group_formation_reported = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 wpa_config_set_network_defaults(ssid);
1768 ssid->temporary = 1;
1769 ssid->p2p_group = 1;
1770 ssid->p2p_persistent_group = params->persistent_group;
1771 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1772 WPAS_MODE_P2P_GO;
1773 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001774 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001775 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 ssid->ssid = os_zalloc(params->ssid_len + 1);
1777 if (ssid->ssid) {
1778 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1779 ssid->ssid_len = params->ssid_len;
1780 }
1781 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1782 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1783 ssid->proto = WPA_PROTO_RSN;
1784 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001785 ssid->group_cipher = WPA_CIPHER_CCMP;
1786 if (params->freq > 56160) {
1787 /*
1788 * Enable GCMP instead of CCMP as pairwise_cipher and
1789 * group_cipher in 60 GHz.
1790 */
1791 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1792 ssid->group_cipher = WPA_CIPHER_GCMP;
1793 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001794 if (os_strlen(params->passphrase) > 0) {
1795 ssid->passphrase = os_strdup(params->passphrase);
1796 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001797 wpa_msg_global(wpa_s, MSG_ERROR,
1798 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001799 wpa_config_remove_network(wpa_s->conf, ssid->id);
1800 return;
1801 }
1802 } else
1803 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001804 ssid->psk_set = params->psk_set;
1805 if (ssid->psk_set)
1806 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001807 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001808 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001809 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
1811 wpa_s->ap_configured_cb = p2p_go_configured;
1812 wpa_s->ap_configured_cb_ctx = wpa_s;
1813 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001814 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001815 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 wpa_s->reassociate = 1;
1817 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001818 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1819 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 wpa_supplicant_req_scan(wpa_s, 0, 0);
1821}
1822
1823
1824static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1825 const struct wpa_supplicant *src)
1826{
1827 struct wpa_config *d;
1828 const struct wpa_config *s;
1829
1830 d = dst->conf;
1831 s = src->conf;
1832
1833#define C(n) if (s->n) d->n = os_strdup(s->n)
1834 C(device_name);
1835 C(manufacturer);
1836 C(model_name);
1837 C(model_number);
1838 C(serial_number);
1839 C(config_methods);
1840#undef C
1841
1842 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1843 os_memcpy(d->sec_device_type, s->sec_device_type,
1844 sizeof(d->sec_device_type));
1845 d->num_sec_device_types = s->num_sec_device_types;
1846
1847 d->p2p_group_idle = s->p2p_group_idle;
1848 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001849 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001850 d->max_num_sta = s->max_num_sta;
1851 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001852 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001853 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001854 d->dtim_period = s->dtim_period;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001855 d->p2p_go_ctwindow = s->p2p_go_ctwindow;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001856 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001857 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001858 d->passive_scan = s->passive_scan;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001859
1860 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1861 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1862 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864}
1865
1866
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001867static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1868 char *ifname, size_t len)
1869{
1870 char *ifname_ptr = wpa_s->ifname;
1871
1872 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1873 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1874 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1875 }
1876
1877 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1878 if (os_strlen(ifname) >= IFNAMSIZ &&
1879 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 int res;
1881
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001882 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001883 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1884 if (os_snprintf_error(len, res) && len)
1885 ifname[len - 1] = '\0';
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001886 }
1887}
1888
1889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001890static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1891 enum wpa_driver_if_type type)
1892{
1893 char ifname[120], force_ifname[120];
1894
1895 if (wpa_s->pending_interface_name[0]) {
1896 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1897 "- skip creation of a new one");
1898 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1899 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1900 "unknown?! ifname='%s'",
1901 wpa_s->pending_interface_name);
1902 return -1;
1903 }
1904 return 0;
1905 }
1906
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001907 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 force_ifname[0] = '\0';
1909
1910 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1911 ifname);
1912 wpa_s->p2p_group_idx++;
1913
1914 wpa_s->pending_interface_type = type;
1915 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1916 wpa_s->pending_interface_addr, NULL) < 0) {
1917 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1918 "interface");
1919 return -1;
1920 }
1921
1922 if (force_ifname[0]) {
1923 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1924 force_ifname);
1925 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1926 sizeof(wpa_s->pending_interface_name));
1927 } else
1928 os_strlcpy(wpa_s->pending_interface_name, ifname,
1929 sizeof(wpa_s->pending_interface_name));
1930 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1931 MACSTR, wpa_s->pending_interface_name,
1932 MAC2STR(wpa_s->pending_interface_addr));
1933
1934 return 0;
1935}
1936
1937
1938static void wpas_p2p_remove_pending_group_interface(
1939 struct wpa_supplicant *wpa_s)
1940{
1941 if (!wpa_s->pending_interface_name[0] ||
1942 is_zero_ether_addr(wpa_s->pending_interface_addr))
1943 return; /* No pending virtual interface */
1944
1945 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1946 wpa_s->pending_interface_name);
1947 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1948 wpa_s->pending_interface_name);
1949 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1950 wpa_s->pending_interface_name[0] = '\0';
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001951 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952}
1953
1954
1955static struct wpa_supplicant *
1956wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1957{
1958 struct wpa_interface iface;
1959 struct wpa_supplicant *group_wpa_s;
1960
1961 if (!wpa_s->pending_interface_name[0]) {
1962 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1963 if (!wpas_p2p_create_iface(wpa_s))
1964 return NULL;
1965 /*
1966 * Something has forced us to remove the pending interface; try
1967 * to create a new one and hope for the best that we will get
1968 * the same local address.
1969 */
1970 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1971 WPA_IF_P2P_CLIENT) < 0)
1972 return NULL;
1973 }
1974
1975 os_memset(&iface, 0, sizeof(iface));
1976 iface.ifname = wpa_s->pending_interface_name;
1977 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001978 if (wpa_s->conf->ctrl_interface == NULL &&
1979 wpa_s->parent != wpa_s &&
1980 wpa_s->p2p_mgmt &&
1981 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1982 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1983 else
1984 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 iface.driver_param = wpa_s->conf->driver_param;
1986 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1987 if (group_wpa_s == NULL) {
1988 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1989 "wpa_supplicant interface");
1990 return NULL;
1991 }
1992 wpa_s->pending_interface_name[0] = '\0';
1993 group_wpa_s->parent = wpa_s;
1994 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1995 P2P_GROUP_INTERFACE_CLIENT;
1996 wpa_s->global->p2p_group_formation = group_wpa_s;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001997 wpa_s->global->pending_group_iface_for_p2ps = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998
1999 wpas_p2p_clone_config(group_wpa_s, wpa_s);
2000
2001 return group_wpa_s;
2002}
2003
2004
2005static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
2006 void *timeout_ctx)
2007{
2008 struct wpa_supplicant *wpa_s = eloop_ctx;
2009 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07002010 wpas_p2p_group_formation_failed(wpa_s);
2011}
2012
2013
2014void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
2015{
2016 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2017 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 if (wpa_s->global->p2p)
2019 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 wpas_group_formation_completed(wpa_s, 0);
2021}
2022
2023
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002024static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
2025{
2026 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
2027 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2028 wpa_s->parent, NULL);
2029 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2030 wpa_s->parent, NULL);
2031 wpa_s->global->p2p_fail_on_wps_complete = 0;
2032}
2033
2034
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002035void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
2036{
2037 if (wpa_s->global->p2p_group_formation != wpa_s)
2038 return;
2039 /* Speed up group formation timeout since this cannot succeed */
2040 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
2041 wpa_s->parent, NULL);
2042 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
2043 wpa_s->parent, NULL);
2044}
2045
2046
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002047static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048{
2049 struct wpa_supplicant *wpa_s = ctx;
2050
2051 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2052 wpa_drv_cancel_remain_on_channel(wpa_s);
2053 wpa_s->off_channel_freq = 0;
2054 wpa_s->roc_waiting_drv_freq = 0;
2055 }
2056
2057 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002058 wpa_msg_global(wpa_s, MSG_INFO,
2059 P2P_EVENT_GO_NEG_FAILURE "status=%d",
2060 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002061 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002062 wpas_p2p_remove_pending_group_interface(wpa_s);
2063 return;
2064 }
2065
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002066 if (wpa_s->p2p_go_ht40)
2067 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002068 if (wpa_s->p2p_go_vht)
2069 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002070
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07002071 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
2072 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
2073 " wps_method=%s",
2074 res->role_go ? "GO" : "client", res->freq, res->ht40,
2075 MAC2STR(res->peer_device_addr),
2076 MAC2STR(res->peer_interface_addr),
2077 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002078 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002079
Dmitry Shmidt04949592012-07-19 12:16:46 -07002080 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
2081 struct wpa_ssid *ssid;
2082 ssid = wpa_config_get_network(wpa_s->conf,
2083 wpa_s->p2p_persistent_id);
2084 if (ssid && ssid->disabled == 2 &&
2085 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
2086 size_t len = os_strlen(ssid->passphrase);
2087 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
2088 "on requested persistent group");
2089 os_memcpy(res->passphrase, ssid->passphrase, len);
2090 res->passphrase[len] = '\0';
2091 }
2092 }
2093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 if (wpa_s->create_p2p_iface) {
2095 struct wpa_supplicant *group_wpa_s =
2096 wpas_p2p_init_group_interface(wpa_s, res->role_go);
2097 if (group_wpa_s == NULL) {
2098 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002099 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
2100 wpa_s, NULL);
2101 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 return;
2103 }
2104 if (group_wpa_s != wpa_s) {
2105 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
2106 sizeof(group_wpa_s->p2p_pin));
2107 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
2108 }
2109 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
2110 wpa_s->pending_interface_name[0] = '\0';
2111 group_wpa_s->p2p_in_provisioning = 1;
2112
2113 if (res->role_go)
2114 wpas_start_wps_go(group_wpa_s, res, 1);
2115 else
2116 wpas_start_wps_enrollee(group_wpa_s, res);
2117 } else {
2118 wpa_s->p2p_in_provisioning = 1;
2119 wpa_s->global->p2p_group_formation = wpa_s;
2120
2121 if (res->role_go)
2122 wpas_start_wps_go(wpa_s, res, 1);
2123 else
2124 wpas_start_wps_enrollee(ctx, res);
2125 }
2126
2127 wpa_s->p2p_long_listen = 0;
2128 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2129
2130 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2131 eloop_register_timeout(15 + res->peer_config_timeout / 100,
2132 (res->peer_config_timeout % 100) * 10000,
2133 wpas_p2p_group_formation_timeout, wpa_s, NULL);
2134}
2135
2136
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002137static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138{
2139 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002140 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
2141 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142
2143 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
2144}
2145
2146
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002147static void wpas_dev_found(void *ctx, const u8 *addr,
2148 const struct p2p_peer_info *info,
2149 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002151#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 struct wpa_supplicant *wpa_s = ctx;
2153 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002154 char *wfd_dev_info_hex = NULL;
2155
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002156#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002157 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
2158 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07002159#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002160
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002161 if (info->p2ps_instance) {
2162 char str[256];
2163 const u8 *buf = wpabuf_head(info->p2ps_instance);
2164 size_t len = wpabuf_len(info->p2ps_instance);
2165
2166 while (len) {
2167 u32 id;
2168 u16 methods;
2169 u8 str_len;
2170
2171 if (len < 4 + 2 + 1)
2172 break;
2173 id = WPA_GET_LE32(buf);
2174 buf += sizeof(u32);
2175 methods = WPA_GET_BE16(buf);
2176 buf += sizeof(u16);
2177 str_len = *buf++;
2178 if (str_len > len - 4 - 2 - 1)
2179 break;
2180 os_memcpy(str, buf, str_len);
2181 str[str_len] = '\0';
2182 buf += str_len;
2183 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
2184
2185 wpa_msg_global(wpa_s, MSG_INFO,
2186 P2P_EVENT_DEVICE_FOUND MACSTR
2187 " p2p_dev_addr=" MACSTR
2188 " pri_dev_type=%s name='%s'"
2189 " config_methods=0x%x"
2190 " dev_capab=0x%x"
2191 " group_capab=0x%x"
2192 " adv_id=%x asp_svc=%s%s",
2193 MAC2STR(addr),
2194 MAC2STR(info->p2p_device_addr),
2195 wps_dev_type_bin2str(
2196 info->pri_dev_type,
2197 devtype, sizeof(devtype)),
2198 info->device_name, methods,
2199 info->dev_capab, info->group_capab,
2200 id, str,
2201 info->vendor_elems ?
2202 " vendor_elems=1" : "");
2203 }
2204 goto done;
2205 }
2206
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002207 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
2208 " p2p_dev_addr=" MACSTR
2209 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002210 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002211 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
2212 wps_dev_type_bin2str(info->pri_dev_type, devtype,
2213 sizeof(devtype)),
2214 info->device_name, info->config_methods,
2215 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002216 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002217 wfd_dev_info_hex ? wfd_dev_info_hex : "",
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002218 info->vendor_elems ? " vendor_elems=1" : "",
2219 new_device);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002221done:
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002222 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08002223#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08002224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
2226}
2227
2228
2229static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
2230{
2231 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002232
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002233 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
2234 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
2237}
2238
2239
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002240static void wpas_find_stopped(void *ctx)
2241{
2242 struct wpa_supplicant *wpa_s = ctx;
2243 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
2244}
2245
2246
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002247struct wpas_p2p_listen_work {
2248 unsigned int freq;
2249 unsigned int duration;
2250 struct wpabuf *probe_resp_ie;
2251};
2252
2253
2254static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
2255{
2256 if (lwork == NULL)
2257 return;
2258 wpabuf_free(lwork->probe_resp_ie);
2259 os_free(lwork);
2260}
2261
2262
2263static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
2264{
2265 struct wpas_p2p_listen_work *lwork;
2266
2267 if (!wpa_s->p2p_listen_work)
2268 return;
2269
2270 lwork = wpa_s->p2p_listen_work->ctx;
2271 wpas_p2p_listen_work_free(lwork);
2272 radio_work_done(wpa_s->p2p_listen_work);
2273 wpa_s->p2p_listen_work = NULL;
2274}
2275
2276
2277static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
2278{
2279 struct wpa_supplicant *wpa_s = work->wpa_s;
2280 struct wpas_p2p_listen_work *lwork = work->ctx;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002281 unsigned int duration;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002282
2283 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08002284 if (work->started) {
2285 wpa_s->p2p_listen_work = NULL;
2286 wpas_stop_listen(wpa_s);
2287 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002288 wpas_p2p_listen_work_free(lwork);
2289 return;
2290 }
2291
2292 wpa_s->p2p_listen_work = work;
2293
2294 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
2295
2296 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
2297 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
2298 "report received Probe Request frames");
2299 wpas_p2p_listen_work_done(wpa_s);
2300 return;
2301 }
2302
2303 wpa_s->pending_listen_freq = lwork->freq;
2304 wpa_s->pending_listen_duration = lwork->duration;
2305
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002306 duration = lwork->duration;
2307#ifdef CONFIG_TESTING_OPTIONS
2308 if (wpa_s->extra_roc_dur) {
2309 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2310 duration, duration + wpa_s->extra_roc_dur);
2311 duration += wpa_s->extra_roc_dur;
2312 }
2313#endif /* CONFIG_TESTING_OPTIONS */
2314
2315 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002316 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2317 "to remain on channel (%u MHz) for Listen "
2318 "state", lwork->freq);
2319 wpas_p2p_listen_work_done(wpa_s);
2320 wpa_s->pending_listen_freq = 0;
2321 return;
2322 }
2323 wpa_s->off_channel_freq = 0;
2324 wpa_s->roc_waiting_drv_freq = lwork->freq;
2325}
2326
2327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002328static int wpas_start_listen(void *ctx, unsigned int freq,
2329 unsigned int duration,
2330 const struct wpabuf *probe_resp_ie)
2331{
2332 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002333 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002335 if (wpa_s->p2p_listen_work) {
2336 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 return -1;
2338 }
2339
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002340 lwork = os_zalloc(sizeof(*lwork));
2341 if (lwork == NULL)
2342 return -1;
2343 lwork->freq = freq;
2344 lwork->duration = duration;
2345 if (probe_resp_ie) {
2346 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2347 if (lwork->probe_resp_ie == NULL) {
2348 wpas_p2p_listen_work_free(lwork);
2349 return -1;
2350 }
2351 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002352
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002353 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2354 lwork) < 0) {
2355 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 return -1;
2357 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358
2359 return 0;
2360}
2361
2362
2363static void wpas_stop_listen(void *ctx)
2364{
2365 struct wpa_supplicant *wpa_s = ctx;
2366 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2367 wpa_drv_cancel_remain_on_channel(wpa_s);
2368 wpa_s->off_channel_freq = 0;
2369 wpa_s->roc_waiting_drv_freq = 0;
2370 }
2371 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2372 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002373 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374}
2375
2376
2377static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2378{
2379 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002380 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381}
2382
2383
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002384/*
2385 * DNS Header section is used only to calculate compression pointers, so the
2386 * contents of this data does not matter, but the length needs to be reserved
2387 * in the virtual packet.
2388 */
2389#define DNS_HEADER_LEN 12
2390
2391/*
2392 * 27-octet in-memory packet from P2P specification containing two implied
2393 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2394 */
2395#define P2P_SD_IN_MEMORY_LEN 27
2396
2397static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2398 u8 **spos, const u8 *end)
2399{
2400 while (*spos < end) {
2401 u8 val = ((*spos)[0] & 0xc0) >> 6;
2402 int len;
2403
2404 if (val == 1 || val == 2) {
2405 /* These are reserved values in RFC 1035 */
2406 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2407 "sequence starting with 0x%x", val);
2408 return -1;
2409 }
2410
2411 if (val == 3) {
2412 u16 offset;
2413 u8 *spos_tmp;
2414
2415 /* Offset */
2416 if (*spos + 2 > end) {
2417 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2418 "DNS offset field");
2419 return -1;
2420 }
2421
2422 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2423 if (offset >= *spos - start) {
2424 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2425 "pointer offset %u", offset);
2426 return -1;
2427 }
2428
2429 (*spos) += 2;
2430 spos_tmp = start + offset;
2431 return p2p_sd_dns_uncompress_label(upos, uend, start,
2432 &spos_tmp,
2433 *spos - 2);
2434 }
2435
2436 /* Label */
2437 len = (*spos)[0] & 0x3f;
2438 if (len == 0)
2439 return 0;
2440
2441 (*spos)++;
2442 if (*spos + len > end) {
2443 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2444 "sequence - no room for label with length "
2445 "%u", len);
2446 return -1;
2447 }
2448
2449 if (*upos + len + 2 > uend)
2450 return -2;
2451
2452 os_memcpy(*upos, *spos, len);
2453 *spos += len;
2454 *upos += len;
2455 (*upos)[0] = '.';
2456 (*upos)++;
2457 (*upos)[0] = '\0';
2458 }
2459
2460 return 0;
2461}
2462
2463
2464/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2465 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2466 * not large enough */
2467static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2468 size_t msg_len, size_t offset)
2469{
2470 /* 27-octet in-memory packet from P2P specification */
2471 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2472 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2473 u8 *tmp, *end, *spos;
2474 char *upos, *uend;
2475 int ret = 0;
2476
2477 if (buf_len < 2)
2478 return -1;
2479 if (offset > msg_len)
2480 return -1;
2481
2482 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2483 if (tmp == NULL)
2484 return -1;
2485 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2486 end = spos + msg_len;
2487 spos += offset;
2488
2489 os_memset(tmp, 0, DNS_HEADER_LEN);
2490 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2491 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2492
2493 upos = buf;
2494 uend = buf + buf_len;
2495
2496 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2497 if (ret) {
2498 os_free(tmp);
2499 return ret;
2500 }
2501
2502 if (upos == buf) {
2503 upos[0] = '.';
2504 upos[1] = '\0';
2505 } else if (upos[-1] == '.')
2506 upos[-1] = '\0';
2507
2508 os_free(tmp);
2509 return 0;
2510}
2511
2512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513static struct p2p_srv_bonjour *
2514wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2515 const struct wpabuf *query)
2516{
2517 struct p2p_srv_bonjour *bsrv;
2518 size_t len;
2519
2520 len = wpabuf_len(query);
2521 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2522 struct p2p_srv_bonjour, list) {
2523 if (len == wpabuf_len(bsrv->query) &&
2524 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2525 len) == 0)
2526 return bsrv;
2527 }
2528 return NULL;
2529}
2530
2531
2532static struct p2p_srv_upnp *
2533wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2534 const char *service)
2535{
2536 struct p2p_srv_upnp *usrv;
2537
2538 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2539 struct p2p_srv_upnp, list) {
2540 if (version == usrv->version &&
2541 os_strcmp(service, usrv->service) == 0)
2542 return usrv;
2543 }
2544 return NULL;
2545}
2546
2547
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002548static void wpas_sd_add_empty(struct wpabuf *resp, u8 srv_proto,
2549 u8 srv_trans_id, u8 status)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002550{
2551 u8 *len_pos;
2552
2553 if (wpabuf_tailroom(resp) < 5)
2554 return;
2555
2556 /* Length (to be filled) */
2557 len_pos = wpabuf_put(resp, 2);
2558 wpabuf_put_u8(resp, srv_proto);
2559 wpabuf_put_u8(resp, srv_trans_id);
2560 /* Status Code */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002561 wpabuf_put_u8(resp, status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 /* Response Data: empty */
2563 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2564}
2565
2566
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002567static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2568 u8 srv_trans_id)
2569{
2570 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2571 P2P_SD_PROTO_NOT_AVAILABLE);
2572}
2573
2574
2575static void wpas_sd_add_bad_request(struct wpabuf *resp, u8 srv_proto,
2576 u8 srv_trans_id)
2577{
2578 wpas_sd_add_empty(resp, srv_proto, srv_trans_id, P2P_SD_BAD_REQUEST);
2579}
2580
2581
2582static void wpas_sd_add_not_found(struct wpabuf *resp, u8 srv_proto,
2583 u8 srv_trans_id)
2584{
2585 wpas_sd_add_empty(resp, srv_proto, srv_trans_id,
2586 P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2587}
2588
2589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2591 struct wpabuf *resp, u8 srv_trans_id)
2592{
2593 struct p2p_srv_bonjour *bsrv;
2594 u8 *len_pos;
2595
2596 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2597
2598 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2599 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2600 return;
2601 }
2602
2603 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2604 struct p2p_srv_bonjour, list) {
2605 if (wpabuf_tailroom(resp) <
2606 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2607 return;
2608 /* Length (to be filled) */
2609 len_pos = wpabuf_put(resp, 2);
2610 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2611 wpabuf_put_u8(resp, srv_trans_id);
2612 /* Status Code */
2613 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2614 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2615 wpabuf_head(bsrv->resp),
2616 wpabuf_len(bsrv->resp));
2617 /* Response Data */
2618 wpabuf_put_buf(resp, bsrv->query); /* Key */
2619 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2620 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2621 2);
2622 }
2623}
2624
2625
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002626static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2627 size_t query_len)
2628{
2629 char str_rx[256], str_srv[256];
2630
2631 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2632 return 0; /* Too short to include DNS Type and Version */
2633 if (os_memcmp(query + query_len - 3,
2634 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2635 3) != 0)
2636 return 0; /* Mismatch in DNS Type or Version */
2637 if (query_len == wpabuf_len(bsrv->query) &&
2638 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2639 return 1; /* Binary match */
2640
2641 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2642 0))
2643 return 0; /* Failed to uncompress query */
2644 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2645 wpabuf_head(bsrv->query),
2646 wpabuf_len(bsrv->query) - 3, 0))
2647 return 0; /* Failed to uncompress service */
2648
2649 return os_strcmp(str_rx, str_srv) == 0;
2650}
2651
2652
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2654 struct wpabuf *resp, u8 srv_trans_id,
2655 const u8 *query, size_t query_len)
2656{
2657 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002659 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660
2661 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2662 query, query_len);
2663 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2664 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2665 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2666 srv_trans_id);
2667 return;
2668 }
2669
2670 if (query_len == 0) {
2671 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2672 return;
2673 }
2674
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002675 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2676 struct p2p_srv_bonjour, list) {
2677 if (!match_bonjour_query(bsrv, query, query_len))
2678 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002679
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002680 if (wpabuf_tailroom(resp) <
2681 5 + query_len + wpabuf_len(bsrv->resp))
2682 return;
2683
2684 matches++;
2685
2686 /* Length (to be filled) */
2687 len_pos = wpabuf_put(resp, 2);
2688 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2689 wpabuf_put_u8(resp, srv_trans_id);
2690
2691 /* Status Code */
2692 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2693 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2694 wpabuf_head(bsrv->resp),
2695 wpabuf_len(bsrv->resp));
2696
2697 /* Response Data */
2698 wpabuf_put_data(resp, query, query_len); /* Key */
2699 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2700
2701 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2702 }
2703
2704 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2706 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002707 if (wpabuf_tailroom(resp) < 5)
2708 return;
2709
2710 /* Length (to be filled) */
2711 len_pos = wpabuf_put(resp, 2);
2712 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2713 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714
2715 /* Status Code */
2716 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2717 /* Response Data: empty */
2718 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2719 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721}
2722
2723
2724static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2725 struct wpabuf *resp, u8 srv_trans_id)
2726{
2727 struct p2p_srv_upnp *usrv;
2728 u8 *len_pos;
2729
2730 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2731
2732 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2733 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2734 return;
2735 }
2736
2737 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2738 struct p2p_srv_upnp, list) {
2739 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2740 return;
2741
2742 /* Length (to be filled) */
2743 len_pos = wpabuf_put(resp, 2);
2744 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2745 wpabuf_put_u8(resp, srv_trans_id);
2746
2747 /* Status Code */
2748 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2749 /* Response Data */
2750 wpabuf_put_u8(resp, usrv->version);
2751 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2752 usrv->service);
2753 wpabuf_put_str(resp, usrv->service);
2754 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2755 2);
2756 }
2757}
2758
2759
2760static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2761 struct wpabuf *resp, u8 srv_trans_id,
2762 const u8 *query, size_t query_len)
2763{
2764 struct p2p_srv_upnp *usrv;
2765 u8 *len_pos;
2766 u8 version;
2767 char *str;
2768 int count = 0;
2769
2770 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2771 query, query_len);
2772
2773 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2774 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2775 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2776 srv_trans_id);
2777 return;
2778 }
2779
2780 if (query_len == 0) {
2781 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2782 return;
2783 }
2784
2785 if (wpabuf_tailroom(resp) < 5)
2786 return;
2787
2788 /* Length (to be filled) */
2789 len_pos = wpabuf_put(resp, 2);
2790 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2791 wpabuf_put_u8(resp, srv_trans_id);
2792
2793 version = query[0];
2794 str = os_malloc(query_len);
2795 if (str == NULL)
2796 return;
2797 os_memcpy(str, query + 1, query_len - 1);
2798 str[query_len - 1] = '\0';
2799
2800 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2801 struct p2p_srv_upnp, list) {
2802 if (version != usrv->version)
2803 continue;
2804
2805 if (os_strcmp(str, "ssdp:all") != 0 &&
2806 os_strstr(usrv->service, str) == NULL)
2807 continue;
2808
2809 if (wpabuf_tailroom(resp) < 2)
2810 break;
2811 if (count == 0) {
2812 /* Status Code */
2813 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2814 /* Response Data */
2815 wpabuf_put_u8(resp, version);
2816 } else
2817 wpabuf_put_u8(resp, ',');
2818
2819 count++;
2820
2821 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2822 usrv->service);
2823 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2824 break;
2825 wpabuf_put_str(resp, usrv->service);
2826 }
2827 os_free(str);
2828
2829 if (count == 0) {
2830 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2831 "available");
2832 /* Status Code */
2833 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2834 /* Response Data: empty */
2835 }
2836
2837 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2838}
2839
2840
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002841#ifdef CONFIG_WIFI_DISPLAY
2842static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2843 struct wpabuf *resp, u8 srv_trans_id,
2844 const u8 *query, size_t query_len)
2845{
2846 const u8 *pos;
2847 u8 role;
2848 u8 *len_pos;
2849
2850 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2851
2852 if (!wpa_s->global->wifi_display) {
2853 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2854 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2855 srv_trans_id);
2856 return;
2857 }
2858
2859 if (query_len < 1) {
2860 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2861 "Role");
2862 return;
2863 }
2864
2865 if (wpabuf_tailroom(resp) < 5)
2866 return;
2867
2868 pos = query;
2869 role = *pos++;
2870 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2871
2872 /* TODO: role specific handling */
2873
2874 /* Length (to be filled) */
2875 len_pos = wpabuf_put(resp, 2);
2876 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2877 wpabuf_put_u8(resp, srv_trans_id);
2878 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2879
2880 while (pos < query + query_len) {
2881 if (*pos < MAX_WFD_SUBELEMS &&
2882 wpa_s->global->wfd_subelem[*pos] &&
2883 wpabuf_tailroom(resp) >=
2884 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2885 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2886 "subelement %u", *pos);
2887 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2888 }
2889 pos++;
2890 }
2891
2892 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2893}
2894#endif /* CONFIG_WIFI_DISPLAY */
2895
2896
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002897static int find_p2ps_substr(struct p2ps_advertisement *adv_data,
2898 const u8 *needle, size_t needle_len)
2899{
2900 const u8 *haystack = (const u8 *) adv_data->svc_info;
2901 size_t haystack_len, i;
2902
2903 /* Allow search term to be empty */
2904 if (!needle || !needle_len)
2905 return 1;
2906
2907 if (!haystack)
2908 return 0;
2909
2910 haystack_len = os_strlen(adv_data->svc_info);
2911 for (i = 0; i < haystack_len; i++) {
2912 if (haystack_len - i < needle_len)
2913 break;
2914 if (os_memcmp(haystack + i, needle, needle_len) == 0)
2915 return 1;
2916 }
2917
2918 return 0;
2919}
2920
2921
2922static void wpas_sd_req_asp(struct wpa_supplicant *wpa_s,
2923 struct wpabuf *resp, u8 srv_trans_id,
2924 const u8 *query, size_t query_len)
2925{
2926 struct p2ps_advertisement *adv_data;
2927 const u8 *svc = &query[1];
2928 const u8 *info = NULL;
2929 size_t svc_len = query[0];
2930 size_t info_len = 0;
2931 int prefix = 0;
2932 u8 *count_pos = NULL;
2933 u8 *len_pos = NULL;
2934
2935 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for ASP", query, query_len);
2936
2937 if (!wpa_s->global->p2p) {
2938 wpa_printf(MSG_DEBUG, "P2P: ASP protocol not available");
2939 wpas_sd_add_proto_not_avail(resp, P2P_SERV_P2PS, srv_trans_id);
2940 return;
2941 }
2942
2943 /* Info block is optional */
2944 if (svc_len + 1 < query_len) {
2945 info = &svc[svc_len];
2946 info_len = *info++;
2947 }
2948
2949 /* Range check length of svc string and info block */
2950 if (svc_len + (info_len ? info_len + 2 : 1) > query_len) {
2951 wpa_printf(MSG_DEBUG, "P2P: ASP bad request");
2952 wpas_sd_add_bad_request(resp, P2P_SERV_P2PS, srv_trans_id);
2953 return;
2954 }
2955
2956 /* Detect and correct for prefix search */
2957 if (svc_len && svc[svc_len - 1] == '*') {
2958 prefix = 1;
2959 svc_len--;
2960 }
2961
2962 for (adv_data = p2p_get_p2ps_adv_list(wpa_s->global->p2p);
2963 adv_data; adv_data = adv_data->next) {
2964 /* If not a prefix match, reject length mismatches */
2965 if (!prefix && svc_len != os_strlen(adv_data->svc_name))
2966 continue;
2967
2968 /* Search each service for request */
2969 if (os_memcmp(adv_data->svc_name, svc, svc_len) == 0 &&
2970 find_p2ps_substr(adv_data, info, info_len)) {
2971 size_t len = os_strlen(adv_data->svc_name);
2972 size_t svc_info_len = 0;
2973
2974 if (adv_data->svc_info)
2975 svc_info_len = os_strlen(adv_data->svc_info);
2976
2977 if (len > 0xff || svc_info_len > 0xffff)
2978 return;
2979
2980 /* Length & Count to be filled as we go */
2981 if (!len_pos && !count_pos) {
2982 if (wpabuf_tailroom(resp) <
2983 len + svc_info_len + 16)
2984 return;
2985
2986 len_pos = wpabuf_put(resp, 2);
2987 wpabuf_put_u8(resp, P2P_SERV_P2PS);
2988 wpabuf_put_u8(resp, srv_trans_id);
2989 /* Status Code */
2990 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2991 count_pos = wpabuf_put(resp, 1);
2992 *count_pos = 0;
2993 } else if (wpabuf_tailroom(resp) <
2994 len + svc_info_len + 10)
2995 return;
2996
2997 if (svc_info_len) {
2998 wpa_printf(MSG_DEBUG,
2999 "P2P: Add Svc: %s info: %s",
3000 adv_data->svc_name,
3001 adv_data->svc_info);
3002 } else {
3003 wpa_printf(MSG_DEBUG, "P2P: Add Svc: %s",
3004 adv_data->svc_name);
3005 }
3006
3007 /* Advertisement ID */
3008 wpabuf_put_le32(resp, adv_data->id);
3009
3010 /* Config Methods */
3011 wpabuf_put_be16(resp, adv_data->config_methods);
3012
3013 /* Service Name */
3014 wpabuf_put_u8(resp, (u8) len);
3015 wpabuf_put_data(resp, adv_data->svc_name, len);
3016
3017 /* Service State */
3018 wpabuf_put_u8(resp, adv_data->state);
3019
3020 /* Service Information */
3021 wpabuf_put_le16(resp, (u16) svc_info_len);
3022 wpabuf_put_data(resp, adv_data->svc_info, svc_info_len);
3023
3024 /* Update length and count */
3025 (*count_pos)++;
3026 WPA_PUT_LE16(len_pos,
3027 (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
3028 }
3029 }
3030
3031 /* Return error if no matching svc found */
3032 if (count_pos == NULL) {
3033 wpa_printf(MSG_DEBUG, "P2P: ASP service not found");
3034 wpas_sd_add_not_found(resp, P2P_SERV_P2PS, srv_trans_id);
3035 }
3036}
3037
3038
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003039static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
3040 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041{
3042 struct wpa_supplicant *wpa_s = ctx;
3043 const u8 *pos = tlvs;
3044 const u8 *end = tlvs + tlvs_len;
3045 const u8 *tlv_end;
3046 u16 slen;
3047 struct wpabuf *resp;
3048 u8 srv_proto, srv_trans_id;
3049 size_t buf_len;
3050 char *buf;
3051
3052 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
3053 tlvs, tlvs_len);
3054 buf_len = 2 * tlvs_len + 1;
3055 buf = os_malloc(buf_len);
3056 if (buf) {
3057 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3058 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
3059 MACSTR " %u %u %s",
3060 freq, MAC2STR(sa), dialog_token, update_indic,
3061 buf);
3062 os_free(buf);
3063 }
3064
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003065 if (wpa_s->p2p_sd_over_ctrl_iface) {
3066 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3067 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003068 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003069 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070
3071 resp = wpabuf_alloc(10000);
3072 if (resp == NULL)
3073 return;
3074
3075 while (pos + 1 < end) {
3076 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
3077 slen = WPA_GET_LE16(pos);
3078 pos += 2;
3079 if (pos + slen > end || slen < 2) {
3080 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
3081 "length");
3082 wpabuf_free(resp);
3083 return;
3084 }
3085 tlv_end = pos + slen;
3086
3087 srv_proto = *pos++;
3088 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3089 srv_proto);
3090 srv_trans_id = *pos++;
3091 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3092 srv_trans_id);
3093
3094 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
3095 pos, tlv_end - pos);
3096
3097
3098 if (wpa_s->force_long_sd) {
3099 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
3100 "response");
3101 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3102 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3103 goto done;
3104 }
3105
3106 switch (srv_proto) {
3107 case P2P_SERV_ALL_SERVICES:
3108 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
3109 "for all services");
3110 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
3111 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
3112 wpa_printf(MSG_DEBUG, "P2P: No service "
3113 "discovery protocols available");
3114 wpas_sd_add_proto_not_avail(
3115 resp, P2P_SERV_ALL_SERVICES,
3116 srv_trans_id);
3117 break;
3118 }
3119 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
3120 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
3121 break;
3122 case P2P_SERV_BONJOUR:
3123 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
3124 pos, tlv_end - pos);
3125 break;
3126 case P2P_SERV_UPNP:
3127 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
3128 pos, tlv_end - pos);
3129 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003130#ifdef CONFIG_WIFI_DISPLAY
3131 case P2P_SERV_WIFI_DISPLAY:
3132 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
3133 pos, tlv_end - pos);
3134 break;
3135#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003136 case P2P_SERV_P2PS:
3137 wpas_sd_req_asp(wpa_s, resp, srv_trans_id,
3138 pos, tlv_end - pos);
3139 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 default:
3141 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
3142 "protocol %u", srv_proto);
3143 wpas_sd_add_proto_not_avail(resp, srv_proto,
3144 srv_trans_id);
3145 break;
3146 }
3147
3148 pos = tlv_end;
3149 }
3150
3151done:
3152 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
3153 update_indic, tlvs, tlvs_len);
3154
3155 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
3156
3157 wpabuf_free(resp);
3158}
3159
3160
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003161static void wpas_sd_p2ps_serv_response(struct wpa_supplicant *wpa_s,
3162 const u8 *sa, u8 srv_trans_id,
3163 const u8 *pos, const u8 *tlv_end)
3164{
3165 u8 left = *pos++;
3166 u32 adv_id;
3167 u8 svc_status;
3168 u16 config_methods;
3169 char svc_str[256];
3170
3171 while (left-- && pos < tlv_end) {
3172 char *buf = NULL;
3173 size_t buf_len;
3174 u8 svc_len;
3175
3176 /* Sanity check fixed length+svc_str */
3177 if (pos + 6 >= tlv_end)
3178 break;
3179 svc_len = pos[6];
3180 if (pos + svc_len + 10 > tlv_end)
3181 break;
3182
3183 /* Advertisement ID */
3184 adv_id = WPA_GET_LE32(pos);
3185 pos += sizeof(u32);
3186
3187 /* Config Methods */
3188 config_methods = WPA_GET_BE16(pos);
3189 pos += sizeof(u16);
3190
3191 /* Service Name */
3192 pos++; /* svc_len */
3193 os_memcpy(svc_str, pos, svc_len);
3194 svc_str[svc_len] = '\0';
3195 pos += svc_len;
3196
3197 /* Service Status */
3198 svc_status = *pos++;
3199
3200 /* Service Information Length */
3201 buf_len = WPA_GET_LE16(pos);
3202 pos += sizeof(u16);
3203
3204 /* Sanity check buffer length */
3205 if (buf_len > (unsigned int) (tlv_end - pos))
3206 break;
3207
3208 if (buf_len) {
3209 buf = os_zalloc(2 * buf_len + 1);
3210 if (buf) {
3211 utf8_escape((const char *) pos, buf_len, buf,
3212 2 * buf_len + 1);
3213 }
3214 }
3215
3216 pos += buf_len;
3217
3218 if (buf) {
3219 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3220 MACSTR " %x %x %x %x %s '%s'",
3221 MAC2STR(sa), srv_trans_id, adv_id,
3222 svc_status, config_methods, svc_str,
3223 buf);
3224 os_free(buf);
3225 } else {
3226 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_SERV_ASP_RESP
3227 MACSTR " %x %x %x %x %s",
3228 MAC2STR(sa), srv_trans_id, adv_id,
3229 svc_status, config_methods, svc_str);
3230 }
3231 }
3232}
3233
3234
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003235static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
3236 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237{
3238 struct wpa_supplicant *wpa_s = ctx;
3239 const u8 *pos = tlvs;
3240 const u8 *end = tlvs + tlvs_len;
3241 const u8 *tlv_end;
3242 u16 slen;
3243 size_t buf_len;
3244 char *buf;
3245
3246 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
3247 tlvs, tlvs_len);
3248 if (tlvs_len > 1500) {
3249 /* TODO: better way for handling this */
3250 wpa_msg_ctrl(wpa_s, MSG_INFO,
3251 P2P_EVENT_SERV_DISC_RESP MACSTR
3252 " %u <long response: %u bytes>",
3253 MAC2STR(sa), update_indic,
3254 (unsigned int) tlvs_len);
3255 } else {
3256 buf_len = 2 * tlvs_len + 1;
3257 buf = os_malloc(buf_len);
3258 if (buf) {
3259 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
3260 wpa_msg_ctrl(wpa_s, MSG_INFO,
3261 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
3262 MAC2STR(sa), update_indic, buf);
3263 os_free(buf);
3264 }
3265 }
3266
3267 while (pos < end) {
3268 u8 srv_proto, srv_trans_id, status;
3269
3270 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
3271 slen = WPA_GET_LE16(pos);
3272 pos += 2;
3273 if (pos + slen > end || slen < 3) {
3274 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
3275 "length");
3276 return;
3277 }
3278 tlv_end = pos + slen;
3279
3280 srv_proto = *pos++;
3281 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
3282 srv_proto);
3283 srv_trans_id = *pos++;
3284 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
3285 srv_trans_id);
3286 status = *pos++;
3287 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
3288 status);
3289
3290 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
3291 pos, tlv_end - pos);
3292
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003293 if (srv_proto == P2P_SERV_P2PS && pos < tlv_end) {
3294 wpas_sd_p2ps_serv_response(wpa_s, sa, srv_trans_id,
3295 pos, tlv_end);
3296 }
3297
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003298 pos = tlv_end;
3299 }
3300
3301 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
3302}
3303
3304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003305u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
3306 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003309 return 0;
3310 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311}
3312
3313
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003314u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
3315 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003316{
3317 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003318 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319
3320 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
3321 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003322 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003323 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
3324 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
3325 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
3326 wpabuf_put_u8(tlvs, version);
3327 wpabuf_put_str(tlvs, query);
3328 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3329 wpabuf_free(tlvs);
3330 return ret;
3331}
3332
3333
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003334u64 wpas_p2p_sd_request_asp(struct wpa_supplicant *wpa_s, const u8 *dst, u8 id,
3335 const char *svc_str, const char *info_substr)
3336{
3337 struct wpabuf *tlvs;
3338 size_t plen, svc_len, substr_len = 0;
3339 u64 ret;
3340
3341 svc_len = os_strlen(svc_str);
3342 if (info_substr)
3343 substr_len = os_strlen(info_substr);
3344
3345 if (svc_len > 0xff || substr_len > 0xff)
3346 return 0;
3347
3348 plen = 1 + 1 + 1 + svc_len + 1 + substr_len;
3349 tlvs = wpabuf_alloc(2 + plen);
3350 if (tlvs == NULL)
3351 return 0;
3352
3353 wpabuf_put_le16(tlvs, plen);
3354 wpabuf_put_u8(tlvs, P2P_SERV_P2PS);
3355 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3356 wpabuf_put_u8(tlvs, (u8) svc_len); /* Service String Length */
3357 wpabuf_put_data(tlvs, svc_str, svc_len);
3358 wpabuf_put_u8(tlvs, (u8) substr_len); /* Info Substring Length */
3359 wpabuf_put_data(tlvs, info_substr, substr_len);
3360 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
3361 wpabuf_free(tlvs);
3362
3363 return ret;
3364}
3365
3366
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003367#ifdef CONFIG_WIFI_DISPLAY
3368
3369static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
3370 const struct wpabuf *tlvs)
3371{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003372 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3373 return 0;
3374 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
3375}
3376
3377
3378#define MAX_WFD_SD_SUBELEMS 20
3379
3380static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
3381 const char *subelems)
3382{
3383 u8 *len;
3384 const char *pos;
3385 int val;
3386 int count = 0;
3387
3388 len = wpabuf_put(tlvs, 2);
3389 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
3390 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
3391
3392 wpabuf_put_u8(tlvs, role);
3393
3394 pos = subelems;
3395 while (*pos) {
3396 val = atoi(pos);
3397 if (val >= 0 && val < 256) {
3398 wpabuf_put_u8(tlvs, val);
3399 count++;
3400 if (count == MAX_WFD_SD_SUBELEMS)
3401 break;
3402 }
3403 pos = os_strchr(pos + 1, ',');
3404 if (pos == NULL)
3405 break;
3406 pos++;
3407 }
3408
3409 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
3410}
3411
3412
3413u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
3414 const u8 *dst, const char *role)
3415{
3416 struct wpabuf *tlvs;
3417 u64 ret;
3418 const char *subelems;
3419 u8 id = 1;
3420
3421 subelems = os_strchr(role, ' ');
3422 if (subelems == NULL)
3423 return 0;
3424 subelems++;
3425
3426 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
3427 if (tlvs == NULL)
3428 return 0;
3429
3430 if (os_strstr(role, "[source]"))
3431 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
3432 if (os_strstr(role, "[pri-sink]"))
3433 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
3434 if (os_strstr(role, "[sec-sink]"))
3435 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
3436 if (os_strstr(role, "[source+sink]"))
3437 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
3438
3439 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
3440 wpabuf_free(tlvs);
3441 return ret;
3442}
3443
3444#endif /* CONFIG_WIFI_DISPLAY */
3445
3446
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003447int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003449 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3450 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003451 return p2p_sd_cancel_request(wpa_s->global->p2p,
3452 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003453}
3454
3455
3456void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
3457 const u8 *dst, u8 dialog_token,
3458 const struct wpabuf *resp_tlvs)
3459{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003460 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3461 return;
3462 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
3463 resp_tlvs);
3464}
3465
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07003466
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003467void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
3468{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 if (wpa_s->global->p2p)
3470 p2p_sd_service_update(wpa_s->global->p2p);
3471}
3472
3473
3474static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
3475{
3476 dl_list_del(&bsrv->list);
3477 wpabuf_free(bsrv->query);
3478 wpabuf_free(bsrv->resp);
3479 os_free(bsrv);
3480}
3481
3482
3483static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
3484{
3485 dl_list_del(&usrv->list);
3486 os_free(usrv->service);
3487 os_free(usrv);
3488}
3489
3490
3491void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
3492{
3493 struct p2p_srv_bonjour *bsrv, *bn;
3494 struct p2p_srv_upnp *usrv, *un;
3495
3496 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
3497 struct p2p_srv_bonjour, list)
3498 wpas_p2p_srv_bonjour_free(bsrv);
3499
3500 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
3501 struct p2p_srv_upnp, list)
3502 wpas_p2p_srv_upnp_free(usrv);
3503
3504 wpas_p2p_sd_service_update(wpa_s);
3505}
3506
3507
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003508int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant *wpa_s, u32 adv_id)
3509{
3510 if (adv_id == 0)
3511 return 1;
3512
3513 if (p2p_service_p2ps_id(wpa_s->global->p2p, adv_id))
3514 return 1;
3515
3516 return 0;
3517}
3518
3519
3520int wpas_p2p_service_del_asp(struct wpa_supplicant *wpa_s, u32 adv_id)
3521{
3522 return p2p_service_del_asp(wpa_s->global->p2p, adv_id);
3523}
3524
3525
3526int wpas_p2p_service_add_asp(struct wpa_supplicant *wpa_s,
3527 int auto_accept, u32 adv_id,
3528 const char *adv_str, u8 svc_state,
3529 u16 config_methods, const char *svc_info)
3530{
3531 return p2p_service_add_asp(wpa_s->global->p2p, auto_accept, adv_id,
3532 adv_str, svc_state, config_methods,
3533 svc_info);
3534}
3535
3536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003537int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
3538 struct wpabuf *query, struct wpabuf *resp)
3539{
3540 struct p2p_srv_bonjour *bsrv;
3541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 bsrv = os_zalloc(sizeof(*bsrv));
3543 if (bsrv == NULL)
3544 return -1;
3545 bsrv->query = query;
3546 bsrv->resp = resp;
3547 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
3548
3549 wpas_p2p_sd_service_update(wpa_s);
3550 return 0;
3551}
3552
3553
3554int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
3555 const struct wpabuf *query)
3556{
3557 struct p2p_srv_bonjour *bsrv;
3558
3559 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
3560 if (bsrv == NULL)
3561 return -1;
3562 wpas_p2p_srv_bonjour_free(bsrv);
3563 wpas_p2p_sd_service_update(wpa_s);
3564 return 0;
3565}
3566
3567
3568int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
3569 const char *service)
3570{
3571 struct p2p_srv_upnp *usrv;
3572
3573 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
3574 return 0; /* Already listed */
3575 usrv = os_zalloc(sizeof(*usrv));
3576 if (usrv == NULL)
3577 return -1;
3578 usrv->version = version;
3579 usrv->service = os_strdup(service);
3580 if (usrv->service == NULL) {
3581 os_free(usrv);
3582 return -1;
3583 }
3584 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
3585
3586 wpas_p2p_sd_service_update(wpa_s);
3587 return 0;
3588}
3589
3590
3591int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
3592 const char *service)
3593{
3594 struct p2p_srv_upnp *usrv;
3595
3596 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
3597 if (usrv == NULL)
3598 return -1;
3599 wpas_p2p_srv_upnp_free(usrv);
3600 wpas_p2p_sd_service_update(wpa_s);
3601 return 0;
3602}
3603
3604
3605static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3606 const u8 *peer, const char *params,
3607 unsigned int generated_pin)
3608{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003609 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3610 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003611}
3612
3613
3614static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3615 const u8 *peer, const char *params)
3616{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003617 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3618 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003619}
3620
3621
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003622static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3623 const u8 *dev_addr, const u8 *pri_dev_type,
3624 const char *dev_name, u16 supp_config_methods,
3625 u8 dev_capab, u8 group_capab, const u8 *group_id,
3626 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003627{
3628 struct wpa_supplicant *wpa_s = ctx;
3629 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003630 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631 u8 empty_dev_type[8];
3632 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003633 struct wpa_supplicant *group = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003634 int res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003635
3636 if (group_id) {
3637 for (group = wpa_s->global->ifaces; group; group = group->next)
3638 {
3639 struct wpa_ssid *s = group->current_ssid;
3640 if (s != NULL &&
3641 s->mode == WPAS_MODE_P2P_GO &&
3642 group_id_len - ETH_ALEN == s->ssid_len &&
3643 os_memcmp(group_id + ETH_ALEN, s->ssid,
3644 s->ssid_len) == 0)
3645 break;
3646 }
3647 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648
3649 if (pri_dev_type == NULL) {
3650 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3651 pri_dev_type = empty_dev_type;
3652 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003653 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3654 " pri_dev_type=%s name='%s' config_methods=0x%x "
3655 "dev_capab=0x%x group_capab=0x%x%s%s",
3656 MAC2STR(dev_addr),
3657 wps_dev_type_bin2str(pri_dev_type, devtype,
3658 sizeof(devtype)),
3659 dev_name, supp_config_methods, dev_capab, group_capab,
3660 group ? " group=" : "",
3661 group ? group->ifname : "");
3662 if (os_snprintf_error(sizeof(params), res))
3663 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003665
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 if (config_methods & WPS_CONFIG_DISPLAY) {
3667 generated_pin = wps_generate_pin();
3668 wpas_prov_disc_local_display(wpa_s, peer, params,
3669 generated_pin);
3670 } else if (config_methods & WPS_CONFIG_KEYPAD)
3671 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3672 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003673 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3674 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07003675
3676 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3677 P2P_PROV_DISC_SUCCESS,
3678 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003679}
3680
3681
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003682static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683{
3684 struct wpa_supplicant *wpa_s = ctx;
3685 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003686 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003687
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003688 if (wpa_s->pending_pd_before_join &&
3689 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3690 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3691 wpa_s->pending_pd_before_join = 0;
3692 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3693 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003694 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003695 return;
3696 }
3697
Dmitry Shmidt04949592012-07-19 12:16:46 -07003698 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003699 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3700 int res;
3701
3702 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3703 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3704 if (os_snprintf_error(sizeof(params), res))
3705 params[sizeof(params) - 1] = '\0';
3706 } else
Dmitry Shmidt04949592012-07-19 12:16:46 -07003707 params[0] = '\0';
3708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003709 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07003710 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003711 else if (config_methods & WPS_CONFIG_KEYPAD) {
3712 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07003713 wpas_prov_disc_local_display(wpa_s, peer, params,
3714 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003716 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3717 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718
Jouni Malinen75ecf522011-06-27 15:19:46 -07003719 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3720 P2P_PROV_DISC_SUCCESS,
3721 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722}
3723
3724
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003725static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003726 enum p2p_prov_disc_status status,
3727 u32 adv_id, const u8 *adv_mac,
3728 const char *deferred_session_resp)
Jouni Malinen75ecf522011-06-27 15:19:46 -07003729{
3730 struct wpa_supplicant *wpa_s = ctx;
3731
Dmitry Shmidt04949592012-07-19 12:16:46 -07003732 if (wpa_s->p2p_fallback_to_go_neg) {
3733 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3734 "failed - fall back to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003735 wpa_msg_global(wpa_s->parent, MSG_INFO,
3736 P2P_EVENT_FALLBACK_TO_GO_NEG
3737 "reason=PD-failed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003738 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3739 return;
3740 }
3741
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003742 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003743 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003744 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3745 "join-existing-group operation (no ACK for PD "
3746 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003747 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003748 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07003749 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003750
Dmitry Shmidt216983b2015-02-06 10:50:36 -08003751 if (adv_id && adv_mac && deferred_session_resp) {
3752 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3753 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x"
3754 " deferred_session_resp='%s'",
3755 MAC2STR(peer), status, adv_id,
3756 deferred_session_resp);
3757 } else if (adv_id && adv_mac) {
3758 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3759 " p2p_dev_addr=" MACSTR " status=%d adv_id=%x",
3760 MAC2STR(peer), status, adv_id);
3761 } else {
3762 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3763 " p2p_dev_addr=" MACSTR " status=%d",
3764 MAC2STR(peer), status);
3765 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003766
Jouni Malinen75ecf522011-06-27 15:19:46 -07003767 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3768 status, 0, 0);
3769}
3770
3771
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003772static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3773{
3774 if (channels == NULL)
3775 return 1; /* Assume no restrictions */
3776 return p2p_channels_includes_freq(channels, freq);
3777
3778}
3779
3780
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003781/**
3782 * Pick the best frequency to use from all the currently used frequencies.
3783 */
3784static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3785 struct wpa_used_freq_data *freqs,
3786 unsigned int num)
3787{
3788 unsigned int i, c;
3789
3790 /* find a candidate freq that is supported by P2P */
3791 for (c = 0; c < num; c++)
3792 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3793 break;
3794
3795 if (c == num)
3796 return 0;
3797
3798 /* once we have a candidate, try to find a 'better' one */
3799 for (i = c + 1; i < num; i++) {
3800 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3801 continue;
3802
3803 /*
3804 * 1. Infrastructure station interfaces have higher preference.
3805 * 2. P2P Clients have higher preference.
3806 * 3. All others.
3807 */
3808 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3809 c = i;
3810 break;
3811 }
3812
3813 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3814 c = i;
3815 }
3816 return freqs[c].freq;
3817}
3818
3819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003820static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3821 const u8 *go_dev_addr, const u8 *ssid,
3822 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003823 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003824 const struct p2p_channels *channels,
3825 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826{
3827 struct wpa_supplicant *wpa_s = ctx;
3828 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003829 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003830 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003831 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832
3833 if (!persistent_group) {
3834 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003835 " to join an active group (SSID: %s)",
3836 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3838 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3839 == 0 ||
3840 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3841 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3842 "authorized invitation");
3843 goto accept_inv;
3844 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003845
3846#ifdef CONFIG_WPS_NFC
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003847 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3848 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003849 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003850 wpa_s->p2p_wps_method = WPS_NFC;
3851 wpa_s->pending_join_wps_method = WPS_NFC;
3852 os_memcpy(wpa_s->pending_join_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003853 go_dev_addr, ETH_ALEN);
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08003854 os_memcpy(wpa_s->pending_join_iface_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003855 bssid, ETH_ALEN);
3856 goto accept_inv;
3857 }
3858#endif /* CONFIG_WPS_NFC */
3859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003860 /*
3861 * Do not accept the invitation automatically; notify user and
3862 * request approval.
3863 */
3864 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3865 }
3866
3867 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3868 if (grp) {
3869 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3870 "running persistent group");
3871 if (*go)
3872 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3873 goto accept_inv;
3874 }
3875
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003876 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3877 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3878 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3879 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003880 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003881 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003882 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3883
3884 for (s = wpa_s->conf->ssid; s; s = s->next) {
3885 if (s->disabled == 2 &&
3886 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3887 s->ssid_len == ssid_len &&
3888 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3889 break;
3890 }
3891
3892 if (!s) {
3893 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3894 " requested reinvocation of an unknown group",
3895 MAC2STR(sa));
3896 return P2P_SC_FAIL_UNKNOWN_GROUP;
3897 }
3898
3899 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3900 *go = 1;
3901 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3902 wpa_printf(MSG_DEBUG, "P2P: The only available "
3903 "interface is already in use - reject "
3904 "invitation");
3905 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3906 }
3907 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3908 } else if (s->mode == WPAS_MODE_P2P_GO) {
3909 *go = 1;
3910 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3911 {
3912 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3913 "interface address for the group");
3914 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3915 }
3916 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3917 ETH_ALEN);
3918 }
3919
3920accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003921 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3922
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003923 best_freq = 0;
3924 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3925 sizeof(struct wpa_used_freq_data));
3926 if (freqs) {
3927 int num_channels = wpa_s->num_multichan_concurrent;
3928 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3929 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3930 os_free(freqs);
3931 }
3932
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003933 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003934 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003935 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003936 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003937
3938 if (wpa_s->num_multichan_concurrent < 2 ||
3939 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3940 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003941 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943 }
3944
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003945 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3946 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003947 if (*go == 0) {
3948 /* We are the client */
3949 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3950 "running a GO but we are capable of MCC, "
3951 "figure out the best channel to use");
3952 *force_freq = 0;
3953 } else if (!freq_included(channels, *force_freq)) {
3954 /* We are the GO, and *force_freq is not in the
3955 * intersection */
3956 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3957 "in intersection but we are capable of MCC, "
3958 "figure out the best channel to use",
3959 *force_freq);
3960 *force_freq = 0;
3961 }
3962 }
3963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 return P2P_SC_SUCCESS;
3965}
3966
3967
3968static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3969 const u8 *ssid, size_t ssid_len,
3970 const u8 *go_dev_addr, u8 status,
3971 int op_freq)
3972{
3973 struct wpa_supplicant *wpa_s = ctx;
3974 struct wpa_ssid *s;
3975
3976 for (s = wpa_s->conf->ssid; s; s = s->next) {
3977 if (s->disabled == 2 &&
3978 s->ssid_len == ssid_len &&
3979 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3980 break;
3981 }
3982
3983 if (status == P2P_SC_SUCCESS) {
3984 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003985 " was accepted; op_freq=%d MHz, SSID=%s",
3986 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003987 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003988 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003990 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003991 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003993 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003995 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003996 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 }
3998 return;
3999 }
4000
4001 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4002 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
4003 " was rejected (status %u)", MAC2STR(sa), status);
4004 return;
4005 }
4006
4007 if (!s) {
4008 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004009 wpa_msg_global(wpa_s, MSG_INFO,
4010 P2P_EVENT_INVITATION_RECEIVED
4011 "sa=" MACSTR " go_dev_addr=" MACSTR
4012 " bssid=" MACSTR " unknown-network",
4013 MAC2STR(sa), MAC2STR(go_dev_addr),
4014 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004015 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004016 wpa_msg_global(wpa_s, MSG_INFO,
4017 P2P_EVENT_INVITATION_RECEIVED
4018 "sa=" MACSTR " go_dev_addr=" MACSTR
4019 " unknown-network",
4020 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 }
4022 return;
4023 }
4024
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004025 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004026 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4027 "sa=" MACSTR " persistent=%d freq=%d",
4028 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004029 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004030 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
4031 "sa=" MACSTR " persistent=%d",
4032 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034}
4035
4036
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004037static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
4038 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004039 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004040{
4041 size_t i;
4042
4043 if (ssid == NULL)
4044 return;
4045
4046 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004047 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004048 ETH_ALEN) == 0)
4049 break;
4050 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07004051 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004052 if (ssid->mode != WPAS_MODE_P2P_GO &&
4053 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
4054 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
4055 "due to invitation result", ssid->id);
4056 wpas_notify_network_removed(wpa_s, ssid);
4057 wpa_config_remove_network(wpa_s->conf, ssid->id);
4058 return;
4059 }
4060 return; /* Peer not found in client list */
4061 }
4062
4063 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004064 "group %d client list%s",
4065 MAC2STR(peer), ssid->id,
4066 inv ? " due to invitation result" : "");
Dmitry Shmidtff787d52015-01-12 13:01:47 -08004067 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
4068 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4069 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004070 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004071 if (wpa_s->parent->conf->update_config &&
4072 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
4073 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004074}
4075
4076
4077static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
4078 const u8 *peer)
4079{
4080 struct wpa_ssid *ssid;
4081
4082 wpa_s = wpa_s->global->p2p_invite_group;
4083 if (wpa_s == NULL)
4084 return; /* No known invitation group */
4085 ssid = wpa_s->current_ssid;
4086 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
4087 !ssid->p2p_persistent_group)
4088 return; /* Not operating as a GO in persistent group */
4089 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
4090 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004091 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004092}
4093
4094
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004095static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004096 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004097 const u8 *peer, int neg_freq,
4098 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004099{
4100 struct wpa_supplicant *wpa_s = ctx;
4101 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07004102 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004103
4104 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004105 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4106 "status=%d " MACSTR,
4107 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004108 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004109 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
4110 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004111 }
4112 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
4113
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004114 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
4115 status, MAC2STR(peer));
4116 if (wpa_s->pending_invite_ssid_id == -1) {
4117 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
4118 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004119 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004121
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004122 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
4123 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
4124 "invitation exchange to indicate readiness for "
4125 "re-invocation");
4126 }
4127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004129 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
4130 ssid = wpa_config_get_network(
4131 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004132 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004133 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004134 wpas_p2p_remove_pending_group_interface(wpa_s);
4135 return;
4136 }
4137
4138 ssid = wpa_config_get_network(wpa_s->conf,
4139 wpa_s->pending_invite_ssid_id);
4140 if (ssid == NULL) {
4141 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
4142 "data matching with invitation");
4143 return;
4144 }
4145
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07004146 /*
4147 * The peer could have missed our ctrl::ack frame for Invitation
4148 * Response and continue retransmitting the frame. To reduce the
4149 * likelihood of the peer not getting successful TX status for the
4150 * Invitation Response frame, wait a short time here before starting
4151 * the persistent group so that we will remain on the current channel to
4152 * acknowledge any possible retransmission from the peer.
4153 */
4154 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
4155 "starting persistent group");
4156 os_sleep(0, 50000);
4157
Dmitry Shmidt15907092014-03-25 10:42:57 -07004158 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
4159 freq_included(channels, neg_freq))
4160 freq = neg_freq;
4161 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
4162 freq_included(channels, peer_oper_freq))
4163 freq = peer_oper_freq;
4164 else
4165 freq = 0;
4166
4167 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
4168 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03004170 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004171 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07004172 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004173 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
4174 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004175 ssid->mode == WPAS_MODE_P2P_GO ?
4176 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4177 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178}
4179
4180
Dmitry Shmidt04949592012-07-19 12:16:46 -07004181static int wpas_p2p_disallowed_freq(struct wpa_global *global,
4182 unsigned int freq)
4183{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004184 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
4185 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07004186 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004187}
4188
4189
4190static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
4191{
4192 reg->channel[reg->channels] = chan;
4193 reg->channels++;
4194}
4195
4196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004198 struct p2p_channels *chan,
4199 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004200{
4201 int i, cla = 0;
4202
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004203 wpa_s->global->p2p_24ghz_social_channels = 1;
4204
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004205 os_memset(cli_chan, 0, sizeof(*cli_chan));
4206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
4208 "band");
4209
4210 /* Operating class 81 - 2.4 GHz band channels 1..13 */
4211 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004212 chan->reg_class[cla].channels = 0;
4213 for (i = 0; i < 11; i++) {
4214 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
4215 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
4216 }
4217 if (chan->reg_class[cla].channels)
4218 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004219
4220 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
4221 "band");
4222
4223 /* Operating class 115 - 5 GHz, channels 36-48 */
4224 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004225 chan->reg_class[cla].channels = 0;
4226 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
4227 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
4228 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
4229 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
4230 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
4231 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
4232 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
4233 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
4234 if (chan->reg_class[cla].channels)
4235 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236
4237 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
4238 "band");
4239
4240 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
4241 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004242 chan->reg_class[cla].channels = 0;
4243 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
4244 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
4245 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
4246 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
4247 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
4248 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
4249 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
4250 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
4251 if (chan->reg_class[cla].channels)
4252 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253
4254 chan->reg_classes = cla;
4255 return 0;
4256}
4257
4258
4259static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
4260 u16 num_modes,
4261 enum hostapd_hw_mode mode)
4262{
4263 u16 i;
4264
4265 for (i = 0; i < num_modes; i++) {
4266 if (modes[i].mode == mode)
4267 return &modes[i];
4268 }
4269
4270 return NULL;
4271}
4272
4273
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004274enum chan_allowed {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004275 NOT_ALLOWED, NO_IR, ALLOWED
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004276};
4277
Dmitry Shmidt04949592012-07-19 12:16:46 -07004278static int has_channel(struct wpa_global *global,
4279 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280{
4281 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004282 unsigned int freq;
4283
4284 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
4285 chan * 5;
4286 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004287 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288
4289 for (i = 0; i < mode->num_channels; i++) {
4290 if (mode->channels[i].chan == chan) {
4291 if (flags)
4292 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004293 if (mode->channels[i].flag &
4294 (HOSTAPD_CHAN_DISABLED |
4295 HOSTAPD_CHAN_RADAR))
4296 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004297 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
4298 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004299 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300 }
4301 }
4302
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004303 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004304}
4305
4306
4307struct p2p_oper_class_map {
4308 enum hostapd_hw_mode mode;
4309 u8 op_class;
4310 u8 min_chan;
4311 u8 max_chan;
4312 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004313 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004314};
4315
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004316static struct p2p_oper_class_map op_class[] = {
4317 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004318#if 0 /* Do not enable HT40 on 2 GHz for now */
4319 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
4320 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
4321#endif
4322 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
4323 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
4324 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
4325 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
4326 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
4327 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004328
4329 /*
4330 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
4331 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
4332 * 80 MHz, but currently use the following definition for simplicity
4333 * (these center frequencies are not actual channels, which makes
4334 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
4335 * removing invalid channels.
4336 */
4337 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004338 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004339 { -1, 0, 0, 0, 0, BW20 }
4340};
4341
4342
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004343static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
4344 struct hostapd_hw_modes *mode,
4345 u8 channel)
4346{
4347 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
4348 unsigned int i;
4349
4350 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
4351 return 0;
4352
4353 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
4354 /*
4355 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
4356 * so the center channel is 6 channels away from the start/end.
4357 */
4358 if (channel >= center_channels[i] - 6 &&
4359 channel <= center_channels[i] + 6)
4360 return center_channels[i];
4361
4362 return 0;
4363}
4364
4365
4366static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
4367 struct hostapd_hw_modes *mode,
4368 u8 channel, u8 bw)
4369{
4370 u8 center_chan;
4371 int i, flags;
4372 enum chan_allowed res, ret = ALLOWED;
4373
4374 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4375 if (!center_chan)
4376 return NOT_ALLOWED;
4377 if (center_chan >= 58 && center_chan <= 138)
4378 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
4379
4380 /* check all the channels are available */
4381 for (i = 0; i < 4; i++) {
4382 int adj_chan = center_chan - 6 + i * 4;
4383
4384 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
4385 if (res == NOT_ALLOWED)
4386 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004387 if (res == NO_IR)
4388 ret = NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004389
4390 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
4391 return NOT_ALLOWED;
4392 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
4393 return NOT_ALLOWED;
4394 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
4395 return NOT_ALLOWED;
4396 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
4397 return NOT_ALLOWED;
4398 }
4399
4400 return ret;
4401}
4402
4403
4404static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
4405 struct hostapd_hw_modes *mode,
4406 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004407{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08004408 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004409 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004410
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004411 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
4412 if (bw == BW40MINUS) {
4413 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
4414 return NOT_ALLOWED;
4415 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
4416 } else if (bw == BW40PLUS) {
4417 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
4418 return NOT_ALLOWED;
4419 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
4420 } else if (bw == BW80) {
4421 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
4422 }
4423
4424 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
4425 return NOT_ALLOWED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004426 if (res == NO_IR || res2 == NO_IR)
4427 return NO_IR;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004428 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004429}
4430
4431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004433 struct p2p_channels *chan,
4434 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004435{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004436 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004437 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004438
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004439 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004440 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
4441 "of all supported channels; assume dualband "
4442 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004443 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 }
4445
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004446 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004447
4448 for (op = 0; op_class[op].op_class; op++) {
4449 struct p2p_oper_class_map *o = &op_class[op];
4450 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004451 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004452
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004453 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004454 if (mode == NULL)
4455 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004456 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
4457 wpa_s->global->p2p_24ghz_social_channels = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004458 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004459 enum chan_allowed res;
4460 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
4461 if (res == ALLOWED) {
4462 if (reg == NULL) {
4463 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
4464 o->op_class);
4465 reg = &chan->reg_class[cla];
4466 cla++;
4467 reg->reg_class = o->op_class;
4468 }
4469 reg->channel[reg->channels] = ch;
4470 reg->channels++;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004471 } else if (res == NO_IR &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004472 wpa_s->conf->p2p_add_cli_chan) {
4473 if (cli_reg == NULL) {
4474 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
4475 o->op_class);
4476 cli_reg = &cli_chan->reg_class[cli_cla];
4477 cli_cla++;
4478 cli_reg->reg_class = o->op_class;
4479 }
4480 cli_reg->channel[cli_reg->channels] = ch;
4481 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004483 }
4484 if (reg) {
4485 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
4486 reg->channel, reg->channels);
4487 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004488 if (cli_reg) {
4489 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
4490 cli_reg->channel, cli_reg->channels);
4491 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004492 }
4493
4494 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004495 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004496
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004497 return 0;
4498}
4499
4500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004501int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
4502 struct hostapd_hw_modes *mode, u8 channel)
4503{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004504 int op;
4505 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004506
4507 for (op = 0; op_class[op].op_class; op++) {
4508 struct p2p_oper_class_map *o = &op_class[op];
4509 u8 ch;
4510
4511 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
4512 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
4513 o->bw == BW20 || ch != channel)
4514 continue;
4515 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004516 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004517 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004518 }
4519 }
4520 return 0;
4521}
4522
4523
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004524int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
4525 struct hostapd_hw_modes *mode, u8 channel)
4526{
4527 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
4528 return 0;
4529
4530 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
4531}
4532
4533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004534static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
4535 size_t buf_len)
4536{
4537 struct wpa_supplicant *wpa_s = ctx;
4538
4539 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4540 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
4541 break;
4542 }
4543 if (wpa_s == NULL)
4544 return -1;
4545
4546 return wpa_drv_get_noa(wpa_s, buf, buf_len);
4547}
4548
4549
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004550struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
4551 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004552{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004553 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4554 struct wpa_ssid *s = wpa_s->current_ssid;
4555 if (s == NULL)
4556 continue;
4557 if (s->mode != WPAS_MODE_P2P_GO &&
4558 s->mode != WPAS_MODE_AP &&
4559 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4560 continue;
4561 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07004562 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004563 continue;
4564 return wpa_s;
4565 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004566
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004567 return NULL;
4568
4569}
4570
4571
4572struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
4573 const u8 *peer_dev_addr)
4574{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004575 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4576 struct wpa_ssid *ssid = wpa_s->current_ssid;
4577 if (ssid == NULL)
4578 continue;
4579 if (ssid->mode != WPAS_MODE_INFRA)
4580 continue;
4581 if (wpa_s->wpa_state != WPA_COMPLETED &&
4582 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
4583 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004584 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
4585 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004586 }
4587
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004588 return NULL;
4589}
4590
4591
4592static int wpas_go_connected(void *ctx, const u8 *dev_addr)
4593{
4594 struct wpa_supplicant *wpa_s = ctx;
4595
4596 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004597}
4598
4599
Dmitry Shmidt18463232014-01-24 12:29:41 -08004600static int wpas_is_concurrent_session_active(void *ctx)
4601{
4602 struct wpa_supplicant *wpa_s = ctx;
4603 struct wpa_supplicant *ifs;
4604
4605 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4606 if (ifs == wpa_s)
4607 continue;
4608 if (ifs->wpa_state > WPA_ASSOCIATED)
4609 return 1;
4610 }
4611 return 0;
4612}
4613
4614
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004615static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4616{
4617 struct wpa_supplicant *wpa_s = ctx;
4618 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4619}
4620
4621
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004622int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4623 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004624{
4625 struct wpa_interface iface;
4626 struct wpa_supplicant *p2pdev_wpa_s;
4627 char ifname[100];
4628 char force_name[100];
4629 int ret;
4630
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004631 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4632 wpa_s->ifname);
4633 if (os_snprintf_error(sizeof(ifname), ret))
4634 return -1;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004635 force_name[0] = '\0';
4636 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4637 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
4638 force_name, wpa_s->pending_interface_addr, NULL);
4639 if (ret < 0) {
4640 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4641 return ret;
4642 }
4643 os_strlcpy(wpa_s->pending_interface_name, ifname,
4644 sizeof(wpa_s->pending_interface_name));
4645
4646 os_memset(&iface, 0, sizeof(iface));
4647 iface.p2p_mgmt = 1;
4648 iface.ifname = wpa_s->pending_interface_name;
4649 iface.driver = wpa_s->driver->name;
4650 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004651
4652 /*
4653 * If a P2P Device configuration file was given, use it as the interface
4654 * configuration file (instead of using parent's configuration file.
4655 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07004656 if (conf_p2p_dev) {
4657 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08004658 iface.ctrl_interface = NULL;
4659 } else {
4660 iface.confname = wpa_s->confname;
4661 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4662 }
4663 iface.conf_p2p_dev = NULL;
4664
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004665 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
4666 if (!p2pdev_wpa_s) {
4667 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4668 return -1;
4669 }
4670 p2pdev_wpa_s->parent = wpa_s;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004671 wpa_s->p2p_dev = p2pdev_wpa_s;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004672
4673 wpa_s->pending_interface_name[0] = '\0';
4674 return 0;
4675}
4676
4677
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004678static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4679 const u8 *noa, size_t noa_len)
4680{
4681 struct wpa_supplicant *wpa_s, *intf = ctx;
4682 char hex[100];
4683
4684 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4685 if (wpa_s->waiting_presence_resp)
4686 break;
4687 }
4688 if (!wpa_s) {
4689 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4690 return;
4691 }
4692 wpa_s->waiting_presence_resp = 0;
4693
4694 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4695 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4696 " status=%u noa=%s", MAC2STR(src), status, hex);
4697}
4698
4699
Dmitry Shmidt216983b2015-02-06 10:50:36 -08004700static int wpas_get_persistent_group(void *ctx, const u8 *addr, const u8 *ssid,
4701 size_t ssid_len, u8 *go_dev_addr,
4702 u8 *ret_ssid, size_t *ret_ssid_len)
4703{
4704 struct wpa_supplicant *wpa_s = ctx;
4705 struct wpa_ssid *s;
4706
4707 s = wpas_p2p_get_persistent(wpa_s, addr, ssid, ssid_len);
4708 if (s) {
4709 os_memcpy(ret_ssid, s->ssid, s->ssid_len);
4710 *ret_ssid_len = s->ssid_len;
4711 os_memcpy(go_dev_addr, s->bssid, ETH_ALEN);
4712 return 1;
4713 }
4714
4715 return 0;
4716}
4717
4718
4719static int wpas_get_go_info(void *ctx, u8 *intended_addr,
4720 u8 *ssid, size_t *ssid_len, int *group_iface)
4721{
4722 struct wpa_supplicant *wpa_s = ctx;
4723 struct wpa_ssid *s;
4724 u8 bssid[ETH_ALEN];
4725
4726 s = wpas_p2p_group_go_ssid(wpa_s, bssid);
4727 if (!s) {
4728 s = wpas_p2p_get_persistent_go(wpa_s);
4729 if (s)
4730 os_memcpy(bssid, s->bssid, ETH_ALEN);
4731 }
4732
4733 *group_iface = wpas_p2p_create_iface(wpa_s);
4734 if (!s)
4735 return 0;
4736
4737 os_memcpy(intended_addr, bssid, ETH_ALEN);
4738 os_memcpy(ssid, s->ssid, s->ssid_len);
4739 *ssid_len = s->ssid_len;
4740
4741 return 1;
4742}
4743
4744
4745static int wpas_remove_stale_groups(void *ctx, const u8 *peer, const u8 *go,
4746 const u8 *ssid, size_t ssid_len)
4747{
4748 struct wpa_supplicant *wpa_s = ctx;
4749 struct wpa_ssid *s;
4750 int save_config = 0;
4751 size_t i;
4752
4753 /* Start with our first choice of Persistent Groups */
4754 while ((s = wpas_p2p_get_persistent(wpa_s, peer, NULL, 0))) {
4755 if (go && ssid && ssid_len &&
4756 s->ssid_len == ssid_len &&
4757 os_memcmp(go, s->bssid, ETH_ALEN) == 0 &&
4758 os_memcmp(ssid, s->ssid, ssid_len) == 0)
4759 break;
4760
4761 /* Remove stale persistent group */
4762 if (s->mode != WPAS_MODE_P2P_GO || s->num_p2p_clients <= 1) {
4763 wpa_config_remove_network(wpa_s->conf, s->id);
4764 save_config = 1;
4765 continue;
4766 }
4767
4768 for (i = 0; i < s->num_p2p_clients; i++) {
4769 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
4770 peer, ETH_ALEN) != 0)
4771 continue;
4772
4773 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
4774 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
4775 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
4776 break;
4777 }
4778 s->num_p2p_clients--;
4779 save_config = 1;
4780 }
4781
4782 if (save_config)
4783 p2p_config_write(wpa_s);
4784
4785 /* Return TRUE if valid SSID remains */
4786 return s != NULL;
4787}
4788
4789
4790static void wpas_p2ps_prov_complete(void *ctx, u8 status, const u8 *dev,
4791 const u8 *adv_mac, const u8 *ses_mac,
4792 const u8 *grp_mac, u32 adv_id, u32 ses_id,
4793 u8 conncap, int passwd_id,
4794 const u8 *persist_ssid,
4795 size_t persist_ssid_size, int response_done,
4796 int prov_start, const char *session_info)
4797{
4798 struct wpa_supplicant *wpa_s = ctx;
4799 u8 mac[ETH_ALEN];
4800 struct wpa_ssid *persistent_go, *stale, *s;
4801 int save_config = 0;
4802 struct wpa_supplicant *go_wpa_s;
4803
4804 if (!dev)
4805 return;
4806
4807 os_memset(mac, 0, ETH_ALEN);
4808 if (!adv_mac)
4809 adv_mac = mac;
4810 if (!ses_mac)
4811 ses_mac = mac;
4812 if (!grp_mac)
4813 grp_mac = mac;
4814
4815 if (prov_start) {
4816 if (session_info == NULL) {
4817 wpa_msg_global(wpa_s, MSG_INFO,
4818 P2P_EVENT_P2PS_PROVISION_START MACSTR
4819 " adv_id=%x conncap=%x"
4820 " adv_mac=" MACSTR
4821 " session=%x mac=" MACSTR
4822 " dev_passwd_id=%d",
4823 MAC2STR(dev), adv_id, conncap,
4824 MAC2STR(adv_mac),
4825 ses_id, MAC2STR(ses_mac),
4826 passwd_id);
4827 } else {
4828 wpa_msg_global(wpa_s, MSG_INFO,
4829 P2P_EVENT_P2PS_PROVISION_START MACSTR
4830 " adv_id=%x conncap=%x"
4831 " adv_mac=" MACSTR
4832 " session=%x mac=" MACSTR
4833 " dev_passwd_id=%d info='%s'",
4834 MAC2STR(dev), adv_id, conncap,
4835 MAC2STR(adv_mac),
4836 ses_id, MAC2STR(ses_mac),
4837 passwd_id, session_info);
4838 }
4839 return;
4840 }
4841
4842 go_wpa_s = wpas_p2p_get_go_group(wpa_s);
4843 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
4844
4845 if (status && status != P2P_SC_SUCCESS_DEFERRED) {
4846 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4847 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4848
4849 if (persistent_go && !persistent_go->num_p2p_clients) {
4850 /* remove empty persistent GO */
4851 wpa_config_remove_network(wpa_s->conf,
4852 persistent_go->id);
4853 }
4854
4855 wpa_msg_global(wpa_s, MSG_INFO,
4856 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4857 " status=%d"
4858 " adv_id=%x adv_mac=" MACSTR
4859 " session=%x mac=" MACSTR,
4860 MAC2STR(dev), status,
4861 adv_id, MAC2STR(adv_mac),
4862 ses_id, MAC2STR(ses_mac));
4863 return;
4864 }
4865
4866 /* Clean up stale persistent groups with this device */
4867 s = wpas_p2p_get_persistent(wpa_s, dev, persist_ssid,
4868 persist_ssid_size);
4869 for (;;) {
4870 stale = wpas_p2p_get_persistent(wpa_s, dev, NULL, 0);
4871 if (!stale)
4872 break;
4873
4874 if (s && s->ssid_len == stale->ssid_len &&
4875 os_memcmp(stale->bssid, s->bssid, ETH_ALEN) == 0 &&
4876 os_memcmp(stale->ssid, s->ssid, s->ssid_len) == 0)
4877 break;
4878
4879 /* Remove stale persistent group */
4880 if (stale->mode != WPAS_MODE_P2P_GO ||
4881 stale->num_p2p_clients <= 1) {
4882 wpa_config_remove_network(wpa_s->conf, stale->id);
4883 } else {
4884 size_t i;
4885
4886 for (i = 0; i < stale->num_p2p_clients; i++) {
4887 if (os_memcmp(stale->p2p_client_list +
4888 i * ETH_ALEN,
4889 dev, ETH_ALEN) == 0) {
4890 os_memmove(stale->p2p_client_list +
4891 i * ETH_ALEN,
4892 stale->p2p_client_list +
4893 (i + 1) * ETH_ALEN,
4894 (stale->num_p2p_clients -
4895 i - 1) * ETH_ALEN);
4896 break;
4897 }
4898 }
4899 stale->num_p2p_clients--;
4900 }
4901 save_config = 1;
4902 }
4903
4904 if (save_config)
4905 p2p_config_write(wpa_s);
4906
4907 if (s) {
4908 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4909 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4910
4911 if (persistent_go && s != persistent_go &&
4912 !persistent_go->num_p2p_clients) {
4913 /* remove empty persistent GO */
4914 wpa_config_remove_network(wpa_s->conf,
4915 persistent_go->id);
4916 /* Save config */
4917 }
4918
4919 wpa_msg_global(wpa_s, MSG_INFO,
4920 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4921 " status=%d"
4922 " adv_id=%x adv_mac=" MACSTR
4923 " session=%x mac=" MACSTR
4924 " persist=%d",
4925 MAC2STR(dev), status,
4926 adv_id, MAC2STR(adv_mac),
4927 ses_id, MAC2STR(ses_mac), s->id);
4928 return;
4929 }
4930
4931 if (conncap == P2PS_SETUP_GROUP_OWNER) {
4932 const char *go_ifname = NULL;
4933 if (!go_wpa_s) {
4934 wpa_s->global->pending_p2ps_group = 1;
4935
4936 if (wpa_s->conf->p2p_no_group_iface)
4937 go_ifname = wpa_s->ifname;
4938 else if (wpa_s->pending_interface_name[0])
4939 go_ifname = wpa_s->pending_interface_name;
4940
4941 if (!go_ifname) {
4942 wpas_p2ps_prov_complete(
4943 wpa_s, P2P_SC_FAIL_UNKNOWN_GROUP,
4944 dev, adv_mac, ses_mac,
4945 NULL, adv_id, ses_id, 0, 0,
4946 NULL, 0, 0, 0, NULL);
4947 return;
4948 }
4949
4950 /* If PD Resp complete, start up the GO */
4951 if (response_done && persistent_go) {
4952 wpas_p2p_group_add_persistent(
4953 wpa_s, persistent_go,
4954 0, 0, 0, 0, 0, NULL,
4955 persistent_go->mode ==
4956 WPAS_MODE_P2P_GO ?
4957 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
4958 0);
4959 } else if (response_done) {
4960 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
4961 }
4962
4963 if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4964 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4965 wpa_s->p2ps_join_addr_valid = 1;
4966 wpa_dbg(wpa_s, MSG_DEBUG,
4967 "P2PS: Saving PIN for " MACSTR,
4968 MAC2STR(dev));
4969 }
4970 } else if (passwd_id == DEV_PW_P2PS_DEFAULT) {
4971 go_ifname = go_wpa_s->ifname;
4972
4973 wpa_dbg(go_wpa_s, MSG_DEBUG,
4974 "P2P: Setting PIN-1 For " MACSTR, MAC2STR(dev));
4975 wpa_supplicant_ap_wps_pin(go_wpa_s, dev, "12345670",
4976 NULL, 0, 0);
4977
4978 os_memcpy(wpa_s->p2ps_join_addr, dev, ETH_ALEN);
4979 wpa_s->p2ps_join_addr_valid = 1;
4980 wpa_dbg(wpa_s, MSG_DEBUG,
4981 "P2PS: Saving PIN for " MACSTR, MAC2STR(dev));
4982 }
4983
4984 wpa_msg_global(wpa_s, MSG_INFO,
4985 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
4986 " status=%d conncap=%x"
4987 " adv_id=%x adv_mac=" MACSTR
4988 " session=%x mac=" MACSTR
4989 " dev_passwd_id=%d go=%s",
4990 MAC2STR(dev), status, conncap,
4991 adv_id, MAC2STR(adv_mac),
4992 ses_id, MAC2STR(ses_mac),
4993 passwd_id, go_ifname);
4994 return;
4995 }
4996
4997 if (go_wpa_s && !p2p_group_go_member_count(wpa_s))
4998 wpas_p2p_group_remove(wpa_s, go_wpa_s->ifname);
4999
5000 if (persistent_go && !persistent_go->num_p2p_clients) {
5001 /* remove empty persistent GO */
5002 wpa_config_remove_network(wpa_s->conf, persistent_go->id);
5003 }
5004
5005 if (conncap == P2PS_SETUP_CLIENT) {
5006 wpa_msg_global(wpa_s, MSG_INFO,
5007 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5008 " status=%d conncap=%x"
5009 " adv_id=%x adv_mac=" MACSTR
5010 " session=%x mac=" MACSTR
5011 " dev_passwd_id=%d join=" MACSTR,
5012 MAC2STR(dev), status, conncap,
5013 adv_id, MAC2STR(adv_mac),
5014 ses_id, MAC2STR(ses_mac),
5015 passwd_id, MAC2STR(grp_mac));
5016 } else {
5017 wpa_msg_global(wpa_s, MSG_INFO,
5018 P2P_EVENT_P2PS_PROVISION_DONE MACSTR
5019 " status=%d conncap=%x"
5020 " adv_id=%x adv_mac=" MACSTR
5021 " session=%x mac=" MACSTR
5022 " dev_passwd_id=%d",
5023 MAC2STR(dev), status, conncap,
5024 adv_id, MAC2STR(adv_mac),
5025 ses_id, MAC2STR(ses_mac),
5026 passwd_id);
5027 }
5028}
5029
5030
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005031static int _wpas_p2p_in_progress(void *ctx)
5032{
5033 struct wpa_supplicant *wpa_s = ctx;
5034 return wpas_p2p_in_progress(wpa_s);
5035}
5036
5037
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005038static int wpas_prov_disc_resp_cb(void *ctx)
5039{
5040 struct wpa_supplicant *wpa_s = ctx;
5041 struct wpa_ssid *persistent_go;
5042
5043 if (!wpa_s->global->pending_p2ps_group)
5044 return 0;
5045
5046 wpa_s->global->pending_p2ps_group = 0;
5047
5048 if (wpas_p2p_get_go_group(wpa_s))
5049 return 0;
5050 persistent_go = wpas_p2p_get_persistent_go(wpa_s);
5051
5052 if (persistent_go) {
5053 wpas_p2p_group_add_persistent(
5054 wpa_s, persistent_go, 0, 0, 0, 0, 0, NULL,
5055 persistent_go->mode == WPAS_MODE_P2P_GO ?
5056 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
5057 } else {
5058 wpas_p2p_group_add(wpa_s, 1, 0, 0, 0);
5059 }
5060
5061 return 1;
5062}
5063
5064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005065/**
5066 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
5067 * @global: Pointer to global data from wpa_supplicant_init()
5068 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5069 * Returns: 0 on success, -1 on failure
5070 */
5071int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
5072{
5073 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005074 int i;
5075
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005076 if (wpa_s->conf->p2p_disabled)
5077 return 0;
5078
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005079 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5080 return 0;
5081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005082 if (global->p2p)
5083 return 0;
5084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005085 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005086 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005087 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005088 p2p.p2p_scan = wpas_p2p_scan;
5089 p2p.send_action = wpas_send_action;
5090 p2p.send_action_done = wpas_send_action_done;
5091 p2p.go_neg_completed = wpas_go_neg_completed;
5092 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
5093 p2p.dev_found = wpas_dev_found;
5094 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005095 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005096 p2p.start_listen = wpas_start_listen;
5097 p2p.stop_listen = wpas_stop_listen;
5098 p2p.send_probe_resp = wpas_send_probe_resp;
5099 p2p.sd_request = wpas_sd_request;
5100 p2p.sd_response = wpas_sd_response;
5101 p2p.prov_disc_req = wpas_prov_disc_req;
5102 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005103 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005104 p2p.invitation_process = wpas_invitation_process;
5105 p2p.invitation_received = wpas_invitation_received;
5106 p2p.invitation_result = wpas_invitation_result;
5107 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005108 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005109 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08005110 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07005111 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005112 p2p.get_persistent_group = wpas_get_persistent_group;
5113 p2p.get_go_info = wpas_get_go_info;
5114 p2p.remove_stale_groups = wpas_remove_stale_groups;
5115 p2p.p2ps_prov_complete = wpas_p2ps_prov_complete;
5116 p2p.prov_disc_resp_cb = wpas_prov_disc_resp_cb;
5117 p2p.p2ps_group_capability = p2ps_group_capability;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005118
5119 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005120 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005121 p2p.dev_name = wpa_s->conf->device_name;
5122 p2p.manufacturer = wpa_s->conf->manufacturer;
5123 p2p.model_name = wpa_s->conf->model_name;
5124 p2p.model_number = wpa_s->conf->model_number;
5125 p2p.serial_number = wpa_s->conf->serial_number;
5126 if (wpa_s->wps) {
5127 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
5128 p2p.config_methods = wpa_s->wps->config_methods;
5129 }
5130
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005131 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
5132 wpa_printf(MSG_ERROR,
5133 "P2P: Failed to configure supported channel list");
5134 return -1;
5135 }
5136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005137 if (wpa_s->conf->p2p_listen_reg_class &&
5138 wpa_s->conf->p2p_listen_channel) {
5139 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
5140 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005141 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005142 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143 /*
5144 * Pick one of the social channels randomly as the listen
5145 * channel.
5146 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005147 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
5148 &p2p.channel) != 0) {
5149 wpa_printf(MSG_ERROR,
5150 "P2P: Failed to select random social channel as listen channel");
5151 return -1;
5152 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005153 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005154 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005155 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
5156 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157
5158 if (wpa_s->conf->p2p_oper_reg_class &&
5159 wpa_s->conf->p2p_oper_channel) {
5160 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5161 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
5162 p2p.cfg_op_channel = 1;
5163 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
5164 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5165
5166 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005167 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005168 * Use random operation channel from 2.4 GHz band social
5169 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
5170 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005171 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005172 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
5173 &p2p.op_channel) != 0) {
5174 wpa_printf(MSG_ERROR,
5175 "P2P: Failed to select random social channel as operation channel");
5176 return -1;
5177 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005178 p2p.cfg_op_channel = 0;
5179 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
5180 "%d:%d", p2p.op_reg_class, p2p.op_channel);
5181 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005182
5183 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
5184 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
5185 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
5186 }
5187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005188 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5189 os_memcpy(p2p.country, wpa_s->conf->country, 2);
5190 p2p.country[2] = 0x04;
5191 } else
5192 os_memcpy(p2p.country, "XX\x04", 3);
5193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005194 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
5195 WPS_DEV_TYPE_LEN);
5196
5197 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
5198 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
5199 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
5200
5201 p2p.concurrent_operations = !!(wpa_s->drv_flags &
5202 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
5203
5204 p2p.max_peers = 100;
5205
5206 if (wpa_s->conf->p2p_ssid_postfix) {
5207 p2p.ssid_postfix_len =
5208 os_strlen(wpa_s->conf->p2p_ssid_postfix);
5209 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
5210 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
5211 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
5212 p2p.ssid_postfix_len);
5213 }
5214
5215 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
5216
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005217 p2p.max_listen = wpa_s->max_remain_on_chan;
5218
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07005219 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
5220 wpa_s->conf->p2p_passphrase_len <= 63)
5221 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
5222 else
5223 p2p.passphrase_len = 8;
5224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005225 global->p2p = p2p_init(&p2p);
5226 if (global->p2p == NULL)
5227 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005228 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005229
5230 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5231 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5232 continue;
5233 p2p_add_wps_vendor_extension(
5234 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
5235 }
5236
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005237 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
5238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005239 return 0;
5240}
5241
5242
5243/**
5244 * wpas_p2p_deinit - Deinitialize per-interface P2P data
5245 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5246 *
5247 * This function deinitialize per-interface P2P data.
5248 */
5249void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
5250{
5251 if (wpa_s->driver && wpa_s->drv_priv)
5252 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005253
5254 if (wpa_s->go_params) {
5255 /* Clear any stored provisioning info */
5256 p2p_clear_provisioning_info(
5257 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005258 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005259 }
5260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005261 os_free(wpa_s->go_params);
5262 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07005263 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005264 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5265 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5266 wpa_s->p2p_long_listen = 0;
5267 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5268 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5269 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08005270 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005271 wpas_p2p_listen_work_done(wpa_s);
5272 if (wpa_s->p2p_send_action_work) {
5273 os_free(wpa_s->p2p_send_action_work->ctx);
5274 radio_work_done(wpa_s->p2p_send_action_work);
5275 wpa_s->p2p_send_action_work = NULL;
5276 }
5277 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005278
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005279 wpabuf_free(wpa_s->p2p_oob_dev_pw);
5280 wpa_s->p2p_oob_dev_pw = NULL;
5281
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005282 os_free(wpa_s->p2p_group_common_freqs);
5283 wpa_s->p2p_group_common_freqs = NULL;
5284 wpa_s->p2p_group_common_freqs_num = 0;
5285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005286 /* TODO: remove group interface from the driver if this wpa_s instance
5287 * is on top of a P2P group interface */
5288}
5289
5290
5291/**
5292 * wpas_p2p_deinit_global - Deinitialize global P2P module
5293 * @global: Pointer to global data from wpa_supplicant_init()
5294 *
5295 * This function deinitializes the global (per device) P2P module.
5296 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005297static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298{
5299 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005300
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005301 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005302
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005303 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304
5305 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005306 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
5307 wpa_s = wpa_s->next;
5308 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005309 tmp = global->ifaces;
5310 while (tmp &&
5311 (tmp == wpa_s ||
5312 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
5313 tmp = tmp->next;
5314 }
5315 if (tmp == NULL)
5316 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005317 /* Disconnect from the P2P group and deinit the interface */
5318 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005319 }
5320
5321 /*
5322 * Deinit GO data on any possibly remaining interface (if main
5323 * interface is used as GO).
5324 */
5325 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5326 if (wpa_s->ap_iface)
5327 wpas_p2p_group_deinit(wpa_s);
5328 }
5329
5330 p2p_deinit(global->p2p);
5331 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005332 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333}
5334
5335
5336static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
5337{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005338 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
5339 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005340 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341 if (wpa_s->drv_flags &
5342 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
5343 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
5344 return 1; /* P2P group requires a new interface in every case
5345 */
5346 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
5347 return 0; /* driver does not support concurrent operations */
5348 if (wpa_s->global->ifaces->next)
5349 return 1; /* more that one interface already in use */
5350 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
5351 return 1; /* this interface is already in use */
5352 return 0;
5353}
5354
5355
5356static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
5357 const u8 *peer_addr,
5358 enum p2p_wps_method wps_method,
5359 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005360 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005361 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005362{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005363 if (persistent_group && wpa_s->conf->persistent_reconnect)
5364 persistent_group = 2;
5365
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005366 /*
5367 * Increase GO config timeout if HT40 is used since it takes some time
5368 * to scan channels for coex purposes before the BSS can be started.
5369 */
5370 p2p_set_config_timeout(wpa_s->global->p2p,
5371 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
5372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
5374 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005375 persistent_group, ssid ? ssid->ssid : NULL,
5376 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005377 wpa_s->p2p_pd_before_go_neg, pref_freq,
5378 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5379 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005380}
5381
5382
5383static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
5384 const u8 *peer_addr,
5385 enum p2p_wps_method wps_method,
5386 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005387 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005388 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005390 if (persistent_group && wpa_s->conf->persistent_reconnect)
5391 persistent_group = 2;
5392
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
5394 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005395 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005396 ssid ? ssid->ssid_len : 0, pref_freq,
5397 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
5398 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399}
5400
5401
5402static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
5403{
5404 wpa_s->p2p_join_scan_count++;
5405 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
5406 wpa_s->p2p_join_scan_count);
5407 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
5408 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
5409 " for join operationg - stop join attempt",
5410 MAC2STR(wpa_s->pending_join_iface_addr));
5411 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005412 if (wpa_s->p2p_auto_pd) {
5413 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005414 wpa_msg_global(wpa_s, MSG_INFO,
5415 P2P_EVENT_PROV_DISC_FAILURE
5416 " p2p_dev_addr=" MACSTR " status=N/A",
5417 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005418 return;
5419 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005420 wpa_msg_global(wpa_s->parent, MSG_INFO,
5421 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422 }
5423}
5424
5425
Dmitry Shmidt04949592012-07-19 12:16:46 -07005426static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
5427{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005428 int res;
5429 unsigned int num, i;
5430 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005431
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005432 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
5433 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07005434 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005435 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005436
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005437 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5438 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005439 if (!freqs)
5440 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005441
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005442 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5443 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005444
5445 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005446 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005447 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
5448 freq);
5449 res = 0;
5450 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005451 }
5452 }
5453
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005454 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005455 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005456
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005457exit_free:
5458 os_free(freqs);
5459 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005460}
5461
5462
5463static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
5464 const u8 *peer_dev_addr)
5465{
5466 struct wpa_bss *bss;
5467 int updated;
5468
5469 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
5470 if (bss == NULL)
5471 return -1;
5472 if (bss->last_update_idx < wpa_s->bss_update_idx) {
5473 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
5474 "last scan");
5475 return 0;
5476 }
5477
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005478 updated = os_reltime_before(&wpa_s->p2p_auto_started,
5479 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005480 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
5481 "%ld.%06ld (%supdated in last scan)",
5482 bss->last_update.sec, bss->last_update.usec,
5483 updated ? "": "not ");
5484
5485 return updated;
5486}
5487
5488
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005489static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
5490 struct wpa_scan_results *scan_res)
5491{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005492 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 int freq;
5494 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07005495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5497
5498 if (wpa_s->global->p2p_disabled)
5499 return;
5500
Dmitry Shmidt04949592012-07-19 12:16:46 -07005501 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
5502 scan_res ? (int) scan_res->num : -1,
5503 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005504
5505 if (scan_res)
5506 wpas_p2p_scan_res_handler(wpa_s, scan_res);
5507
Dmitry Shmidt04949592012-07-19 12:16:46 -07005508 if (wpa_s->p2p_auto_pd) {
5509 int join = wpas_p2p_peer_go(wpa_s,
5510 wpa_s->pending_join_dev_addr);
5511 if (join == 0 &&
5512 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
5513 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005514 bss = wpa_bss_get_bssid_latest(
5515 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005516 if (bss) {
5517 freq = bss->freq;
5518 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
5519 "the peer " MACSTR " at %d MHz",
5520 wpa_s->auto_pd_scan_retry,
5521 MAC2STR(wpa_s->
5522 pending_join_dev_addr),
5523 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005524 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005525 return;
5526 }
5527 }
5528
5529 if (join < 0)
5530 join = 0;
5531
5532 wpa_s->p2p_auto_pd = 0;
5533 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
5534 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
5535 MAC2STR(wpa_s->pending_join_dev_addr), join);
5536 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005537 wpa_s->pending_join_dev_addr, NULL,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005538 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005539 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005540 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005541 wpa_msg_global(wpa_s, MSG_INFO,
5542 P2P_EVENT_PROV_DISC_FAILURE
5543 " p2p_dev_addr=" MACSTR " status=N/A",
5544 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07005545 }
5546 return;
5547 }
5548
5549 if (wpa_s->p2p_auto_join) {
5550 int join = wpas_p2p_peer_go(wpa_s,
5551 wpa_s->pending_join_dev_addr);
5552 if (join < 0) {
5553 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
5554 "running a GO -> use GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005555 wpa_msg_global(wpa_s->parent, MSG_INFO,
5556 P2P_EVENT_FALLBACK_TO_GO_NEG
5557 "reason=peer-not-running-GO");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005558 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
5559 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
5560 wpa_s->p2p_persistent_group, 0, 0, 0,
5561 wpa_s->p2p_go_intent,
5562 wpa_s->p2p_connect_freq,
5563 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005564 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005565 wpa_s->p2p_go_ht40,
5566 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005567 return;
5568 }
5569
5570 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
5571 "try to join the group", join ? "" :
5572 " in older scan");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005573 if (!join) {
5574 wpa_msg_global(wpa_s->parent, MSG_INFO,
5575 P2P_EVENT_FALLBACK_TO_GO_NEG_ENABLED);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005576 wpa_s->p2p_fallback_to_go_neg = 1;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08005577 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005578 }
5579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005580 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5581 wpa_s->pending_join_iface_addr);
5582 if (freq < 0 &&
5583 p2p_get_interface_addr(wpa_s->global->p2p,
5584 wpa_s->pending_join_dev_addr,
5585 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005586 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
5587 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005588 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
5589 "address for join from " MACSTR " to " MACSTR
5590 " based on newly discovered P2P peer entry",
5591 MAC2STR(wpa_s->pending_join_iface_addr),
5592 MAC2STR(iface_addr));
5593 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
5594 ETH_ALEN);
5595
5596 freq = p2p_get_oper_freq(wpa_s->global->p2p,
5597 wpa_s->pending_join_iface_addr);
5598 }
5599 if (freq >= 0) {
5600 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
5601 "from P2P peer table: %d MHz", freq);
5602 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005603 if (wpa_s->p2p_join_ssid_len) {
5604 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5605 MACSTR " and SSID %s",
5606 MAC2STR(wpa_s->pending_join_iface_addr),
5607 wpa_ssid_txt(wpa_s->p2p_join_ssid,
5608 wpa_s->p2p_join_ssid_len));
5609 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
5610 wpa_s->p2p_join_ssid,
5611 wpa_s->p2p_join_ssid_len);
5612 }
5613 if (!bss) {
5614 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
5615 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
5616 bss = wpa_bss_get_bssid_latest(wpa_s,
5617 wpa_s->pending_join_iface_addr);
5618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005619 if (bss) {
5620 freq = bss->freq;
5621 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07005622 "from BSS table: %d MHz (SSID %s)", freq,
5623 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624 }
5625 if (freq > 0) {
5626 u16 method;
5627
Dmitry Shmidt04949592012-07-19 12:16:46 -07005628 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005629 wpa_msg_global(wpa_s->parent, MSG_INFO,
5630 P2P_EVENT_GROUP_FORMATION_FAILURE
5631 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07005632 return;
5633 }
5634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005635 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
5636 "prior to joining an existing group (GO " MACSTR
5637 " freq=%u MHz)",
5638 MAC2STR(wpa_s->pending_join_dev_addr), freq);
5639 wpa_s->pending_pd_before_join = 1;
5640
5641 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005642 case WPS_PIN_DISPLAY:
5643 method = WPS_CONFIG_KEYPAD;
5644 break;
5645 case WPS_PIN_KEYPAD:
5646 method = WPS_CONFIG_DISPLAY;
5647 break;
5648 case WPS_PBC:
5649 method = WPS_CONFIG_PUSHBUTTON;
5650 break;
5651 default:
5652 method = 0;
5653 break;
5654 }
5655
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005656 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
5657 wpa_s->pending_join_dev_addr) ==
5658 method)) {
5659 /*
5660 * We have already performed provision discovery for
5661 * joining the group. Proceed directly to join
5662 * operation without duplicated provision discovery. */
5663 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
5664 "with " MACSTR " already done - proceed to "
5665 "join",
5666 MAC2STR(wpa_s->pending_join_dev_addr));
5667 wpa_s->pending_pd_before_join = 0;
5668 goto start;
5669 }
5670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005671 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08005672 wpa_s->pending_join_dev_addr,
5673 NULL, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005674 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005675 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
5676 "Discovery Request before joining an "
5677 "existing group");
5678 wpa_s->pending_pd_before_join = 0;
5679 goto start;
5680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681 return;
5682 }
5683
5684 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
5685 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5686 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5687 wpas_p2p_check_join_scan_limit(wpa_s);
5688 return;
5689
5690start:
5691 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005692 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693}
5694
5695
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005696static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
5697 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005698{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005699 int ret;
5700 struct wpa_driver_scan_params params;
5701 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005702 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005703 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005704
5705 os_memset(&params, 0, sizeof(params));
5706
5707 /* P2P Wildcard SSID */
5708 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005709 if (ssid && ssid_len) {
5710 params.ssids[0].ssid = ssid;
5711 params.ssids[0].ssid_len = ssid_len;
5712 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
5713 wpa_s->p2p_join_ssid_len = ssid_len;
5714 } else {
5715 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
5716 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
5717 wpa_s->p2p_join_ssid_len = 0;
5718 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005719
5720 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005721 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
5722 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
5723 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005724 if (wps_ie == NULL) {
5725 wpas_p2p_scan_res_join(wpa_s, NULL);
5726 return;
5727 }
5728
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005729 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
5730 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005731 if (ies == NULL) {
5732 wpabuf_free(wps_ie);
5733 wpas_p2p_scan_res_join(wpa_s, NULL);
5734 return;
5735 }
5736 wpabuf_put_buf(ies, wps_ie);
5737 wpabuf_free(wps_ie);
5738
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005739 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005740
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005741 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005742 params.extra_ies = wpabuf_head(ies);
5743 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08005744
5745 if (!freq) {
5746 int oper_freq;
5747 /*
5748 * If freq is not provided, check the operating freq of the GO
5749 * and use a single channel scan on if possible.
5750 */
5751 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
5752 wpa_s->pending_join_iface_addr);
5753 if (oper_freq > 0)
5754 freq = oper_freq;
5755 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005756 if (freq > 0) {
5757 freqs[0] = freq;
5758 params.freqs = freqs;
5759 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005760
5761 /*
5762 * Run a scan to update BSS table and start Provision Discovery once
5763 * the new scan results become available.
5764 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005765 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005766 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005767 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005768 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005769 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07005770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005771
5772 wpabuf_free(ies);
5773
5774 if (ret) {
5775 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
5776 "try again later");
5777 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5778 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
5779 wpas_p2p_check_join_scan_limit(wpa_s);
5780 }
5781}
5782
5783
Dmitry Shmidt04949592012-07-19 12:16:46 -07005784static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
5785{
5786 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005787 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005788}
5789
5790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005791static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005792 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005793 int auto_join, int op_freq,
5794 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795{
5796 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005797 MACSTR " dev " MACSTR " op_freq=%d)%s",
5798 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005799 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08005800 if (ssid && ssid_len) {
5801 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
5802 wpa_ssid_txt(ssid, ssid_len));
5803 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005804
Dmitry Shmidt04949592012-07-19 12:16:46 -07005805 wpa_s->p2p_auto_pd = 0;
5806 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005807 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
5808 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
5809 wpa_s->pending_join_wps_method = wps_method;
5810
5811 /* Make sure we are not running find during connection establishment */
5812 wpas_p2p_stop_find(wpa_s);
5813
5814 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005815 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005816 return 0;
5817}
5818
5819
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005820static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
5821 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005822{
5823 struct wpa_supplicant *group;
5824 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005825 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005826
5827 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
5828 if (group == NULL)
5829 return -1;
5830 if (group != wpa_s) {
5831 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
5832 sizeof(group->p2p_pin));
5833 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08005834 } else {
5835 /*
5836 * Need to mark the current interface for p2p_group_formation
5837 * when a separate group interface is not used. This is needed
5838 * to allow p2p_cancel stop a pending p2p_connect-join.
5839 * wpas_p2p_init_group_interface() addresses this for the case
5840 * where a separate group interface is used.
5841 */
5842 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005843 }
5844
5845 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005846 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005847
5848 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005849 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
5851 ETH_ALEN);
5852 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005853 if (freq && ssid && ssid_len) {
5854 res.freq = freq;
5855 res.ssid_len = ssid_len;
5856 os_memcpy(res.ssid, ssid, ssid_len);
5857 } else {
5858 bss = wpa_bss_get_bssid_latest(wpa_s,
5859 wpa_s->pending_join_iface_addr);
5860 if (bss) {
5861 res.freq = bss->freq;
5862 res.ssid_len = bss->ssid_len;
5863 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
5864 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
5865 bss->freq,
5866 wpa_ssid_txt(bss->ssid, bss->ssid_len));
5867 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005868 }
5869
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005870 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
5871 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
5872 "starting client");
5873 wpa_drv_cancel_remain_on_channel(wpa_s);
5874 wpa_s->off_channel_freq = 0;
5875 wpa_s->roc_waiting_drv_freq = 0;
5876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877 wpas_start_wps_enrollee(group, &res);
5878
5879 /*
5880 * Allow a longer timeout for join-a-running-group than normal 15
5881 * second group formation timeout since the GO may not have authorized
5882 * our connection yet.
5883 */
5884 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
5885 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
5886 wpa_s, NULL);
5887
5888 return 0;
5889}
5890
5891
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005892static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005893 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005894{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005895 struct wpa_used_freq_data *freqs;
5896 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005897 unsigned int freq_in_use = 0, num, i;
5898
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005899 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5900 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005901 if (!freqs)
5902 return -1;
5903
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005904 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5905 wpa_s->num_multichan_concurrent);
5906
5907 /*
5908 * It is possible that the total number of used frequencies is bigger
5909 * than the number of frequencies used for P2P, so get the system wide
5910 * number of unused frequencies.
5911 */
5912 num_unused = wpas_p2p_num_unused_channels(wpa_s);
5913
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005914 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005915 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
5916 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005917
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005918 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005919 int ret;
5920 if (go)
5921 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
5922 else
5923 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
5924 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005925 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
5926 "(%u MHz) is not supported for P2P uses",
5927 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005928 res = -3;
5929 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005930 }
5931
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005932 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005933 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005934 freq_in_use = 1;
5935 }
5936
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005937 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005938 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
5939 freq);
5940 res = -2;
5941 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005942 }
5943 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
5944 "requested channel (%u MHz)", freq);
5945 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005946 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005947 }
5948
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005949 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005950
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005951 /* We have a candidate frequency to use */
5952 if (best_freq > 0) {
5953 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005954 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005955 best_freq);
5956 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07005957 } else {
5958 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005959 best_freq);
5960 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005961 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005962 } else if (num_unused > 0) {
5963 wpa_printf(MSG_DEBUG,
5964 "P2P: Current operating channels are not available for P2P. Try to use another channel");
5965 *force_freq = 0;
5966 } else {
5967 wpa_printf(MSG_DEBUG,
5968 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5969 res = -2;
5970 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005971 }
5972
5973exit_ok:
5974 res = 0;
5975exit_free:
5976 os_free(freqs);
5977 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005978}
5979
5980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005981/**
5982 * wpas_p2p_connect - Request P2P Group Formation to be started
5983 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5984 * @peer_addr: Address of the peer P2P Device
5985 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5986 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07005987 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988 * @join: Whether to join an existing group (as a client) instead of starting
5989 * Group Owner negotiation; @peer_addr is BSSID in that case
5990 * @auth: Whether to only authorize the connection instead of doing that and
5991 * initiating Group Owner negotiation
5992 * @go_intent: GO Intent or -1 to use default
5993 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07005994 * @persistent_id: Persistent group credentials to use for forcing GO
5995 * parameters or -1 to generate new values (SSID/passphrase)
5996 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5997 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005998 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005999 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006000 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
6001 * failure, -2 on failure due to channel not currently available,
6002 * -3 if forced channel is not supported
6003 */
6004int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6005 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006006 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006007 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006008 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006009{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006010 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006011 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006012 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006013 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006014 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015
6016 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6017 return -1;
6018
Dmitry Shmidt04949592012-07-19 12:16:46 -07006019 if (persistent_id >= 0) {
6020 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
6021 if (ssid == NULL || ssid->disabled != 2 ||
6022 ssid->mode != WPAS_MODE_P2P_GO)
6023 return -1;
6024 }
6025
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006026 os_free(wpa_s->global->add_psk);
6027 wpa_s->global->add_psk = NULL;
6028
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006029 wpa_s->global->p2p_fail_on_wps_complete = 0;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006030 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006032 if (go_intent < 0)
6033 go_intent = wpa_s->conf->p2p_go_intent;
6034
6035 if (!auth)
6036 wpa_s->p2p_long_listen = 0;
6037
6038 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006039 wpa_s->p2p_persistent_group = !!persistent_group;
6040 wpa_s->p2p_persistent_id = persistent_id;
6041 wpa_s->p2p_go_intent = go_intent;
6042 wpa_s->p2p_connect_freq = freq;
6043 wpa_s->p2p_fallback_to_go_neg = 0;
6044 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006045 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006046 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006047
6048 if (pin)
6049 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
6050 else if (wps_method == WPS_PIN_DISPLAY) {
6051 ret = wps_generate_pin();
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006052 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
6053 "%08d", ret);
6054 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
6055 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006056 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
6057 wpa_s->p2p_pin);
6058 } else
6059 wpa_s->p2p_pin[0] = '\0';
6060
Dmitry Shmidt04949592012-07-19 12:16:46 -07006061 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006062 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
6063 if (auth) {
6064 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
6065 "connect a running group from " MACSTR,
6066 MAC2STR(peer_addr));
6067 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6068 return ret;
6069 }
6070 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
6071 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
6072 iface_addr) < 0) {
6073 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
6074 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
6075 dev_addr);
6076 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006077 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006078 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006079 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
6080 "%ld.%06ld",
6081 wpa_s->p2p_auto_started.sec,
6082 wpa_s->p2p_auto_started.usec);
6083 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006084 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006085 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006086 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006087 return -1;
6088 return ret;
6089 }
6090
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006091 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6092 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006093 if (res)
6094 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08006095 wpas_p2p_set_own_freq_preference(wpa_s,
6096 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006097
6098 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
6099
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006100 if (wpa_s->create_p2p_iface) {
6101 /* Prepare to add a new interface for the group */
6102 iftype = WPA_IF_P2P_GROUP;
6103 if (go_intent == 15)
6104 iftype = WPA_IF_P2P_GO;
6105 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
6106 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
6107 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006108 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006110
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006111 if_addr = wpa_s->pending_interface_addr;
6112 } else
6113 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006114
6115 if (auth) {
6116 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006117 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006118 force_freq, persistent_group, ssid,
6119 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006120 return -1;
6121 return ret;
6122 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006123
6124 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
6125 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006126 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006127 if (wpa_s->create_p2p_iface)
6128 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129 return -1;
6130 }
6131 return ret;
6132}
6133
6134
6135/**
6136 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
6137 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6138 * @freq: Frequency of the channel in MHz
6139 * @duration: Duration of the stay on the channel in milliseconds
6140 *
6141 * This callback is called when the driver indicates that it has started the
6142 * requested remain-on-channel duration.
6143 */
6144void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6145 unsigned int freq, unsigned int duration)
6146{
6147 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6148 return;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006149 wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
6150 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6151 wpa_s->roc_waiting_drv_freq, freq, duration);
6152 if (wpa_s->off_channel_freq &&
6153 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006154 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
6155 wpa_s->pending_listen_duration);
6156 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08006157 } else {
6158 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
6159 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
6160 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006161 }
6162}
6163
6164
Jithu Jance57cea1a2014-08-20 10:22:04 -07006165int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006166{
6167 /* Limit maximum Listen state time based on driver limitation. */
6168 if (timeout > wpa_s->max_remain_on_chan)
6169 timeout = wpa_s->max_remain_on_chan;
6170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006171 return p2p_listen(wpa_s->global->p2p, timeout);
6172}
6173
6174
6175/**
6176 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
6177 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6178 * @freq: Frequency of the channel in MHz
6179 *
6180 * This callback is called when the driver indicates that a remain-on-channel
6181 * operation has been completed, i.e., the duration on the requested channel
6182 * has timed out.
6183 */
6184void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
6185 unsigned int freq)
6186{
6187 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
6188 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006189 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006190 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006191 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6192 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07006193 if (wpa_s->p2p_long_listen > 0)
6194 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006195 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
6196 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006197 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006198 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006199 if (wpa_s->p2p_long_listen > 0) {
6200 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
6201 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006202 } else {
6203 /*
6204 * When listen duration is over, stop listen & update p2p_state
6205 * to IDLE.
6206 */
6207 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006208 }
6209}
6210
6211
6212/**
6213 * wpas_p2p_group_remove - Remove a P2P group
6214 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6215 * @ifname: Network interface name of the group interface or "*" to remove all
6216 * groups
6217 * Returns: 0 on success, -1 on failure
6218 *
6219 * This function is used to remove a P2P group. This can be used to disconnect
6220 * from a group in which the local end is a P2P Client or to end a P2P Group in
6221 * case the local end is the Group Owner. If a virtual network interface was
6222 * created for this group, that interface will be removed. Otherwise, only the
6223 * configured P2P group network will be removed from the interface.
6224 */
6225int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
6226{
6227 struct wpa_global *global = wpa_s->global;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006228 struct wpa_supplicant *calling_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006229
6230 if (os_strcmp(ifname, "*") == 0) {
6231 struct wpa_supplicant *prev;
6232 wpa_s = global->ifaces;
6233 while (wpa_s) {
6234 prev = wpa_s;
6235 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07006236 if (prev->p2p_group_interface !=
6237 NOT_P2P_GROUP_INTERFACE ||
6238 (prev->current_ssid &&
6239 prev->current_ssid->p2p_group))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006240 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006241 }
6242 return 0;
6243 }
6244
6245 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6246 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6247 break;
6248 }
6249
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006250 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006251}
6252
6253
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006254static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
6255{
6256 unsigned int r;
6257
6258 if (freq == 2) {
6259 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
6260 "band");
6261 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006262 p2p_supported_freq_go(wpa_s->global->p2p,
6263 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006264 freq = wpa_s->best_24_freq;
6265 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
6266 "channel: %d MHz", freq);
6267 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006268 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6269 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006270 freq = 2412 + (r % 3) * 25;
6271 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
6272 "channel: %d MHz", freq);
6273 }
6274 }
6275
6276 if (freq == 5) {
6277 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
6278 "band");
6279 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006280 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006281 wpa_s->best_5_freq)) {
6282 freq = wpa_s->best_5_freq;
6283 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
6284 "channel: %d MHz", freq);
6285 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006286 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6287 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006288 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006289 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006290 wpa_printf(MSG_DEBUG, "P2P: Could not select "
6291 "5 GHz channel for P2P group");
6292 return -1;
6293 }
6294 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
6295 "channel: %d MHz", freq);
6296 }
6297 }
6298
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006299 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006300 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
6301 "(%u MHz) is not supported for P2P uses",
6302 freq);
6303 return -1;
6304 }
6305
6306 return freq;
6307}
6308
6309
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006310static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
6311 struct p2p_go_neg_results *params,
6312 const struct p2p_channels *channels)
6313{
6314 unsigned int i, r;
6315
6316 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006317 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6318 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006319
6320 for (i = 0; i < 3; i++) {
6321 params->freq = 2412 + ((r + i) % 3) * 25;
6322 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006323 freq_included(channels, params->freq) &&
6324 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006325 goto out;
6326 }
6327
6328 /* try all channels in reg. class 81 */
6329 for (i = 0; i < 11; i++) {
6330 params->freq = 2412 + i * 5;
6331 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006332 freq_included(channels, params->freq) &&
6333 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006334 goto out;
6335 }
6336
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006337 /* try social channel class 180 channel 2 */
6338 params->freq = 58320 + 1 * 2160;
6339 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6340 freq_included(channels, params->freq) &&
6341 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6342 goto out;
6343
6344 /* try all channels in reg. class 180 */
6345 for (i = 0; i < 4; i++) {
6346 params->freq = 58320 + i * 2160;
6347 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
6348 freq_included(channels, params->freq) &&
6349 p2p_supported_freq(wpa_s->global->p2p, params->freq))
6350 goto out;
6351 }
6352
6353 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006354 return -1;
6355out:
6356 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
6357 params->freq);
6358 return 0;
6359}
6360
6361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006362static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
6363 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006364 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006365 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006366{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006367 struct wpa_used_freq_data *freqs;
6368 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006369 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006370
6371 os_memset(params, 0, sizeof(*params));
6372 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006373 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006374 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006375 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006376 if (!freq_included(channels, freq)) {
6377 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
6378 "accepted", freq);
6379 return -1;
6380 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006381 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
6382 "frequency %d MHz", freq);
6383 params->freq = freq;
6384 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
6385 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006386 wpa_s->conf->p2p_oper_channel <= 11 &&
6387 freq_included(channels,
6388 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006389 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
6390 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6391 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006392 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
6393 wpa_s->conf->p2p_oper_reg_class == 116 ||
6394 wpa_s->conf->p2p_oper_reg_class == 117 ||
6395 wpa_s->conf->p2p_oper_reg_class == 124 ||
6396 wpa_s->conf->p2p_oper_reg_class == 126 ||
6397 wpa_s->conf->p2p_oper_reg_class == 127) &&
6398 freq_included(channels,
6399 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006400 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
6401 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
6402 "frequency %d MHz", params->freq);
6403 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6404 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006405 p2p_supported_freq_go(wpa_s->global->p2p,
6406 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006407 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006408 params->freq = wpa_s->best_overall_freq;
6409 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
6410 "channel %d MHz", params->freq);
6411 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6412 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006413 p2p_supported_freq_go(wpa_s->global->p2p,
6414 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006415 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006416 params->freq = wpa_s->best_24_freq;
6417 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
6418 "channel %d MHz", params->freq);
6419 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
6420 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006421 p2p_supported_freq_go(wpa_s->global->p2p,
6422 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006423 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006424 params->freq = wpa_s->best_5_freq;
6425 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
6426 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07006427 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
6428 channels))) {
6429 params->freq = pref_freq;
6430 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
6431 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006432 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006433 /* no preference, select some channel */
6434 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07006435 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006436 }
6437
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006438 freqs = os_calloc(wpa_s->num_multichan_concurrent,
6439 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006440 if (!freqs)
6441 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006442
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006443 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006444 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006445
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006446 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
6447
6448 /* First try the best used frequency if possible */
6449 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
6450 params->freq = cand_freq;
6451 } else if (!freq) {
6452 /* Try any of the used frequencies */
6453 for (i = 0; i < num; i++) {
6454 if (freq_included(channels, freqs[i].freq)) {
6455 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
6456 freqs[i].freq);
6457 params->freq = freqs[i].freq;
6458 break;
6459 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006460 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006461
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006462 if (i == num) {
6463 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006464 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006465 os_free(freqs);
6466 return -1;
6467 } else {
6468 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
6469 }
6470 }
6471 } else {
6472 for (i = 0; i < num; i++) {
6473 if (freqs[i].freq == freq)
6474 break;
6475 }
6476
6477 if (i == num) {
6478 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
6479 if (freq)
6480 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
6481 os_free(freqs);
6482 return -1;
6483 } else {
6484 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
6485 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08006486 }
6487 }
6488
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006489 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006490 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006491}
6492
6493
6494static struct wpa_supplicant *
6495wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
6496 int go)
6497{
6498 struct wpa_supplicant *group_wpa_s;
6499
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006500 if (!wpas_p2p_create_iface(wpa_s)) {
6501 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
6502 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07006503 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006504 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006506
6507 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006508 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006509 wpa_msg_global(wpa_s, MSG_ERROR,
6510 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006511 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006513 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
6514 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006515 wpa_msg_global(wpa_s, MSG_ERROR,
6516 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 wpas_p2p_remove_pending_group_interface(wpa_s);
6518 return NULL;
6519 }
6520
Dmitry Shmidtaa532512012-09-24 10:35:31 -07006521 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
6522 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006523 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006524 return group_wpa_s;
6525}
6526
6527
6528/**
6529 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
6530 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
6531 * @persistent_group: Whether to create a persistent group
6532 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006533 * @ht40: Start GO with 40 MHz channel width
6534 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006535 * Returns: 0 on success, -1 on failure
6536 *
6537 * This function creates a new P2P group with the local end as the Group Owner,
6538 * i.e., without using Group Owner Negotiation.
6539 */
6540int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006541 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006542{
6543 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006544
6545 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6546 return -1;
6547
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006548 os_free(wpa_s->global->add_psk);
6549 wpa_s->global->add_psk = NULL;
6550
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006551 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006552 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006553 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07006554
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006555 freq = wpas_p2p_select_go_freq(wpa_s, freq);
6556 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006557 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006558
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006559 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006560 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006561 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006562 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006563 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
6564 "(%u MHz) is not supported for P2P uses",
6565 params.freq);
6566 return -1;
6567 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006568 p2p_go_params(wpa_s->global->p2p, &params);
6569 params.persistent_group = persistent_group;
6570
6571 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
6572 if (wpa_s == NULL)
6573 return -1;
6574 wpas_start_wps_go(wpa_s, &params, 0);
6575
6576 return 0;
6577}
6578
6579
6580static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07006581 struct wpa_ssid *params, int addr_allocated,
6582 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006583{
6584 struct wpa_ssid *ssid;
6585
6586 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
6587 if (wpa_s == NULL)
6588 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006589 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006590
6591 wpa_supplicant_ap_deinit(wpa_s);
6592
6593 ssid = wpa_config_add_network(wpa_s->conf);
6594 if (ssid == NULL)
6595 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596 wpa_config_set_network_defaults(ssid);
6597 ssid->temporary = 1;
6598 ssid->proto = WPA_PROTO_RSN;
6599 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
6600 ssid->group_cipher = WPA_CIPHER_CCMP;
6601 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
6602 ssid->ssid = os_malloc(params->ssid_len);
6603 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006604 wpa_config_remove_network(wpa_s->conf, ssid->id);
6605 return -1;
6606 }
6607 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
6608 ssid->ssid_len = params->ssid_len;
6609 ssid->p2p_group = 1;
6610 ssid->export_keys = 1;
6611 if (params->psk_set) {
6612 os_memcpy(ssid->psk, params->psk, 32);
6613 ssid->psk_set = 1;
6614 }
6615 if (params->passphrase)
6616 ssid->passphrase = os_strdup(params->passphrase);
6617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006618 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07006619 wpa_s->p2p_in_invitation = 1;
6620 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006621
Dmitry Shmidt15907092014-03-25 10:42:57 -07006622 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6623 NULL);
6624 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6625 wpas_p2p_group_formation_timeout,
6626 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08006627 wpa_supplicant_select_network(wpa_s, ssid);
6628
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006629 return 0;
6630}
6631
6632
6633int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
6634 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006635 int force_freq, int neg_freq, int ht40,
6636 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07006637 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006638{
6639 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006640 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006641
6642 if (ssid->disabled != 2 || ssid->ssid == NULL)
6643 return -1;
6644
6645 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
6646 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
6647 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
6648 "already running");
6649 return 0;
6650 }
6651
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006652 os_free(wpa_s->global->add_psk);
6653 wpa_s->global->add_psk = NULL;
6654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006655 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006656 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006657
Dmitry Shmidt04949592012-07-19 12:16:46 -07006658 wpa_s->p2p_fallback_to_go_neg = 0;
6659
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006660 if (ssid->mode == WPAS_MODE_P2P_GO) {
6661 if (force_freq > 0) {
6662 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
6663 if (freq < 0)
6664 return -1;
6665 } else {
6666 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
6667 if (freq < 0 ||
6668 (freq > 0 && !freq_included(channels, freq)))
6669 freq = 0;
6670 }
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006671 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006672 freq = neg_freq;
6673 if (freq < 0 ||
6674 (freq > 0 && !freq_included(channels, freq)))
Dmitry Shmidt96be6222014-02-13 10:16:51 -08006675 freq = 0;
6676 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07006677
Dmitry Shmidt15907092014-03-25 10:42:57 -07006678 if (ssid->mode == WPAS_MODE_INFRA)
6679 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
6680
6681 if (ssid->mode != WPAS_MODE_P2P_GO)
6682 return -1;
6683
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006684 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006685 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006686
6687 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006688 params.psk_set = ssid->psk_set;
6689 if (params.psk_set)
6690 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006691 if (ssid->passphrase) {
6692 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
6693 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
6694 "persistent group");
6695 return -1;
6696 }
6697 os_strlcpy(params.passphrase, ssid->passphrase,
6698 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006700 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
6701 params.ssid_len = ssid->ssid_len;
6702 params.persistent_group = 1;
6703
6704 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
6705 if (wpa_s == NULL)
6706 return -1;
6707
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08006708 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
6709
Dmitry Shmidt56052862013-10-04 10:23:25 -07006710 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006711 wpas_start_wps_go(wpa_s, &params, 0);
6712
6713 return 0;
6714}
6715
6716
6717static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
6718 struct wpabuf *proberesp_ies)
6719{
6720 struct wpa_supplicant *wpa_s = ctx;
6721 if (wpa_s->ap_iface) {
6722 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006723 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
6724 wpabuf_free(beacon_ies);
6725 wpabuf_free(proberesp_ies);
6726 return;
6727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006728 if (beacon_ies) {
6729 wpabuf_free(hapd->p2p_beacon_ie);
6730 hapd->p2p_beacon_ie = beacon_ies;
6731 }
6732 wpabuf_free(hapd->p2p_probe_resp_ie);
6733 hapd->p2p_probe_resp_ie = proberesp_ies;
6734 } else {
6735 wpabuf_free(beacon_ies);
6736 wpabuf_free(proberesp_ies);
6737 }
6738 wpa_supplicant_ap_update_beacon(wpa_s);
6739}
6740
6741
6742static void wpas_p2p_idle_update(void *ctx, int idle)
6743{
6744 struct wpa_supplicant *wpa_s = ctx;
6745 if (!wpa_s->ap_iface)
6746 return;
6747 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006748 if (idle) {
6749 if (wpa_s->global->p2p_fail_on_wps_complete &&
6750 wpa_s->p2p_in_provisioning) {
6751 wpas_p2p_grpform_fail_after_wps(wpa_s);
6752 return;
6753 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006754 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006755 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006756 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
6757}
6758
6759
6760struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006761 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006762{
6763 struct p2p_group *group;
6764 struct p2p_group_config *cfg;
6765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006766 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6767 return NULL;
6768
6769 cfg = os_zalloc(sizeof(*cfg));
6770 if (cfg == NULL)
6771 return NULL;
6772
Dmitry Shmidt04949592012-07-19 12:16:46 -07006773 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006774 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006775 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006776 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006777 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
6778 if (wpa_s->max_stations &&
6779 wpa_s->max_stations < wpa_s->conf->max_num_sta)
6780 cfg->max_clients = wpa_s->max_stations;
6781 else
6782 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006783 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
6784 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006785 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006786 cfg->cb_ctx = wpa_s;
6787 cfg->ie_update = wpas_p2p_ie_update;
6788 cfg->idle_update = wpas_p2p_idle_update;
6789
6790 group = p2p_group_init(wpa_s->global->p2p, cfg);
6791 if (group == NULL)
6792 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006793 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006794 p2p_group_notif_formation_done(group);
6795 wpa_s->p2p_group = group;
6796 return group;
6797}
6798
6799
6800void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6801 int registrar)
6802{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006803 struct wpa_ssid *ssid = wpa_s->current_ssid;
6804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006805 if (!wpa_s->p2p_in_provisioning) {
6806 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
6807 "provisioning not in progress");
6808 return;
6809 }
6810
Dmitry Shmidt04949592012-07-19 12:16:46 -07006811 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6812 u8 go_dev_addr[ETH_ALEN];
6813 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
6814 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6815 ssid->ssid_len);
6816 /* Clear any stored provisioning info */
6817 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
6818 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006820 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
6821 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006822 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006823 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
6824 /*
6825 * Use a separate timeout for initial data connection to
6826 * complete to allow the group to be removed automatically if
6827 * something goes wrong in this step before the P2P group idle
6828 * timeout mechanism is taken into use.
6829 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07006830 wpa_dbg(wpa_s, MSG_DEBUG,
6831 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
6832 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006833 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
6834 wpas_p2p_group_formation_timeout,
6835 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07006836 } else if (ssid) {
6837 /*
6838 * Use a separate timeout for initial data connection to
6839 * complete to allow the group to be removed automatically if
6840 * the client does not complete data connection successfully.
6841 */
6842 wpa_dbg(wpa_s, MSG_DEBUG,
6843 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
6844 P2P_MAX_INITIAL_CONN_WAIT_GO);
6845 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
6846 wpas_p2p_group_formation_timeout,
6847 wpa_s->parent, NULL);
6848 /*
6849 * Complete group formation on first successful data connection
6850 */
6851 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006852 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006853 if (wpa_s->global->p2p)
6854 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006855 wpas_group_formation_completed(wpa_s, 1);
6856}
6857
6858
Jouni Malinen75ecf522011-06-27 15:19:46 -07006859void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
6860 struct wps_event_fail *fail)
6861{
6862 if (!wpa_s->p2p_in_provisioning) {
6863 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
6864 "provisioning not in progress");
6865 return;
6866 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006867
6868 if (wpa_s->go_params) {
6869 p2p_clear_provisioning_info(
6870 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006871 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006872 }
6873
Jouni Malinen75ecf522011-06-27 15:19:46 -07006874 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006875
6876 if (wpa_s == wpa_s->global->p2p_group_formation) {
6877 /*
6878 * Allow some time for the failed WPS negotiation exchange to
6879 * complete, but remove the group since group formation cannot
6880 * succeed after provisioning failure.
6881 */
6882 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
6883 wpa_s->global->p2p_fail_on_wps_complete = 1;
6884 eloop_deplete_timeout(0, 50000,
6885 wpas_p2p_group_formation_timeout,
6886 wpa_s->parent, NULL);
6887 }
6888}
6889
6890
6891int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
6892{
6893 if (!wpa_s->global->p2p_fail_on_wps_complete ||
6894 !wpa_s->p2p_in_provisioning)
6895 return 0;
6896
6897 wpas_p2p_grpform_fail_after_wps(wpa_s);
6898
6899 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006900}
6901
6902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006903int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006904 const char *config_method,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006905 enum wpas_p2p_prov_disc_use use,
6906 struct p2ps_provision *p2ps_prov)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006907{
6908 u16 config_methods;
6909
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006910 wpa_s->global->pending_p2ps_group = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006911 wpa_s->p2p_fallback_to_go_neg = 0;
6912 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006913 if (p2ps_prov && use == WPAS_P2P_PD_FOR_ASP) {
6914 p2ps_prov->conncap = p2ps_group_capability(
6915 wpa_s, P2PS_SETUP_NONE, p2ps_prov->role);
6916 wpa_printf(MSG_DEBUG,
6917 "P2P: %s conncap: %d - ASP parsed: %x %x %d %s",
6918 __func__, p2ps_prov->conncap,
6919 p2ps_prov->adv_id, p2ps_prov->conncap,
6920 p2ps_prov->status, p2ps_prov->info);
6921
6922 config_methods = 0;
6923 } else if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006924 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006925 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006926 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006927 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
6928 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006929 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006930 else {
6931 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006932 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006933 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006934 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006935
Dmitry Shmidt04949592012-07-19 12:16:46 -07006936 if (use == WPAS_P2P_PD_AUTO) {
6937 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
6938 wpa_s->pending_pd_config_methods = config_methods;
6939 wpa_s->p2p_auto_pd = 1;
6940 wpa_s->p2p_auto_join = 0;
6941 wpa_s->pending_pd_before_join = 0;
6942 wpa_s->auto_pd_scan_retry = 0;
6943 wpas_p2p_stop_find(wpa_s);
6944 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006945 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006946 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
6947 wpa_s->p2p_auto_started.sec,
6948 wpa_s->p2p_auto_started.usec);
6949 wpas_p2p_join_scan(wpa_s, NULL);
6950 return 0;
6951 }
6952
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006953 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled) {
6954 os_free(p2ps_prov);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006955 return -1;
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006956 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006957
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006958 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr, p2ps_prov,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006959 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006960 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006961}
6962
6963
6964int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
6965 char *end)
6966{
6967 return p2p_scan_result_text(ies, ies_len, buf, end);
6968}
6969
6970
6971static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
6972{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006973 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006974 return;
6975
Dmitry Shmidt03658832014-08-13 11:03:49 -07006976 wpas_p2p_action_tx_clear(wpa_s);
6977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006978 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
6979 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006980 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006981}
6982
6983
6984int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
6985 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006986 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08006987 const u8 *dev_id, unsigned int search_delay,
6988 u8 seek_cnt, const char **seek_string)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006989{
6990 wpas_p2p_clear_pending_action_tx(wpa_s);
6991 wpa_s->p2p_long_listen = 0;
6992
Dmitry Shmidt04949592012-07-19 12:16:46 -07006993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6994 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006995 return -1;
6996
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006997 wpa_supplicant_cancel_sched_scan(wpa_s);
6998
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006999 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007000 num_req_dev_types, req_dev_types, dev_id,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007001 search_delay, seek_cnt, seek_string);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007002}
7003
7004
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007005static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
7006 struct wpa_scan_results *scan_res)
7007{
7008 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7009
7010 if (wpa_s->p2p_scan_work) {
7011 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
7012 wpa_s->p2p_scan_work = NULL;
7013 radio_work_done(work);
7014 }
7015
7016 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7017 return;
7018
7019 /*
7020 * Indicate that results have been processed so that the P2P module can
7021 * continue pending tasks.
7022 */
7023 p2p_scan_res_handled(wpa_s->global->p2p);
7024}
7025
7026
7027static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007028{
7029 wpas_p2p_clear_pending_action_tx(wpa_s);
7030 wpa_s->p2p_long_listen = 0;
7031 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7032 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007033
7034 if (wpa_s->global->p2p)
7035 p2p_stop_find(wpa_s->global->p2p);
7036
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007037 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
7038 wpa_printf(MSG_DEBUG,
7039 "P2P: Do not consider the scan results after stop_find");
7040 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
7041 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007042}
7043
7044
7045void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
7046{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007047 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08007048 if (!wpa_s->global->pending_group_iface_for_p2ps)
7049 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007050}
7051
7052
7053static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
7054{
7055 struct wpa_supplicant *wpa_s = eloop_ctx;
7056 wpa_s->p2p_long_listen = 0;
7057}
7058
7059
7060int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
7061{
7062 int res;
7063
7064 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7065 return -1;
7066
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007067 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007068 wpas_p2p_clear_pending_action_tx(wpa_s);
7069
7070 if (timeout == 0) {
7071 /*
7072 * This is a request for unlimited Listen state. However, at
7073 * least for now, this is mapped to a Listen state for one
7074 * hour.
7075 */
7076 timeout = 3600;
7077 }
7078 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
7079 wpa_s->p2p_long_listen = 0;
7080
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007081 /*
7082 * Stop previous find/listen operation to avoid trying to request a new
7083 * remain-on-channel operation while the driver is still running the
7084 * previous one.
7085 */
7086 if (wpa_s->global->p2p)
7087 p2p_stop_find(wpa_s->global->p2p);
7088
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007089 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
7090 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
7091 wpa_s->p2p_long_listen = timeout * 1000;
7092 eloop_register_timeout(timeout, 0,
7093 wpas_p2p_long_listen_timeout,
7094 wpa_s, NULL);
7095 }
7096
7097 return res;
7098}
7099
7100
7101int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
7102 u8 *buf, size_t len, int p2p_group)
7103{
7104 struct wpabuf *p2p_ie;
7105 int ret;
7106
7107 if (wpa_s->global->p2p_disabled)
7108 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007109 if (wpa_s->conf->p2p_disabled)
7110 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007111 if (wpa_s->global->p2p == NULL)
7112 return -1;
7113 if (bss == NULL)
7114 return -1;
7115
7116 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
7117 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
7118 p2p_group, p2p_ie);
7119 wpabuf_free(p2p_ie);
7120
7121 return ret;
7122}
7123
7124
7125int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007126 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007127 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007128{
7129 if (wpa_s->global->p2p_disabled)
7130 return 0;
7131 if (wpa_s->global->p2p == NULL)
7132 return 0;
7133
Dmitry Shmidt04949592012-07-19 12:16:46 -07007134 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
7135 ie, ie_len)) {
7136 case P2P_PREQ_NOT_P2P:
7137 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
7138 ssi_signal);
7139 /* fall through */
7140 case P2P_PREQ_MALFORMED:
7141 case P2P_PREQ_NOT_LISTEN:
7142 case P2P_PREQ_NOT_PROCESSED:
7143 default: /* make gcc happy */
7144 return 0;
7145 case P2P_PREQ_PROCESSED:
7146 return 1;
7147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007148}
7149
7150
7151void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
7152 const u8 *sa, const u8 *bssid,
7153 u8 category, const u8 *data, size_t len, int freq)
7154{
7155 if (wpa_s->global->p2p_disabled)
7156 return;
7157 if (wpa_s->global->p2p == NULL)
7158 return;
7159
7160 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
7161 freq);
7162}
7163
7164
7165void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
7166{
7167 if (wpa_s->global->p2p_disabled)
7168 return;
7169 if (wpa_s->global->p2p == NULL)
7170 return;
7171
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007172 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007173}
7174
7175
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007176static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007177{
7178 p2p_group_deinit(wpa_s->p2p_group);
7179 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007180
7181 wpa_s->ap_configured_cb = NULL;
7182 wpa_s->ap_configured_cb_ctx = NULL;
7183 wpa_s->ap_configured_cb_data = NULL;
7184 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007185}
7186
7187
7188int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
7189{
7190 wpa_s->p2p_long_listen = 0;
7191
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7193 return -1;
7194
7195 return p2p_reject(wpa_s->global->p2p, addr);
7196}
7197
7198
7199/* Invite to reinvoke a persistent group */
7200int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03007201 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007202 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007203{
7204 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007205 u8 *bssid = NULL;
7206 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007207 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007208 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007209
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007210 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007211 if (peer_addr)
7212 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
7213 else
7214 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007215
Jouni Malinen31be0a42012-08-31 21:20:51 +03007216 wpa_s->p2p_persistent_go_freq = freq;
7217 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007218 if (ssid->mode == WPAS_MODE_P2P_GO) {
7219 role = P2P_INVITE_ROLE_GO;
7220 if (peer_addr == NULL) {
7221 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
7222 "address in invitation command");
7223 return -1;
7224 }
7225 if (wpas_p2p_create_iface(wpa_s)) {
7226 if (wpas_p2p_add_group_interface(wpa_s,
7227 WPA_IF_P2P_GO) < 0) {
7228 wpa_printf(MSG_ERROR, "P2P: Failed to "
7229 "allocate a new interface for the "
7230 "group");
7231 return -1;
7232 }
7233 bssid = wpa_s->pending_interface_addr;
7234 } else
7235 bssid = wpa_s->own_addr;
7236 } else {
7237 role = P2P_INVITE_ROLE_CLIENT;
7238 peer_addr = ssid->bssid;
7239 }
7240 wpa_s->pending_invite_ssid_id = ssid->id;
7241
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007242 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7243 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007244 if (res)
7245 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07007246
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007247 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7248 return -1;
7249
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08007250 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
7251 no_pref_freq_given && pref_freq > 0 &&
7252 wpa_s->num_multichan_concurrent > 1 &&
7253 wpas_p2p_num_unused_channels(wpa_s) > 0) {
7254 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
7255 pref_freq);
7256 pref_freq = 0;
7257 }
7258
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08007259 /*
7260 * Stop any find/listen operations before invitation and possibly
7261 * connection establishment.
7262 */
7263 wpas_p2p_stop_find_oper(wpa_s);
7264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007265 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007266 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007267 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007268}
7269
7270
7271/* Invite to join an active group */
7272int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
7273 const u8 *peer_addr, const u8 *go_dev_addr)
7274{
7275 struct wpa_global *global = wpa_s->global;
7276 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007277 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007278 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007279 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007280 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007281 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007282
Jouni Malinen31be0a42012-08-31 21:20:51 +03007283 wpa_s->p2p_persistent_go_freq = 0;
7284 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007285 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03007286
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007287 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7288 if (os_strcmp(wpa_s->ifname, ifname) == 0)
7289 break;
7290 }
7291 if (wpa_s == NULL) {
7292 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
7293 return -1;
7294 }
7295
7296 ssid = wpa_s->current_ssid;
7297 if (ssid == NULL) {
7298 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
7299 "invitation");
7300 return -1;
7301 }
7302
Dmitry Shmidt700a1372013-03-15 14:14:44 -07007303 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08007304 persistent = ssid->p2p_persistent_group &&
7305 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
7306 ssid->ssid, ssid->ssid_len);
7307
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007308 if (ssid->mode == WPAS_MODE_P2P_GO) {
7309 role = P2P_INVITE_ROLE_ACTIVE_GO;
7310 bssid = wpa_s->own_addr;
7311 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007312 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007313 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007314 } else {
7315 role = P2P_INVITE_ROLE_CLIENT;
7316 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
7317 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
7318 "invite to current group");
7319 return -1;
7320 }
7321 bssid = wpa_s->bssid;
7322 if (go_dev_addr == NULL &&
7323 !is_zero_ether_addr(wpa_s->go_dev_addr))
7324 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07007325 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7326 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007327 }
7328 wpa_s->parent->pending_invite_ssid_id = -1;
7329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007330 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7331 return -1;
7332
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007333 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
7334 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007335 if (res)
7336 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07007337 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007338
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007339 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08007340 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007341 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007342}
7343
7344
7345void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
7346{
7347 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007348 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07007349 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007350 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007351 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007352 u8 ip[3 * 4];
7353 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007354
Dmitry Shmidt04949592012-07-19 12:16:46 -07007355 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
7356 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7357 wpa_s->parent, NULL);
7358 }
7359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007360 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007361 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007362
7363 wpa_s->show_group_started = 0;
7364
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007365 os_memset(go_dev_addr, 0, ETH_ALEN);
7366 if (ssid->bssid_set)
7367 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
7368 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
7369 ssid->ssid_len);
7370 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
7371
7372 if (wpa_s->global->p2p_group_formation == wpa_s)
7373 wpa_s->global->p2p_group_formation = NULL;
7374
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007375 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
7376 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007377
7378 ip_addr[0] = '\0';
7379 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007380 int res;
7381
7382 res = os_snprintf(ip_addr, sizeof(ip_addr),
7383 " ip_addr=%u.%u.%u.%u "
7384 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
7385 ip[0], ip[1], ip[2], ip[3],
7386 ip[4], ip[5], ip[6], ip[7],
7387 ip[8], ip[9], ip[10], ip[11]);
7388 if (os_snprintf_error(sizeof(ip_addr), res))
7389 ip_addr[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007390 }
7391
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07007392 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
7393 ssid->passphrase == NULL && ssid->psk_set ?
7394 ssid->psk : NULL,
7395 ssid->passphrase, go_dev_addr, persistent,
7396 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007397
7398 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07007399 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
7400 ssid, go_dev_addr);
7401 if (network_id < 0)
7402 network_id = ssid->id;
7403 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007404}
7405
7406
7407int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
7408 u32 interval1, u32 duration2, u32 interval2)
7409{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007410 int ret;
7411
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007412 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7413 return -1;
7414
7415 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
7416 wpa_s->current_ssid == NULL ||
7417 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
7418 return -1;
7419
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08007420 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
7421 wpa_s->own_addr, wpa_s->assoc_freq,
7422 duration1, interval1, duration2, interval2);
7423 if (ret == 0)
7424 wpa_s->waiting_presence_resp = 1;
7425
7426 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007427}
7428
7429
7430int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
7431 unsigned int interval)
7432{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007433 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7434 return -1;
7435
7436 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
7437}
7438
7439
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007440static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
7441{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08007442 if (wpa_s->current_ssid == NULL) {
7443 /*
7444 * current_ssid can be cleared when P2P client interface gets
7445 * disconnected, so assume this interface was used as P2P
7446 * client.
7447 */
7448 return 1;
7449 }
7450 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007451 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
7452}
7453
7454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007455static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
7456{
7457 struct wpa_supplicant *wpa_s = eloop_ctx;
7458
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007459 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007460 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
7461 "disabled");
7462 return;
7463 }
7464
Dmitry Shmidt04949592012-07-19 12:16:46 -07007465 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
7466 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007467 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007468}
7469
7470
7471static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
7472{
Dmitry Shmidt04949592012-07-19 12:16:46 -07007473 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007474
Dmitry Shmidt04949592012-07-19 12:16:46 -07007475 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7476 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
7477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007478 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7479 return;
7480
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007481 timeout = wpa_s->conf->p2p_group_idle;
7482 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
7483 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
7484 timeout = P2P_MAX_CLIENT_IDLE;
7485
7486 if (timeout == 0)
7487 return;
7488
Dmitry Shmidt04949592012-07-19 12:16:46 -07007489 if (timeout < 0) {
7490 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
7491 timeout = 0; /* special client mode no-timeout */
7492 else
7493 return;
7494 }
7495
7496 if (wpa_s->p2p_in_provisioning) {
7497 /*
7498 * Use the normal group formation timeout during the
7499 * provisioning phase to avoid terminating this process too
7500 * early due to group idle timeout.
7501 */
7502 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7503 "during provisioning");
7504 return;
7505 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05307506
Dmitry Shmidt04949592012-07-19 12:16:46 -07007507 if (wpa_s->show_group_started) {
7508 /*
7509 * Use the normal group formation timeout between the end of
7510 * the provisioning phase and completion of 4-way handshake to
7511 * avoid terminating this process too early due to group idle
7512 * timeout.
7513 */
7514 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
7515 "while waiting for initial 4-way handshake to "
7516 "complete");
7517 return;
7518 }
7519
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007520 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007521 timeout);
7522 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
7523 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007524}
7525
7526
Jouni Malinen2b89da82012-08-31 22:04:41 +03007527/* Returns 1 if the interface was removed */
7528int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
7529 u16 reason_code, const u8 *ie, size_t ie_len,
7530 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007531{
7532 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03007533 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007534
Dmitry Shmidt04949592012-07-19 12:16:46 -07007535 if (!locally_generated)
7536 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7537 ie_len);
7538
7539 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
7540 wpa_s->current_ssid &&
7541 wpa_s->current_ssid->p2p_group &&
7542 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
7543 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
7544 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03007545 if (wpas_p2p_group_delete(wpa_s,
7546 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
7547 > 0)
7548 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07007549 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03007550
7551 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007552}
7553
7554
7555void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07007556 u16 reason_code, const u8 *ie, size_t ie_len,
7557 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007558{
7559 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7560 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007561
Dmitry Shmidt04949592012-07-19 12:16:46 -07007562 if (!locally_generated)
7563 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
7564 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007565}
7566
7567
7568void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
7569{
7570 struct p2p_data *p2p = wpa_s->global->p2p;
7571
7572 if (p2p == NULL)
7573 return;
7574
7575 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
7576 return;
7577
7578 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
7579 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
7580
7581 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
7582 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
7583
7584 if (wpa_s->wps &&
7585 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
7586 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
7587
7588 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
7589 p2p_set_uuid(p2p, wpa_s->wps->uuid);
7590
7591 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
7592 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
7593 p2p_set_model_name(p2p, wpa_s->conf->model_name);
7594 p2p_set_model_number(p2p, wpa_s->conf->model_number);
7595 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
7596 }
7597
7598 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
7599 p2p_set_sec_dev_types(p2p,
7600 (void *) wpa_s->conf->sec_device_type,
7601 wpa_s->conf->num_sec_device_types);
7602
7603 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
7604 int i;
7605 p2p_remove_wps_vendor_extensions(p2p);
7606 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
7607 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
7608 continue;
7609 p2p_add_wps_vendor_extension(
7610 p2p, wpa_s->conf->wps_vendor_ext[i]);
7611 }
7612 }
7613
7614 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
7615 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
7616 char country[3];
7617 country[0] = wpa_s->conf->country[0];
7618 country[1] = wpa_s->conf->country[1];
7619 country[2] = 0x04;
7620 p2p_set_country(p2p, country);
7621 }
7622
7623 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
7624 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
7625 wpa_s->conf->p2p_ssid_postfix ?
7626 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
7627 0);
7628 }
7629
7630 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
7631 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007632
7633 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
7634 u8 reg_class, channel;
7635 int ret;
7636 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007637 u8 channel_forced;
7638
Jouni Malinen75ecf522011-06-27 15:19:46 -07007639 if (wpa_s->conf->p2p_listen_reg_class &&
7640 wpa_s->conf->p2p_listen_channel) {
7641 reg_class = wpa_s->conf->p2p_listen_reg_class;
7642 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007643 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007644 } else {
7645 reg_class = 81;
7646 /*
7647 * Pick one of the social channels randomly as the
7648 * listen channel.
7649 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007650 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7651 channel = 1;
7652 else
7653 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007654 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007655 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007656 ret = p2p_set_listen_channel(p2p, reg_class, channel,
7657 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07007658 if (ret)
7659 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
7660 "failed: %d", ret);
7661 }
7662 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
7663 u8 op_reg_class, op_channel, cfg_op_channel;
7664 int ret = 0;
7665 unsigned int r;
7666 if (wpa_s->conf->p2p_oper_reg_class &&
7667 wpa_s->conf->p2p_oper_channel) {
7668 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
7669 op_channel = wpa_s->conf->p2p_oper_channel;
7670 cfg_op_channel = 1;
7671 } else {
7672 op_reg_class = 81;
7673 /*
7674 * Use random operation channel from (1, 6, 11)
7675 *if no other preference is indicated.
7676 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07007677 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
7678 op_channel = 1;
7679 else
7680 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07007681 cfg_op_channel = 0;
7682 }
7683 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
7684 cfg_op_channel);
7685 if (ret)
7686 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
7687 "failed: %d", ret);
7688 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007689
7690 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
7691 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
7692 wpa_s->conf->p2p_pref_chan) < 0) {
7693 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
7694 "update failed");
7695 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007696
7697 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
7698 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
7699 "update failed");
7700 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07007701 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07007702
7703 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
7704 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007705}
7706
7707
7708int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
7709 int duration)
7710{
7711 if (!wpa_s->ap_iface)
7712 return -1;
7713 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
7714 duration);
7715}
7716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007717
7718int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
7719{
7720 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7721 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007722
7723 wpa_s->global->cross_connection = enabled;
7724 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
7725
7726 if (!enabled) {
7727 struct wpa_supplicant *iface;
7728
7729 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7730 {
7731 if (iface->cross_connect_enabled == 0)
7732 continue;
7733
7734 iface->cross_connect_enabled = 0;
7735 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007736 wpa_msg_global(iface->parent, MSG_INFO,
7737 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7738 iface->ifname,
7739 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007740 }
7741 }
7742
7743 return 0;
7744}
7745
7746
7747static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
7748{
7749 struct wpa_supplicant *iface;
7750
7751 if (!uplink->global->cross_connection)
7752 return;
7753
7754 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7755 if (!iface->cross_connect_enabled)
7756 continue;
7757 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7758 0)
7759 continue;
7760 if (iface->ap_iface == NULL)
7761 continue;
7762 if (iface->cross_connect_in_use)
7763 continue;
7764
7765 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007766 wpa_msg_global(iface->parent, MSG_INFO,
7767 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7768 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007769 }
7770}
7771
7772
7773static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
7774{
7775 struct wpa_supplicant *iface;
7776
7777 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
7778 if (!iface->cross_connect_enabled)
7779 continue;
7780 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
7781 0)
7782 continue;
7783 if (!iface->cross_connect_in_use)
7784 continue;
7785
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007786 wpa_msg_global(iface->parent, MSG_INFO,
7787 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
7788 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007789 iface->cross_connect_in_use = 0;
7790 }
7791}
7792
7793
7794void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
7795{
7796 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
7797 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
7798 wpa_s->cross_connect_disallowed)
7799 wpas_p2p_disable_cross_connect(wpa_s);
7800 else
7801 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007802 if (!wpa_s->ap_iface &&
7803 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
7804 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007805}
7806
7807
7808void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
7809{
7810 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08007811 if (!wpa_s->ap_iface &&
7812 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
7813 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007814 wpas_p2p_set_group_idle_timeout(wpa_s);
7815}
7816
7817
7818static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
7819{
7820 struct wpa_supplicant *iface;
7821
7822 if (!wpa_s->global->cross_connection)
7823 return;
7824
7825 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7826 if (iface == wpa_s)
7827 continue;
7828 if (iface->drv_flags &
7829 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
7830 continue;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08007831 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
7832 iface != wpa_s->parent)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007833 continue;
7834
7835 wpa_s->cross_connect_enabled = 1;
7836 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
7837 sizeof(wpa_s->cross_connect_uplink));
7838 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
7839 "%s to %s whenever uplink is available",
7840 wpa_s->ifname, wpa_s->cross_connect_uplink);
7841
7842 if (iface->ap_iface || iface->current_ssid == NULL ||
7843 iface->current_ssid->mode != WPAS_MODE_INFRA ||
7844 iface->cross_connect_disallowed ||
7845 iface->wpa_state != WPA_COMPLETED)
7846 break;
7847
7848 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07007849 wpa_msg_global(wpa_s->parent, MSG_INFO,
7850 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
7851 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007852 break;
7853 }
7854}
7855
7856
7857int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
7858{
7859 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
7860 !wpa_s->p2p_in_provisioning)
7861 return 0; /* not P2P client operation */
7862
7863 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
7864 "session overlap");
7865 if (wpa_s != wpa_s->parent)
7866 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07007867 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007868 return 1;
7869}
7870
7871
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07007872void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
7873{
7874 struct wpa_supplicant *wpa_s = eloop_ctx;
7875 wpas_p2p_notif_pbc_overlap(wpa_s);
7876}
7877
7878
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007879void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
7880{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007881 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007882 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007883
7884 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
7885 return;
7886
7887 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007888 os_memset(&cli_chan, 0, sizeof(cli_chan));
7889 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007890 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
7891 "channel list");
7892 return;
7893 }
7894
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07007895 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07007896
7897 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
7898 int freq;
7899 if (!ifs->current_ssid ||
7900 !ifs->current_ssid->p2p_group ||
7901 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
7902 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
7903 continue;
7904 freq = ifs->current_ssid->frequency;
7905 if (freq_included(&chan, freq)) {
7906 wpa_dbg(ifs, MSG_DEBUG,
7907 "P2P GO operating frequency %d MHz in valid range",
7908 freq);
7909 continue;
7910 }
7911
7912 wpa_dbg(ifs, MSG_DEBUG,
7913 "P2P GO operating in invalid frequency %d MHz", freq);
7914 /* TODO: Consider using CSA or removing the group within
7915 * wpa_supplicant */
7916 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
7917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007918}
7919
7920
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007921static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
7922 struct wpa_scan_results *scan_res)
7923{
7924 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
7925}
7926
7927
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007928int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
7929{
7930 struct wpa_global *global = wpa_s->global;
7931 int found = 0;
7932 const u8 *peer;
7933
7934 if (global->p2p == NULL)
7935 return -1;
7936
7937 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
7938
7939 if (wpa_s->pending_interface_name[0] &&
7940 !is_zero_ether_addr(wpa_s->pending_interface_addr))
7941 found = 1;
7942
7943 peer = p2p_get_go_neg_peer(global->p2p);
7944 if (peer) {
7945 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
7946 MACSTR, MAC2STR(peer));
7947 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07007948 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007949 }
7950
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08007951 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
7952 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
7953 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
7954 found = 1;
7955 }
7956
7957 if (wpa_s->pending_pd_before_join) {
7958 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
7959 wpa_s->pending_pd_before_join = 0;
7960 found = 1;
7961 }
7962
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007963 wpas_p2p_stop_find(wpa_s);
7964
7965 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7966 if (wpa_s == global->p2p_group_formation &&
7967 (wpa_s->p2p_in_provisioning ||
7968 wpa_s->parent->pending_interface_type ==
7969 WPA_IF_P2P_CLIENT)) {
7970 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
7971 "formation found - cancelling",
7972 wpa_s->ifname);
7973 found = 1;
7974 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7975 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07007976 if (wpa_s->p2p_in_provisioning) {
7977 wpas_group_formation_completed(wpa_s, 0);
7978 break;
7979 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07007980 wpas_p2p_group_delete(wpa_s,
7981 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007982 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07007983 } else if (wpa_s->p2p_in_invitation) {
7984 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
7985 wpa_s->ifname);
7986 found = 1;
7987 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007988 }
7989 }
7990
7991 if (!found) {
7992 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7993 return -1;
7994 }
7995
7996 return 0;
7997}
7998
7999
8000void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
8001{
8002 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
8003 return;
8004
8005 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
8006 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008007 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008008}
8009
8010
8011void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
8012 int freq_24, int freq_5, int freq_overall)
8013{
8014 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008015 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008016 return;
8017 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
8018}
8019
8020
8021int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
8022{
8023 u8 peer[ETH_ALEN];
8024 struct p2p_data *p2p = wpa_s->global->p2p;
8025
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008026 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008027 return -1;
8028
8029 if (hwaddr_aton(addr, peer))
8030 return -1;
8031
8032 return p2p_unauthorize(p2p, peer);
8033}
8034
8035
8036/**
8037 * wpas_p2p_disconnect - Disconnect from a P2P Group
8038 * @wpa_s: Pointer to wpa_supplicant data
8039 * Returns: 0 on success, -1 on failure
8040 *
8041 * This can be used to disconnect from a group in which the local end is a P2P
8042 * Client or to end a P2P Group in case the local end is the Group Owner. If a
8043 * virtual network interface was created for this group, that interface will be
8044 * removed. Otherwise, only the configured P2P group network will be removed
8045 * from the interface.
8046 */
8047int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
8048{
8049
8050 if (wpa_s == NULL)
8051 return -1;
8052
Jouni Malinen2b89da82012-08-31 22:04:41 +03008053 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
8054 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008055}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008056
8057
8058int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
8059{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008060 int ret;
8061
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008062 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8063 return 0;
8064
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008065 ret = p2p_in_progress(wpa_s->global->p2p);
8066 if (ret == 0) {
8067 /*
8068 * Check whether there is an ongoing WPS provisioning step (or
8069 * other parts of group formation) on another interface since
8070 * p2p_in_progress() does not report this to avoid issues for
8071 * scans during such provisioning step.
8072 */
8073 if (wpa_s->global->p2p_group_formation &&
8074 wpa_s->global->p2p_group_formation != wpa_s) {
8075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
8076 "in group formation",
8077 wpa_s->global->p2p_group_formation->ifname);
8078 ret = 1;
8079 }
8080 }
8081
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008082 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08008083 struct os_reltime now;
8084 os_get_reltime(&now);
8085 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
8086 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008087 /* Wait for the first client has expired */
8088 wpa_s->global->p2p_go_wait_client.sec = 0;
8089 } else {
8090 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
8091 ret = 1;
8092 }
8093 }
8094
Dmitry Shmidtf8623282013-02-20 14:34:59 -08008095 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008096}
8097
8098
8099void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
8100 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008101{
8102 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
8103 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8104 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07008105 /**
8106 * Remove the network by scheduling the group formation
8107 * timeout to happen immediately. The teardown code
8108 * needs to be scheduled to run asynch later so that we
8109 * don't delete data from under ourselves unexpectedly.
8110 * Calling wpas_p2p_group_formation_timeout directly
8111 * causes a series of crashes in WPS failure scenarios.
8112 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008113 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
8114 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07008115 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
8116 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008117 }
8118}
8119
8120
8121struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008122 const u8 *addr, const u8 *ssid,
8123 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008124{
8125 struct wpa_ssid *s;
8126 size_t i;
8127
8128 for (s = wpa_s->conf->ssid; s; s = s->next) {
8129 if (s->disabled != 2)
8130 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008131 if (ssid &&
8132 (ssid_len != s->ssid_len ||
8133 os_memcmp(ssid, s->ssid, ssid_len) != 0))
8134 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008135 if (addr == NULL) {
8136 if (s->mode == WPAS_MODE_P2P_GO)
8137 return s;
8138 continue;
8139 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008140 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
8141 return s; /* peer is GO in the persistent group */
8142 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
8143 continue;
8144 for (i = 0; i < s->num_p2p_clients; i++) {
Dmitry Shmidtff787d52015-01-12 13:01:47 -08008145 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008146 addr, ETH_ALEN) == 0)
8147 return s; /* peer is P2P client in persistent
8148 * group */
8149 }
8150 }
8151
8152 return NULL;
8153}
8154
8155
8156void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
8157 const u8 *addr)
8158{
Dmitry Shmidt56052862013-10-04 10:23:25 -07008159 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
8160 wpa_s->parent, NULL) > 0) {
8161 /*
8162 * This can happen if WPS provisioning step is not terminated
8163 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
8164 * peer was able to connect, there is no need to time out group
8165 * formation after this, though. In addition, this is used with
8166 * the initial connection wait on the GO as a separate formation
8167 * timeout and as such, expected to be hit after the initial WPS
8168 * provisioning step.
8169 */
8170 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008171
8172 if (!wpa_s->p2p_go_group_formation_completed &&
8173 !wpa_s->group_formation_reported) {
8174 /*
8175 * GO has not yet notified group formation success since
8176 * the WPS step was not completed cleanly. Do that
8177 * notification now since the P2P Client was able to
8178 * connect and as such, must have received the
8179 * credential from the WPS step.
8180 */
8181 if (wpa_s->global->p2p)
8182 p2p_wps_success_cb(wpa_s->global->p2p, addr);
8183 wpas_group_formation_completed(wpa_s, 1);
8184 }
Dmitry Shmidt56052862013-10-04 10:23:25 -07008185 }
8186 if (!wpa_s->p2p_go_group_formation_completed) {
8187 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
8188 wpa_s->p2p_go_group_formation_completed = 1;
8189 wpa_s->global->p2p_group_formation = NULL;
8190 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07008191 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07008192 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07008193 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08008194 if (addr == NULL)
8195 return;
8196 wpas_p2p_add_persistent_group_client(wpa_s, addr);
8197}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08008198
Dmitry Shmidt04949592012-07-19 12:16:46 -07008199
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008200static int wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
8201 int group_added)
Dmitry Shmidt04949592012-07-19 12:16:46 -07008202{
8203 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008204 int ret = 0;
8205
Dmitry Shmidt04949592012-07-19 12:16:46 -07008206 if (wpa_s->global->p2p_group_formation)
8207 group = wpa_s->global->p2p_group_formation;
8208 wpa_s = wpa_s->parent;
8209 offchannel_send_action_done(wpa_s);
8210 if (group_added)
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008211 ret = wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008212 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
8213 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
8214 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
8215 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
8216 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008217 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07008218 wpa_s->p2p_go_ht40,
8219 wpa_s->p2p_go_vht);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008220 return ret;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008221}
8222
8223
8224int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
8225{
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008226 int res;
8227
Dmitry Shmidt04949592012-07-19 12:16:46 -07008228 if (!wpa_s->p2p_fallback_to_go_neg ||
8229 wpa_s->p2p_in_provisioning <= 5)
8230 return 0;
8231
8232 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
8233 return 0; /* peer operating as a GO */
8234
8235 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
8236 "fallback to GO Negotiation");
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008237 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_FALLBACK_TO_GO_NEG
8238 "reason=GO-not-found");
8239 res = wpas_p2p_fallback_to_go_neg(wpa_s, 1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07008240
Dmitry Shmidt7f656022015-02-25 14:36:37 -08008241 return res == 1 ? 2 : 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07008242}
8243
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008244
8245unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
8246{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008247 struct wpa_supplicant *ifs;
8248
8249 if (wpa_s->wpa_state > WPA_SCANNING) {
8250 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
8251 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008252 wpa_s->conf->p2p_search_delay);
8253 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008254 }
8255
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08008256 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
8257 radio_list) {
8258 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008259 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
8260 "delay due to concurrent operation on "
8261 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07008262 wpa_s->conf->p2p_search_delay,
8263 ifs->ifname);
8264 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07008265 }
8266 }
8267
8268 return 0;
8269}
8270
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07008271
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008272static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
8273 struct wpa_ssid *s, const u8 *addr,
8274 int iface_addr)
8275{
8276 struct psk_list_entry *psk, *tmp;
8277 int changed = 0;
8278
8279 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
8280 list) {
8281 if ((iface_addr && !psk->p2p &&
8282 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
8283 (!iface_addr && psk->p2p &&
8284 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
8285 wpa_dbg(wpa_s, MSG_DEBUG,
8286 "P2P: Remove persistent group PSK list entry for "
8287 MACSTR " p2p=%u",
8288 MAC2STR(psk->addr), psk->p2p);
8289 dl_list_del(&psk->list);
8290 os_free(psk);
8291 changed++;
8292 }
8293 }
8294
8295 return changed;
8296}
8297
8298
8299void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
8300 const u8 *p2p_dev_addr,
8301 const u8 *psk, size_t psk_len)
8302{
8303 struct wpa_ssid *ssid = wpa_s->current_ssid;
8304 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008305 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008306
8307 if (psk_len != sizeof(p->psk))
8308 return;
8309
8310 if (p2p_dev_addr) {
8311 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
8312 " p2p_dev_addr=" MACSTR,
8313 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
8314 if (is_zero_ether_addr(p2p_dev_addr))
8315 p2p_dev_addr = NULL;
8316 } else {
8317 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
8318 MAC2STR(mac_addr));
8319 }
8320
8321 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
8322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
8323 /* To be added to persistent group once created */
8324 if (wpa_s->global->add_psk == NULL) {
8325 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
8326 if (wpa_s->global->add_psk == NULL)
8327 return;
8328 }
8329 p = wpa_s->global->add_psk;
8330 if (p2p_dev_addr) {
8331 p->p2p = 1;
8332 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8333 } else {
8334 p->p2p = 0;
8335 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8336 }
8337 os_memcpy(p->psk, psk, psk_len);
8338 return;
8339 }
8340
8341 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
8342 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
8343 return;
8344 }
8345
8346 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
8347 ssid->ssid_len);
8348 if (!persistent) {
8349 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
8350 return;
8351 }
8352
8353 p = os_zalloc(sizeof(*p));
8354 if (p == NULL)
8355 return;
8356 if (p2p_dev_addr) {
8357 p->p2p = 1;
8358 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
8359 } else {
8360 p->p2p = 0;
8361 os_memcpy(p->addr, mac_addr, ETH_ALEN);
8362 }
8363 os_memcpy(p->psk, psk, psk_len);
8364
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07008365 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
8366 (last = dl_list_last(&persistent->psk_list,
8367 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008368 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
8369 MACSTR " (p2p=%u) to make room for a new one",
8370 MAC2STR(last->addr), last->p2p);
8371 dl_list_del(&last->list);
8372 os_free(last);
8373 }
8374
8375 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
8376 p2p_dev_addr ? p2p_dev_addr : mac_addr,
8377 p2p_dev_addr == NULL);
8378 if (p2p_dev_addr) {
8379 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
8380 MACSTR, MAC2STR(p2p_dev_addr));
8381 } else {
8382 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
8383 MAC2STR(mac_addr));
8384 }
8385 dl_list_add(&persistent->psk_list, &p->list);
8386
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008387 if (wpa_s->parent->conf->update_config &&
8388 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
8389 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008390}
8391
8392
8393static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
8394 struct wpa_ssid *s, const u8 *addr,
8395 int iface_addr)
8396{
8397 int res;
8398
8399 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08008400 if (res > 0 && wpa_s->conf->update_config &&
8401 wpa_config_write(wpa_s->confname, wpa_s->conf))
8402 wpa_dbg(wpa_s, MSG_DEBUG,
8403 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008404}
8405
8406
8407static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
8408 const u8 *peer, int iface_addr)
8409{
8410 struct hostapd_data *hapd;
8411 struct hostapd_wpa_psk *psk, *prev, *rem;
8412 struct sta_info *sta;
8413
8414 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
8415 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
8416 return;
8417
8418 /* Remove per-station PSK entry */
8419 hapd = wpa_s->ap_iface->bss[0];
8420 prev = NULL;
8421 psk = hapd->conf->ssid.wpa_psk;
8422 while (psk) {
8423 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
8424 (!iface_addr &&
8425 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
8426 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
8427 MACSTR " iface_addr=%d",
8428 MAC2STR(peer), iface_addr);
8429 if (prev)
8430 prev->next = psk->next;
8431 else
8432 hapd->conf->ssid.wpa_psk = psk->next;
8433 rem = psk;
8434 psk = psk->next;
8435 os_free(rem);
8436 } else {
8437 prev = psk;
8438 psk = psk->next;
8439 }
8440 }
8441
8442 /* Disconnect from group */
8443 if (iface_addr)
8444 sta = ap_get_sta(hapd, peer);
8445 else
8446 sta = ap_get_sta_p2p(hapd, peer);
8447 if (sta) {
8448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
8449 " (iface_addr=%d) from group",
8450 MAC2STR(peer), iface_addr);
8451 hostapd_drv_sta_deauth(hapd, sta->addr,
8452 WLAN_REASON_DEAUTH_LEAVING);
8453 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
8454 }
8455}
8456
8457
8458void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
8459 int iface_addr)
8460{
8461 struct wpa_ssid *s;
8462 struct wpa_supplicant *w;
8463
8464 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
8465
8466 /* Remove from any persistent group */
8467 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
8468 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
8469 continue;
8470 if (!iface_addr)
8471 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
8472 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
8473 }
8474
8475 /* Remove from any operating group */
8476 for (w = wpa_s->global->ifaces; w; w = w->next)
8477 wpas_p2p_remove_client_go(w, peer, iface_addr);
8478}
8479
8480
8481static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
8482{
8483 struct wpa_supplicant *wpa_s = eloop_ctx;
8484 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
8485}
8486
8487
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08008488static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
8489{
8490 struct wpa_supplicant *wpa_s = eloop_ctx;
8491
8492 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
8493 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
8494}
8495
8496
8497int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
8498 struct wpa_ssid *ssid)
8499{
8500 struct wpa_supplicant *iface;
8501
8502 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8503 if (!iface->current_ssid ||
8504 iface->current_ssid->frequency == freq ||
8505 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
8506 !iface->current_ssid->p2p_group))
8507 continue;
8508
8509 /* Remove the connection with least priority */
8510 if (!wpas_is_p2p_prioritized(iface)) {
8511 /* STA connection has priority over existing
8512 * P2P connection, so remove the interface. */
8513 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
8514 eloop_register_timeout(0, 0,
8515 wpas_p2p_group_freq_conflict,
8516 iface, NULL);
8517 /* If connection in progress is P2P connection, do not
8518 * proceed for the connection. */
8519 if (wpa_s == iface)
8520 return -1;
8521 else
8522 return 0;
8523 } else {
8524 /* P2P connection has priority, disable the STA network
8525 */
8526 wpa_supplicant_disable_network(wpa_s->global->ifaces,
8527 ssid);
8528 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
8529 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
8530 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
8531 ETH_ALEN);
8532 /* If P2P connection is in progress, continue
8533 * connecting...*/
8534 if (wpa_s == iface)
8535 return 0;
8536 else
8537 return -1;
8538 }
8539 }
8540
8541 return 0;
8542}
8543
8544
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07008545int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
8546{
8547 struct wpa_ssid *ssid = wpa_s->current_ssid;
8548
8549 if (ssid == NULL || !ssid->p2p_group)
8550 return 0;
8551
8552 if (wpa_s->p2p_last_4way_hs_fail &&
8553 wpa_s->p2p_last_4way_hs_fail == ssid) {
8554 u8 go_dev_addr[ETH_ALEN];
8555 struct wpa_ssid *persistent;
8556
8557 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
8558 ssid->ssid,
8559 ssid->ssid_len) <= 0) {
8560 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
8561 goto disconnect;
8562 }
8563
8564 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
8565 MACSTR, MAC2STR(go_dev_addr));
8566 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
8567 ssid->ssid,
8568 ssid->ssid_len);
8569 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
8570 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
8571 goto disconnect;
8572 }
8573 wpa_msg_global(wpa_s->parent, MSG_INFO,
8574 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
8575 persistent->id);
8576 disconnect:
8577 wpa_s->p2p_last_4way_hs_fail = NULL;
8578 /*
8579 * Remove the group from a timeout to avoid issues with caller
8580 * continuing to use the interface if this is on a P2P group
8581 * interface.
8582 */
8583 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
8584 wpa_s, NULL);
8585 return 1;
8586 }
8587
8588 wpa_s->p2p_last_4way_hs_fail = ssid;
8589 return 0;
8590}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008591
8592
8593#ifdef CONFIG_WPS_NFC
8594
8595static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
8596 struct wpabuf *p2p)
8597{
8598 struct wpabuf *ret;
8599 size_t wsc_len;
8600
8601 if (p2p == NULL) {
8602 wpabuf_free(wsc);
8603 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
8604 return NULL;
8605 }
8606
8607 wsc_len = wsc ? wpabuf_len(wsc) : 0;
8608 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
8609 if (ret == NULL) {
8610 wpabuf_free(wsc);
8611 wpabuf_free(p2p);
8612 return NULL;
8613 }
8614
8615 wpabuf_put_be16(ret, wsc_len);
8616 if (wsc)
8617 wpabuf_put_buf(ret, wsc);
8618 wpabuf_put_be16(ret, wpabuf_len(p2p));
8619 wpabuf_put_buf(ret, p2p);
8620
8621 wpabuf_free(wsc);
8622 wpabuf_free(p2p);
8623 wpa_hexdump_buf(MSG_DEBUG,
8624 "P2P: Generated NFC connection handover message", ret);
8625
8626 if (ndef && ret) {
8627 struct wpabuf *tmp;
8628 tmp = ndef_build_p2p(ret);
8629 wpabuf_free(ret);
8630 if (tmp == NULL) {
8631 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
8632 return NULL;
8633 }
8634 ret = tmp;
8635 }
8636
8637 return ret;
8638}
8639
8640
8641static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
8642 struct wpa_ssid **ssid, u8 *go_dev_addr)
8643{
8644 struct wpa_supplicant *iface;
8645
8646 if (go_dev_addr)
8647 os_memset(go_dev_addr, 0, ETH_ALEN);
8648 if (ssid)
8649 *ssid = NULL;
8650 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8651 if (iface->wpa_state < WPA_ASSOCIATING ||
8652 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
8653 !iface->current_ssid->p2p_group ||
8654 iface->current_ssid->mode != WPAS_MODE_INFRA)
8655 continue;
8656 if (ssid)
8657 *ssid = iface->current_ssid;
8658 if (go_dev_addr)
8659 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
8660 return iface->assoc_freq;
8661 }
8662 return 0;
8663}
8664
8665
8666struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
8667 int ndef)
8668{
8669 struct wpabuf *wsc, *p2p;
8670 struct wpa_ssid *ssid;
8671 u8 go_dev_addr[ETH_ALEN];
8672 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8673
8674 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
8675 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
8676 return NULL;
8677 }
8678
8679 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8680 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8681 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
8682 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
8683 return NULL;
8684 }
8685
8686 if (cli_freq == 0) {
8687 wsc = wps_build_nfc_handover_req_p2p(
8688 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
8689 } else
8690 wsc = NULL;
8691 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
8692 go_dev_addr, ssid ? ssid->ssid : NULL,
8693 ssid ? ssid->ssid_len : 0);
8694
8695 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8696}
8697
8698
8699struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
8700 int ndef, int tag)
8701{
8702 struct wpabuf *wsc, *p2p;
8703 struct wpa_ssid *ssid;
8704 u8 go_dev_addr[ETH_ALEN];
8705 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
8706
8707 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8708 return NULL;
8709
8710 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8711 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8712 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8713 return NULL;
8714
8715 if (cli_freq == 0) {
8716 wsc = wps_build_nfc_handover_sel_p2p(
8717 wpa_s->parent->wps,
8718 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
8719 DEV_PW_NFC_CONNECTION_HANDOVER,
8720 wpa_s->conf->wps_nfc_dh_pubkey,
8721 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
8722 } else
8723 wsc = NULL;
8724 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
8725 go_dev_addr, ssid ? ssid->ssid : NULL,
8726 ssid ? ssid->ssid_len : 0);
8727
8728 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
8729}
8730
8731
8732static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
8733 struct p2p_nfc_params *params)
8734{
8735 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
8736 "connection handover (freq=%d)",
8737 params->go_freq);
8738
8739 if (params->go_freq && params->go_ssid_len) {
8740 wpa_s->p2p_wps_method = WPS_NFC;
8741 wpa_s->pending_join_wps_method = WPS_NFC;
8742 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
8743 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
8744 ETH_ALEN);
8745 return wpas_p2p_join_start(wpa_s, params->go_freq,
8746 params->go_ssid,
8747 params->go_ssid_len);
8748 }
8749
8750 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8751 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
8752 params->go_freq, -1, 0, 1, 1);
8753}
8754
8755
8756static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
8757 struct p2p_nfc_params *params, int tag)
8758{
8759 int res, persistent;
8760 struct wpa_ssid *ssid;
8761
8762 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
8763 "connection handover");
8764 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
8765 ssid = wpa_s->current_ssid;
8766 if (ssid == NULL)
8767 continue;
8768 if (ssid->mode != WPAS_MODE_P2P_GO)
8769 continue;
8770 if (wpa_s->ap_iface == NULL)
8771 continue;
8772 break;
8773 }
8774 if (wpa_s == NULL) {
8775 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
8776 return -1;
8777 }
8778
8779 if (wpa_s->parent->p2p_oob_dev_pw_id !=
8780 DEV_PW_NFC_CONNECTION_HANDOVER &&
8781 !wpa_s->parent->p2p_oob_dev_pw) {
8782 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
8783 return -1;
8784 }
8785 res = wpas_ap_wps_add_nfc_pw(
8786 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
8787 wpa_s->parent->p2p_oob_dev_pw,
8788 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
8789 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
8790 if (res)
8791 return res;
8792
8793 if (!tag) {
8794 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
8795 return 0;
8796 }
8797
8798 if (!params->peer ||
8799 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
8800 return 0;
8801
8802 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
8803 " to join", MAC2STR(params->peer->p2p_device_addr));
8804
8805 wpa_s->global->p2p_invite_group = wpa_s;
8806 persistent = ssid->p2p_persistent_group &&
8807 wpas_p2p_get_persistent(wpa_s->parent,
8808 params->peer->p2p_device_addr,
8809 ssid->ssid, ssid->ssid_len);
8810 wpa_s->parent->pending_invite_ssid_id = -1;
8811
8812 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
8813 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
8814 ssid->ssid, ssid->ssid_len, ssid->frequency,
8815 wpa_s->global->p2p_dev_addr, persistent, 0,
8816 wpa_s->parent->p2p_oob_dev_pw_id);
8817}
8818
8819
8820static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
8821 struct p2p_nfc_params *params,
8822 int forced_freq)
8823{
8824 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
8825 "connection handover");
8826 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8827 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
8828 forced_freq, -1, 0, 1, 1);
8829}
8830
8831
8832static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
8833 struct p2p_nfc_params *params,
8834 int forced_freq)
8835{
8836 int res;
8837
8838 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
8839 "connection handover");
8840 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
8841 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
8842 forced_freq, -1, 0, 1, 1);
8843 if (res)
8844 return res;
8845
8846 res = wpas_p2p_listen(wpa_s, 60);
8847 if (res) {
8848 p2p_unauthorize(wpa_s->global->p2p,
8849 params->peer->p2p_device_addr);
8850 }
8851
8852 return res;
8853}
8854
8855
8856static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
8857 const struct wpabuf *data,
8858 int sel, int tag, int forced_freq)
8859{
8860 const u8 *pos, *end;
8861 u16 len, id;
8862 struct p2p_nfc_params params;
8863 int res;
8864
8865 os_memset(&params, 0, sizeof(params));
8866 params.sel = sel;
8867
8868 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
8869
8870 pos = wpabuf_head(data);
8871 end = pos + wpabuf_len(data);
8872
8873 if (end - pos < 2) {
8874 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
8875 "attributes");
8876 return -1;
8877 }
8878 len = WPA_GET_BE16(pos);
8879 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008880 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008881 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
8882 "attributes");
8883 return -1;
8884 }
8885 params.wsc_attr = pos;
8886 params.wsc_len = len;
8887 pos += len;
8888
8889 if (end - pos < 2) {
8890 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
8891 "attributes");
8892 return -1;
8893 }
8894 len = WPA_GET_BE16(pos);
8895 pos += 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08008896 if (len > end - pos) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08008897 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
8898 "attributes");
8899 return -1;
8900 }
8901 params.p2p_attr = pos;
8902 params.p2p_len = len;
8903 pos += len;
8904
8905 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
8906 params.wsc_attr, params.wsc_len);
8907 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
8908 params.p2p_attr, params.p2p_len);
8909 if (pos < end) {
8910 wpa_hexdump(MSG_DEBUG,
8911 "P2P: Ignored extra data after P2P attributes",
8912 pos, end - pos);
8913 }
8914
8915 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
8916 if (res)
8917 return res;
8918
8919 if (params.next_step == NO_ACTION)
8920 return 0;
8921
8922 if (params.next_step == BOTH_GO) {
8923 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
8924 MAC2STR(params.peer->p2p_device_addr));
8925 return 0;
8926 }
8927
8928 if (params.next_step == PEER_CLIENT) {
8929 if (!is_zero_ether_addr(params.go_dev_addr)) {
8930 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8931 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
8932 " ssid=\"%s\"",
8933 MAC2STR(params.peer->p2p_device_addr),
8934 params.go_freq,
8935 MAC2STR(params.go_dev_addr),
8936 wpa_ssid_txt(params.go_ssid,
8937 params.go_ssid_len));
8938 } else {
8939 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
8940 "peer=" MACSTR " freq=%d",
8941 MAC2STR(params.peer->p2p_device_addr),
8942 params.go_freq);
8943 }
8944 return 0;
8945 }
8946
8947 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
8948 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
8949 MACSTR, MAC2STR(params.peer->p2p_device_addr));
8950 return 0;
8951 }
8952
8953 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8954 wpa_s->p2p_oob_dev_pw = NULL;
8955
8956 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
8957 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
8958 "received");
8959 return -1;
8960 }
8961
8962 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
8963 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
8964 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
8965 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8966 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
8967 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
8968 wpa_s->p2p_peer_oob_pk_hash_known = 1;
8969
8970 if (tag) {
8971 if (id < 0x10) {
8972 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
8973 "peer OOB Device Password Id %u", id);
8974 return -1;
8975 }
8976 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
8977 "Device Password Id %u", id);
8978 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
8979 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8980 params.oob_dev_pw_len -
8981 WPS_OOB_PUBKEY_HASH_LEN - 2);
8982 wpa_s->p2p_oob_dev_pw_id = id;
8983 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
8984 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
8985 params.oob_dev_pw_len -
8986 WPS_OOB_PUBKEY_HASH_LEN - 2);
8987 if (wpa_s->p2p_oob_dev_pw == NULL)
8988 return -1;
8989
8990 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
8991 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
8992 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
8993 return -1;
8994 } else {
8995 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
8996 "without Device Password");
8997 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
8998 }
8999
9000 switch (params.next_step) {
9001 case NO_ACTION:
9002 case BOTH_GO:
9003 case PEER_CLIENT:
9004 /* already covered above */
9005 return 0;
9006 case JOIN_GROUP:
9007 return wpas_p2p_nfc_join_group(wpa_s, &params);
9008 case AUTH_JOIN:
9009 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
9010 case INIT_GO_NEG:
9011 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
9012 case RESP_GO_NEG:
9013 /* TODO: use own OOB Dev Pw */
9014 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
9015 }
9016
9017 return -1;
9018}
9019
9020
9021int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
9022 const struct wpabuf *data, int forced_freq)
9023{
9024 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9025 return -1;
9026
9027 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
9028}
9029
9030
9031int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
9032 const struct wpabuf *req,
9033 const struct wpabuf *sel, int forced_freq)
9034{
9035 struct wpabuf *tmp;
9036 int ret;
9037
9038 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9039 return -1;
9040
9041 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
9042
9043 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
9044 wpabuf_head(req), wpabuf_len(req));
9045 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
9046 wpabuf_head(sel), wpabuf_len(sel));
9047 if (forced_freq)
9048 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
9049 tmp = ndef_parse_p2p(init ? sel : req);
9050 if (tmp == NULL) {
9051 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
9052 return -1;
9053 }
9054
9055 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
9056 forced_freq);
9057 wpabuf_free(tmp);
9058
9059 return ret;
9060}
9061
9062
9063int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
9064{
9065 const u8 *if_addr;
9066 int go_intent = wpa_s->conf->p2p_go_intent;
9067 struct wpa_supplicant *iface;
9068
9069 if (wpa_s->global->p2p == NULL)
9070 return -1;
9071
9072 if (!enabled) {
9073 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
9074 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
9075 {
9076 if (!iface->ap_iface)
9077 continue;
9078 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
9079 }
9080 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
9081 0, NULL);
9082 if (wpa_s->p2p_nfc_tag_enabled)
9083 wpas_p2p_remove_pending_group_interface(wpa_s);
9084 wpa_s->p2p_nfc_tag_enabled = 0;
9085 return 0;
9086 }
9087
9088 if (wpa_s->global->p2p_disabled)
9089 return -1;
9090
9091 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
9092 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
9093 wpa_s->conf->wps_nfc_dev_pw == NULL ||
9094 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
9095 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
9096 "to allow static handover cases");
9097 return -1;
9098 }
9099
9100 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
9101
9102 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9103 wpabuf_free(wpa_s->p2p_oob_dev_pw);
9104 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9105 if (wpa_s->p2p_oob_dev_pw == NULL)
9106 return -1;
9107 wpa_s->p2p_peer_oob_pk_hash_known = 0;
9108
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07009109 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
9110 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
9111 /*
9112 * P2P Group Interface present and the command came on group
9113 * interface, so enable the token for the current interface.
9114 */
9115 wpa_s->create_p2p_iface = 0;
9116 } else {
9117 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
9118 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08009119
9120 if (wpa_s->create_p2p_iface) {
9121 enum wpa_driver_if_type iftype;
9122 /* Prepare to add a new interface for the group */
9123 iftype = WPA_IF_P2P_GROUP;
9124 if (go_intent == 15)
9125 iftype = WPA_IF_P2P_GO;
9126 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
9127 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
9128 "interface for the group");
9129 return -1;
9130 }
9131
9132 if_addr = wpa_s->pending_interface_addr;
9133 } else
9134 if_addr = wpa_s->own_addr;
9135
9136 wpa_s->p2p_nfc_tag_enabled = enabled;
9137
9138 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
9139 struct hostapd_data *hapd;
9140 if (iface->ap_iface == NULL)
9141 continue;
9142 hapd = iface->ap_iface->bss[0];
9143 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
9144 hapd->conf->wps_nfc_dh_pubkey =
9145 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
9146 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
9147 hapd->conf->wps_nfc_dh_privkey =
9148 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
9149 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
9150 hapd->conf->wps_nfc_dev_pw =
9151 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
9152 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
9153
9154 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
9155 wpa_dbg(iface, MSG_DEBUG,
9156 "P2P: Failed to enable NFC Tag for GO");
9157 }
9158 }
9159 p2p_set_authorized_oob_dev_pw_id(
9160 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
9161 if_addr);
9162
9163 return 0;
9164}
9165
9166#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009167
9168
9169static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
9170 struct wpa_used_freq_data *freqs,
9171 unsigned int num)
9172{
9173 u8 curr_chan, cand, chan;
9174 unsigned int i;
9175
9176 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
9177 for (i = 0, cand = 0; i < num; i++) {
9178 ieee80211_freq_to_chan(freqs[i].freq, &chan);
9179 if (curr_chan == chan) {
9180 cand = 0;
9181 break;
9182 }
9183
9184 if (chan == 1 || chan == 6 || chan == 11)
9185 cand = chan;
9186 }
9187
9188 if (cand) {
9189 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt216983b2015-02-06 10:50:36 -08009190 "P2P: Update Listen channel to %u based on operating channel",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009191 cand);
9192 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
9193 }
9194}
9195
9196
9197void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
9198{
9199 struct wpa_used_freq_data *freqs;
9200 unsigned int num = wpa_s->num_multichan_concurrent;
9201
9202 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
9203 return;
9204
9205 /*
9206 * If possible, optimize the Listen channel to be a channel that is
9207 * already used by one of the other interfaces.
9208 */
9209 if (!wpa_s->conf->p2p_optimize_listen_chan)
9210 return;
9211
9212 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
9213 return;
9214
9215 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
9216 if (!freqs)
9217 return;
9218
9219 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
9220
9221 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
9222 os_free(freqs);
9223}
9224
9225
9226void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
9227{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07009228 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
9229 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
9230 "the management interface is being removed");
9231 wpas_p2p_deinit_global(wpa_s->global);
9232 }
9233}
9234
9235
9236void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
9237{
9238 if (wpa_s->ap_iface->bss)
9239 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
9240 wpas_p2p_group_deinit(wpa_s);
9241}