blob: 640154c823815962902be889a574889f28f11351 [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 Shmidt04949592012-07-19 12:16:46 -0700120static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
121 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800122static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry 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 Shmidt8d520ff2011-05-09 14:06:53 -0700126
127
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700128/*
129 * Get the number of concurrent channels that the HW can operate, but that are
130 * currently not in use by any of the wpa_supplicant interfaces.
131 */
132static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
133{
134 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800135 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700136
137 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
138 if (!freqs)
139 return -1;
140
141 num = get_shared_radio_freqs(wpa_s, freqs,
142 wpa_s->num_multichan_concurrent);
143 os_free(freqs);
144
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800145 unused = wpa_s->num_multichan_concurrent - num;
146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
147 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700155static unsigned int
156wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 struct wpa_used_freq_data *p2p_freqs,
158 unsigned int len)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700159{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700160 struct wpa_used_freq_data *freqs;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700161 unsigned int num, i, j;
162
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700163 freqs = os_calloc(wpa_s->num_multichan_concurrent,
164 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700165 if (!freqs)
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700166 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700168 num = get_shared_radio_freqs_data(wpa_s, freqs,
169 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700171 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700172
173 for (i = 0, j = 0; i < num && j < len; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700174 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700175 p2p_freqs[j++] = freqs[i];
176 }
177
178 os_free(freqs);
179
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700180 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800181
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700182 return j;
183}
184
185
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700186static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
187 int freq)
188{
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
190 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800191 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
192 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
193 wpas_p2p_num_unused_channels(wpa_s) > 0) {
194 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
195 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700196 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800197 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700198 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
199}
200
201
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
203 struct wpa_scan_results *scan_res)
204{
205 size_t i;
206
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800207 if (wpa_s->p2p_scan_work) {
208 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
209 wpa_s->p2p_scan_work = NULL;
210 radio_work_done(work);
211 }
212
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
214 return;
215
216 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
217 (int) scan_res->num);
218
219 for (i = 0; i < scan_res->num; i++) {
220 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800221 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700222 const u8 *ies;
223 size_t ies_len;
224
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800225 time_tmp_age.sec = bss->age / 1000;
226 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800227 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700228
229 ies = (const u8 *) (bss + 1);
230 ies_len = bss->ie_len;
231 if (bss->beacon_ie_len > 0 &&
232 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
233 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
234 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
235 MACSTR, MAC2STR(bss->bssid));
236 ies = ies + ies_len;
237 ies_len = bss->beacon_ie_len;
238 }
239
240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800242 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700243 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 break;
245 }
246
247 p2p_scan_res_handled(wpa_s->global->p2p);
248}
249
250
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800251static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
252{
253 struct wpa_supplicant *wpa_s = work->wpa_s;
254 struct wpa_driver_scan_params *params = work->ctx;
255 int ret;
256
257 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800258 if (!work->started) {
259 wpa_scan_free_params(params);
260 return;
261 }
262
263 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800264 return;
265 }
266
267 ret = wpa_drv_scan(wpa_s, params);
268 wpa_scan_free_params(params);
269 work->ctx = NULL;
270 if (ret) {
271 radio_work_done(work);
272 return;
273 }
274
275 os_get_reltime(&wpa_s->scan_trigger_time);
276 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
277 wpa_s->own_scan_requested = 1;
278 wpa_s->p2p_scan_work = work;
279}
280
281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700282static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
283 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285{
286 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800287 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700289 unsigned int num_channels = 0;
290 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800291 size_t ielen;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700292 u8 *n, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293
294 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
295 return -1;
296
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800297 if (wpa_s->p2p_scan_work) {
298 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
299 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700300 }
301
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800302 params = os_zalloc(sizeof(*params));
303 if (params == NULL)
304 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305
306 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800307 params->num_ssids = 1;
308 n = os_malloc(P2P_WILDCARD_SSID_LEN);
309 if (n == NULL)
310 goto fail;
311 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
312 params->ssids[0].ssid = n;
313 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314
315 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700316 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
317 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 num_req_dev_types, req_dev_types);
319 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800320 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800322 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
323 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 if (ies == NULL) {
325 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800326 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 }
328 wpabuf_put_buf(ies, wps_ie);
329 wpabuf_free(wps_ie);
330
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800331 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800333 params->p2p_probe = 1;
334 n = os_malloc(wpabuf_len(ies));
335 if (n == NULL) {
336 wpabuf_free(ies);
337 goto fail;
338 }
339 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
340 params->extra_ies = n;
341 params->extra_ies_len = wpabuf_len(ies);
342 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343
344 switch (type) {
345 case P2P_SCAN_SOCIAL:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700346 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
347 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800348 if (params->freqs == NULL)
349 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700350 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
351 if (p2p_supported_freq(wpa_s->global->p2p,
352 social_channels_freq[i]))
353 params->freqs[num_channels++] =
354 social_channels_freq[i];
355 }
356 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357 break;
358 case P2P_SCAN_FULL:
359 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700361 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
362 sizeof(int));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800363 if (params->freqs == NULL)
364 goto fail;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700365 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
366 if (p2p_supported_freq(wpa_s->global->p2p,
367 social_channels_freq[i]))
368 params->freqs[num_channels++] =
369 social_channels_freq[i];
370 }
371 if (p2p_supported_freq(wpa_s->global->p2p, freq))
372 params->freqs[num_channels++] = freq;
373 params->freqs[num_channels++] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 break;
375 }
376
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800377 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800378 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
379 params) < 0)
380 goto fail;
381 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800383fail:
384 wpa_scan_free_params(params);
385 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386}
387
388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
390{
391 switch (p2p_group_interface) {
392 case P2P_GROUP_INTERFACE_PENDING:
393 return WPA_IF_P2P_GROUP;
394 case P2P_GROUP_INTERFACE_GO:
395 return WPA_IF_P2P_GO;
396 case P2P_GROUP_INTERFACE_CLIENT:
397 return WPA_IF_P2P_CLIENT;
398 }
399
400 return WPA_IF_P2P_GROUP;
401}
402
403
404static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
405 const u8 *ssid,
406 size_t ssid_len, int *go)
407{
408 struct wpa_ssid *s;
409
410 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
411 for (s = wpa_s->conf->ssid; s; s = s->next) {
412 if (s->disabled != 0 || !s->p2p_group ||
413 s->ssid_len != ssid_len ||
414 os_memcmp(ssid, s->ssid, ssid_len) != 0)
415 continue;
416 if (s->mode == WPAS_MODE_P2P_GO &&
417 s != wpa_s->current_ssid)
418 continue;
419 if (go)
420 *go = s->mode == WPAS_MODE_P2P_GO;
421 return wpa_s;
422 }
423 }
424
425 return NULL;
426}
427
428
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700429static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
430 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431{
432 struct wpa_ssid *ssid;
433 char *gtype;
434 const char *reason;
435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 ssid = wpa_s->current_ssid;
437 if (ssid == NULL) {
438 /*
439 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700440 * pending P2P group interface waiting for provisioning or a
441 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 */
443 ssid = wpa_s->conf->ssid;
444 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700445 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446 break;
447 ssid = ssid->next;
448 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300449 if (ssid == NULL &&
450 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
451 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700452 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
453 "not found");
454 return -1;
455 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 }
457 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
458 gtype = "GO";
459 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
460 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
461 wpa_s->reassociate = 0;
462 wpa_s->disconnected = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 gtype = "client";
464 } else
465 gtype = "GO";
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700466
467 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
468 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
469
470 if (os_strcmp(gtype, "client") == 0)
471 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 if (wpa_s->cross_connect_in_use) {
474 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700475 wpa_msg_global(wpa_s->parent, MSG_INFO,
476 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
477 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700479 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 case P2P_GROUP_REMOVAL_REQUESTED:
481 reason = " reason=REQUESTED";
482 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700483 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
484 reason = " reason=FORMATION_FAILED";
485 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
487 reason = " reason=IDLE";
488 break;
489 case P2P_GROUP_REMOVAL_UNAVAILABLE:
490 reason = " reason=UNAVAILABLE";
491 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700492 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
493 reason = " reason=GO_ENDING_SESSION";
494 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700495 case P2P_GROUP_REMOVAL_PSK_FAILURE:
496 reason = " reason=PSK_FAILURE";
497 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800498 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
499 reason = " reason=FREQ_CONFLICT";
500 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 default:
502 reason = "";
503 break;
504 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700505 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700506 wpa_msg_global(wpa_s->parent, MSG_INFO,
507 P2P_EVENT_GROUP_REMOVED "%s %s%s",
508 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700509 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800511 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
512 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700513 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
514 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800515 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700516 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800517 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
518 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700519 wpa_s->p2p_in_provisioning = 0;
520 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700521
Dmitry Shmidt15907092014-03-25 10:42:57 -0700522 wpa_s->p2p_in_invitation = 0;
523
Dmitry Shmidt96571392013-10-14 12:54:46 -0700524 /*
525 * Make sure wait for the first client does not remain active after the
526 * group has been removed.
527 */
528 wpa_s->global->p2p_go_wait_client.sec = 0;
529
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
531 struct wpa_global *global;
532 char *ifname;
533 enum wpa_driver_if_type type;
534 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
535 wpa_s->ifname);
536 global = wpa_s->global;
537 ifname = os_strdup(wpa_s->ifname);
538 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700539 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 wpa_s = global->ifaces;
541 if (wpa_s && ifname)
542 wpa_drv_if_remove(wpa_s, type, ifname);
543 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300544 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 }
546
Dmitry Shmidt96571392013-10-14 12:54:46 -0700547 if (!wpa_s->p2p_go_group_formation_completed) {
548 wpa_s->global->p2p_group_formation = NULL;
549 wpa_s->p2p_in_provisioning = 0;
550 }
551
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800552 wpa_s->show_group_started = 0;
553 os_free(wpa_s->go_params);
554 wpa_s->go_params = NULL;
555
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800556 wpa_s->waiting_presence_resp = 0;
557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
559 if (ssid && (ssid->p2p_group ||
560 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
561 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
562 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700563 if (ssid == wpa_s->current_ssid) {
564 wpa_sm_set_config(wpa_s->wpa, NULL);
565 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700567 }
568 /*
569 * Networks objects created during any P2P activities are not
570 * exposed out as they might/will confuse certain non-P2P aware
571 * applications since these network objects won't behave like
572 * regular ones.
573 *
574 * Likewise, we don't send out network removed signals for such
575 * network objects.
576 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577 wpa_config_remove_network(wpa_s->conf, id);
578 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800579 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 } else {
581 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
582 "found");
583 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700584 if (wpa_s->ap_iface)
585 wpa_supplicant_ap_deinit(wpa_s);
586 else
587 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700588
589 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590}
591
592
593static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
594 u8 *go_dev_addr,
595 const u8 *ssid, size_t ssid_len)
596{
597 struct wpa_bss *bss;
598 const u8 *bssid;
599 struct wpabuf *p2p;
600 u8 group_capab;
601 const u8 *addr;
602
603 if (wpa_s->go_params)
604 bssid = wpa_s->go_params->peer_interface_addr;
605 else
606 bssid = wpa_s->bssid;
607
608 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800609 if (bss == NULL && wpa_s->go_params &&
610 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
611 bss = wpa_bss_get_p2p_dev_addr(
612 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 if (bss == NULL) {
614 u8 iface_addr[ETH_ALEN];
615 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
616 iface_addr) == 0)
617 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
618 }
619 if (bss == NULL) {
620 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
621 "group is persistent - BSS " MACSTR " not found",
622 MAC2STR(bssid));
623 return 0;
624 }
625
626 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700627 if (p2p == NULL)
628 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
629 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 if (p2p == NULL) {
631 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
632 "group is persistent - BSS " MACSTR
633 " did not include P2P IE", MAC2STR(bssid));
634 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
635 (u8 *) (bss + 1), bss->ie_len);
636 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
637 ((u8 *) bss + 1) + bss->ie_len,
638 bss->beacon_ie_len);
639 return 0;
640 }
641
642 group_capab = p2p_get_group_capab(p2p);
643 addr = p2p_get_go_dev_addr(p2p);
644 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
645 "group_capab=0x%x", group_capab);
646 if (addr) {
647 os_memcpy(go_dev_addr, addr, ETH_ALEN);
648 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
649 MAC2STR(addr));
650 } else
651 os_memset(go_dev_addr, 0, ETH_ALEN);
652 wpabuf_free(p2p);
653
654 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
655 "go_dev_addr=" MACSTR,
656 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
657
658 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
659}
660
661
Jouni Malinen75ecf522011-06-27 15:19:46 -0700662static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
663 struct wpa_ssid *ssid,
664 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665{
666 struct wpa_ssid *s;
667 int changed = 0;
668
669 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
670 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
671 for (s = wpa_s->conf->ssid; s; s = s->next) {
672 if (s->disabled == 2 &&
673 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
674 s->ssid_len == ssid->ssid_len &&
675 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
676 break;
677 }
678
679 if (s) {
680 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
681 "entry");
682 if (ssid->passphrase && !s->passphrase)
683 changed = 1;
684 else if (ssid->passphrase && s->passphrase &&
685 os_strcmp(ssid->passphrase, s->passphrase) != 0)
686 changed = 1;
687 } else {
688 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
689 "entry");
690 changed = 1;
691 s = wpa_config_add_network(wpa_s->conf);
692 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700693 return -1;
694
695 /*
696 * Instead of network_added we emit persistent_group_added
697 * notification. Also to keep the defense checks in
698 * persistent_group obj registration method, we set the
699 * relevant flags in s to designate it as a persistent group.
700 */
701 s->p2p_group = 1;
702 s->p2p_persistent_group = 1;
703 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 wpa_config_set_network_defaults(s);
705 }
706
707 s->p2p_group = 1;
708 s->p2p_persistent_group = 1;
709 s->disabled = 2;
710 s->bssid_set = 1;
711 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
712 s->mode = ssid->mode;
713 s->auth_alg = WPA_AUTH_ALG_OPEN;
714 s->key_mgmt = WPA_KEY_MGMT_PSK;
715 s->proto = WPA_PROTO_RSN;
716 s->pairwise_cipher = WPA_CIPHER_CCMP;
717 s->export_keys = 1;
718 if (ssid->passphrase) {
719 os_free(s->passphrase);
720 s->passphrase = os_strdup(ssid->passphrase);
721 }
722 if (ssid->psk_set) {
723 s->psk_set = 1;
724 os_memcpy(s->psk, ssid->psk, 32);
725 }
726 if (s->passphrase && !s->psk_set)
727 wpa_config_update_psk(s);
728 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
729 os_free(s->ssid);
730 s->ssid = os_malloc(ssid->ssid_len);
731 }
732 if (s->ssid) {
733 s->ssid_len = ssid->ssid_len;
734 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
735 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700736 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
737 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
738 wpa_s->global->add_psk = NULL;
739 changed = 1;
740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 if (changed && wpa_s->conf->update_config &&
743 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
744 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
745 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700746
747 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748}
749
750
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
752 const u8 *addr)
753{
754 struct wpa_ssid *ssid, *s;
755 u8 *n;
756 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700757 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800758
759 ssid = wpa_s->current_ssid;
760 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
761 !ssid->p2p_persistent_group)
762 return;
763
764 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
765 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
766 continue;
767
768 if (s->ssid_len == ssid->ssid_len &&
769 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
770 break;
771 }
772
773 if (s == NULL)
774 return;
775
776 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
777 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700778 ETH_ALEN) != 0)
779 continue;
780
781 if (i == s->num_p2p_clients - 1)
782 return; /* already the most recent entry */
783
784 /* move the entry to mark it most recent */
785 os_memmove(s->p2p_client_list + i * ETH_ALEN,
786 s->p2p_client_list + (i + 1) * ETH_ALEN,
787 (s->num_p2p_clients - i - 1) * ETH_ALEN);
788 os_memcpy(s->p2p_client_list +
789 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
790 found = 1;
791 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 }
793
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700794 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
795 n = os_realloc_array(s->p2p_client_list,
796 s->num_p2p_clients + 1, ETH_ALEN);
797 if (n == NULL)
798 return;
799 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
800 s->p2p_client_list = n;
801 s->num_p2p_clients++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -0700802 } else if (!found && s->p2p_client_list) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700803 /* Not enough room for an additional entry - drop the oldest
804 * entry */
805 os_memmove(s->p2p_client_list,
806 s->p2p_client_list + ETH_ALEN,
807 (s->num_p2p_clients - 1) * ETH_ALEN);
808 os_memcpy(s->p2p_client_list +
809 (s->num_p2p_clients - 1) * ETH_ALEN,
810 addr, ETH_ALEN);
811 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800812
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813 if (wpa_s->parent->conf->update_config &&
814 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
815 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816}
817
818
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700819static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
820 int go, struct wpa_ssid *ssid, int freq,
821 const u8 *psk, const char *passphrase,
822 const u8 *go_dev_addr, int persistent,
823 const char *extra)
824{
825 const char *ssid_txt;
826 char psk_txt[65];
827
828 if (psk)
829 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
830 else
831 psk_txt[0] = '\0';
832
833 if (ssid)
834 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
835 else
836 ssid_txt = "";
837
838 if (passphrase && passphrase[0] == '\0')
839 passphrase = NULL;
840
841 /*
842 * Include PSK/passphrase only in the control interface message and
843 * leave it out from the debug log entry.
844 */
845 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
846 P2P_EVENT_GROUP_STARTED
847 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
848 MACSTR "%s%s",
849 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
850 psk ? " psk=" : "", psk_txt,
851 passphrase ? " passphrase=\"" : "",
852 passphrase ? passphrase : "",
853 passphrase ? "\"" : "",
854 MAC2STR(go_dev_addr),
855 persistent ? " [PERSISTENT]" : "", extra);
856 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
857 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
858 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
859 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
860 extra);
861}
862
863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
865 int success)
866{
867 struct wpa_ssid *ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 int client;
869 int persistent;
870 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700871 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872
873 /*
874 * This callback is likely called for the main interface. Update wpa_s
875 * to use the group interface if a new interface was created for the
876 * group.
877 */
878 if (wpa_s->global->p2p_group_formation)
879 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700880 if (wpa_s->p2p_go_group_formation_completed) {
881 wpa_s->global->p2p_group_formation = NULL;
882 wpa_s->p2p_in_provisioning = 0;
883 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700884 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885
886 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700887 wpa_msg_global(wpa_s->parent, MSG_INFO,
888 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700889 wpas_p2p_group_delete(wpa_s,
890 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 return;
892 }
893
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700894 wpa_msg_global(wpa_s->parent, MSG_INFO,
895 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896
897 ssid = wpa_s->current_ssid;
898 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
899 ssid->mode = WPAS_MODE_P2P_GO;
900 p2p_group_notif_formation_done(wpa_s->p2p_group);
901 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
902 }
903
904 persistent = 0;
905 if (ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 client = ssid->mode == WPAS_MODE_INFRA;
907 if (ssid->mode == WPAS_MODE_P2P_GO) {
908 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700909 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
910 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 } else
912 persistent = wpas_p2p_persistent_group(wpa_s,
913 go_dev_addr,
914 ssid->ssid,
915 ssid->ssid_len);
916 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 client = wpa_s->p2p_group_interface ==
918 P2P_GROUP_INTERFACE_CLIENT;
919 os_memset(go_dev_addr, 0, ETH_ALEN);
920 }
921
922 wpa_s->show_group_started = 0;
923 if (client) {
924 /*
925 * Indicate event only after successfully completed 4-way
926 * handshake, i.e., when the interface is ready for data
927 * packets.
928 */
929 wpa_s->show_group_started = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 } else {
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -0700931 wpas_p2p_group_started(wpa_s, 1, ssid,
932 ssid ? ssid->frequency : 0,
933 ssid && ssid->passphrase == NULL &&
934 ssid->psk_set ? ssid->psk : NULL,
935 ssid ? ssid->passphrase : NULL,
936 go_dev_addr, persistent, "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 wpas_p2p_cross_connect_setup(wpa_s);
938 wpas_p2p_set_group_idle_timeout(wpa_s);
939 }
940
941 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700942 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
943 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700944 else {
945 os_free(wpa_s->global->add_psk);
946 wpa_s->global->add_psk = NULL;
947 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700949 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700950 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700951 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800952 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954}
955
956
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800957struct send_action_work {
958 unsigned int freq;
959 u8 dst[ETH_ALEN];
960 u8 src[ETH_ALEN];
961 u8 bssid[ETH_ALEN];
962 size_t len;
963 unsigned int wait_time;
964 u8 buf[0];
965};
966
967
968static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
969 void *timeout_ctx)
970{
971 struct wpa_supplicant *wpa_s = eloop_ctx;
972
973 if (!wpa_s->p2p_send_action_work)
974 return;
975
976 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
977 os_free(wpa_s->p2p_send_action_work->ctx);
978 radio_work_done(wpa_s->p2p_send_action_work);
979 wpa_s->p2p_send_action_work = NULL;
980}
981
982
Dmitry Shmidt03658832014-08-13 11:03:49 -0700983static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800985 if (wpa_s->p2p_send_action_work) {
986 struct send_action_work *awork;
987 awork = wpa_s->p2p_send_action_work->ctx;
988 if (awork->wait_time == 0) {
989 os_free(awork);
990 radio_work_done(wpa_s->p2p_send_action_work);
991 wpa_s->p2p_send_action_work = NULL;
992 } else {
993 /*
994 * In theory, this should not be needed, but number of
995 * places in the P2P code is still using non-zero wait
996 * time for the last Action frame in the sequence and
997 * some of these do not call send_action_done().
998 */
999 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1000 wpa_s, NULL);
1001 eloop_register_timeout(
1002 0, awork->wait_time * 1000,
1003 wpas_p2p_send_action_work_timeout,
1004 wpa_s, NULL);
1005 }
1006 }
Dmitry Shmidt03658832014-08-13 11:03:49 -07001007}
1008
1009
1010static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1011 unsigned int freq,
1012 const u8 *dst, const u8 *src,
1013 const u8 *bssid,
1014 const u8 *data, size_t data_len,
1015 enum offchannel_send_action_result
1016 result)
1017{
1018 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1019
1020 wpas_p2p_action_tx_clear(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1023 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025 switch (result) {
1026 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1027 res = P2P_SEND_ACTION_SUCCESS;
1028 break;
1029 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1030 res = P2P_SEND_ACTION_NO_ACK;
1031 break;
1032 case OFFCHANNEL_SEND_ACTION_FAILED:
1033 res = P2P_SEND_ACTION_FAILED;
1034 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001035 }
1036
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001037 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001039 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1040 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001041 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001042 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1043 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001045 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1046 "during p2p_connect-auto");
1047 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1048 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 }
1050}
1051
1052
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001053static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1054{
1055 struct wpa_supplicant *wpa_s = work->wpa_s;
1056 struct send_action_work *awork = work->ctx;
1057
1058 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001059 if (work->started) {
1060 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1061 wpa_s, NULL);
1062 wpa_s->p2p_send_action_work = NULL;
1063 offchannel_send_action_done(wpa_s);
1064 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001065 os_free(awork);
1066 return;
1067 }
1068
1069 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1070 awork->bssid, awork->buf, awork->len,
1071 awork->wait_time,
1072 wpas_p2p_send_action_tx_status, 1) < 0) {
1073 os_free(awork);
1074 radio_work_done(work);
1075 return;
1076 }
1077 wpa_s->p2p_send_action_work = work;
1078}
1079
1080
1081static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1082 unsigned int freq, const u8 *dst,
1083 const u8 *src, const u8 *bssid, const u8 *buf,
1084 size_t len, unsigned int wait_time)
1085{
1086 struct send_action_work *awork;
1087
1088 if (wpa_s->p2p_send_action_work) {
1089 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1090 return -1;
1091 }
1092
1093 awork = os_zalloc(sizeof(*awork) + len);
1094 if (awork == NULL)
1095 return -1;
1096
1097 awork->freq = freq;
1098 os_memcpy(awork->dst, dst, ETH_ALEN);
1099 os_memcpy(awork->src, src, ETH_ALEN);
1100 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1101 awork->len = len;
1102 awork->wait_time = wait_time;
1103 os_memcpy(awork->buf, buf, len);
1104
1105 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1106 wpas_send_action_cb, awork) < 0) {
1107 os_free(awork);
1108 return -1;
1109 }
1110
1111 return 0;
1112}
1113
1114
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1116 const u8 *src, const u8 *bssid, const u8 *buf,
1117 size_t len, unsigned int wait_time)
1118{
1119 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001120 int listen_freq = -1, send_freq = -1;
1121
1122 if (wpa_s->p2p_listen_work)
1123 listen_freq = wpa_s->p2p_listen_work->freq;
1124 if (wpa_s->p2p_send_action_work)
1125 send_freq = wpa_s->p2p_send_action_work->freq;
1126 if (listen_freq != (int) freq && send_freq != (int) freq) {
1127 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1128 listen_freq, send_freq);
1129 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1130 len, wait_time);
1131 }
1132
1133 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001134 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1135 wait_time,
1136 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001137}
1138
1139
1140static void wpas_send_action_done(void *ctx)
1141{
1142 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001143
1144 if (wpa_s->p2p_send_action_work) {
1145 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1146 wpa_s, NULL);
1147 os_free(wpa_s->p2p_send_action_work->ctx);
1148 radio_work_done(wpa_s->p2p_send_action_work);
1149 wpa_s->p2p_send_action_work = NULL;
1150 }
1151
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001152 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153}
1154
1155
1156static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1157 struct p2p_go_neg_results *params)
1158{
1159 if (wpa_s->go_params == NULL) {
1160 wpa_s->go_params = os_malloc(sizeof(*params));
1161 if (wpa_s->go_params == NULL)
1162 return -1;
1163 }
1164 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1165 return 0;
1166}
1167
1168
1169static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1170 struct p2p_go_neg_results *res)
1171{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001172 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1173 " dev_addr " MACSTR " wps_method %d",
1174 MAC2STR(res->peer_interface_addr),
1175 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1177 res->ssid, res->ssid_len);
1178 wpa_supplicant_ap_deinit(wpa_s);
1179 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001180 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001182#ifdef CONFIG_WPS_NFC
1183 } else if (res->wps_method == WPS_NFC) {
1184 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1185 res->peer_interface_addr,
1186 wpa_s->parent->p2p_oob_dev_pw,
1187 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1188 wpa_s->parent->p2p_oob_dev_pw_id ==
1189 DEV_PW_NFC_CONNECTION_HANDOVER ?
1190 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1191 NULL,
1192 NULL, 0, 0);
1193#endif /* CONFIG_WPS_NFC */
1194 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 u16 dev_pw_id = DEV_PW_DEFAULT;
1196 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1197 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1198 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1199 wpa_s->p2p_pin, 1, dev_pw_id);
1200 }
1201}
1202
1203
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001204static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1205 struct wpa_ssid *ssid)
1206{
1207 struct wpa_ssid *persistent;
1208 struct psk_list_entry *psk;
1209 struct hostapd_data *hapd;
1210
1211 if (!wpa_s->ap_iface)
1212 return;
1213
1214 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1215 ssid->ssid_len);
1216 if (persistent == NULL)
1217 return;
1218
1219 hapd = wpa_s->ap_iface->bss[0];
1220
1221 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1222 list) {
1223 struct hostapd_wpa_psk *hpsk;
1224
1225 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1226 MACSTR " psk=%d",
1227 MAC2STR(psk->addr), psk->p2p);
1228 hpsk = os_zalloc(sizeof(*hpsk));
1229 if (hpsk == NULL)
1230 break;
1231 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1232 if (psk->p2p)
1233 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1234 else
1235 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1236 hpsk->next = hapd->conf->ssid.wpa_psk;
1237 hapd->conf->ssid.wpa_psk = hpsk;
1238 }
1239}
1240
1241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242static void p2p_go_configured(void *ctx, void *data)
1243{
1244 struct wpa_supplicant *wpa_s = ctx;
1245 struct p2p_go_neg_results *params = data;
1246 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001247 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001248
1249 ssid = wpa_s->current_ssid;
1250 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1251 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1252 if (wpa_s->global->p2p_group_formation == wpa_s)
1253 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07001254 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1255 params->passphrase[0] == '\0' ?
1256 params->psk : NULL,
1257 params->passphrase,
1258 wpa_s->global->p2p_dev_addr,
1259 params->persistent_group, "");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001260
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001261 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001262 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001263 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001265 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001266 wpas_p2p_add_psk_list(wpa_s, ssid);
1267 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001268 if (network_id < 0)
1269 network_id = ssid->id;
1270 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 wpas_p2p_cross_connect_setup(wpa_s);
1272 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001273
1274 if (wpa_s->p2p_first_connection_timeout) {
1275 wpa_dbg(wpa_s, MSG_DEBUG,
1276 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1277 wpa_s->p2p_first_connection_timeout);
1278 wpa_s->p2p_go_group_formation_completed = 0;
1279 wpa_s->global->p2p_group_formation = wpa_s;
1280 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1281 wpa_s->parent, NULL);
1282 eloop_register_timeout(
1283 wpa_s->p2p_first_connection_timeout, 0,
1284 wpas_p2p_group_formation_timeout,
1285 wpa_s->parent, NULL);
1286 }
1287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 return;
1289 }
1290
1291 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1292 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1293 params->peer_interface_addr)) {
1294 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1295 "filtering");
1296 return;
1297 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001298 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001301#ifdef CONFIG_WPS_NFC
1302 } else if (params->wps_method == WPS_NFC) {
1303 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1304 DEV_PW_NFC_CONNECTION_HANDOVER &&
1305 !wpa_s->parent->p2p_oob_dev_pw) {
1306 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1307 return;
1308 }
1309 wpas_ap_wps_add_nfc_pw(
1310 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1311 wpa_s->parent->p2p_oob_dev_pw,
1312 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1313 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1314#endif /* CONFIG_WPS_NFC */
1315 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001317 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 os_free(wpa_s->go_params);
1319 wpa_s->go_params = NULL;
1320}
1321
1322
1323static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1324 struct p2p_go_neg_results *params,
1325 int group_formation)
1326{
1327 struct wpa_ssid *ssid;
1328
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001329 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1330 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1331 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1332 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001333 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335
1336 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001337 if (ssid == NULL) {
1338 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001342 wpa_s->show_group_started = 0;
1343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001344 wpa_config_set_network_defaults(ssid);
1345 ssid->temporary = 1;
1346 ssid->p2p_group = 1;
1347 ssid->p2p_persistent_group = params->persistent_group;
1348 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1349 WPAS_MODE_P2P_GO;
1350 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001351 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001352 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 ssid->ssid = os_zalloc(params->ssid_len + 1);
1354 if (ssid->ssid) {
1355 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1356 ssid->ssid_len = params->ssid_len;
1357 }
1358 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1359 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1360 ssid->proto = WPA_PROTO_RSN;
1361 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362 if (os_strlen(params->passphrase) > 0) {
1363 ssid->passphrase = os_strdup(params->passphrase);
1364 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001365 wpa_msg_global(wpa_s, MSG_ERROR,
1366 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001367 wpa_config_remove_network(wpa_s->conf, ssid->id);
1368 return;
1369 }
1370 } else
1371 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001372 ssid->psk_set = params->psk_set;
1373 if (ssid->psk_set)
1374 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001375 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001376 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001377 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378
1379 wpa_s->ap_configured_cb = p2p_go_configured;
1380 wpa_s->ap_configured_cb_ctx = wpa_s;
1381 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07001382 wpa_s->scan_req = NORMAL_SCAN_REQ;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001383 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 wpa_s->reassociate = 1;
1385 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001386 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1387 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 wpa_supplicant_req_scan(wpa_s, 0, 0);
1389}
1390
1391
1392static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1393 const struct wpa_supplicant *src)
1394{
1395 struct wpa_config *d;
1396 const struct wpa_config *s;
1397
1398 d = dst->conf;
1399 s = src->conf;
1400
1401#define C(n) if (s->n) d->n = os_strdup(s->n)
1402 C(device_name);
1403 C(manufacturer);
1404 C(model_name);
1405 C(model_number);
1406 C(serial_number);
1407 C(config_methods);
1408#undef C
1409
1410 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1411 os_memcpy(d->sec_device_type, s->sec_device_type,
1412 sizeof(d->sec_device_type));
1413 d->num_sec_device_types = s->num_sec_device_types;
1414
1415 d->p2p_group_idle = s->p2p_group_idle;
1416 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001417 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001418 d->max_num_sta = s->max_num_sta;
1419 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001420 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001421 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001422 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001423 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001424 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001425
1426 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1427 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1428 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430}
1431
1432
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001433static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1434 char *ifname, size_t len)
1435{
1436 char *ifname_ptr = wpa_s->ifname;
1437
1438 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1439 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1440 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1441 }
1442
1443 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1444 if (os_strlen(ifname) >= IFNAMSIZ &&
1445 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1446 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001447 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001448 }
1449}
1450
1451
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1453 enum wpa_driver_if_type type)
1454{
1455 char ifname[120], force_ifname[120];
1456
1457 if (wpa_s->pending_interface_name[0]) {
1458 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1459 "- skip creation of a new one");
1460 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1461 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1462 "unknown?! ifname='%s'",
1463 wpa_s->pending_interface_name);
1464 return -1;
1465 }
1466 return 0;
1467 }
1468
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001469 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 force_ifname[0] = '\0';
1471
1472 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1473 ifname);
1474 wpa_s->p2p_group_idx++;
1475
1476 wpa_s->pending_interface_type = type;
1477 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1478 wpa_s->pending_interface_addr, NULL) < 0) {
1479 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1480 "interface");
1481 return -1;
1482 }
1483
1484 if (force_ifname[0]) {
1485 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1486 force_ifname);
1487 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1488 sizeof(wpa_s->pending_interface_name));
1489 } else
1490 os_strlcpy(wpa_s->pending_interface_name, ifname,
1491 sizeof(wpa_s->pending_interface_name));
1492 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1493 MACSTR, wpa_s->pending_interface_name,
1494 MAC2STR(wpa_s->pending_interface_addr));
1495
1496 return 0;
1497}
1498
1499
1500static void wpas_p2p_remove_pending_group_interface(
1501 struct wpa_supplicant *wpa_s)
1502{
1503 if (!wpa_s->pending_interface_name[0] ||
1504 is_zero_ether_addr(wpa_s->pending_interface_addr))
1505 return; /* No pending virtual interface */
1506
1507 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1508 wpa_s->pending_interface_name);
1509 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1510 wpa_s->pending_interface_name);
1511 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1512 wpa_s->pending_interface_name[0] = '\0';
1513}
1514
1515
1516static struct wpa_supplicant *
1517wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1518{
1519 struct wpa_interface iface;
1520 struct wpa_supplicant *group_wpa_s;
1521
1522 if (!wpa_s->pending_interface_name[0]) {
1523 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1524 if (!wpas_p2p_create_iface(wpa_s))
1525 return NULL;
1526 /*
1527 * Something has forced us to remove the pending interface; try
1528 * to create a new one and hope for the best that we will get
1529 * the same local address.
1530 */
1531 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1532 WPA_IF_P2P_CLIENT) < 0)
1533 return NULL;
1534 }
1535
1536 os_memset(&iface, 0, sizeof(iface));
1537 iface.ifname = wpa_s->pending_interface_name;
1538 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001539 if (wpa_s->conf->ctrl_interface == NULL &&
1540 wpa_s->parent != wpa_s &&
1541 wpa_s->p2p_mgmt &&
1542 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1543 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1544 else
1545 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 iface.driver_param = wpa_s->conf->driver_param;
1547 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1548 if (group_wpa_s == NULL) {
1549 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1550 "wpa_supplicant interface");
1551 return NULL;
1552 }
1553 wpa_s->pending_interface_name[0] = '\0';
1554 group_wpa_s->parent = wpa_s;
1555 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1556 P2P_GROUP_INTERFACE_CLIENT;
1557 wpa_s->global->p2p_group_formation = group_wpa_s;
1558
1559 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1560
1561 return group_wpa_s;
1562}
1563
1564
1565static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1566 void *timeout_ctx)
1567{
1568 struct wpa_supplicant *wpa_s = eloop_ctx;
1569 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001570 wpas_p2p_group_formation_failed(wpa_s);
1571}
1572
1573
1574void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1575{
1576 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1577 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 if (wpa_s->global->p2p)
1579 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 wpas_group_formation_completed(wpa_s, 0);
1581}
1582
1583
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001584static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1585{
1586 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1587 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1588 wpa_s->parent, NULL);
1589 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1590 wpa_s->parent, NULL);
1591 wpa_s->global->p2p_fail_on_wps_complete = 0;
1592}
1593
1594
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001595void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1596{
1597 if (wpa_s->global->p2p_group_formation != wpa_s)
1598 return;
1599 /* Speed up group formation timeout since this cannot succeed */
1600 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1601 wpa_s->parent, NULL);
1602 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1603 wpa_s->parent, NULL);
1604}
1605
1606
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001607static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608{
1609 struct wpa_supplicant *wpa_s = ctx;
1610
1611 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1612 wpa_drv_cancel_remain_on_channel(wpa_s);
1613 wpa_s->off_channel_freq = 0;
1614 wpa_s->roc_waiting_drv_freq = 0;
1615 }
1616
1617 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001618 wpa_msg_global(wpa_s, MSG_INFO,
1619 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1620 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001621 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 wpas_p2p_remove_pending_group_interface(wpa_s);
1623 return;
1624 }
1625
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001626 if (wpa_s->p2p_go_ht40)
1627 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001628 if (wpa_s->p2p_go_vht)
1629 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001630
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001631 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1632 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1633 " wps_method=%s",
1634 res->role_go ? "GO" : "client", res->freq, res->ht40,
1635 MAC2STR(res->peer_device_addr),
1636 MAC2STR(res->peer_interface_addr),
1637 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001638 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639
Dmitry Shmidt04949592012-07-19 12:16:46 -07001640 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1641 struct wpa_ssid *ssid;
1642 ssid = wpa_config_get_network(wpa_s->conf,
1643 wpa_s->p2p_persistent_id);
1644 if (ssid && ssid->disabled == 2 &&
1645 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1646 size_t len = os_strlen(ssid->passphrase);
1647 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1648 "on requested persistent group");
1649 os_memcpy(res->passphrase, ssid->passphrase, len);
1650 res->passphrase[len] = '\0';
1651 }
1652 }
1653
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654 if (wpa_s->create_p2p_iface) {
1655 struct wpa_supplicant *group_wpa_s =
1656 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1657 if (group_wpa_s == NULL) {
1658 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001659 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1660 wpa_s, NULL);
1661 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 return;
1663 }
1664 if (group_wpa_s != wpa_s) {
1665 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1666 sizeof(group_wpa_s->p2p_pin));
1667 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1668 }
1669 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1670 wpa_s->pending_interface_name[0] = '\0';
1671 group_wpa_s->p2p_in_provisioning = 1;
1672
1673 if (res->role_go)
1674 wpas_start_wps_go(group_wpa_s, res, 1);
1675 else
1676 wpas_start_wps_enrollee(group_wpa_s, res);
1677 } else {
1678 wpa_s->p2p_in_provisioning = 1;
1679 wpa_s->global->p2p_group_formation = wpa_s;
1680
1681 if (res->role_go)
1682 wpas_start_wps_go(wpa_s, res, 1);
1683 else
1684 wpas_start_wps_enrollee(ctx, res);
1685 }
1686
1687 wpa_s->p2p_long_listen = 0;
1688 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1689
1690 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1691 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1692 (res->peer_config_timeout % 100) * 10000,
1693 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1694}
1695
1696
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001697static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698{
1699 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001700 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1701 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702
1703 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1704}
1705
1706
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001707static void wpas_dev_found(void *ctx, const u8 *addr,
1708 const struct p2p_peer_info *info,
1709 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 struct wpa_supplicant *wpa_s = ctx;
1713 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001714 char *wfd_dev_info_hex = NULL;
1715
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001716#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001717 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1718 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001719#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001720
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001721 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1722 " p2p_dev_addr=" MACSTR
1723 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07001724 "dev_capab=0x%x group_capab=0x%x%s%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001725 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1726 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1727 sizeof(devtype)),
1728 info->device_name, info->config_methods,
1729 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001730 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07001731 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1732 info->vendor_elems ? " vendor_elems=1" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001734 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001735#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001736
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001737 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1738}
1739
1740
1741static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1742{
1743 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001744
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001745 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1746 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1749}
1750
1751
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001752static void wpas_find_stopped(void *ctx)
1753{
1754 struct wpa_supplicant *wpa_s = ctx;
1755 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1756}
1757
1758
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001759struct wpas_p2p_listen_work {
1760 unsigned int freq;
1761 unsigned int duration;
1762 struct wpabuf *probe_resp_ie;
1763};
1764
1765
1766static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1767{
1768 if (lwork == NULL)
1769 return;
1770 wpabuf_free(lwork->probe_resp_ie);
1771 os_free(lwork);
1772}
1773
1774
1775static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1776{
1777 struct wpas_p2p_listen_work *lwork;
1778
1779 if (!wpa_s->p2p_listen_work)
1780 return;
1781
1782 lwork = wpa_s->p2p_listen_work->ctx;
1783 wpas_p2p_listen_work_free(lwork);
1784 radio_work_done(wpa_s->p2p_listen_work);
1785 wpa_s->p2p_listen_work = NULL;
1786}
1787
1788
1789static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1790{
1791 struct wpa_supplicant *wpa_s = work->wpa_s;
1792 struct wpas_p2p_listen_work *lwork = work->ctx;
1793
1794 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001795 if (work->started) {
1796 wpa_s->p2p_listen_work = NULL;
1797 wpas_stop_listen(wpa_s);
1798 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001799 wpas_p2p_listen_work_free(lwork);
1800 return;
1801 }
1802
1803 wpa_s->p2p_listen_work = work;
1804
1805 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1806
1807 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1808 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1809 "report received Probe Request frames");
1810 wpas_p2p_listen_work_done(wpa_s);
1811 return;
1812 }
1813
1814 wpa_s->pending_listen_freq = lwork->freq;
1815 wpa_s->pending_listen_duration = lwork->duration;
1816
1817 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1818 {
1819 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1820 "to remain on channel (%u MHz) for Listen "
1821 "state", lwork->freq);
1822 wpas_p2p_listen_work_done(wpa_s);
1823 wpa_s->pending_listen_freq = 0;
1824 return;
1825 }
1826 wpa_s->off_channel_freq = 0;
1827 wpa_s->roc_waiting_drv_freq = lwork->freq;
1828}
1829
1830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831static int wpas_start_listen(void *ctx, unsigned int freq,
1832 unsigned int duration,
1833 const struct wpabuf *probe_resp_ie)
1834{
1835 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001836 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001838 if (wpa_s->p2p_listen_work) {
1839 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 return -1;
1841 }
1842
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001843 lwork = os_zalloc(sizeof(*lwork));
1844 if (lwork == NULL)
1845 return -1;
1846 lwork->freq = freq;
1847 lwork->duration = duration;
1848 if (probe_resp_ie) {
1849 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1850 if (lwork->probe_resp_ie == NULL) {
1851 wpas_p2p_listen_work_free(lwork);
1852 return -1;
1853 }
1854 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001856 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1857 lwork) < 0) {
1858 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 return -1;
1860 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861
1862 return 0;
1863}
1864
1865
1866static void wpas_stop_listen(void *ctx)
1867{
1868 struct wpa_supplicant *wpa_s = ctx;
1869 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1870 wpa_drv_cancel_remain_on_channel(wpa_s);
1871 wpa_s->off_channel_freq = 0;
1872 wpa_s->roc_waiting_drv_freq = 0;
1873 }
1874 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1875 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001876 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877}
1878
1879
1880static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1881{
1882 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001883 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884}
1885
1886
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001887/*
1888 * DNS Header section is used only to calculate compression pointers, so the
1889 * contents of this data does not matter, but the length needs to be reserved
1890 * in the virtual packet.
1891 */
1892#define DNS_HEADER_LEN 12
1893
1894/*
1895 * 27-octet in-memory packet from P2P specification containing two implied
1896 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1897 */
1898#define P2P_SD_IN_MEMORY_LEN 27
1899
1900static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1901 u8 **spos, const u8 *end)
1902{
1903 while (*spos < end) {
1904 u8 val = ((*spos)[0] & 0xc0) >> 6;
1905 int len;
1906
1907 if (val == 1 || val == 2) {
1908 /* These are reserved values in RFC 1035 */
1909 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1910 "sequence starting with 0x%x", val);
1911 return -1;
1912 }
1913
1914 if (val == 3) {
1915 u16 offset;
1916 u8 *spos_tmp;
1917
1918 /* Offset */
1919 if (*spos + 2 > end) {
1920 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1921 "DNS offset field");
1922 return -1;
1923 }
1924
1925 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1926 if (offset >= *spos - start) {
1927 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1928 "pointer offset %u", offset);
1929 return -1;
1930 }
1931
1932 (*spos) += 2;
1933 spos_tmp = start + offset;
1934 return p2p_sd_dns_uncompress_label(upos, uend, start,
1935 &spos_tmp,
1936 *spos - 2);
1937 }
1938
1939 /* Label */
1940 len = (*spos)[0] & 0x3f;
1941 if (len == 0)
1942 return 0;
1943
1944 (*spos)++;
1945 if (*spos + len > end) {
1946 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1947 "sequence - no room for label with length "
1948 "%u", len);
1949 return -1;
1950 }
1951
1952 if (*upos + len + 2 > uend)
1953 return -2;
1954
1955 os_memcpy(*upos, *spos, len);
1956 *spos += len;
1957 *upos += len;
1958 (*upos)[0] = '.';
1959 (*upos)++;
1960 (*upos)[0] = '\0';
1961 }
1962
1963 return 0;
1964}
1965
1966
1967/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1968 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1969 * not large enough */
1970static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1971 size_t msg_len, size_t offset)
1972{
1973 /* 27-octet in-memory packet from P2P specification */
1974 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1975 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1976 u8 *tmp, *end, *spos;
1977 char *upos, *uend;
1978 int ret = 0;
1979
1980 if (buf_len < 2)
1981 return -1;
1982 if (offset > msg_len)
1983 return -1;
1984
1985 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1986 if (tmp == NULL)
1987 return -1;
1988 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1989 end = spos + msg_len;
1990 spos += offset;
1991
1992 os_memset(tmp, 0, DNS_HEADER_LEN);
1993 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1994 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1995
1996 upos = buf;
1997 uend = buf + buf_len;
1998
1999 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2000 if (ret) {
2001 os_free(tmp);
2002 return ret;
2003 }
2004
2005 if (upos == buf) {
2006 upos[0] = '.';
2007 upos[1] = '\0';
2008 } else if (upos[-1] == '.')
2009 upos[-1] = '\0';
2010
2011 os_free(tmp);
2012 return 0;
2013}
2014
2015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016static struct p2p_srv_bonjour *
2017wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2018 const struct wpabuf *query)
2019{
2020 struct p2p_srv_bonjour *bsrv;
2021 size_t len;
2022
2023 len = wpabuf_len(query);
2024 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2025 struct p2p_srv_bonjour, list) {
2026 if (len == wpabuf_len(bsrv->query) &&
2027 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2028 len) == 0)
2029 return bsrv;
2030 }
2031 return NULL;
2032}
2033
2034
2035static struct p2p_srv_upnp *
2036wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2037 const char *service)
2038{
2039 struct p2p_srv_upnp *usrv;
2040
2041 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2042 struct p2p_srv_upnp, list) {
2043 if (version == usrv->version &&
2044 os_strcmp(service, usrv->service) == 0)
2045 return usrv;
2046 }
2047 return NULL;
2048}
2049
2050
2051static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2052 u8 srv_trans_id)
2053{
2054 u8 *len_pos;
2055
2056 if (wpabuf_tailroom(resp) < 5)
2057 return;
2058
2059 /* Length (to be filled) */
2060 len_pos = wpabuf_put(resp, 2);
2061 wpabuf_put_u8(resp, srv_proto);
2062 wpabuf_put_u8(resp, srv_trans_id);
2063 /* Status Code */
2064 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2065 /* Response Data: empty */
2066 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2067}
2068
2069
2070static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2071 struct wpabuf *resp, u8 srv_trans_id)
2072{
2073 struct p2p_srv_bonjour *bsrv;
2074 u8 *len_pos;
2075
2076 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2077
2078 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2079 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2080 return;
2081 }
2082
2083 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2084 struct p2p_srv_bonjour, list) {
2085 if (wpabuf_tailroom(resp) <
2086 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2087 return;
2088 /* Length (to be filled) */
2089 len_pos = wpabuf_put(resp, 2);
2090 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2091 wpabuf_put_u8(resp, srv_trans_id);
2092 /* Status Code */
2093 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2094 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2095 wpabuf_head(bsrv->resp),
2096 wpabuf_len(bsrv->resp));
2097 /* Response Data */
2098 wpabuf_put_buf(resp, bsrv->query); /* Key */
2099 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2100 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2101 2);
2102 }
2103}
2104
2105
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002106static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2107 size_t query_len)
2108{
2109 char str_rx[256], str_srv[256];
2110
2111 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2112 return 0; /* Too short to include DNS Type and Version */
2113 if (os_memcmp(query + query_len - 3,
2114 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2115 3) != 0)
2116 return 0; /* Mismatch in DNS Type or Version */
2117 if (query_len == wpabuf_len(bsrv->query) &&
2118 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2119 return 1; /* Binary match */
2120
2121 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2122 0))
2123 return 0; /* Failed to uncompress query */
2124 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2125 wpabuf_head(bsrv->query),
2126 wpabuf_len(bsrv->query) - 3, 0))
2127 return 0; /* Failed to uncompress service */
2128
2129 return os_strcmp(str_rx, str_srv) == 0;
2130}
2131
2132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2134 struct wpabuf *resp, u8 srv_trans_id,
2135 const u8 *query, size_t query_len)
2136{
2137 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002139 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140
2141 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2142 query, query_len);
2143 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2144 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2145 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2146 srv_trans_id);
2147 return;
2148 }
2149
2150 if (query_len == 0) {
2151 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2152 return;
2153 }
2154
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002155 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2156 struct p2p_srv_bonjour, list) {
2157 if (!match_bonjour_query(bsrv, query, query_len))
2158 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002160 if (wpabuf_tailroom(resp) <
2161 5 + query_len + wpabuf_len(bsrv->resp))
2162 return;
2163
2164 matches++;
2165
2166 /* Length (to be filled) */
2167 len_pos = wpabuf_put(resp, 2);
2168 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2169 wpabuf_put_u8(resp, srv_trans_id);
2170
2171 /* Status Code */
2172 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2173 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2174 wpabuf_head(bsrv->resp),
2175 wpabuf_len(bsrv->resp));
2176
2177 /* Response Data */
2178 wpabuf_put_data(resp, query, query_len); /* Key */
2179 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2180
2181 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2182 }
2183
2184 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2186 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002187 if (wpabuf_tailroom(resp) < 5)
2188 return;
2189
2190 /* Length (to be filled) */
2191 len_pos = wpabuf_put(resp, 2);
2192 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2193 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194
2195 /* Status Code */
2196 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2197 /* Response Data: empty */
2198 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2199 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201}
2202
2203
2204static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2205 struct wpabuf *resp, u8 srv_trans_id)
2206{
2207 struct p2p_srv_upnp *usrv;
2208 u8 *len_pos;
2209
2210 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2211
2212 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2213 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2214 return;
2215 }
2216
2217 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2218 struct p2p_srv_upnp, list) {
2219 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2220 return;
2221
2222 /* Length (to be filled) */
2223 len_pos = wpabuf_put(resp, 2);
2224 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2225 wpabuf_put_u8(resp, srv_trans_id);
2226
2227 /* Status Code */
2228 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2229 /* Response Data */
2230 wpabuf_put_u8(resp, usrv->version);
2231 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2232 usrv->service);
2233 wpabuf_put_str(resp, usrv->service);
2234 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2235 2);
2236 }
2237}
2238
2239
2240static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2241 struct wpabuf *resp, u8 srv_trans_id,
2242 const u8 *query, size_t query_len)
2243{
2244 struct p2p_srv_upnp *usrv;
2245 u8 *len_pos;
2246 u8 version;
2247 char *str;
2248 int count = 0;
2249
2250 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2251 query, query_len);
2252
2253 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2254 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2255 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2256 srv_trans_id);
2257 return;
2258 }
2259
2260 if (query_len == 0) {
2261 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2262 return;
2263 }
2264
2265 if (wpabuf_tailroom(resp) < 5)
2266 return;
2267
2268 /* Length (to be filled) */
2269 len_pos = wpabuf_put(resp, 2);
2270 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2271 wpabuf_put_u8(resp, srv_trans_id);
2272
2273 version = query[0];
2274 str = os_malloc(query_len);
2275 if (str == NULL)
2276 return;
2277 os_memcpy(str, query + 1, query_len - 1);
2278 str[query_len - 1] = '\0';
2279
2280 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2281 struct p2p_srv_upnp, list) {
2282 if (version != usrv->version)
2283 continue;
2284
2285 if (os_strcmp(str, "ssdp:all") != 0 &&
2286 os_strstr(usrv->service, str) == NULL)
2287 continue;
2288
2289 if (wpabuf_tailroom(resp) < 2)
2290 break;
2291 if (count == 0) {
2292 /* Status Code */
2293 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2294 /* Response Data */
2295 wpabuf_put_u8(resp, version);
2296 } else
2297 wpabuf_put_u8(resp, ',');
2298
2299 count++;
2300
2301 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2302 usrv->service);
2303 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2304 break;
2305 wpabuf_put_str(resp, usrv->service);
2306 }
2307 os_free(str);
2308
2309 if (count == 0) {
2310 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2311 "available");
2312 /* Status Code */
2313 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2314 /* Response Data: empty */
2315 }
2316
2317 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2318}
2319
2320
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002321#ifdef CONFIG_WIFI_DISPLAY
2322static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2323 struct wpabuf *resp, u8 srv_trans_id,
2324 const u8 *query, size_t query_len)
2325{
2326 const u8 *pos;
2327 u8 role;
2328 u8 *len_pos;
2329
2330 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2331
2332 if (!wpa_s->global->wifi_display) {
2333 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2334 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2335 srv_trans_id);
2336 return;
2337 }
2338
2339 if (query_len < 1) {
2340 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2341 "Role");
2342 return;
2343 }
2344
2345 if (wpabuf_tailroom(resp) < 5)
2346 return;
2347
2348 pos = query;
2349 role = *pos++;
2350 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2351
2352 /* TODO: role specific handling */
2353
2354 /* Length (to be filled) */
2355 len_pos = wpabuf_put(resp, 2);
2356 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2357 wpabuf_put_u8(resp, srv_trans_id);
2358 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2359
2360 while (pos < query + query_len) {
2361 if (*pos < MAX_WFD_SUBELEMS &&
2362 wpa_s->global->wfd_subelem[*pos] &&
2363 wpabuf_tailroom(resp) >=
2364 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2365 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2366 "subelement %u", *pos);
2367 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2368 }
2369 pos++;
2370 }
2371
2372 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2373}
2374#endif /* CONFIG_WIFI_DISPLAY */
2375
2376
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002377static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2378 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002379{
2380 struct wpa_supplicant *wpa_s = ctx;
2381 const u8 *pos = tlvs;
2382 const u8 *end = tlvs + tlvs_len;
2383 const u8 *tlv_end;
2384 u16 slen;
2385 struct wpabuf *resp;
2386 u8 srv_proto, srv_trans_id;
2387 size_t buf_len;
2388 char *buf;
2389
2390 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2391 tlvs, tlvs_len);
2392 buf_len = 2 * tlvs_len + 1;
2393 buf = os_malloc(buf_len);
2394 if (buf) {
2395 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2396 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2397 MACSTR " %u %u %s",
2398 freq, MAC2STR(sa), dialog_token, update_indic,
2399 buf);
2400 os_free(buf);
2401 }
2402
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002403 if (wpa_s->p2p_sd_over_ctrl_iface) {
2404 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2405 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002407 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002408
2409 resp = wpabuf_alloc(10000);
2410 if (resp == NULL)
2411 return;
2412
2413 while (pos + 1 < end) {
2414 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2415 slen = WPA_GET_LE16(pos);
2416 pos += 2;
2417 if (pos + slen > end || slen < 2) {
2418 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2419 "length");
2420 wpabuf_free(resp);
2421 return;
2422 }
2423 tlv_end = pos + slen;
2424
2425 srv_proto = *pos++;
2426 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2427 srv_proto);
2428 srv_trans_id = *pos++;
2429 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2430 srv_trans_id);
2431
2432 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2433 pos, tlv_end - pos);
2434
2435
2436 if (wpa_s->force_long_sd) {
2437 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2438 "response");
2439 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2440 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2441 goto done;
2442 }
2443
2444 switch (srv_proto) {
2445 case P2P_SERV_ALL_SERVICES:
2446 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2447 "for all services");
2448 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2449 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2450 wpa_printf(MSG_DEBUG, "P2P: No service "
2451 "discovery protocols available");
2452 wpas_sd_add_proto_not_avail(
2453 resp, P2P_SERV_ALL_SERVICES,
2454 srv_trans_id);
2455 break;
2456 }
2457 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2458 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2459 break;
2460 case P2P_SERV_BONJOUR:
2461 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2462 pos, tlv_end - pos);
2463 break;
2464 case P2P_SERV_UPNP:
2465 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2466 pos, tlv_end - pos);
2467 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002468#ifdef CONFIG_WIFI_DISPLAY
2469 case P2P_SERV_WIFI_DISPLAY:
2470 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2471 pos, tlv_end - pos);
2472 break;
2473#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 default:
2475 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2476 "protocol %u", srv_proto);
2477 wpas_sd_add_proto_not_avail(resp, srv_proto,
2478 srv_trans_id);
2479 break;
2480 }
2481
2482 pos = tlv_end;
2483 }
2484
2485done:
2486 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2487 update_indic, tlvs, tlvs_len);
2488
2489 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2490
2491 wpabuf_free(resp);
2492}
2493
2494
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002495static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2496 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497{
2498 struct wpa_supplicant *wpa_s = ctx;
2499 const u8 *pos = tlvs;
2500 const u8 *end = tlvs + tlvs_len;
2501 const u8 *tlv_end;
2502 u16 slen;
2503 size_t buf_len;
2504 char *buf;
2505
2506 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2507 tlvs, tlvs_len);
2508 if (tlvs_len > 1500) {
2509 /* TODO: better way for handling this */
2510 wpa_msg_ctrl(wpa_s, MSG_INFO,
2511 P2P_EVENT_SERV_DISC_RESP MACSTR
2512 " %u <long response: %u bytes>",
2513 MAC2STR(sa), update_indic,
2514 (unsigned int) tlvs_len);
2515 } else {
2516 buf_len = 2 * tlvs_len + 1;
2517 buf = os_malloc(buf_len);
2518 if (buf) {
2519 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2520 wpa_msg_ctrl(wpa_s, MSG_INFO,
2521 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2522 MAC2STR(sa), update_indic, buf);
2523 os_free(buf);
2524 }
2525 }
2526
2527 while (pos < end) {
2528 u8 srv_proto, srv_trans_id, status;
2529
2530 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2531 slen = WPA_GET_LE16(pos);
2532 pos += 2;
2533 if (pos + slen > end || slen < 3) {
2534 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2535 "length");
2536 return;
2537 }
2538 tlv_end = pos + slen;
2539
2540 srv_proto = *pos++;
2541 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2542 srv_proto);
2543 srv_trans_id = *pos++;
2544 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2545 srv_trans_id);
2546 status = *pos++;
2547 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2548 status);
2549
2550 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2551 pos, tlv_end - pos);
2552
2553 pos = tlv_end;
2554 }
2555
2556 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2557}
2558
2559
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002560u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2561 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002564 return 0;
2565 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002566}
2567
2568
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002569u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2570 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571{
2572 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002573 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574
2575 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2576 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002577 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2579 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2580 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2581 wpabuf_put_u8(tlvs, version);
2582 wpabuf_put_str(tlvs, query);
2583 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2584 wpabuf_free(tlvs);
2585 return ret;
2586}
2587
2588
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002589#ifdef CONFIG_WIFI_DISPLAY
2590
2591static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2592 const struct wpabuf *tlvs)
2593{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002594 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2595 return 0;
2596 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2597}
2598
2599
2600#define MAX_WFD_SD_SUBELEMS 20
2601
2602static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2603 const char *subelems)
2604{
2605 u8 *len;
2606 const char *pos;
2607 int val;
2608 int count = 0;
2609
2610 len = wpabuf_put(tlvs, 2);
2611 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2612 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2613
2614 wpabuf_put_u8(tlvs, role);
2615
2616 pos = subelems;
2617 while (*pos) {
2618 val = atoi(pos);
2619 if (val >= 0 && val < 256) {
2620 wpabuf_put_u8(tlvs, val);
2621 count++;
2622 if (count == MAX_WFD_SD_SUBELEMS)
2623 break;
2624 }
2625 pos = os_strchr(pos + 1, ',');
2626 if (pos == NULL)
2627 break;
2628 pos++;
2629 }
2630
2631 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2632}
2633
2634
2635u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2636 const u8 *dst, const char *role)
2637{
2638 struct wpabuf *tlvs;
2639 u64 ret;
2640 const char *subelems;
2641 u8 id = 1;
2642
2643 subelems = os_strchr(role, ' ');
2644 if (subelems == NULL)
2645 return 0;
2646 subelems++;
2647
2648 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2649 if (tlvs == NULL)
2650 return 0;
2651
2652 if (os_strstr(role, "[source]"))
2653 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2654 if (os_strstr(role, "[pri-sink]"))
2655 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2656 if (os_strstr(role, "[sec-sink]"))
2657 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2658 if (os_strstr(role, "[source+sink]"))
2659 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2660
2661 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2662 wpabuf_free(tlvs);
2663 return ret;
2664}
2665
2666#endif /* CONFIG_WIFI_DISPLAY */
2667
2668
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002669int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002671 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2672 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002673 return p2p_sd_cancel_request(wpa_s->global->p2p,
2674 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675}
2676
2677
2678void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2679 const u8 *dst, u8 dialog_token,
2680 const struct wpabuf *resp_tlvs)
2681{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2683 return;
2684 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2685 resp_tlvs);
2686}
2687
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002688
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2690{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002691 if (wpa_s->global->p2p)
2692 p2p_sd_service_update(wpa_s->global->p2p);
2693}
2694
2695
2696static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2697{
2698 dl_list_del(&bsrv->list);
2699 wpabuf_free(bsrv->query);
2700 wpabuf_free(bsrv->resp);
2701 os_free(bsrv);
2702}
2703
2704
2705static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2706{
2707 dl_list_del(&usrv->list);
2708 os_free(usrv->service);
2709 os_free(usrv);
2710}
2711
2712
2713void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2714{
2715 struct p2p_srv_bonjour *bsrv, *bn;
2716 struct p2p_srv_upnp *usrv, *un;
2717
2718 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2719 struct p2p_srv_bonjour, list)
2720 wpas_p2p_srv_bonjour_free(bsrv);
2721
2722 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2723 struct p2p_srv_upnp, list)
2724 wpas_p2p_srv_upnp_free(usrv);
2725
2726 wpas_p2p_sd_service_update(wpa_s);
2727}
2728
2729
2730int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2731 struct wpabuf *query, struct wpabuf *resp)
2732{
2733 struct p2p_srv_bonjour *bsrv;
2734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 bsrv = os_zalloc(sizeof(*bsrv));
2736 if (bsrv == NULL)
2737 return -1;
2738 bsrv->query = query;
2739 bsrv->resp = resp;
2740 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2741
2742 wpas_p2p_sd_service_update(wpa_s);
2743 return 0;
2744}
2745
2746
2747int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2748 const struct wpabuf *query)
2749{
2750 struct p2p_srv_bonjour *bsrv;
2751
2752 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2753 if (bsrv == NULL)
2754 return -1;
2755 wpas_p2p_srv_bonjour_free(bsrv);
2756 wpas_p2p_sd_service_update(wpa_s);
2757 return 0;
2758}
2759
2760
2761int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2762 const char *service)
2763{
2764 struct p2p_srv_upnp *usrv;
2765
2766 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2767 return 0; /* Already listed */
2768 usrv = os_zalloc(sizeof(*usrv));
2769 if (usrv == NULL)
2770 return -1;
2771 usrv->version = version;
2772 usrv->service = os_strdup(service);
2773 if (usrv->service == NULL) {
2774 os_free(usrv);
2775 return -1;
2776 }
2777 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2778
2779 wpas_p2p_sd_service_update(wpa_s);
2780 return 0;
2781}
2782
2783
2784int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2785 const char *service)
2786{
2787 struct p2p_srv_upnp *usrv;
2788
2789 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2790 if (usrv == NULL)
2791 return -1;
2792 wpas_p2p_srv_upnp_free(usrv);
2793 wpas_p2p_sd_service_update(wpa_s);
2794 return 0;
2795}
2796
2797
2798static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2799 const u8 *peer, const char *params,
2800 unsigned int generated_pin)
2801{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002802 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2803 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804}
2805
2806
2807static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2808 const u8 *peer, const char *params)
2809{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002810 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2811 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812}
2813
2814
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002815static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2816 const u8 *dev_addr, const u8 *pri_dev_type,
2817 const char *dev_name, u16 supp_config_methods,
2818 u8 dev_capab, u8 group_capab, const u8 *group_id,
2819 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820{
2821 struct wpa_supplicant *wpa_s = ctx;
2822 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002823 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 u8 empty_dev_type[8];
2825 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002826 struct wpa_supplicant *group = NULL;
2827
2828 if (group_id) {
2829 for (group = wpa_s->global->ifaces; group; group = group->next)
2830 {
2831 struct wpa_ssid *s = group->current_ssid;
2832 if (s != NULL &&
2833 s->mode == WPAS_MODE_P2P_GO &&
2834 group_id_len - ETH_ALEN == s->ssid_len &&
2835 os_memcmp(group_id + ETH_ALEN, s->ssid,
2836 s->ssid_len) == 0)
2837 break;
2838 }
2839 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840
2841 if (pri_dev_type == NULL) {
2842 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2843 pri_dev_type = empty_dev_type;
2844 }
2845 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2846 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002847 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848 MAC2STR(dev_addr),
2849 wps_dev_type_bin2str(pri_dev_type, devtype,
2850 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002851 dev_name, supp_config_methods, dev_capab, group_capab,
2852 group ? " group=" : "",
2853 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002855
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 if (config_methods & WPS_CONFIG_DISPLAY) {
2857 generated_pin = wps_generate_pin();
2858 wpas_prov_disc_local_display(wpa_s, peer, params,
2859 generated_pin);
2860 } else if (config_methods & WPS_CONFIG_KEYPAD)
2861 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2862 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002863 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2864 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002865
2866 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2867 P2P_PROV_DISC_SUCCESS,
2868 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869}
2870
2871
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002872static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873{
2874 struct wpa_supplicant *wpa_s = ctx;
2875 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002876 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002878 if (wpa_s->pending_pd_before_join &&
2879 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2880 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2881 wpa_s->pending_pd_before_join = 0;
2882 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2883 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002884 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002885 return;
2886 }
2887
Dmitry Shmidt04949592012-07-19 12:16:46 -07002888 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2889 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2890 os_snprintf(params, sizeof(params), " peer_go=%d",
2891 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2892 else
2893 params[0] = '\0';
2894
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002896 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002897 else if (config_methods & WPS_CONFIG_KEYPAD) {
2898 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002899 wpas_prov_disc_local_display(wpa_s, peer, params,
2900 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002902 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2903 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904
Jouni Malinen75ecf522011-06-27 15:19:46 -07002905 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2906 P2P_PROV_DISC_SUCCESS,
2907 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908}
2909
2910
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002911static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2912 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002913{
2914 struct wpa_supplicant *wpa_s = ctx;
2915
Dmitry Shmidt04949592012-07-19 12:16:46 -07002916 if (wpa_s->p2p_fallback_to_go_neg) {
2917 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2918 "failed - fall back to GO Negotiation");
2919 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2920 return;
2921 }
2922
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002923 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002924 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002925 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2926 "join-existing-group operation (no ACK for PD "
2927 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002928 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002929 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002930 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002931
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002932 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2933 " p2p_dev_addr=" MACSTR " status=%d",
2934 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002935
Jouni Malinen75ecf522011-06-27 15:19:46 -07002936 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2937 status, 0, 0);
2938}
2939
2940
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002941static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2942{
2943 if (channels == NULL)
2944 return 1; /* Assume no restrictions */
2945 return p2p_channels_includes_freq(channels, freq);
2946
2947}
2948
2949
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002950/**
2951 * Pick the best frequency to use from all the currently used frequencies.
2952 */
2953static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2954 struct wpa_used_freq_data *freqs,
2955 unsigned int num)
2956{
2957 unsigned int i, c;
2958
2959 /* find a candidate freq that is supported by P2P */
2960 for (c = 0; c < num; c++)
2961 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2962 break;
2963
2964 if (c == num)
2965 return 0;
2966
2967 /* once we have a candidate, try to find a 'better' one */
2968 for (i = c + 1; i < num; i++) {
2969 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2970 continue;
2971
2972 /*
2973 * 1. Infrastructure station interfaces have higher preference.
2974 * 2. P2P Clients have higher preference.
2975 * 3. All others.
2976 */
2977 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2978 c = i;
2979 break;
2980 }
2981
2982 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2983 c = i;
2984 }
2985 return freqs[c].freq;
2986}
2987
2988
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002989static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2990 const u8 *go_dev_addr, const u8 *ssid,
2991 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002992 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002993 const struct p2p_channels *channels,
2994 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002995{
2996 struct wpa_supplicant *wpa_s = ctx;
2997 struct wpa_ssid *s;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002998 struct wpa_used_freq_data *freqs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 struct wpa_supplicant *grp;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003000 int best_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003001
3002 if (!persistent_group) {
3003 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003004 " to join an active group (SSID: %s)",
3005 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003006 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3007 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3008 == 0 ||
3009 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3010 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3011 "authorized invitation");
3012 goto accept_inv;
3013 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003014
3015#ifdef CONFIG_WPS_NFC
3016 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
3017 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
3018 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3019 wpa_s->parent->p2p_wps_method = WPS_NFC;
3020 wpa_s->parent->pending_join_wps_method = WPS_NFC;
3021 os_memcpy(wpa_s->parent->pending_join_dev_addr,
3022 go_dev_addr, ETH_ALEN);
3023 os_memcpy(wpa_s->parent->pending_join_iface_addr,
3024 bssid, ETH_ALEN);
3025 goto accept_inv;
3026 }
3027#endif /* CONFIG_WPS_NFC */
3028
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003029 /*
3030 * Do not accept the invitation automatically; notify user and
3031 * request approval.
3032 */
3033 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3034 }
3035
3036 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3037 if (grp) {
3038 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3039 "running persistent group");
3040 if (*go)
3041 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3042 goto accept_inv;
3043 }
3044
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003045 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3046 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3047 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3048 "invitation to re-invoke a persistent group");
Dmitry Shmidt76cd2cc2014-05-27 12:56:04 -07003049 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003050 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3052
3053 for (s = wpa_s->conf->ssid; s; s = s->next) {
3054 if (s->disabled == 2 &&
3055 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3056 s->ssid_len == ssid_len &&
3057 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3058 break;
3059 }
3060
3061 if (!s) {
3062 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3063 " requested reinvocation of an unknown group",
3064 MAC2STR(sa));
3065 return P2P_SC_FAIL_UNKNOWN_GROUP;
3066 }
3067
3068 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3069 *go = 1;
3070 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3071 wpa_printf(MSG_DEBUG, "P2P: The only available "
3072 "interface is already in use - reject "
3073 "invitation");
3074 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3075 }
3076 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3077 } else if (s->mode == WPAS_MODE_P2P_GO) {
3078 *go = 1;
3079 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3080 {
3081 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3082 "interface address for the group");
3083 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3084 }
3085 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3086 ETH_ALEN);
3087 }
3088
3089accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003090 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3091
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003092 best_freq = 0;
3093 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3094 sizeof(struct wpa_used_freq_data));
3095 if (freqs) {
3096 int num_channels = wpa_s->num_multichan_concurrent;
3097 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3098 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3099 os_free(freqs);
3100 }
3101
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003102 /* Get one of the frequencies currently in use */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003103 if (best_freq > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003104 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003105 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003106
3107 if (wpa_s->num_multichan_concurrent < 2 ||
3108 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3109 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 -07003110 *force_freq = best_freq;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003111 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 }
3113
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003114 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3115 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003116 if (*go == 0) {
3117 /* We are the client */
3118 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3119 "running a GO but we are capable of MCC, "
3120 "figure out the best channel to use");
3121 *force_freq = 0;
3122 } else if (!freq_included(channels, *force_freq)) {
3123 /* We are the GO, and *force_freq is not in the
3124 * intersection */
3125 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3126 "in intersection but we are capable of MCC, "
3127 "figure out the best channel to use",
3128 *force_freq);
3129 *force_freq = 0;
3130 }
3131 }
3132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003133 return P2P_SC_SUCCESS;
3134}
3135
3136
3137static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3138 const u8 *ssid, size_t ssid_len,
3139 const u8 *go_dev_addr, u8 status,
3140 int op_freq)
3141{
3142 struct wpa_supplicant *wpa_s = ctx;
3143 struct wpa_ssid *s;
3144
3145 for (s = wpa_s->conf->ssid; s; s = s->next) {
3146 if (s->disabled == 2 &&
3147 s->ssid_len == ssid_len &&
3148 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3149 break;
3150 }
3151
3152 if (status == P2P_SC_SUCCESS) {
3153 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003154 " was accepted; op_freq=%d MHz, SSID=%s",
3155 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003156 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003157 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003159 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003160 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003162 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003163 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003164 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003165 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 }
3167 return;
3168 }
3169
3170 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3171 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3172 " was rejected (status %u)", MAC2STR(sa), status);
3173 return;
3174 }
3175
3176 if (!s) {
3177 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003178 wpa_msg_global(wpa_s, MSG_INFO,
3179 P2P_EVENT_INVITATION_RECEIVED
3180 "sa=" MACSTR " go_dev_addr=" MACSTR
3181 " bssid=" MACSTR " unknown-network",
3182 MAC2STR(sa), MAC2STR(go_dev_addr),
3183 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003185 wpa_msg_global(wpa_s, MSG_INFO,
3186 P2P_EVENT_INVITATION_RECEIVED
3187 "sa=" MACSTR " go_dev_addr=" MACSTR
3188 " unknown-network",
3189 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190 }
3191 return;
3192 }
3193
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003194 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003195 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3196 "sa=" MACSTR " persistent=%d freq=%d",
3197 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003198 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003199 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3200 "sa=" MACSTR " persistent=%d",
3201 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003202 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203}
3204
3205
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003206static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3207 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003208 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003209{
3210 size_t i;
3211
3212 if (ssid == NULL)
3213 return;
3214
3215 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3216 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3217 ETH_ALEN) == 0)
3218 break;
3219 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07003220 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003221 if (ssid->mode != WPAS_MODE_P2P_GO &&
3222 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3223 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3224 "due to invitation result", ssid->id);
3225 wpas_notify_network_removed(wpa_s, ssid);
3226 wpa_config_remove_network(wpa_s->conf, ssid->id);
3227 return;
3228 }
3229 return; /* Peer not found in client list */
3230 }
3231
3232 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003233 "group %d client list%s",
3234 MAC2STR(peer), ssid->id,
3235 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003236 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3237 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3238 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3239 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003240 if (wpa_s->parent->conf->update_config &&
3241 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3242 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003243}
3244
3245
3246static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3247 const u8 *peer)
3248{
3249 struct wpa_ssid *ssid;
3250
3251 wpa_s = wpa_s->global->p2p_invite_group;
3252 if (wpa_s == NULL)
3253 return; /* No known invitation group */
3254 ssid = wpa_s->current_ssid;
3255 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3256 !ssid->p2p_persistent_group)
3257 return; /* Not operating as a GO in persistent group */
3258 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3259 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003260 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003261}
3262
3263
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003264static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003265 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003266 const u8 *peer, int neg_freq,
3267 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003268{
3269 struct wpa_supplicant *wpa_s = ctx;
3270 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003271 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272
3273 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003274 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3275 "status=%d " MACSTR,
3276 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003277 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003278 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3279 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 }
3281 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3282
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003283 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3284 status, MAC2STR(peer));
3285 if (wpa_s->pending_invite_ssid_id == -1) {
3286 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3287 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003288 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003289 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003291 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3292 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3293 "invitation exchange to indicate readiness for "
3294 "re-invocation");
3295 }
3296
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003297 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003298 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3299 ssid = wpa_config_get_network(
3300 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003301 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003302 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003303 wpas_p2p_remove_pending_group_interface(wpa_s);
3304 return;
3305 }
3306
3307 ssid = wpa_config_get_network(wpa_s->conf,
3308 wpa_s->pending_invite_ssid_id);
3309 if (ssid == NULL) {
3310 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3311 "data matching with invitation");
3312 return;
3313 }
3314
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003315 /*
3316 * The peer could have missed our ctrl::ack frame for Invitation
3317 * Response and continue retransmitting the frame. To reduce the
3318 * likelihood of the peer not getting successful TX status for the
3319 * Invitation Response frame, wait a short time here before starting
3320 * the persistent group so that we will remain on the current channel to
3321 * acknowledge any possible retransmission from the peer.
3322 */
3323 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3324 "starting persistent group");
3325 os_sleep(0, 50000);
3326
Dmitry Shmidt15907092014-03-25 10:42:57 -07003327 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3328 freq_included(channels, neg_freq))
3329 freq = neg_freq;
3330 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3331 freq_included(channels, peer_oper_freq))
3332 freq = peer_oper_freq;
3333 else
3334 freq = 0;
3335
3336 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3337 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003339 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003340 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003341 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003342 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3343 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003344 ssid->mode == WPAS_MODE_P2P_GO ?
3345 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3346 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347}
3348
3349
Dmitry Shmidt04949592012-07-19 12:16:46 -07003350static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3351 unsigned int freq)
3352{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003353 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3354 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003355 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003356}
3357
3358
3359static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3360{
3361 reg->channel[reg->channels] = chan;
3362 reg->channels++;
3363}
3364
3365
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003366static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003367 struct p2p_channels *chan,
3368 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369{
3370 int i, cla = 0;
3371
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003372 os_memset(cli_chan, 0, sizeof(*cli_chan));
3373
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3375 "band");
3376
3377 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3378 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003379 chan->reg_class[cla].channels = 0;
3380 for (i = 0; i < 11; i++) {
3381 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3382 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3383 }
3384 if (chan->reg_class[cla].channels)
3385 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003386
3387 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3388 "band");
3389
3390 /* Operating class 115 - 5 GHz, channels 36-48 */
3391 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003392 chan->reg_class[cla].channels = 0;
3393 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3394 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3395 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3396 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3397 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3398 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3399 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3400 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3401 if (chan->reg_class[cla].channels)
3402 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003403
3404 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3405 "band");
3406
3407 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3408 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003409 chan->reg_class[cla].channels = 0;
3410 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3411 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3412 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3413 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3414 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3415 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3416 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3417 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3418 if (chan->reg_class[cla].channels)
3419 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420
3421 chan->reg_classes = cla;
3422 return 0;
3423}
3424
3425
3426static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3427 u16 num_modes,
3428 enum hostapd_hw_mode mode)
3429{
3430 u16 i;
3431
3432 for (i = 0; i < num_modes; i++) {
3433 if (modes[i].mode == mode)
3434 return &modes[i];
3435 }
3436
3437 return NULL;
3438}
3439
3440
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003441enum chan_allowed {
3442 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3443};
3444
Dmitry Shmidt04949592012-07-19 12:16:46 -07003445static int has_channel(struct wpa_global *global,
3446 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447{
3448 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003449 unsigned int freq;
3450
3451 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3452 chan * 5;
3453 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003454 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455
3456 for (i = 0; i < mode->num_channels; i++) {
3457 if (mode->channels[i].chan == chan) {
3458 if (flags)
3459 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003460 if (mode->channels[i].flag &
3461 (HOSTAPD_CHAN_DISABLED |
3462 HOSTAPD_CHAN_RADAR))
3463 return NOT_ALLOWED;
3464 if (mode->channels[i].flag &
3465 (HOSTAPD_CHAN_PASSIVE_SCAN |
3466 HOSTAPD_CHAN_NO_IBSS))
3467 return PASSIVE_ONLY;
3468 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 }
3470 }
3471
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003472 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473}
3474
3475
3476struct p2p_oper_class_map {
3477 enum hostapd_hw_mode mode;
3478 u8 op_class;
3479 u8 min_chan;
3480 u8 max_chan;
3481 u8 inc;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003482 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003483};
3484
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003485static struct p2p_oper_class_map op_class[] = {
3486 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003487#if 0 /* Do not enable HT40 on 2 GHz for now */
3488 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3489 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3490#endif
3491 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3492 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3493 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3494 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3495 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3496 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003497
3498 /*
3499 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3500 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3501 * 80 MHz, but currently use the following definition for simplicity
3502 * (these center frequencies are not actual channels, which makes
3503 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3504 * removing invalid channels.
3505 */
3506 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003507 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003508 { -1, 0, 0, 0, 0, BW20 }
3509};
3510
3511
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003512static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3513 struct hostapd_hw_modes *mode,
3514 u8 channel)
3515{
3516 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3517 unsigned int i;
3518
3519 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3520 return 0;
3521
3522 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3523 /*
3524 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3525 * so the center channel is 6 channels away from the start/end.
3526 */
3527 if (channel >= center_channels[i] - 6 &&
3528 channel <= center_channels[i] + 6)
3529 return center_channels[i];
3530
3531 return 0;
3532}
3533
3534
3535static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3536 struct hostapd_hw_modes *mode,
3537 u8 channel, u8 bw)
3538{
3539 u8 center_chan;
3540 int i, flags;
3541 enum chan_allowed res, ret = ALLOWED;
3542
3543 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3544 if (!center_chan)
3545 return NOT_ALLOWED;
3546 if (center_chan >= 58 && center_chan <= 138)
3547 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3548
3549 /* check all the channels are available */
3550 for (i = 0; i < 4; i++) {
3551 int adj_chan = center_chan - 6 + i * 4;
3552
3553 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3554 if (res == NOT_ALLOWED)
3555 return NOT_ALLOWED;
3556 if (res == PASSIVE_ONLY)
3557 ret = PASSIVE_ONLY;
3558
3559 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3560 return NOT_ALLOWED;
3561 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3562 return NOT_ALLOWED;
3563 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3564 return NOT_ALLOWED;
3565 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3566 return NOT_ALLOWED;
3567 }
3568
3569 return ret;
3570}
3571
3572
3573static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3574 struct hostapd_hw_modes *mode,
3575 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003576{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003577 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003578 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003579
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003580 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3581 if (bw == BW40MINUS) {
3582 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3583 return NOT_ALLOWED;
3584 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3585 } else if (bw == BW40PLUS) {
3586 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3587 return NOT_ALLOWED;
3588 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3589 } else if (bw == BW80) {
3590 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3591 }
3592
3593 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3594 return NOT_ALLOWED;
3595 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3596 return PASSIVE_ONLY;
3597 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003598}
3599
3600
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003601static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003602 struct p2p_channels *chan,
3603 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003605 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003606 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003607
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003608 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3610 "of all supported channels; assume dualband "
3611 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003612 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 }
3614
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003615 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616
3617 for (op = 0; op_class[op].op_class; op++) {
3618 struct p2p_oper_class_map *o = &op_class[op];
3619 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003620 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003621
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003622 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003623 if (mode == NULL)
3624 continue;
3625 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003626 enum chan_allowed res;
3627 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3628 if (res == ALLOWED) {
3629 if (reg == NULL) {
3630 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3631 o->op_class);
3632 reg = &chan->reg_class[cla];
3633 cla++;
3634 reg->reg_class = o->op_class;
3635 }
3636 reg->channel[reg->channels] = ch;
3637 reg->channels++;
3638 } else if (res == PASSIVE_ONLY &&
3639 wpa_s->conf->p2p_add_cli_chan) {
3640 if (cli_reg == NULL) {
3641 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3642 o->op_class);
3643 cli_reg = &cli_chan->reg_class[cli_cla];
3644 cli_cla++;
3645 cli_reg->reg_class = o->op_class;
3646 }
3647 cli_reg->channel[cli_reg->channels] = ch;
3648 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003650 }
3651 if (reg) {
3652 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3653 reg->channel, reg->channels);
3654 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003655 if (cli_reg) {
3656 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3657 cli_reg->channel, cli_reg->channels);
3658 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 }
3660
3661 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003662 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664 return 0;
3665}
3666
3667
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003668int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3669 struct hostapd_hw_modes *mode, u8 channel)
3670{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003671 int op;
3672 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003673
3674 for (op = 0; op_class[op].op_class; op++) {
3675 struct p2p_oper_class_map *o = &op_class[op];
3676 u8 ch;
3677
3678 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3679 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3680 o->bw == BW20 || ch != channel)
3681 continue;
3682 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003683 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003684 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003685 }
3686 }
3687 return 0;
3688}
3689
3690
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003691int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3692 struct hostapd_hw_modes *mode, u8 channel)
3693{
3694 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3695 return 0;
3696
3697 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3698}
3699
3700
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003701static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3702 size_t buf_len)
3703{
3704 struct wpa_supplicant *wpa_s = ctx;
3705
3706 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3707 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3708 break;
3709 }
3710 if (wpa_s == NULL)
3711 return -1;
3712
3713 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3714}
3715
3716
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003717struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3718 const u8 *ssid, size_t ssid_len)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003719{
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003720 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3721 struct wpa_ssid *s = wpa_s->current_ssid;
3722 if (s == NULL)
3723 continue;
3724 if (s->mode != WPAS_MODE_P2P_GO &&
3725 s->mode != WPAS_MODE_AP &&
3726 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3727 continue;
3728 if (s->ssid_len != ssid_len ||
Dmitry Shmidt03658832014-08-13 11:03:49 -07003729 os_memcmp(ssid, s->ssid, ssid_len) != 0)
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003730 continue;
3731 return wpa_s;
3732 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003733
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003734 return NULL;
3735
3736}
3737
3738
3739struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3740 const u8 *peer_dev_addr)
3741{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003742 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3743 struct wpa_ssid *ssid = wpa_s->current_ssid;
3744 if (ssid == NULL)
3745 continue;
3746 if (ssid->mode != WPAS_MODE_INFRA)
3747 continue;
3748 if (wpa_s->wpa_state != WPA_COMPLETED &&
3749 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3750 continue;
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003751 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3752 return wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003753 }
3754
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07003755 return NULL;
3756}
3757
3758
3759static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3760{
3761 struct wpa_supplicant *wpa_s = ctx;
3762
3763 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003764}
3765
3766
Dmitry Shmidt18463232014-01-24 12:29:41 -08003767static int wpas_is_concurrent_session_active(void *ctx)
3768{
3769 struct wpa_supplicant *wpa_s = ctx;
3770 struct wpa_supplicant *ifs;
3771
3772 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3773 if (ifs == wpa_s)
3774 continue;
3775 if (ifs->wpa_state > WPA_ASSOCIATED)
3776 return 1;
3777 }
3778 return 0;
3779}
3780
3781
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003782static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3783{
3784 struct wpa_supplicant *wpa_s = ctx;
3785 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3786}
3787
3788
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003789int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3790 const char *conf_p2p_dev)
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003791{
3792 struct wpa_interface iface;
3793 struct wpa_supplicant *p2pdev_wpa_s;
3794 char ifname[100];
3795 char force_name[100];
3796 int ret;
3797
3798 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3799 wpa_s->ifname);
3800 force_name[0] = '\0';
3801 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3802 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3803 force_name, wpa_s->pending_interface_addr, NULL);
3804 if (ret < 0) {
3805 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3806 return ret;
3807 }
3808 os_strlcpy(wpa_s->pending_interface_name, ifname,
3809 sizeof(wpa_s->pending_interface_name));
3810
3811 os_memset(&iface, 0, sizeof(iface));
3812 iface.p2p_mgmt = 1;
3813 iface.ifname = wpa_s->pending_interface_name;
3814 iface.driver = wpa_s->driver->name;
3815 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003816
3817 /*
3818 * If a P2P Device configuration file was given, use it as the interface
3819 * configuration file (instead of using parent's configuration file.
3820 */
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07003821 if (conf_p2p_dev) {
3822 iface.confname = conf_p2p_dev;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003823 iface.ctrl_interface = NULL;
3824 } else {
3825 iface.confname = wpa_s->confname;
3826 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3827 }
3828 iface.conf_p2p_dev = NULL;
3829
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003830 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3831 if (!p2pdev_wpa_s) {
3832 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3833 return -1;
3834 }
3835 p2pdev_wpa_s->parent = wpa_s;
3836
3837 wpa_s->pending_interface_name[0] = '\0';
3838 return 0;
3839}
3840
3841
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003842static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3843 const u8 *noa, size_t noa_len)
3844{
3845 struct wpa_supplicant *wpa_s, *intf = ctx;
3846 char hex[100];
3847
3848 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3849 if (wpa_s->waiting_presence_resp)
3850 break;
3851 }
3852 if (!wpa_s) {
3853 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3854 return;
3855 }
3856 wpa_s->waiting_presence_resp = 0;
3857
3858 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3859 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3860 " status=%u noa=%s", MAC2STR(src), status, hex);
3861}
3862
3863
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003864static int _wpas_p2p_in_progress(void *ctx)
3865{
3866 struct wpa_supplicant *wpa_s = ctx;
3867 return wpas_p2p_in_progress(wpa_s);
3868}
3869
3870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003871/**
3872 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3873 * @global: Pointer to global data from wpa_supplicant_init()
3874 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3875 * Returns: 0 on success, -1 on failure
3876 */
3877int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3878{
3879 struct p2p_config p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003880 int i;
3881
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003882 if (wpa_s->conf->p2p_disabled)
3883 return 0;
3884
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3886 return 0;
3887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 if (global->p2p)
3889 return 0;
3890
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003891 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003892 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003893 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894 p2p.p2p_scan = wpas_p2p_scan;
3895 p2p.send_action = wpas_send_action;
3896 p2p.send_action_done = wpas_send_action_done;
3897 p2p.go_neg_completed = wpas_go_neg_completed;
3898 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3899 p2p.dev_found = wpas_dev_found;
3900 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003901 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003902 p2p.start_listen = wpas_start_listen;
3903 p2p.stop_listen = wpas_stop_listen;
3904 p2p.send_probe_resp = wpas_send_probe_resp;
3905 p2p.sd_request = wpas_sd_request;
3906 p2p.sd_response = wpas_sd_response;
3907 p2p.prov_disc_req = wpas_prov_disc_req;
3908 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003909 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910 p2p.invitation_process = wpas_invitation_process;
3911 p2p.invitation_received = wpas_invitation_received;
3912 p2p.invitation_result = wpas_invitation_result;
3913 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003914 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003915 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003916 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07003917 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003918
3919 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003920 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 p2p.dev_name = wpa_s->conf->device_name;
3922 p2p.manufacturer = wpa_s->conf->manufacturer;
3923 p2p.model_name = wpa_s->conf->model_name;
3924 p2p.model_number = wpa_s->conf->model_number;
3925 p2p.serial_number = wpa_s->conf->serial_number;
3926 if (wpa_s->wps) {
3927 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3928 p2p.config_methods = wpa_s->wps->config_methods;
3929 }
3930
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003931 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3932 wpa_printf(MSG_ERROR,
3933 "P2P: Failed to configure supported channel list");
3934 return -1;
3935 }
3936
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003937 if (wpa_s->conf->p2p_listen_reg_class &&
3938 wpa_s->conf->p2p_listen_channel) {
3939 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3940 p2p.channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003941 p2p.channel_forced = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003943 /*
3944 * Pick one of the social channels randomly as the listen
3945 * channel.
3946 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003947 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
3948 &p2p.channel) != 0) {
3949 wpa_printf(MSG_ERROR,
3950 "P2P: Failed to select random social channel as listen channel");
3951 return -1;
3952 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003953 p2p.channel_forced = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 }
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003955 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
3956 p2p.reg_class, p2p.channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957
3958 if (wpa_s->conf->p2p_oper_reg_class &&
3959 wpa_s->conf->p2p_oper_channel) {
3960 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3961 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3962 p2p.cfg_op_channel = 1;
3963 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3964 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3965
3966 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003967 /*
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003968 * Use random operation channel from 2.4 GHz band social
3969 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
3970 * other preference is indicated.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003972 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
3973 &p2p.op_channel) != 0) {
3974 wpa_printf(MSG_ERROR,
3975 "P2P: Failed to select random social channel as operation channel");
3976 return -1;
3977 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 p2p.cfg_op_channel = 0;
3979 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3980 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3981 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003982
3983 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3984 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3985 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3986 }
3987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003988 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3989 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3990 p2p.country[2] = 0x04;
3991 } else
3992 os_memcpy(p2p.country, "XX\x04", 3);
3993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003994 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3995 WPS_DEV_TYPE_LEN);
3996
3997 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3998 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3999 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4000
4001 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4002 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4003
4004 p2p.max_peers = 100;
4005
4006 if (wpa_s->conf->p2p_ssid_postfix) {
4007 p2p.ssid_postfix_len =
4008 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4009 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4010 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4011 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4012 p2p.ssid_postfix_len);
4013 }
4014
4015 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4016
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004017 p2p.max_listen = wpa_s->max_remain_on_chan;
4018
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004019 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4020 wpa_s->conf->p2p_passphrase_len <= 63)
4021 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4022 else
4023 p2p.passphrase_len = 8;
4024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004025 global->p2p = p2p_init(&p2p);
4026 if (global->p2p == NULL)
4027 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004028 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004029
4030 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4031 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4032 continue;
4033 p2p_add_wps_vendor_extension(
4034 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4035 }
4036
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004037 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004039 return 0;
4040}
4041
4042
4043/**
4044 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4045 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4046 *
4047 * This function deinitialize per-interface P2P data.
4048 */
4049void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4050{
4051 if (wpa_s->driver && wpa_s->drv_priv)
4052 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004053
4054 if (wpa_s->go_params) {
4055 /* Clear any stored provisioning info */
4056 p2p_clear_provisioning_info(
4057 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004058 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004059 }
4060
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 os_free(wpa_s->go_params);
4062 wpa_s->go_params = NULL;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07004063 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4065 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4066 wpa_s->p2p_long_listen = 0;
4067 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4068 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4069 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08004070 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004071 wpas_p2p_listen_work_done(wpa_s);
4072 if (wpa_s->p2p_send_action_work) {
4073 os_free(wpa_s->p2p_send_action_work->ctx);
4074 radio_work_done(wpa_s->p2p_send_action_work);
4075 wpa_s->p2p_send_action_work = NULL;
4076 }
4077 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004078
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004079 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4080 wpa_s->p2p_oob_dev_pw = NULL;
4081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004082 /* TODO: remove group interface from the driver if this wpa_s instance
4083 * is on top of a P2P group interface */
4084}
4085
4086
4087/**
4088 * wpas_p2p_deinit_global - Deinitialize global P2P module
4089 * @global: Pointer to global data from wpa_supplicant_init()
4090 *
4091 * This function deinitializes the global (per device) P2P module.
4092 */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004093static void wpas_p2p_deinit_global(struct wpa_global *global)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004094{
4095 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004096
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004097 wpa_s = global->ifaces;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004098
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004099 wpas_p2p_service_flush(global->p2p_init_wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004100
4101 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4103 wpa_s = wpa_s->next;
4104 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004105 tmp = global->ifaces;
4106 while (tmp &&
4107 (tmp == wpa_s ||
4108 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4109 tmp = tmp->next;
4110 }
4111 if (tmp == NULL)
4112 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004113 /* Disconnect from the P2P group and deinit the interface */
4114 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004115 }
4116
4117 /*
4118 * Deinit GO data on any possibly remaining interface (if main
4119 * interface is used as GO).
4120 */
4121 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4122 if (wpa_s->ap_iface)
4123 wpas_p2p_group_deinit(wpa_s);
4124 }
4125
4126 p2p_deinit(global->p2p);
4127 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004128 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004129}
4130
4131
4132static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4133{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004134 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4135 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004136 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004137 if (wpa_s->drv_flags &
4138 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4139 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4140 return 1; /* P2P group requires a new interface in every case
4141 */
4142 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4143 return 0; /* driver does not support concurrent operations */
4144 if (wpa_s->global->ifaces->next)
4145 return 1; /* more that one interface already in use */
4146 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4147 return 1; /* this interface is already in use */
4148 return 0;
4149}
4150
4151
4152static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4153 const u8 *peer_addr,
4154 enum p2p_wps_method wps_method,
4155 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004156 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004157 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004159 if (persistent_group && wpa_s->conf->persistent_reconnect)
4160 persistent_group = 2;
4161
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004162 /*
4163 * Increase GO config timeout if HT40 is used since it takes some time
4164 * to scan channels for coex purposes before the BSS can be started.
4165 */
4166 p2p_set_config_timeout(wpa_s->global->p2p,
4167 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004169 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4170 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004171 persistent_group, ssid ? ssid->ssid : NULL,
4172 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004173 wpa_s->p2p_pd_before_go_neg, pref_freq,
4174 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4175 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176}
4177
4178
4179static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4180 const u8 *peer_addr,
4181 enum p2p_wps_method wps_method,
4182 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004183 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004184 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004185{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004186 if (persistent_group && wpa_s->conf->persistent_reconnect)
4187 persistent_group = 2;
4188
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004189 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4190 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004191 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004192 ssid ? ssid->ssid_len : 0, pref_freq,
4193 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4194 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004195}
4196
4197
4198static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4199{
4200 wpa_s->p2p_join_scan_count++;
4201 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4202 wpa_s->p2p_join_scan_count);
4203 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4204 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4205 " for join operationg - stop join attempt",
4206 MAC2STR(wpa_s->pending_join_iface_addr));
4207 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004208 if (wpa_s->p2p_auto_pd) {
4209 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004210 wpa_msg_global(wpa_s, MSG_INFO,
4211 P2P_EVENT_PROV_DISC_FAILURE
4212 " p2p_dev_addr=" MACSTR " status=N/A",
4213 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004214 return;
4215 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004216 wpa_msg_global(wpa_s->parent, MSG_INFO,
4217 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 }
4219}
4220
4221
Dmitry Shmidt04949592012-07-19 12:16:46 -07004222static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4223{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004224 int res;
4225 unsigned int num, i;
4226 struct wpa_used_freq_data *freqs;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004227
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004228 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4229 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004230 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004231 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004232
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004233 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4234 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004235 if (!freqs)
4236 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004237
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004238 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4239 wpa_s->num_multichan_concurrent);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004240
4241 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004242 if (freqs[i].freq == freq) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004243 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4244 freq);
4245 res = 0;
4246 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004247 }
4248 }
4249
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004250 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004251 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004252
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004253exit_free:
4254 os_free(freqs);
4255 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004256}
4257
4258
4259static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4260 const u8 *peer_dev_addr)
4261{
4262 struct wpa_bss *bss;
4263 int updated;
4264
4265 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4266 if (bss == NULL)
4267 return -1;
4268 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4269 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4270 "last scan");
4271 return 0;
4272 }
4273
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004274 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4275 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004276 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4277 "%ld.%06ld (%supdated in last scan)",
4278 bss->last_update.sec, bss->last_update.usec,
4279 updated ? "": "not ");
4280
4281 return updated;
4282}
4283
4284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004285static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4286 struct wpa_scan_results *scan_res)
4287{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004288 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004289 int freq;
4290 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4293
4294 if (wpa_s->global->p2p_disabled)
4295 return;
4296
Dmitry Shmidt04949592012-07-19 12:16:46 -07004297 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4298 scan_res ? (int) scan_res->num : -1,
4299 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004300
4301 if (scan_res)
4302 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4303
Dmitry Shmidt04949592012-07-19 12:16:46 -07004304 if (wpa_s->p2p_auto_pd) {
4305 int join = wpas_p2p_peer_go(wpa_s,
4306 wpa_s->pending_join_dev_addr);
4307 if (join == 0 &&
4308 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4309 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004310 bss = wpa_bss_get_bssid_latest(
4311 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004312 if (bss) {
4313 freq = bss->freq;
4314 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4315 "the peer " MACSTR " at %d MHz",
4316 wpa_s->auto_pd_scan_retry,
4317 MAC2STR(wpa_s->
4318 pending_join_dev_addr),
4319 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004320 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004321 return;
4322 }
4323 }
4324
4325 if (join < 0)
4326 join = 0;
4327
4328 wpa_s->p2p_auto_pd = 0;
4329 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4330 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4331 MAC2STR(wpa_s->pending_join_dev_addr), join);
4332 if (p2p_prov_disc_req(wpa_s->global->p2p,
4333 wpa_s->pending_join_dev_addr,
4334 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004335 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004336 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004337 wpa_msg_global(wpa_s, MSG_INFO,
4338 P2P_EVENT_PROV_DISC_FAILURE
4339 " p2p_dev_addr=" MACSTR " status=N/A",
4340 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004341 }
4342 return;
4343 }
4344
4345 if (wpa_s->p2p_auto_join) {
4346 int join = wpas_p2p_peer_go(wpa_s,
4347 wpa_s->pending_join_dev_addr);
4348 if (join < 0) {
4349 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4350 "running a GO -> use GO Negotiation");
4351 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4352 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4353 wpa_s->p2p_persistent_group, 0, 0, 0,
4354 wpa_s->p2p_go_intent,
4355 wpa_s->p2p_connect_freq,
4356 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004357 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004358 wpa_s->p2p_go_ht40,
4359 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004360 return;
4361 }
4362
4363 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4364 "try to join the group", join ? "" :
4365 " in older scan");
4366 if (!join)
4367 wpa_s->p2p_fallback_to_go_neg = 1;
4368 }
4369
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004370 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4371 wpa_s->pending_join_iface_addr);
4372 if (freq < 0 &&
4373 p2p_get_interface_addr(wpa_s->global->p2p,
4374 wpa_s->pending_join_dev_addr,
4375 iface_addr) == 0 &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004376 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4377 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004378 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4379 "address for join from " MACSTR " to " MACSTR
4380 " based on newly discovered P2P peer entry",
4381 MAC2STR(wpa_s->pending_join_iface_addr),
4382 MAC2STR(iface_addr));
4383 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4384 ETH_ALEN);
4385
4386 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4387 wpa_s->pending_join_iface_addr);
4388 }
4389 if (freq >= 0) {
4390 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4391 "from P2P peer table: %d MHz", freq);
4392 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004393 if (wpa_s->p2p_join_ssid_len) {
4394 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4395 MACSTR " and SSID %s",
4396 MAC2STR(wpa_s->pending_join_iface_addr),
4397 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4398 wpa_s->p2p_join_ssid_len));
4399 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4400 wpa_s->p2p_join_ssid,
4401 wpa_s->p2p_join_ssid_len);
4402 }
4403 if (!bss) {
4404 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4405 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4406 bss = wpa_bss_get_bssid_latest(wpa_s,
4407 wpa_s->pending_join_iface_addr);
4408 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004409 if (bss) {
4410 freq = bss->freq;
4411 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004412 "from BSS table: %d MHz (SSID %s)", freq,
4413 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004414 }
4415 if (freq > 0) {
4416 u16 method;
4417
Dmitry Shmidt04949592012-07-19 12:16:46 -07004418 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004419 wpa_msg_global(wpa_s->parent, MSG_INFO,
4420 P2P_EVENT_GROUP_FORMATION_FAILURE
4421 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004422 return;
4423 }
4424
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004425 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4426 "prior to joining an existing group (GO " MACSTR
4427 " freq=%u MHz)",
4428 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4429 wpa_s->pending_pd_before_join = 1;
4430
4431 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004432 case WPS_PIN_DISPLAY:
4433 method = WPS_CONFIG_KEYPAD;
4434 break;
4435 case WPS_PIN_KEYPAD:
4436 method = WPS_CONFIG_DISPLAY;
4437 break;
4438 case WPS_PBC:
4439 method = WPS_CONFIG_PUSHBUTTON;
4440 break;
4441 default:
4442 method = 0;
4443 break;
4444 }
4445
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004446 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4447 wpa_s->pending_join_dev_addr) ==
4448 method)) {
4449 /*
4450 * We have already performed provision discovery for
4451 * joining the group. Proceed directly to join
4452 * operation without duplicated provision discovery. */
4453 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4454 "with " MACSTR " already done - proceed to "
4455 "join",
4456 MAC2STR(wpa_s->pending_join_dev_addr));
4457 wpa_s->pending_pd_before_join = 0;
4458 goto start;
4459 }
4460
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004461 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004462 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004463 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004464 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4465 "Discovery Request before joining an "
4466 "existing group");
4467 wpa_s->pending_pd_before_join = 0;
4468 goto start;
4469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004470 return;
4471 }
4472
4473 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4474 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4475 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4476 wpas_p2p_check_join_scan_limit(wpa_s);
4477 return;
4478
4479start:
4480 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004481 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004482}
4483
4484
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004485static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4486 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004487{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488 int ret;
4489 struct wpa_driver_scan_params params;
4490 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004491 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004492 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004493
4494 os_memset(&params, 0, sizeof(params));
4495
4496 /* P2P Wildcard SSID */
4497 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004498 if (ssid && ssid_len) {
4499 params.ssids[0].ssid = ssid;
4500 params.ssids[0].ssid_len = ssid_len;
4501 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4502 wpa_s->p2p_join_ssid_len = ssid_len;
4503 } else {
4504 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4505 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4506 wpa_s->p2p_join_ssid_len = 0;
4507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004508
4509 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004510 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4511 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4512 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004513 if (wps_ie == NULL) {
4514 wpas_p2p_scan_res_join(wpa_s, NULL);
4515 return;
4516 }
4517
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004518 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4519 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004520 if (ies == NULL) {
4521 wpabuf_free(wps_ie);
4522 wpas_p2p_scan_res_join(wpa_s, NULL);
4523 return;
4524 }
4525 wpabuf_put_buf(ies, wps_ie);
4526 wpabuf_free(wps_ie);
4527
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004528 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004529
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004530 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004531 params.extra_ies = wpabuf_head(ies);
4532 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004533
4534 if (!freq) {
4535 int oper_freq;
4536 /*
4537 * If freq is not provided, check the operating freq of the GO
4538 * and use a single channel scan on if possible.
4539 */
4540 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4541 wpa_s->pending_join_iface_addr);
4542 if (oper_freq > 0)
4543 freq = oper_freq;
4544 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004545 if (freq > 0) {
4546 freqs[0] = freq;
4547 params.freqs = freqs;
4548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004549
4550 /*
4551 * Run a scan to update BSS table and start Provision Discovery once
4552 * the new scan results become available.
4553 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004554 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004555 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004556 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004557 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004558 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004559 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004560
4561 wpabuf_free(ies);
4562
4563 if (ret) {
4564 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4565 "try again later");
4566 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4567 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4568 wpas_p2p_check_join_scan_limit(wpa_s);
4569 }
4570}
4571
4572
Dmitry Shmidt04949592012-07-19 12:16:46 -07004573static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4574{
4575 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004576 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004577}
4578
4579
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004580static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004581 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004582 int auto_join, int op_freq,
4583 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004584{
4585 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004586 MACSTR " dev " MACSTR " op_freq=%d)%s",
4587 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004588 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004589 if (ssid && ssid_len) {
4590 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4591 wpa_ssid_txt(ssid, ssid_len));
4592 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593
Dmitry Shmidt04949592012-07-19 12:16:46 -07004594 wpa_s->p2p_auto_pd = 0;
4595 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004596 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4597 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4598 wpa_s->pending_join_wps_method = wps_method;
4599
4600 /* Make sure we are not running find during connection establishment */
4601 wpas_p2p_stop_find(wpa_s);
4602
4603 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004604 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004605 return 0;
4606}
4607
4608
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004609static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4610 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004611{
4612 struct wpa_supplicant *group;
4613 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004614 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004615
4616 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4617 if (group == NULL)
4618 return -1;
4619 if (group != wpa_s) {
4620 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4621 sizeof(group->p2p_pin));
4622 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004623 } else {
4624 /*
4625 * Need to mark the current interface for p2p_group_formation
4626 * when a separate group interface is not used. This is needed
4627 * to allow p2p_cancel stop a pending p2p_connect-join.
4628 * wpas_p2p_init_group_interface() addresses this for the case
4629 * where a separate group interface is used.
4630 */
4631 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632 }
4633
4634 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004635 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004636
4637 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004638 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4640 ETH_ALEN);
4641 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004642 if (freq && ssid && ssid_len) {
4643 res.freq = freq;
4644 res.ssid_len = ssid_len;
4645 os_memcpy(res.ssid, ssid, ssid_len);
4646 } else {
4647 bss = wpa_bss_get_bssid_latest(wpa_s,
4648 wpa_s->pending_join_iface_addr);
4649 if (bss) {
4650 res.freq = bss->freq;
4651 res.ssid_len = bss->ssid_len;
4652 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4653 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4654 bss->freq,
4655 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4656 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004657 }
4658
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004659 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4660 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4661 "starting client");
4662 wpa_drv_cancel_remain_on_channel(wpa_s);
4663 wpa_s->off_channel_freq = 0;
4664 wpa_s->roc_waiting_drv_freq = 0;
4665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666 wpas_start_wps_enrollee(group, &res);
4667
4668 /*
4669 * Allow a longer timeout for join-a-running-group than normal 15
4670 * second group formation timeout since the GO may not have authorized
4671 * our connection yet.
4672 */
4673 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4674 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4675 wpa_s, NULL);
4676
4677 return 0;
4678}
4679
4680
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004681static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004682 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004683{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004684 struct wpa_used_freq_data *freqs;
4685 int res, best_freq, num_unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004686 unsigned int freq_in_use = 0, num, i;
4687
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004688 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4689 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004690 if (!freqs)
4691 return -1;
4692
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004693 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4694 wpa_s->num_multichan_concurrent);
4695
4696 /*
4697 * It is possible that the total number of used frequencies is bigger
4698 * than the number of frequencies used for P2P, so get the system wide
4699 * number of unused frequencies.
4700 */
4701 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4702
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004703 wpa_printf(MSG_DEBUG,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004704 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4705 freq, wpa_s->num_multichan_concurrent, num, num_unused);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004706
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004707 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004708 int ret;
4709 if (go)
4710 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4711 else
4712 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4713 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004714 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4715 "(%u MHz) is not supported for P2P uses",
4716 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004717 res = -3;
4718 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004719 }
4720
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004721 for (i = 0; i < num; i++) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004722 if (freqs[i].freq == freq)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004723 freq_in_use = 1;
4724 }
4725
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004726 if (num_unused <= 0 && !freq_in_use) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004727 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4728 freq);
4729 res = -2;
4730 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004731 }
4732 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4733 "requested channel (%u MHz)", freq);
4734 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004735 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004736 }
4737
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004738 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004739
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004740 /* We have a candidate frequency to use */
4741 if (best_freq > 0) {
4742 if (*pref_freq == 0 && num_unused > 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004743 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004744 best_freq);
4745 *pref_freq = best_freq;
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004746 } else {
4747 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 -07004748 best_freq);
4749 *force_freq = best_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004750 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004751 } else if (num_unused > 0) {
4752 wpa_printf(MSG_DEBUG,
4753 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4754 *force_freq = 0;
4755 } else {
4756 wpa_printf(MSG_DEBUG,
4757 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4758 res = -2;
4759 goto exit_free;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004760 }
4761
4762exit_ok:
4763 res = 0;
4764exit_free:
4765 os_free(freqs);
4766 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004767}
4768
4769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004770/**
4771 * wpas_p2p_connect - Request P2P Group Formation to be started
4772 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4773 * @peer_addr: Address of the peer P2P Device
4774 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4775 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004776 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777 * @join: Whether to join an existing group (as a client) instead of starting
4778 * Group Owner negotiation; @peer_addr is BSSID in that case
4779 * @auth: Whether to only authorize the connection instead of doing that and
4780 * initiating Group Owner negotiation
4781 * @go_intent: GO Intent or -1 to use default
4782 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004783 * @persistent_id: Persistent group credentials to use for forcing GO
4784 * parameters or -1 to generate new values (SSID/passphrase)
4785 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4786 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004787 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004788 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004789 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4790 * failure, -2 on failure due to channel not currently available,
4791 * -3 if forced channel is not supported
4792 */
4793int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4794 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004795 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004796 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004797 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004798{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004799 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004800 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004801 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004802 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004803 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804
4805 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4806 return -1;
4807
Dmitry Shmidt04949592012-07-19 12:16:46 -07004808 if (persistent_id >= 0) {
4809 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4810 if (ssid == NULL || ssid->disabled != 2 ||
4811 ssid->mode != WPAS_MODE_P2P_GO)
4812 return -1;
4813 }
4814
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004815 os_free(wpa_s->global->add_psk);
4816 wpa_s->global->add_psk = NULL;
4817
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004818 wpa_s->global->p2p_fail_on_wps_complete = 0;
4819
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004820 if (go_intent < 0)
4821 go_intent = wpa_s->conf->p2p_go_intent;
4822
4823 if (!auth)
4824 wpa_s->p2p_long_listen = 0;
4825
4826 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004827 wpa_s->p2p_persistent_group = !!persistent_group;
4828 wpa_s->p2p_persistent_id = persistent_id;
4829 wpa_s->p2p_go_intent = go_intent;
4830 wpa_s->p2p_connect_freq = freq;
4831 wpa_s->p2p_fallback_to_go_neg = 0;
4832 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004833 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004834 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004835
4836 if (pin)
4837 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4838 else if (wps_method == WPS_PIN_DISPLAY) {
4839 ret = wps_generate_pin();
4840 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4841 ret);
4842 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4843 wpa_s->p2p_pin);
4844 } else
4845 wpa_s->p2p_pin[0] = '\0';
4846
Dmitry Shmidt04949592012-07-19 12:16:46 -07004847 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4849 if (auth) {
4850 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4851 "connect a running group from " MACSTR,
4852 MAC2STR(peer_addr));
4853 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4854 return ret;
4855 }
4856 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4857 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4858 iface_addr) < 0) {
4859 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4860 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4861 dev_addr);
4862 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004863 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004864 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004865 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4866 "%ld.%06ld",
4867 wpa_s->p2p_auto_started.sec,
4868 wpa_s->p2p_auto_started.usec);
4869 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004870 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004871 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004872 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004873 return -1;
4874 return ret;
4875 }
4876
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004877 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4878 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004879 if (res)
4880 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004881 wpas_p2p_set_own_freq_preference(wpa_s,
4882 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004883
4884 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4885
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004886 if (wpa_s->create_p2p_iface) {
4887 /* Prepare to add a new interface for the group */
4888 iftype = WPA_IF_P2P_GROUP;
4889 if (go_intent == 15)
4890 iftype = WPA_IF_P2P_GO;
4891 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4892 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4893 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004894 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004896
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004897 if_addr = wpa_s->pending_interface_addr;
4898 } else
4899 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900
4901 if (auth) {
4902 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004903 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004904 force_freq, persistent_group, ssid,
4905 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004906 return -1;
4907 return ret;
4908 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004909
4910 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4911 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004912 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004913 if (wpa_s->create_p2p_iface)
4914 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004915 return -1;
4916 }
4917 return ret;
4918}
4919
4920
4921/**
4922 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4923 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4924 * @freq: Frequency of the channel in MHz
4925 * @duration: Duration of the stay on the channel in milliseconds
4926 *
4927 * This callback is called when the driver indicates that it has started the
4928 * requested remain-on-channel duration.
4929 */
4930void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4931 unsigned int freq, unsigned int duration)
4932{
4933 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4934 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004935 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4936 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4937 wpa_s->pending_listen_duration);
4938 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004939 } else {
4940 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4941 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4942 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004943 }
4944}
4945
4946
Jithu Jance57cea1a2014-08-20 10:22:04 -07004947int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004948{
4949 /* Limit maximum Listen state time based on driver limitation. */
4950 if (timeout > wpa_s->max_remain_on_chan)
4951 timeout = wpa_s->max_remain_on_chan;
4952
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004953 return p2p_listen(wpa_s->global->p2p, timeout);
4954}
4955
4956
4957/**
4958 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4959 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4960 * @freq: Frequency of the channel in MHz
4961 *
4962 * This callback is called when the driver indicates that a remain-on-channel
4963 * operation has been completed, i.e., the duration on the requested channel
4964 * has timed out.
4965 */
4966void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4967 unsigned int freq)
4968{
4969 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4970 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004971 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004972 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004973 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4974 return;
Jithu Jance57cea1a2014-08-20 10:22:04 -07004975 if (wpa_s->p2p_long_listen > 0)
4976 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004977 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4978 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004979 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004980 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004981 if (wpa_s->p2p_long_listen > 0) {
4982 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4983 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004984 } else {
4985 /*
4986 * When listen duration is over, stop listen & update p2p_state
4987 * to IDLE.
4988 */
4989 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 }
4991}
4992
4993
4994/**
4995 * wpas_p2p_group_remove - Remove a P2P group
4996 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4997 * @ifname: Network interface name of the group interface or "*" to remove all
4998 * groups
4999 * Returns: 0 on success, -1 on failure
5000 *
5001 * This function is used to remove a P2P group. This can be used to disconnect
5002 * from a group in which the local end is a P2P Client or to end a P2P Group in
5003 * case the local end is the Group Owner. If a virtual network interface was
5004 * created for this group, that interface will be removed. Otherwise, only the
5005 * configured P2P group network will be removed from the interface.
5006 */
5007int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5008{
5009 struct wpa_global *global = wpa_s->global;
5010
5011 if (os_strcmp(ifname, "*") == 0) {
5012 struct wpa_supplicant *prev;
5013 wpa_s = global->ifaces;
5014 while (wpa_s) {
5015 prev = wpa_s;
5016 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07005017 if (prev->p2p_group_interface !=
5018 NOT_P2P_GROUP_INTERFACE ||
5019 (prev->current_ssid &&
5020 prev->current_ssid->p2p_group))
5021 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 }
5023 return 0;
5024 }
5025
5026 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5027 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5028 break;
5029 }
5030
5031 return wpas_p2p_disconnect(wpa_s);
5032}
5033
5034
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005035static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5036{
5037 unsigned int r;
5038
5039 if (freq == 2) {
5040 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5041 "band");
5042 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005043 p2p_supported_freq_go(wpa_s->global->p2p,
5044 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005045 freq = wpa_s->best_24_freq;
5046 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5047 "channel: %d MHz", freq);
5048 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005049 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5050 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005051 freq = 2412 + (r % 3) * 25;
5052 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5053 "channel: %d MHz", freq);
5054 }
5055 }
5056
5057 if (freq == 5) {
5058 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5059 "band");
5060 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005061 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005062 wpa_s->best_5_freq)) {
5063 freq = wpa_s->best_5_freq;
5064 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5065 "channel: %d MHz", freq);
5066 } else {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005067 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5068 return -1;
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005069 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005070 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005071 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5072 "5 GHz channel for P2P group");
5073 return -1;
5074 }
5075 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5076 "channel: %d MHz", freq);
5077 }
5078 }
5079
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005080 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005081 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5082 "(%u MHz) is not supported for P2P uses",
5083 freq);
5084 return -1;
5085 }
5086
5087 return freq;
5088}
5089
5090
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005091static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5092 struct p2p_go_neg_results *params,
5093 const struct p2p_channels *channels)
5094{
5095 unsigned int i, r;
5096
5097 /* first try some random selection of the social channels */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005098 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5099 return -1;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005100
5101 for (i = 0; i < 3; i++) {
5102 params->freq = 2412 + ((r + i) % 3) * 25;
5103 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005104 freq_included(channels, params->freq) &&
5105 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005106 goto out;
5107 }
5108
5109 /* try all channels in reg. class 81 */
5110 for (i = 0; i < 11; i++) {
5111 params->freq = 2412 + i * 5;
5112 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005113 freq_included(channels, params->freq) &&
5114 p2p_supported_freq(wpa_s->global->p2p, params->freq))
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005115 goto out;
5116 }
5117
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005118 /* try social channel class 180 channel 2 */
5119 params->freq = 58320 + 1 * 2160;
5120 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5121 freq_included(channels, params->freq) &&
5122 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5123 goto out;
5124
5125 /* try all channels in reg. class 180 */
5126 for (i = 0; i < 4; i++) {
5127 params->freq = 58320 + i * 2160;
5128 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5129 freq_included(channels, params->freq) &&
5130 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5131 goto out;
5132 }
5133
5134 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005135 return -1;
5136out:
5137 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5138 params->freq);
5139 return 0;
5140}
5141
5142
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005143static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5144 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005145 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005146 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005148 struct wpa_used_freq_data *freqs;
5149 unsigned int pref_freq, cand_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005150 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005151
5152 os_memset(params, 0, sizeof(*params));
5153 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005154 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005155 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005156 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005157 if (!freq_included(channels, freq)) {
5158 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5159 "accepted", freq);
5160 return -1;
5161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005162 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5163 "frequency %d MHz", freq);
5164 params->freq = freq;
5165 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5166 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005167 wpa_s->conf->p2p_oper_channel <= 11 &&
5168 freq_included(channels,
5169 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5171 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5172 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005173 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5174 wpa_s->conf->p2p_oper_reg_class == 116 ||
5175 wpa_s->conf->p2p_oper_reg_class == 117 ||
5176 wpa_s->conf->p2p_oper_reg_class == 124 ||
5177 wpa_s->conf->p2p_oper_reg_class == 126 ||
5178 wpa_s->conf->p2p_oper_reg_class == 127) &&
5179 freq_included(channels,
5180 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005181 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5182 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5183 "frequency %d MHz", params->freq);
5184 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5185 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005186 p2p_supported_freq_go(wpa_s->global->p2p,
5187 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005188 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 params->freq = wpa_s->best_overall_freq;
5190 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5191 "channel %d MHz", params->freq);
5192 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5193 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005194 p2p_supported_freq_go(wpa_s->global->p2p,
5195 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005196 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005197 params->freq = wpa_s->best_24_freq;
5198 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5199 "channel %d MHz", params->freq);
5200 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5201 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005202 p2p_supported_freq_go(wpa_s->global->p2p,
5203 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005204 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005205 params->freq = wpa_s->best_5_freq;
5206 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5207 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005208 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5209 channels))) {
5210 params->freq = pref_freq;
5211 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5212 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005213 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005214 /* no preference, select some channel */
5215 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -07005216 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005217 }
5218
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005219 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5220 sizeof(struct wpa_used_freq_data));
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005221 if (!freqs)
5222 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005223
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005224 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005225 wpa_s->num_multichan_concurrent);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005226
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005227 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5228
5229 /* First try the best used frequency if possible */
5230 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5231 params->freq = cand_freq;
5232 } else if (!freq) {
5233 /* Try any of the used frequencies */
5234 for (i = 0; i < num; i++) {
5235 if (freq_included(channels, freqs[i].freq)) {
5236 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5237 freqs[i].freq);
5238 params->freq = freqs[i].freq;
5239 break;
5240 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005241 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005242
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005243 if (i == num) {
5244 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005245 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005246 os_free(freqs);
5247 return -1;
5248 } else {
5249 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5250 }
5251 }
5252 } else {
5253 for (i = 0; i < num; i++) {
5254 if (freqs[i].freq == freq)
5255 break;
5256 }
5257
5258 if (i == num) {
5259 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5260 if (freq)
5261 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5262 os_free(freqs);
5263 return -1;
5264 } else {
5265 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5266 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005267 }
5268 }
5269
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005270 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005271 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005272}
5273
5274
5275static struct wpa_supplicant *
5276wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5277 int go)
5278{
5279 struct wpa_supplicant *group_wpa_s;
5280
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005281 if (!wpas_p2p_create_iface(wpa_s)) {
5282 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5283 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005284 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005285 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005286 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005287
5288 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005289 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005290 wpa_msg_global(wpa_s, MSG_ERROR,
5291 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005293 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005294 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5295 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005296 wpa_msg_global(wpa_s, MSG_ERROR,
5297 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005298 wpas_p2p_remove_pending_group_interface(wpa_s);
5299 return NULL;
5300 }
5301
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5303 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005304 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005305 return group_wpa_s;
5306}
5307
5308
5309/**
5310 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5311 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5312 * @persistent_group: Whether to create a persistent group
5313 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005314 * @ht40: Start GO with 40 MHz channel width
5315 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005316 * Returns: 0 on success, -1 on failure
5317 *
5318 * This function creates a new P2P group with the local end as the Group Owner,
5319 * i.e., without using Group Owner Negotiation.
5320 */
5321int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005322 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005323{
5324 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325
5326 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5327 return -1;
5328
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005329 os_free(wpa_s->global->add_psk);
5330 wpa_s->global->add_psk = NULL;
5331
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005332 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005333 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005334 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005335
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005336 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5337 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005338 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005340 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005341 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005342 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005343 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005344 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5345 "(%u MHz) is not supported for P2P uses",
5346 params.freq);
5347 return -1;
5348 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005349 p2p_go_params(wpa_s->global->p2p, &params);
5350 params.persistent_group = persistent_group;
5351
5352 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5353 if (wpa_s == NULL)
5354 return -1;
5355 wpas_start_wps_go(wpa_s, &params, 0);
5356
5357 return 0;
5358}
5359
5360
5361static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005362 struct wpa_ssid *params, int addr_allocated,
5363 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005364{
5365 struct wpa_ssid *ssid;
5366
5367 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5368 if (wpa_s == NULL)
5369 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005370 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005371
5372 wpa_supplicant_ap_deinit(wpa_s);
5373
5374 ssid = wpa_config_add_network(wpa_s->conf);
5375 if (ssid == NULL)
5376 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005377 wpa_config_set_network_defaults(ssid);
5378 ssid->temporary = 1;
5379 ssid->proto = WPA_PROTO_RSN;
5380 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5381 ssid->group_cipher = WPA_CIPHER_CCMP;
5382 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5383 ssid->ssid = os_malloc(params->ssid_len);
5384 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005385 wpa_config_remove_network(wpa_s->conf, ssid->id);
5386 return -1;
5387 }
5388 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5389 ssid->ssid_len = params->ssid_len;
5390 ssid->p2p_group = 1;
5391 ssid->export_keys = 1;
5392 if (params->psk_set) {
5393 os_memcpy(ssid->psk, params->psk, 32);
5394 ssid->psk_set = 1;
5395 }
5396 if (params->passphrase)
5397 ssid->passphrase = os_strdup(params->passphrase);
5398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005400 wpa_s->p2p_in_invitation = 1;
5401 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005402
Dmitry Shmidt15907092014-03-25 10:42:57 -07005403 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5404 NULL);
5405 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5406 wpas_p2p_group_formation_timeout,
5407 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005408 wpa_supplicant_select_network(wpa_s, ssid);
5409
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005410 return 0;
5411}
5412
5413
5414int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5415 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005416 int force_freq, int neg_freq, int ht40,
5417 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005418 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419{
5420 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005421 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005422
5423 if (ssid->disabled != 2 || ssid->ssid == NULL)
5424 return -1;
5425
5426 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5427 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5428 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5429 "already running");
5430 return 0;
5431 }
5432
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005433 os_free(wpa_s->global->add_psk);
5434 wpa_s->global->add_psk = NULL;
5435
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005436 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005437 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438
Dmitry Shmidt04949592012-07-19 12:16:46 -07005439 wpa_s->p2p_fallback_to_go_neg = 0;
5440
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005441 if (force_freq > 0) {
5442 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5443 if (freq < 0)
5444 return -1;
5445 } else {
5446 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5447 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5448 freq = 0;
5449 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005450
Dmitry Shmidt15907092014-03-25 10:42:57 -07005451 if (ssid->mode == WPAS_MODE_INFRA)
5452 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5453
5454 if (ssid->mode != WPAS_MODE_P2P_GO)
5455 return -1;
5456
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005457 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005458 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005459
5460 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005461 params.psk_set = ssid->psk_set;
5462 if (params.psk_set)
5463 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005464 if (ssid->passphrase) {
5465 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5466 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5467 "persistent group");
5468 return -1;
5469 }
5470 os_strlcpy(params.passphrase, ssid->passphrase,
5471 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005472 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005473 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5474 params.ssid_len = ssid->ssid_len;
5475 params.persistent_group = 1;
5476
5477 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5478 if (wpa_s == NULL)
5479 return -1;
5480
Dmitry Shmidt56052862013-10-04 10:23:25 -07005481 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482 wpas_start_wps_go(wpa_s, &params, 0);
5483
5484 return 0;
5485}
5486
5487
5488static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5489 struct wpabuf *proberesp_ies)
5490{
5491 struct wpa_supplicant *wpa_s = ctx;
5492 if (wpa_s->ap_iface) {
5493 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005494 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5495 wpabuf_free(beacon_ies);
5496 wpabuf_free(proberesp_ies);
5497 return;
5498 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 if (beacon_ies) {
5500 wpabuf_free(hapd->p2p_beacon_ie);
5501 hapd->p2p_beacon_ie = beacon_ies;
5502 }
5503 wpabuf_free(hapd->p2p_probe_resp_ie);
5504 hapd->p2p_probe_resp_ie = proberesp_ies;
5505 } else {
5506 wpabuf_free(beacon_ies);
5507 wpabuf_free(proberesp_ies);
5508 }
5509 wpa_supplicant_ap_update_beacon(wpa_s);
5510}
5511
5512
5513static void wpas_p2p_idle_update(void *ctx, int idle)
5514{
5515 struct wpa_supplicant *wpa_s = ctx;
5516 if (!wpa_s->ap_iface)
5517 return;
5518 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005519 if (idle) {
5520 if (wpa_s->global->p2p_fail_on_wps_complete &&
5521 wpa_s->p2p_in_provisioning) {
5522 wpas_p2p_grpform_fail_after_wps(wpa_s);
5523 return;
5524 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005526 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005527 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5528}
5529
5530
5531struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005532 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005533{
5534 struct p2p_group *group;
5535 struct p2p_group_config *cfg;
5536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005537 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5538 return NULL;
5539
5540 cfg = os_zalloc(sizeof(*cfg));
5541 if (cfg == NULL)
5542 return NULL;
5543
Dmitry Shmidt04949592012-07-19 12:16:46 -07005544 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005545 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005546 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005547 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005548 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5549 if (wpa_s->max_stations &&
5550 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5551 cfg->max_clients = wpa_s->max_stations;
5552 else
5553 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005554 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5555 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005556 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005557 cfg->cb_ctx = wpa_s;
5558 cfg->ie_update = wpas_p2p_ie_update;
5559 cfg->idle_update = wpas_p2p_idle_update;
5560
5561 group = p2p_group_init(wpa_s->global->p2p, cfg);
5562 if (group == NULL)
5563 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005564 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005565 p2p_group_notif_formation_done(group);
5566 wpa_s->p2p_group = group;
5567 return group;
5568}
5569
5570
5571void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5572 int registrar)
5573{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005574 struct wpa_ssid *ssid = wpa_s->current_ssid;
5575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 if (!wpa_s->p2p_in_provisioning) {
5577 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5578 "provisioning not in progress");
5579 return;
5580 }
5581
Dmitry Shmidt04949592012-07-19 12:16:46 -07005582 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5583 u8 go_dev_addr[ETH_ALEN];
5584 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5585 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5586 ssid->ssid_len);
5587 /* Clear any stored provisioning info */
5588 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5589 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005591 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5592 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005593 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005594 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5595 /*
5596 * Use a separate timeout for initial data connection to
5597 * complete to allow the group to be removed automatically if
5598 * something goes wrong in this step before the P2P group idle
5599 * timeout mechanism is taken into use.
5600 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005601 wpa_dbg(wpa_s, MSG_DEBUG,
5602 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5603 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005604 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5605 wpas_p2p_group_formation_timeout,
5606 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005607 } else if (ssid) {
5608 /*
5609 * Use a separate timeout for initial data connection to
5610 * complete to allow the group to be removed automatically if
5611 * the client does not complete data connection successfully.
5612 */
5613 wpa_dbg(wpa_s, MSG_DEBUG,
5614 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5615 P2P_MAX_INITIAL_CONN_WAIT_GO);
5616 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5617 wpas_p2p_group_formation_timeout,
5618 wpa_s->parent, NULL);
5619 /*
5620 * Complete group formation on first successful data connection
5621 */
5622 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624 if (wpa_s->global->p2p)
5625 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005626 wpas_group_formation_completed(wpa_s, 1);
5627}
5628
5629
Jouni Malinen75ecf522011-06-27 15:19:46 -07005630void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5631 struct wps_event_fail *fail)
5632{
5633 if (!wpa_s->p2p_in_provisioning) {
5634 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5635 "provisioning not in progress");
5636 return;
5637 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005638
5639 if (wpa_s->go_params) {
5640 p2p_clear_provisioning_info(
5641 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005642 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005643 }
5644
Jouni Malinen75ecf522011-06-27 15:19:46 -07005645 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005646
5647 if (wpa_s == wpa_s->global->p2p_group_formation) {
5648 /*
5649 * Allow some time for the failed WPS negotiation exchange to
5650 * complete, but remove the group since group formation cannot
5651 * succeed after provisioning failure.
5652 */
5653 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5654 wpa_s->global->p2p_fail_on_wps_complete = 1;
5655 eloop_deplete_timeout(0, 50000,
5656 wpas_p2p_group_formation_timeout,
5657 wpa_s->parent, NULL);
5658 }
5659}
5660
5661
5662int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5663{
5664 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5665 !wpa_s->p2p_in_provisioning)
5666 return 0;
5667
5668 wpas_p2p_grpform_fail_after_wps(wpa_s);
5669
5670 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005671}
5672
5673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005674int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005675 const char *config_method,
5676 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005677{
5678 u16 config_methods;
5679
Dmitry Shmidt04949592012-07-19 12:16:46 -07005680 wpa_s->p2p_fallback_to_go_neg = 0;
5681 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005682 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005683 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005684 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005686 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5687 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005688 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005689 else {
5690 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005691 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005692 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005693
Dmitry Shmidt04949592012-07-19 12:16:46 -07005694 if (use == WPAS_P2P_PD_AUTO) {
5695 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5696 wpa_s->pending_pd_config_methods = config_methods;
5697 wpa_s->p2p_auto_pd = 1;
5698 wpa_s->p2p_auto_join = 0;
5699 wpa_s->pending_pd_before_join = 0;
5700 wpa_s->auto_pd_scan_retry = 0;
5701 wpas_p2p_stop_find(wpa_s);
5702 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005703 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005704 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5705 wpa_s->p2p_auto_started.sec,
5706 wpa_s->p2p_auto_started.usec);
5707 wpas_p2p_join_scan(wpa_s, NULL);
5708 return 0;
5709 }
5710
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005711 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5712 return -1;
5713
5714 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005715 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005716 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005717}
5718
5719
5720int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5721 char *end)
5722{
5723 return p2p_scan_result_text(ies, ies_len, buf, end);
5724}
5725
5726
5727static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5728{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005729 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005730 return;
5731
Dmitry Shmidt03658832014-08-13 11:03:49 -07005732 wpas_p2p_action_tx_clear(wpa_s);
5733
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005734 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5735 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005736 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005737}
5738
5739
5740int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5741 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005742 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005743 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005744{
5745 wpas_p2p_clear_pending_action_tx(wpa_s);
5746 wpa_s->p2p_long_listen = 0;
5747
Dmitry Shmidt04949592012-07-19 12:16:46 -07005748 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5749 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005750 return -1;
5751
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005752 wpa_supplicant_cancel_sched_scan(wpa_s);
5753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005754 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005755 num_req_dev_types, req_dev_types, dev_id,
5756 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005757}
5758
5759
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005760static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005761{
5762 wpas_p2p_clear_pending_action_tx(wpa_s);
5763 wpa_s->p2p_long_listen = 0;
5764 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5765 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005766
5767 if (wpa_s->global->p2p)
5768 p2p_stop_find(wpa_s->global->p2p);
5769
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005770 return 0;
5771}
5772
5773
5774void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5775{
5776 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5777 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005778 wpas_p2p_remove_pending_group_interface(wpa_s);
5779}
5780
5781
5782static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5783{
5784 struct wpa_supplicant *wpa_s = eloop_ctx;
5785 wpa_s->p2p_long_listen = 0;
5786}
5787
5788
5789int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5790{
5791 int res;
5792
5793 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5794 return -1;
5795
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005796 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005797 wpas_p2p_clear_pending_action_tx(wpa_s);
5798
5799 if (timeout == 0) {
5800 /*
5801 * This is a request for unlimited Listen state. However, at
5802 * least for now, this is mapped to a Listen state for one
5803 * hour.
5804 */
5805 timeout = 3600;
5806 }
5807 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5808 wpa_s->p2p_long_listen = 0;
5809
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005810 /*
5811 * Stop previous find/listen operation to avoid trying to request a new
5812 * remain-on-channel operation while the driver is still running the
5813 * previous one.
5814 */
5815 if (wpa_s->global->p2p)
5816 p2p_stop_find(wpa_s->global->p2p);
5817
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005818 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5819 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5820 wpa_s->p2p_long_listen = timeout * 1000;
5821 eloop_register_timeout(timeout, 0,
5822 wpas_p2p_long_listen_timeout,
5823 wpa_s, NULL);
5824 }
5825
5826 return res;
5827}
5828
5829
5830int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5831 u8 *buf, size_t len, int p2p_group)
5832{
5833 struct wpabuf *p2p_ie;
5834 int ret;
5835
5836 if (wpa_s->global->p2p_disabled)
5837 return -1;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07005838 if (wpa_s->conf->p2p_disabled)
5839 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005840 if (wpa_s->global->p2p == NULL)
5841 return -1;
5842 if (bss == NULL)
5843 return -1;
5844
5845 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5846 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5847 p2p_group, p2p_ie);
5848 wpabuf_free(p2p_ie);
5849
5850 return ret;
5851}
5852
5853
5854int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005855 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005856 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857{
5858 if (wpa_s->global->p2p_disabled)
5859 return 0;
5860 if (wpa_s->global->p2p == NULL)
5861 return 0;
5862
Dmitry Shmidt04949592012-07-19 12:16:46 -07005863 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5864 ie, ie_len)) {
5865 case P2P_PREQ_NOT_P2P:
5866 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5867 ssi_signal);
5868 /* fall through */
5869 case P2P_PREQ_MALFORMED:
5870 case P2P_PREQ_NOT_LISTEN:
5871 case P2P_PREQ_NOT_PROCESSED:
5872 default: /* make gcc happy */
5873 return 0;
5874 case P2P_PREQ_PROCESSED:
5875 return 1;
5876 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005877}
5878
5879
5880void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5881 const u8 *sa, const u8 *bssid,
5882 u8 category, const u8 *data, size_t len, int freq)
5883{
5884 if (wpa_s->global->p2p_disabled)
5885 return;
5886 if (wpa_s->global->p2p == NULL)
5887 return;
5888
5889 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5890 freq);
5891}
5892
5893
5894void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5895{
5896 if (wpa_s->global->p2p_disabled)
5897 return;
5898 if (wpa_s->global->p2p == NULL)
5899 return;
5900
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005901 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005902}
5903
5904
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07005905static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005906{
5907 p2p_group_deinit(wpa_s->p2p_group);
5908 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005909
5910 wpa_s->ap_configured_cb = NULL;
5911 wpa_s->ap_configured_cb_ctx = NULL;
5912 wpa_s->ap_configured_cb_data = NULL;
5913 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005914}
5915
5916
5917int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5918{
5919 wpa_s->p2p_long_listen = 0;
5920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005921 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5922 return -1;
5923
5924 return p2p_reject(wpa_s->global->p2p, addr);
5925}
5926
5927
5928/* Invite to reinvoke a persistent group */
5929int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005930 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005931 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005932{
5933 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005934 u8 *bssid = NULL;
5935 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005936 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005937 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005938
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005939 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005940 if (peer_addr)
5941 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5942 else
5943 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005944
Jouni Malinen31be0a42012-08-31 21:20:51 +03005945 wpa_s->p2p_persistent_go_freq = freq;
5946 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005947 if (ssid->mode == WPAS_MODE_P2P_GO) {
5948 role = P2P_INVITE_ROLE_GO;
5949 if (peer_addr == NULL) {
5950 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5951 "address in invitation command");
5952 return -1;
5953 }
5954 if (wpas_p2p_create_iface(wpa_s)) {
5955 if (wpas_p2p_add_group_interface(wpa_s,
5956 WPA_IF_P2P_GO) < 0) {
5957 wpa_printf(MSG_ERROR, "P2P: Failed to "
5958 "allocate a new interface for the "
5959 "group");
5960 return -1;
5961 }
5962 bssid = wpa_s->pending_interface_addr;
5963 } else
5964 bssid = wpa_s->own_addr;
5965 } else {
5966 role = P2P_INVITE_ROLE_CLIENT;
5967 peer_addr = ssid->bssid;
5968 }
5969 wpa_s->pending_invite_ssid_id = ssid->id;
5970
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005971 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5972 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005973 if (res)
5974 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005976 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5977 return -1;
5978
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005979 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5980 no_pref_freq_given && pref_freq > 0 &&
5981 wpa_s->num_multichan_concurrent > 1 &&
5982 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5983 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5984 pref_freq);
5985 pref_freq = 0;
5986 }
5987
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005988 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005989 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005990 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991}
5992
5993
5994/* Invite to join an active group */
5995int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5996 const u8 *peer_addr, const u8 *go_dev_addr)
5997{
5998 struct wpa_global *global = wpa_s->global;
5999 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006000 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006001 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006002 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006003 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006004 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006005
Jouni Malinen31be0a42012-08-31 21:20:51 +03006006 wpa_s->p2p_persistent_go_freq = 0;
6007 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006008 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03006009
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006010 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6011 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6012 break;
6013 }
6014 if (wpa_s == NULL) {
6015 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6016 return -1;
6017 }
6018
6019 ssid = wpa_s->current_ssid;
6020 if (ssid == NULL) {
6021 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6022 "invitation");
6023 return -1;
6024 }
6025
Dmitry Shmidt700a1372013-03-15 14:14:44 -07006026 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006027 persistent = ssid->p2p_persistent_group &&
6028 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6029 ssid->ssid, ssid->ssid_len);
6030
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006031 if (ssid->mode == WPAS_MODE_P2P_GO) {
6032 role = P2P_INVITE_ROLE_ACTIVE_GO;
6033 bssid = wpa_s->own_addr;
6034 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006035 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006036 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006037 } else {
6038 role = P2P_INVITE_ROLE_CLIENT;
6039 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6040 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6041 "invite to current group");
6042 return -1;
6043 }
6044 bssid = wpa_s->bssid;
6045 if (go_dev_addr == NULL &&
6046 !is_zero_ether_addr(wpa_s->go_dev_addr))
6047 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07006048 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6049 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006050 }
6051 wpa_s->parent->pending_invite_ssid_id = -1;
6052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006053 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6054 return -1;
6055
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006056 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6057 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006058 if (res)
6059 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07006060 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006062 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08006063 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006064 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006065}
6066
6067
6068void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6069{
6070 struct wpa_ssid *ssid = wpa_s->current_ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006071 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07006072 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006073 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006074 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006075 u8 ip[3 * 4];
6076 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006077
Dmitry Shmidt04949592012-07-19 12:16:46 -07006078 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6079 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6080 wpa_s->parent, NULL);
6081 }
6082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006083 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006084 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006085
6086 wpa_s->show_group_started = 0;
6087
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006088 os_memset(go_dev_addr, 0, ETH_ALEN);
6089 if (ssid->bssid_set)
6090 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6091 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6092 ssid->ssid_len);
6093 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6094
6095 if (wpa_s->global->p2p_group_formation == wpa_s)
6096 wpa_s->global->p2p_group_formation = NULL;
6097
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006098 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6099 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08006100
6101 ip_addr[0] = '\0';
6102 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6103 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6104 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6105 ip[0], ip[1], ip[2], ip[3],
6106 ip[4], ip[5], ip[6], ip[7],
6107 ip[8], ip[9], ip[10], ip[11]);
6108 }
6109
Dmitry Shmidt7f0b69e2014-07-28 10:35:20 -07006110 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6111 ssid->passphrase == NULL && ssid->psk_set ?
6112 ssid->psk : NULL,
6113 ssid->passphrase, go_dev_addr, persistent,
6114 ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006115
6116 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07006117 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6118 ssid, go_dev_addr);
6119 if (network_id < 0)
6120 network_id = ssid->id;
6121 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006122}
6123
6124
6125int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6126 u32 interval1, u32 duration2, u32 interval2)
6127{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006128 int ret;
6129
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006130 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6131 return -1;
6132
6133 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6134 wpa_s->current_ssid == NULL ||
6135 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6136 return -1;
6137
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006138 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6139 wpa_s->own_addr, wpa_s->assoc_freq,
6140 duration1, interval1, duration2, interval2);
6141 if (ret == 0)
6142 wpa_s->waiting_presence_resp = 1;
6143
6144 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006145}
6146
6147
6148int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6149 unsigned int interval)
6150{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006151 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6152 return -1;
6153
6154 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6155}
6156
6157
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006158static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6159{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006160 if (wpa_s->current_ssid == NULL) {
6161 /*
6162 * current_ssid can be cleared when P2P client interface gets
6163 * disconnected, so assume this interface was used as P2P
6164 * client.
6165 */
6166 return 1;
6167 }
6168 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006169 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6170}
6171
6172
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006173static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6174{
6175 struct wpa_supplicant *wpa_s = eloop_ctx;
6176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006177 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006178 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6179 "disabled");
6180 return;
6181 }
6182
Dmitry Shmidt04949592012-07-19 12:16:46 -07006183 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6184 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006185 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006186}
6187
6188
6189static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6190{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006191 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006192
Dmitry Shmidt04949592012-07-19 12:16:46 -07006193 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6194 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6195
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006196 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6197 return;
6198
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006199 timeout = wpa_s->conf->p2p_group_idle;
6200 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6201 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6202 timeout = P2P_MAX_CLIENT_IDLE;
6203
6204 if (timeout == 0)
6205 return;
6206
Dmitry Shmidt04949592012-07-19 12:16:46 -07006207 if (timeout < 0) {
6208 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6209 timeout = 0; /* special client mode no-timeout */
6210 else
6211 return;
6212 }
6213
6214 if (wpa_s->p2p_in_provisioning) {
6215 /*
6216 * Use the normal group formation timeout during the
6217 * provisioning phase to avoid terminating this process too
6218 * early due to group idle timeout.
6219 */
6220 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6221 "during provisioning");
6222 return;
6223 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306224
Dmitry Shmidt04949592012-07-19 12:16:46 -07006225 if (wpa_s->show_group_started) {
6226 /*
6227 * Use the normal group formation timeout between the end of
6228 * the provisioning phase and completion of 4-way handshake to
6229 * avoid terminating this process too early due to group idle
6230 * timeout.
6231 */
6232 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6233 "while waiting for initial 4-way handshake to "
6234 "complete");
6235 return;
6236 }
6237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006238 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006239 timeout);
6240 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6241 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242}
6243
6244
Jouni Malinen2b89da82012-08-31 22:04:41 +03006245/* Returns 1 if the interface was removed */
6246int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6247 u16 reason_code, const u8 *ie, size_t ie_len,
6248 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006249{
6250 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006251 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006252
Dmitry Shmidt04949592012-07-19 12:16:46 -07006253 if (!locally_generated)
6254 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6255 ie_len);
6256
6257 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6258 wpa_s->current_ssid &&
6259 wpa_s->current_ssid->p2p_group &&
6260 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6261 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6262 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006263 if (wpas_p2p_group_delete(wpa_s,
6264 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6265 > 0)
6266 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006267 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006268
6269 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006270}
6271
6272
6273void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006274 u16 reason_code, const u8 *ie, size_t ie_len,
6275 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006276{
6277 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6278 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006279
Dmitry Shmidt04949592012-07-19 12:16:46 -07006280 if (!locally_generated)
6281 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6282 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006283}
6284
6285
6286void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6287{
6288 struct p2p_data *p2p = wpa_s->global->p2p;
6289
6290 if (p2p == NULL)
6291 return;
6292
6293 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6294 return;
6295
6296 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6297 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6298
6299 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6300 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6301
6302 if (wpa_s->wps &&
6303 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6304 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6305
6306 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6307 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6308
6309 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6310 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6311 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6312 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6313 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6314 }
6315
6316 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6317 p2p_set_sec_dev_types(p2p,
6318 (void *) wpa_s->conf->sec_device_type,
6319 wpa_s->conf->num_sec_device_types);
6320
6321 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6322 int i;
6323 p2p_remove_wps_vendor_extensions(p2p);
6324 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6325 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6326 continue;
6327 p2p_add_wps_vendor_extension(
6328 p2p, wpa_s->conf->wps_vendor_ext[i]);
6329 }
6330 }
6331
6332 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6333 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6334 char country[3];
6335 country[0] = wpa_s->conf->country[0];
6336 country[1] = wpa_s->conf->country[1];
6337 country[2] = 0x04;
6338 p2p_set_country(p2p, country);
6339 }
6340
6341 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6342 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6343 wpa_s->conf->p2p_ssid_postfix ?
6344 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6345 0);
6346 }
6347
6348 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6349 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006350
6351 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6352 u8 reg_class, channel;
6353 int ret;
6354 unsigned int r;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006355 u8 channel_forced;
6356
Jouni Malinen75ecf522011-06-27 15:19:46 -07006357 if (wpa_s->conf->p2p_listen_reg_class &&
6358 wpa_s->conf->p2p_listen_channel) {
6359 reg_class = wpa_s->conf->p2p_listen_reg_class;
6360 channel = wpa_s->conf->p2p_listen_channel;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006361 channel_forced = 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006362 } else {
6363 reg_class = 81;
6364 /*
6365 * Pick one of the social channels randomly as the
6366 * listen channel.
6367 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006368 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6369 channel = 1;
6370 else
6371 channel = 1 + (r % 3) * 5;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006372 channel_forced = 0;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006373 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07006374 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6375 channel_forced);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006376 if (ret)
6377 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6378 "failed: %d", ret);
6379 }
6380 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6381 u8 op_reg_class, op_channel, cfg_op_channel;
6382 int ret = 0;
6383 unsigned int r;
6384 if (wpa_s->conf->p2p_oper_reg_class &&
6385 wpa_s->conf->p2p_oper_channel) {
6386 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6387 op_channel = wpa_s->conf->p2p_oper_channel;
6388 cfg_op_channel = 1;
6389 } else {
6390 op_reg_class = 81;
6391 /*
6392 * Use random operation channel from (1, 6, 11)
6393 *if no other preference is indicated.
6394 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07006395 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6396 op_channel = 1;
6397 else
6398 op_channel = 1 + (r % 3) * 5;
Jouni Malinen75ecf522011-06-27 15:19:46 -07006399 cfg_op_channel = 0;
6400 }
6401 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6402 cfg_op_channel);
6403 if (ret)
6404 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6405 "failed: %d", ret);
6406 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006407
6408 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6409 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6410 wpa_s->conf->p2p_pref_chan) < 0) {
6411 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6412 "update failed");
6413 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006414
6415 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6416 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6417 "update failed");
6418 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006419 }
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07006420
6421 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6422 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006423}
6424
6425
6426int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6427 int duration)
6428{
6429 if (!wpa_s->ap_iface)
6430 return -1;
6431 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6432 duration);
6433}
6434
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006435
6436int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6437{
6438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6439 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006440
6441 wpa_s->global->cross_connection = enabled;
6442 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6443
6444 if (!enabled) {
6445 struct wpa_supplicant *iface;
6446
6447 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6448 {
6449 if (iface->cross_connect_enabled == 0)
6450 continue;
6451
6452 iface->cross_connect_enabled = 0;
6453 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006454 wpa_msg_global(iface->parent, MSG_INFO,
6455 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6456 iface->ifname,
6457 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006458 }
6459 }
6460
6461 return 0;
6462}
6463
6464
6465static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6466{
6467 struct wpa_supplicant *iface;
6468
6469 if (!uplink->global->cross_connection)
6470 return;
6471
6472 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6473 if (!iface->cross_connect_enabled)
6474 continue;
6475 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6476 0)
6477 continue;
6478 if (iface->ap_iface == NULL)
6479 continue;
6480 if (iface->cross_connect_in_use)
6481 continue;
6482
6483 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006484 wpa_msg_global(iface->parent, MSG_INFO,
6485 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6486 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006487 }
6488}
6489
6490
6491static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6492{
6493 struct wpa_supplicant *iface;
6494
6495 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6496 if (!iface->cross_connect_enabled)
6497 continue;
6498 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6499 0)
6500 continue;
6501 if (!iface->cross_connect_in_use)
6502 continue;
6503
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006504 wpa_msg_global(iface->parent, MSG_INFO,
6505 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6506 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006507 iface->cross_connect_in_use = 0;
6508 }
6509}
6510
6511
6512void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6513{
6514 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6515 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6516 wpa_s->cross_connect_disallowed)
6517 wpas_p2p_disable_cross_connect(wpa_s);
6518 else
6519 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006520 if (!wpa_s->ap_iface &&
6521 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6522 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006523}
6524
6525
6526void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6527{
6528 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006529 if (!wpa_s->ap_iface &&
6530 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6531 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006532 wpas_p2p_set_group_idle_timeout(wpa_s);
6533}
6534
6535
6536static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6537{
6538 struct wpa_supplicant *iface;
6539
6540 if (!wpa_s->global->cross_connection)
6541 return;
6542
6543 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6544 if (iface == wpa_s)
6545 continue;
6546 if (iface->drv_flags &
6547 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6548 continue;
6549 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6550 continue;
6551
6552 wpa_s->cross_connect_enabled = 1;
6553 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6554 sizeof(wpa_s->cross_connect_uplink));
6555 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6556 "%s to %s whenever uplink is available",
6557 wpa_s->ifname, wpa_s->cross_connect_uplink);
6558
6559 if (iface->ap_iface || iface->current_ssid == NULL ||
6560 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6561 iface->cross_connect_disallowed ||
6562 iface->wpa_state != WPA_COMPLETED)
6563 break;
6564
6565 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006566 wpa_msg_global(wpa_s->parent, MSG_INFO,
6567 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6568 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006569 break;
6570 }
6571}
6572
6573
6574int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6575{
6576 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6577 !wpa_s->p2p_in_provisioning)
6578 return 0; /* not P2P client operation */
6579
6580 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6581 "session overlap");
6582 if (wpa_s != wpa_s->parent)
6583 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006584 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006585 return 1;
6586}
6587
6588
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006589void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6590{
6591 struct wpa_supplicant *wpa_s = eloop_ctx;
6592 wpas_p2p_notif_pbc_overlap(wpa_s);
6593}
6594
6595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006596void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6597{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006598 struct p2p_channels chan, cli_chan;
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006599 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006600
6601 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6602 return;
6603
6604 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006605 os_memset(&cli_chan, 0, sizeof(cli_chan));
6606 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006607 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6608 "channel list");
6609 return;
6610 }
6611
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006612 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt684785c2014-05-12 13:34:29 -07006613
6614 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6615 int freq;
6616 if (!ifs->current_ssid ||
6617 !ifs->current_ssid->p2p_group ||
6618 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6619 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6620 continue;
6621 freq = ifs->current_ssid->frequency;
6622 if (freq_included(&chan, freq)) {
6623 wpa_dbg(ifs, MSG_DEBUG,
6624 "P2P GO operating frequency %d MHz in valid range",
6625 freq);
6626 continue;
6627 }
6628
6629 wpa_dbg(ifs, MSG_DEBUG,
6630 "P2P GO operating in invalid frequency %d MHz", freq);
6631 /* TODO: Consider using CSA or removing the group within
6632 * wpa_supplicant */
6633 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6634 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006635}
6636
6637
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006638static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6639 struct wpa_scan_results *scan_res)
6640{
6641 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6642}
6643
6644
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006645int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6646{
6647 struct wpa_global *global = wpa_s->global;
6648 int found = 0;
6649 const u8 *peer;
6650
6651 if (global->p2p == NULL)
6652 return -1;
6653
6654 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6655
6656 if (wpa_s->pending_interface_name[0] &&
6657 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6658 found = 1;
6659
6660 peer = p2p_get_go_neg_peer(global->p2p);
6661 if (peer) {
6662 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6663 MACSTR, MAC2STR(peer));
6664 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006665 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006666 }
6667
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006668 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6669 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6670 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6671 found = 1;
6672 }
6673
6674 if (wpa_s->pending_pd_before_join) {
6675 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6676 wpa_s->pending_pd_before_join = 0;
6677 found = 1;
6678 }
6679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006680 wpas_p2p_stop_find(wpa_s);
6681
6682 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6683 if (wpa_s == global->p2p_group_formation &&
6684 (wpa_s->p2p_in_provisioning ||
6685 wpa_s->parent->pending_interface_type ==
6686 WPA_IF_P2P_CLIENT)) {
6687 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6688 "formation found - cancelling",
6689 wpa_s->ifname);
6690 found = 1;
6691 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6692 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006693 if (wpa_s->p2p_in_provisioning) {
6694 wpas_group_formation_completed(wpa_s, 0);
6695 break;
6696 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006697 wpas_p2p_group_delete(wpa_s,
6698 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006699 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006700 } else if (wpa_s->p2p_in_invitation) {
6701 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6702 wpa_s->ifname);
6703 found = 1;
6704 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006705 }
6706 }
6707
6708 if (!found) {
6709 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6710 return -1;
6711 }
6712
6713 return 0;
6714}
6715
6716
6717void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6718{
6719 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6720 return;
6721
6722 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6723 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006724 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006725}
6726
6727
6728void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6729 int freq_24, int freq_5, int freq_overall)
6730{
6731 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006732 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006733 return;
6734 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6735}
6736
6737
6738int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6739{
6740 u8 peer[ETH_ALEN];
6741 struct p2p_data *p2p = wpa_s->global->p2p;
6742
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006743 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006744 return -1;
6745
6746 if (hwaddr_aton(addr, peer))
6747 return -1;
6748
6749 return p2p_unauthorize(p2p, peer);
6750}
6751
6752
6753/**
6754 * wpas_p2p_disconnect - Disconnect from a P2P Group
6755 * @wpa_s: Pointer to wpa_supplicant data
6756 * Returns: 0 on success, -1 on failure
6757 *
6758 * This can be used to disconnect from a group in which the local end is a P2P
6759 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6760 * virtual network interface was created for this group, that interface will be
6761 * removed. Otherwise, only the configured P2P group network will be removed
6762 * from the interface.
6763 */
6764int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6765{
6766
6767 if (wpa_s == NULL)
6768 return -1;
6769
Jouni Malinen2b89da82012-08-31 22:04:41 +03006770 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6771 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006772}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006773
6774
6775int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6776{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006777 int ret;
6778
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006779 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6780 return 0;
6781
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006782 ret = p2p_in_progress(wpa_s->global->p2p);
6783 if (ret == 0) {
6784 /*
6785 * Check whether there is an ongoing WPS provisioning step (or
6786 * other parts of group formation) on another interface since
6787 * p2p_in_progress() does not report this to avoid issues for
6788 * scans during such provisioning step.
6789 */
6790 if (wpa_s->global->p2p_group_formation &&
6791 wpa_s->global->p2p_group_formation != wpa_s) {
6792 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6793 "in group formation",
6794 wpa_s->global->p2p_group_formation->ifname);
6795 ret = 1;
6796 }
6797 }
6798
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006799 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006800 struct os_reltime now;
6801 os_get_reltime(&now);
6802 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6803 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006804 /* Wait for the first client has expired */
6805 wpa_s->global->p2p_go_wait_client.sec = 0;
6806 } else {
6807 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6808 ret = 1;
6809 }
6810 }
6811
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006812 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006813}
6814
6815
6816void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6817 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006818{
6819 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6820 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6821 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006822 /**
6823 * Remove the network by scheduling the group formation
6824 * timeout to happen immediately. The teardown code
6825 * needs to be scheduled to run asynch later so that we
6826 * don't delete data from under ourselves unexpectedly.
6827 * Calling wpas_p2p_group_formation_timeout directly
6828 * causes a series of crashes in WPS failure scenarios.
6829 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006830 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6831 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006832 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6833 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006834 }
6835}
6836
6837
6838struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006839 const u8 *addr, const u8 *ssid,
6840 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006841{
6842 struct wpa_ssid *s;
6843 size_t i;
6844
6845 for (s = wpa_s->conf->ssid; s; s = s->next) {
6846 if (s->disabled != 2)
6847 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006848 if (ssid &&
6849 (ssid_len != s->ssid_len ||
6850 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6851 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006852 if (addr == NULL) {
6853 if (s->mode == WPAS_MODE_P2P_GO)
6854 return s;
6855 continue;
6856 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006857 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6858 return s; /* peer is GO in the persistent group */
6859 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6860 continue;
6861 for (i = 0; i < s->num_p2p_clients; i++) {
6862 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6863 addr, ETH_ALEN) == 0)
6864 return s; /* peer is P2P client in persistent
6865 * group */
6866 }
6867 }
6868
6869 return NULL;
6870}
6871
6872
6873void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6874 const u8 *addr)
6875{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006876 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6877 wpa_s->parent, NULL) > 0) {
6878 /*
6879 * This can happen if WPS provisioning step is not terminated
6880 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6881 * peer was able to connect, there is no need to time out group
6882 * formation after this, though. In addition, this is used with
6883 * the initial connection wait on the GO as a separate formation
6884 * timeout and as such, expected to be hit after the initial WPS
6885 * provisioning step.
6886 */
6887 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6888 }
6889 if (!wpa_s->p2p_go_group_formation_completed) {
6890 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6891 wpa_s->p2p_go_group_formation_completed = 1;
6892 wpa_s->global->p2p_group_formation = NULL;
6893 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006894 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006895 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006896 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006897 if (addr == NULL)
6898 return;
6899 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6900}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006901
Dmitry Shmidt04949592012-07-19 12:16:46 -07006902
6903static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6904 int group_added)
6905{
6906 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006907 if (wpa_s->global->p2p_group_formation)
6908 group = wpa_s->global->p2p_group_formation;
6909 wpa_s = wpa_s->parent;
6910 offchannel_send_action_done(wpa_s);
6911 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006912 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006913 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6914 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6915 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6916 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6917 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006918 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006919 wpa_s->p2p_go_ht40,
6920 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006921}
6922
6923
6924int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6925{
6926 if (!wpa_s->p2p_fallback_to_go_neg ||
6927 wpa_s->p2p_in_provisioning <= 5)
6928 return 0;
6929
6930 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6931 return 0; /* peer operating as a GO */
6932
6933 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6934 "fallback to GO Negotiation");
6935 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6936
6937 return 1;
6938}
6939
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006940
6941unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6942{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006943 struct wpa_supplicant *ifs;
6944
6945 if (wpa_s->wpa_state > WPA_SCANNING) {
6946 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6947 "concurrent operation",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006948 wpa_s->conf->p2p_search_delay);
6949 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006950 }
6951
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006952 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6953 radio_list) {
6954 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006955 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6956 "delay due to concurrent operation on "
6957 "interface %s",
Dmitry Shmidt09f57ba2014-06-10 16:07:13 -07006958 wpa_s->conf->p2p_search_delay,
6959 ifs->ifname);
6960 return wpa_s->conf->p2p_search_delay;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006961 }
6962 }
6963
6964 return 0;
6965}
6966
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006967
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006968static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6969 struct wpa_ssid *s, const u8 *addr,
6970 int iface_addr)
6971{
6972 struct psk_list_entry *psk, *tmp;
6973 int changed = 0;
6974
6975 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6976 list) {
6977 if ((iface_addr && !psk->p2p &&
6978 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6979 (!iface_addr && psk->p2p &&
6980 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6981 wpa_dbg(wpa_s, MSG_DEBUG,
6982 "P2P: Remove persistent group PSK list entry for "
6983 MACSTR " p2p=%u",
6984 MAC2STR(psk->addr), psk->p2p);
6985 dl_list_del(&psk->list);
6986 os_free(psk);
6987 changed++;
6988 }
6989 }
6990
6991 return changed;
6992}
6993
6994
6995void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6996 const u8 *p2p_dev_addr,
6997 const u8 *psk, size_t psk_len)
6998{
6999 struct wpa_ssid *ssid = wpa_s->current_ssid;
7000 struct wpa_ssid *persistent;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007001 struct psk_list_entry *p, *last;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007002
7003 if (psk_len != sizeof(p->psk))
7004 return;
7005
7006 if (p2p_dev_addr) {
7007 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7008 " p2p_dev_addr=" MACSTR,
7009 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7010 if (is_zero_ether_addr(p2p_dev_addr))
7011 p2p_dev_addr = NULL;
7012 } else {
7013 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7014 MAC2STR(mac_addr));
7015 }
7016
7017 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7018 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7019 /* To be added to persistent group once created */
7020 if (wpa_s->global->add_psk == NULL) {
7021 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7022 if (wpa_s->global->add_psk == NULL)
7023 return;
7024 }
7025 p = wpa_s->global->add_psk;
7026 if (p2p_dev_addr) {
7027 p->p2p = 1;
7028 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7029 } else {
7030 p->p2p = 0;
7031 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7032 }
7033 os_memcpy(p->psk, psk, psk_len);
7034 return;
7035 }
7036
7037 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7038 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7039 return;
7040 }
7041
7042 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7043 ssid->ssid_len);
7044 if (!persistent) {
7045 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7046 return;
7047 }
7048
7049 p = os_zalloc(sizeof(*p));
7050 if (p == NULL)
7051 return;
7052 if (p2p_dev_addr) {
7053 p->p2p = 1;
7054 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7055 } else {
7056 p->p2p = 0;
7057 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7058 }
7059 os_memcpy(p->psk, psk, psk_len);
7060
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07007061 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7062 (last = dl_list_last(&persistent->psk_list,
7063 struct psk_list_entry, list))) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007064 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7065 MACSTR " (p2p=%u) to make room for a new one",
7066 MAC2STR(last->addr), last->p2p);
7067 dl_list_del(&last->list);
7068 os_free(last);
7069 }
7070
7071 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7072 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7073 p2p_dev_addr == NULL);
7074 if (p2p_dev_addr) {
7075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7076 MACSTR, MAC2STR(p2p_dev_addr));
7077 } else {
7078 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7079 MAC2STR(mac_addr));
7080 }
7081 dl_list_add(&persistent->psk_list, &p->list);
7082
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007083 if (wpa_s->parent->conf->update_config &&
7084 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7085 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007086}
7087
7088
7089static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7090 struct wpa_ssid *s, const u8 *addr,
7091 int iface_addr)
7092{
7093 int res;
7094
7095 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08007096 if (res > 0 && wpa_s->conf->update_config &&
7097 wpa_config_write(wpa_s->confname, wpa_s->conf))
7098 wpa_dbg(wpa_s, MSG_DEBUG,
7099 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007100}
7101
7102
7103static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7104 const u8 *peer, int iface_addr)
7105{
7106 struct hostapd_data *hapd;
7107 struct hostapd_wpa_psk *psk, *prev, *rem;
7108 struct sta_info *sta;
7109
7110 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7111 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7112 return;
7113
7114 /* Remove per-station PSK entry */
7115 hapd = wpa_s->ap_iface->bss[0];
7116 prev = NULL;
7117 psk = hapd->conf->ssid.wpa_psk;
7118 while (psk) {
7119 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7120 (!iface_addr &&
7121 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7122 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7123 MACSTR " iface_addr=%d",
7124 MAC2STR(peer), iface_addr);
7125 if (prev)
7126 prev->next = psk->next;
7127 else
7128 hapd->conf->ssid.wpa_psk = psk->next;
7129 rem = psk;
7130 psk = psk->next;
7131 os_free(rem);
7132 } else {
7133 prev = psk;
7134 psk = psk->next;
7135 }
7136 }
7137
7138 /* Disconnect from group */
7139 if (iface_addr)
7140 sta = ap_get_sta(hapd, peer);
7141 else
7142 sta = ap_get_sta_p2p(hapd, peer);
7143 if (sta) {
7144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7145 " (iface_addr=%d) from group",
7146 MAC2STR(peer), iface_addr);
7147 hostapd_drv_sta_deauth(hapd, sta->addr,
7148 WLAN_REASON_DEAUTH_LEAVING);
7149 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7150 }
7151}
7152
7153
7154void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7155 int iface_addr)
7156{
7157 struct wpa_ssid *s;
7158 struct wpa_supplicant *w;
7159
7160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7161
7162 /* Remove from any persistent group */
7163 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7164 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7165 continue;
7166 if (!iface_addr)
7167 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7168 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7169 }
7170
7171 /* Remove from any operating group */
7172 for (w = wpa_s->global->ifaces; w; w = w->next)
7173 wpas_p2p_remove_client_go(w, peer, iface_addr);
7174}
7175
7176
7177static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7178{
7179 struct wpa_supplicant *wpa_s = eloop_ctx;
7180 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7181}
7182
7183
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08007184static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7185{
7186 struct wpa_supplicant *wpa_s = eloop_ctx;
7187
7188 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7189 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7190}
7191
7192
7193int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7194 struct wpa_ssid *ssid)
7195{
7196 struct wpa_supplicant *iface;
7197
7198 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7199 if (!iface->current_ssid ||
7200 iface->current_ssid->frequency == freq ||
7201 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7202 !iface->current_ssid->p2p_group))
7203 continue;
7204
7205 /* Remove the connection with least priority */
7206 if (!wpas_is_p2p_prioritized(iface)) {
7207 /* STA connection has priority over existing
7208 * P2P connection, so remove the interface. */
7209 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7210 eloop_register_timeout(0, 0,
7211 wpas_p2p_group_freq_conflict,
7212 iface, NULL);
7213 /* If connection in progress is P2P connection, do not
7214 * proceed for the connection. */
7215 if (wpa_s == iface)
7216 return -1;
7217 else
7218 return 0;
7219 } else {
7220 /* P2P connection has priority, disable the STA network
7221 */
7222 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7223 ssid);
7224 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7225 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7226 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7227 ETH_ALEN);
7228 /* If P2P connection is in progress, continue
7229 * connecting...*/
7230 if (wpa_s == iface)
7231 return 0;
7232 else
7233 return -1;
7234 }
7235 }
7236
7237 return 0;
7238}
7239
7240
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007241int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7242{
7243 struct wpa_ssid *ssid = wpa_s->current_ssid;
7244
7245 if (ssid == NULL || !ssid->p2p_group)
7246 return 0;
7247
7248 if (wpa_s->p2p_last_4way_hs_fail &&
7249 wpa_s->p2p_last_4way_hs_fail == ssid) {
7250 u8 go_dev_addr[ETH_ALEN];
7251 struct wpa_ssid *persistent;
7252
7253 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7254 ssid->ssid,
7255 ssid->ssid_len) <= 0) {
7256 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7257 goto disconnect;
7258 }
7259
7260 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7261 MACSTR, MAC2STR(go_dev_addr));
7262 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7263 ssid->ssid,
7264 ssid->ssid_len);
7265 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7266 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7267 goto disconnect;
7268 }
7269 wpa_msg_global(wpa_s->parent, MSG_INFO,
7270 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7271 persistent->id);
7272 disconnect:
7273 wpa_s->p2p_last_4way_hs_fail = NULL;
7274 /*
7275 * Remove the group from a timeout to avoid issues with caller
7276 * continuing to use the interface if this is on a P2P group
7277 * interface.
7278 */
7279 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7280 wpa_s, NULL);
7281 return 1;
7282 }
7283
7284 wpa_s->p2p_last_4way_hs_fail = ssid;
7285 return 0;
7286}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007287
7288
7289#ifdef CONFIG_WPS_NFC
7290
7291static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7292 struct wpabuf *p2p)
7293{
7294 struct wpabuf *ret;
7295 size_t wsc_len;
7296
7297 if (p2p == NULL) {
7298 wpabuf_free(wsc);
7299 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7300 return NULL;
7301 }
7302
7303 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7304 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7305 if (ret == NULL) {
7306 wpabuf_free(wsc);
7307 wpabuf_free(p2p);
7308 return NULL;
7309 }
7310
7311 wpabuf_put_be16(ret, wsc_len);
7312 if (wsc)
7313 wpabuf_put_buf(ret, wsc);
7314 wpabuf_put_be16(ret, wpabuf_len(p2p));
7315 wpabuf_put_buf(ret, p2p);
7316
7317 wpabuf_free(wsc);
7318 wpabuf_free(p2p);
7319 wpa_hexdump_buf(MSG_DEBUG,
7320 "P2P: Generated NFC connection handover message", ret);
7321
7322 if (ndef && ret) {
7323 struct wpabuf *tmp;
7324 tmp = ndef_build_p2p(ret);
7325 wpabuf_free(ret);
7326 if (tmp == NULL) {
7327 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7328 return NULL;
7329 }
7330 ret = tmp;
7331 }
7332
7333 return ret;
7334}
7335
7336
7337static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7338 struct wpa_ssid **ssid, u8 *go_dev_addr)
7339{
7340 struct wpa_supplicant *iface;
7341
7342 if (go_dev_addr)
7343 os_memset(go_dev_addr, 0, ETH_ALEN);
7344 if (ssid)
7345 *ssid = NULL;
7346 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7347 if (iface->wpa_state < WPA_ASSOCIATING ||
7348 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7349 !iface->current_ssid->p2p_group ||
7350 iface->current_ssid->mode != WPAS_MODE_INFRA)
7351 continue;
7352 if (ssid)
7353 *ssid = iface->current_ssid;
7354 if (go_dev_addr)
7355 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7356 return iface->assoc_freq;
7357 }
7358 return 0;
7359}
7360
7361
7362struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7363 int ndef)
7364{
7365 struct wpabuf *wsc, *p2p;
7366 struct wpa_ssid *ssid;
7367 u8 go_dev_addr[ETH_ALEN];
7368 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7369
7370 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7371 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7372 return NULL;
7373 }
7374
7375 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7376 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7377 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7378 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7379 return NULL;
7380 }
7381
7382 if (cli_freq == 0) {
7383 wsc = wps_build_nfc_handover_req_p2p(
7384 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7385 } else
7386 wsc = NULL;
7387 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7388 go_dev_addr, ssid ? ssid->ssid : NULL,
7389 ssid ? ssid->ssid_len : 0);
7390
7391 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7392}
7393
7394
7395struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7396 int ndef, int tag)
7397{
7398 struct wpabuf *wsc, *p2p;
7399 struct wpa_ssid *ssid;
7400 u8 go_dev_addr[ETH_ALEN];
7401 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7402
7403 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7404 return NULL;
7405
7406 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7407 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7408 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7409 return NULL;
7410
7411 if (cli_freq == 0) {
7412 wsc = wps_build_nfc_handover_sel_p2p(
7413 wpa_s->parent->wps,
7414 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7415 DEV_PW_NFC_CONNECTION_HANDOVER,
7416 wpa_s->conf->wps_nfc_dh_pubkey,
7417 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7418 } else
7419 wsc = NULL;
7420 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7421 go_dev_addr, ssid ? ssid->ssid : NULL,
7422 ssid ? ssid->ssid_len : 0);
7423
7424 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7425}
7426
7427
7428static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7429 struct p2p_nfc_params *params)
7430{
7431 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7432 "connection handover (freq=%d)",
7433 params->go_freq);
7434
7435 if (params->go_freq && params->go_ssid_len) {
7436 wpa_s->p2p_wps_method = WPS_NFC;
7437 wpa_s->pending_join_wps_method = WPS_NFC;
7438 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7439 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7440 ETH_ALEN);
7441 return wpas_p2p_join_start(wpa_s, params->go_freq,
7442 params->go_ssid,
7443 params->go_ssid_len);
7444 }
7445
7446 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7447 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7448 params->go_freq, -1, 0, 1, 1);
7449}
7450
7451
7452static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7453 struct p2p_nfc_params *params, int tag)
7454{
7455 int res, persistent;
7456 struct wpa_ssid *ssid;
7457
7458 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7459 "connection handover");
7460 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7461 ssid = wpa_s->current_ssid;
7462 if (ssid == NULL)
7463 continue;
7464 if (ssid->mode != WPAS_MODE_P2P_GO)
7465 continue;
7466 if (wpa_s->ap_iface == NULL)
7467 continue;
7468 break;
7469 }
7470 if (wpa_s == NULL) {
7471 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7472 return -1;
7473 }
7474
7475 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7476 DEV_PW_NFC_CONNECTION_HANDOVER &&
7477 !wpa_s->parent->p2p_oob_dev_pw) {
7478 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7479 return -1;
7480 }
7481 res = wpas_ap_wps_add_nfc_pw(
7482 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7483 wpa_s->parent->p2p_oob_dev_pw,
7484 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7485 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7486 if (res)
7487 return res;
7488
7489 if (!tag) {
7490 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7491 return 0;
7492 }
7493
7494 if (!params->peer ||
7495 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7496 return 0;
7497
7498 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7499 " to join", MAC2STR(params->peer->p2p_device_addr));
7500
7501 wpa_s->global->p2p_invite_group = wpa_s;
7502 persistent = ssid->p2p_persistent_group &&
7503 wpas_p2p_get_persistent(wpa_s->parent,
7504 params->peer->p2p_device_addr,
7505 ssid->ssid, ssid->ssid_len);
7506 wpa_s->parent->pending_invite_ssid_id = -1;
7507
7508 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7509 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7510 ssid->ssid, ssid->ssid_len, ssid->frequency,
7511 wpa_s->global->p2p_dev_addr, persistent, 0,
7512 wpa_s->parent->p2p_oob_dev_pw_id);
7513}
7514
7515
7516static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7517 struct p2p_nfc_params *params,
7518 int forced_freq)
7519{
7520 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7521 "connection handover");
7522 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7523 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7524 forced_freq, -1, 0, 1, 1);
7525}
7526
7527
7528static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7529 struct p2p_nfc_params *params,
7530 int forced_freq)
7531{
7532 int res;
7533
7534 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7535 "connection handover");
7536 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7537 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7538 forced_freq, -1, 0, 1, 1);
7539 if (res)
7540 return res;
7541
7542 res = wpas_p2p_listen(wpa_s, 60);
7543 if (res) {
7544 p2p_unauthorize(wpa_s->global->p2p,
7545 params->peer->p2p_device_addr);
7546 }
7547
7548 return res;
7549}
7550
7551
7552static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7553 const struct wpabuf *data,
7554 int sel, int tag, int forced_freq)
7555{
7556 const u8 *pos, *end;
7557 u16 len, id;
7558 struct p2p_nfc_params params;
7559 int res;
7560
7561 os_memset(&params, 0, sizeof(params));
7562 params.sel = sel;
7563
7564 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7565
7566 pos = wpabuf_head(data);
7567 end = pos + wpabuf_len(data);
7568
7569 if (end - pos < 2) {
7570 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7571 "attributes");
7572 return -1;
7573 }
7574 len = WPA_GET_BE16(pos);
7575 pos += 2;
7576 if (pos + len > end) {
7577 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7578 "attributes");
7579 return -1;
7580 }
7581 params.wsc_attr = pos;
7582 params.wsc_len = len;
7583 pos += len;
7584
7585 if (end - pos < 2) {
7586 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7587 "attributes");
7588 return -1;
7589 }
7590 len = WPA_GET_BE16(pos);
7591 pos += 2;
7592 if (pos + len > end) {
7593 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7594 "attributes");
7595 return -1;
7596 }
7597 params.p2p_attr = pos;
7598 params.p2p_len = len;
7599 pos += len;
7600
7601 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7602 params.wsc_attr, params.wsc_len);
7603 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7604 params.p2p_attr, params.p2p_len);
7605 if (pos < end) {
7606 wpa_hexdump(MSG_DEBUG,
7607 "P2P: Ignored extra data after P2P attributes",
7608 pos, end - pos);
7609 }
7610
7611 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7612 if (res)
7613 return res;
7614
7615 if (params.next_step == NO_ACTION)
7616 return 0;
7617
7618 if (params.next_step == BOTH_GO) {
7619 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7620 MAC2STR(params.peer->p2p_device_addr));
7621 return 0;
7622 }
7623
7624 if (params.next_step == PEER_CLIENT) {
7625 if (!is_zero_ether_addr(params.go_dev_addr)) {
7626 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7627 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7628 " ssid=\"%s\"",
7629 MAC2STR(params.peer->p2p_device_addr),
7630 params.go_freq,
7631 MAC2STR(params.go_dev_addr),
7632 wpa_ssid_txt(params.go_ssid,
7633 params.go_ssid_len));
7634 } else {
7635 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7636 "peer=" MACSTR " freq=%d",
7637 MAC2STR(params.peer->p2p_device_addr),
7638 params.go_freq);
7639 }
7640 return 0;
7641 }
7642
7643 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7644 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7645 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7646 return 0;
7647 }
7648
7649 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7650 wpa_s->p2p_oob_dev_pw = NULL;
7651
7652 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7653 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7654 "received");
7655 return -1;
7656 }
7657
7658 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7659 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7660 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7661 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7662 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7663 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7664 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7665
7666 if (tag) {
7667 if (id < 0x10) {
7668 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7669 "peer OOB Device Password Id %u", id);
7670 return -1;
7671 }
7672 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7673 "Device Password Id %u", id);
7674 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7675 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7676 params.oob_dev_pw_len -
7677 WPS_OOB_PUBKEY_HASH_LEN - 2);
7678 wpa_s->p2p_oob_dev_pw_id = id;
7679 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7680 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7681 params.oob_dev_pw_len -
7682 WPS_OOB_PUBKEY_HASH_LEN - 2);
7683 if (wpa_s->p2p_oob_dev_pw == NULL)
7684 return -1;
7685
7686 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7687 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7688 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7689 return -1;
7690 } else {
7691 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7692 "without Device Password");
7693 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7694 }
7695
7696 switch (params.next_step) {
7697 case NO_ACTION:
7698 case BOTH_GO:
7699 case PEER_CLIENT:
7700 /* already covered above */
7701 return 0;
7702 case JOIN_GROUP:
7703 return wpas_p2p_nfc_join_group(wpa_s, &params);
7704 case AUTH_JOIN:
7705 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7706 case INIT_GO_NEG:
7707 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7708 case RESP_GO_NEG:
7709 /* TODO: use own OOB Dev Pw */
7710 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7711 }
7712
7713 return -1;
7714}
7715
7716
7717int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7718 const struct wpabuf *data, int forced_freq)
7719{
7720 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7721 return -1;
7722
7723 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7724}
7725
7726
7727int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7728 const struct wpabuf *req,
7729 const struct wpabuf *sel, int forced_freq)
7730{
7731 struct wpabuf *tmp;
7732 int ret;
7733
7734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7735 return -1;
7736
7737 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7738
7739 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7740 wpabuf_head(req), wpabuf_len(req));
7741 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7742 wpabuf_head(sel), wpabuf_len(sel));
7743 if (forced_freq)
7744 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7745 tmp = ndef_parse_p2p(init ? sel : req);
7746 if (tmp == NULL) {
7747 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7748 return -1;
7749 }
7750
7751 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7752 forced_freq);
7753 wpabuf_free(tmp);
7754
7755 return ret;
7756}
7757
7758
7759int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7760{
7761 const u8 *if_addr;
7762 int go_intent = wpa_s->conf->p2p_go_intent;
7763 struct wpa_supplicant *iface;
7764
7765 if (wpa_s->global->p2p == NULL)
7766 return -1;
7767
7768 if (!enabled) {
7769 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7770 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7771 {
7772 if (!iface->ap_iface)
7773 continue;
7774 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7775 }
7776 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7777 0, NULL);
7778 if (wpa_s->p2p_nfc_tag_enabled)
7779 wpas_p2p_remove_pending_group_interface(wpa_s);
7780 wpa_s->p2p_nfc_tag_enabled = 0;
7781 return 0;
7782 }
7783
7784 if (wpa_s->global->p2p_disabled)
7785 return -1;
7786
7787 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7788 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7789 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7790 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7791 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7792 "to allow static handover cases");
7793 return -1;
7794 }
7795
7796 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7797
7798 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7799 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7800 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7801 if (wpa_s->p2p_oob_dev_pw == NULL)
7802 return -1;
7803 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7804
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007805 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7806 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7807 /*
7808 * P2P Group Interface present and the command came on group
7809 * interface, so enable the token for the current interface.
7810 */
7811 wpa_s->create_p2p_iface = 0;
7812 } else {
7813 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7814 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007815
7816 if (wpa_s->create_p2p_iface) {
7817 enum wpa_driver_if_type iftype;
7818 /* Prepare to add a new interface for the group */
7819 iftype = WPA_IF_P2P_GROUP;
7820 if (go_intent == 15)
7821 iftype = WPA_IF_P2P_GO;
7822 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7823 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7824 "interface for the group");
7825 return -1;
7826 }
7827
7828 if_addr = wpa_s->pending_interface_addr;
7829 } else
7830 if_addr = wpa_s->own_addr;
7831
7832 wpa_s->p2p_nfc_tag_enabled = enabled;
7833
7834 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7835 struct hostapd_data *hapd;
7836 if (iface->ap_iface == NULL)
7837 continue;
7838 hapd = iface->ap_iface->bss[0];
7839 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7840 hapd->conf->wps_nfc_dh_pubkey =
7841 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7842 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7843 hapd->conf->wps_nfc_dh_privkey =
7844 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7845 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7846 hapd->conf->wps_nfc_dev_pw =
7847 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7848 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7849
7850 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7851 wpa_dbg(iface, MSG_DEBUG,
7852 "P2P: Failed to enable NFC Tag for GO");
7853 }
7854 }
7855 p2p_set_authorized_oob_dev_pw_id(
7856 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7857 if_addr);
7858
7859 return 0;
7860}
7861
7862#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07007863
7864
7865static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
7866 struct wpa_used_freq_data *freqs,
7867 unsigned int num)
7868{
7869 u8 curr_chan, cand, chan;
7870 unsigned int i;
7871
7872 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
7873 for (i = 0, cand = 0; i < num; i++) {
7874 ieee80211_freq_to_chan(freqs[i].freq, &chan);
7875 if (curr_chan == chan) {
7876 cand = 0;
7877 break;
7878 }
7879
7880 if (chan == 1 || chan == 6 || chan == 11)
7881 cand = chan;
7882 }
7883
7884 if (cand) {
7885 wpa_dbg(wpa_s, MSG_DEBUG,
7886 "P2P: Update Listen channel to %u baased on operating channel",
7887 cand);
7888 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
7889 }
7890}
7891
7892
7893void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
7894{
7895 struct wpa_used_freq_data *freqs;
7896 unsigned int num = wpa_s->num_multichan_concurrent;
7897
7898 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7899 return;
7900
7901 /*
7902 * If possible, optimize the Listen channel to be a channel that is
7903 * already used by one of the other interfaces.
7904 */
7905 if (!wpa_s->conf->p2p_optimize_listen_chan)
7906 return;
7907
7908 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
7909 return;
7910
7911 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7912 if (!freqs)
7913 return;
7914
7915 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7916
7917 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7918 os_free(freqs);
7919}
7920
7921
7922void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
7923{
7924 if (wpa_s == wpa_s->parent)
7925 wpas_p2p_group_remove(wpa_s, "*");
7926 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
7927 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
7928 "the management interface is being removed");
7929 wpas_p2p_deinit_global(wpa_s->global);
7930 }
7931}
7932
7933
7934void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
7935{
7936 if (wpa_s->ap_iface->bss)
7937 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
7938 wpas_p2p_group_deinit(wpa_s);
7939}