blob: 49b2cd28a0be43b3170f9d8ea905154cc8d980a3 [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 Shmidt61d9df32012-08-29 16:22:06 -070085#ifndef P2P_CONCURRENT_SEARCH_DELAY
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070086#define P2P_CONCURRENT_SEARCH_DELAY 500
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070087#endif /* P2P_CONCURRENT_SEARCH_DELAY */
88
Dmitry Shmidt34af3062013-07-11 10:46:32 -070089#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
90
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070091enum p2p_group_removal_reason {
92 P2P_GROUP_REMOVAL_UNKNOWN,
93 P2P_GROUP_REMOVAL_SILENT,
94 P2P_GROUP_REMOVAL_FORMATION_FAILED,
95 P2P_GROUP_REMOVAL_REQUESTED,
96 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
97 P2P_GROUP_REMOVAL_UNAVAILABLE,
98 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -080099 P2P_GROUP_REMOVAL_PSK_FAILURE,
100 P2P_GROUP_REMOVAL_FREQ_CONFLICT
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700101};
102
Jouni Malinendc7b7132012-09-14 12:53:47 -0700103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
105static struct wpa_supplicant *
106wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
107 int go);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800108static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
109 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt344abd32014-01-14 13:17:00 -0800110static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
111 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700112static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
113static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700114 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800115 int auto_join, int freq,
116 const u8 *ssid, size_t ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700117static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
119static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
120static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800121static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
122 void *timeout_ctx);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800123static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700124static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
125 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800126static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800127static void wpas_stop_listen(void *ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128
129
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700130/*
131 * Get the number of concurrent channels that the HW can operate, but that are
132 * currently not in use by any of the wpa_supplicant interfaces.
133 */
134static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
135{
136 int *freqs;
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800137 int num, unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700138
139 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 if (!freqs)
141 return -1;
142
143 num = get_shared_radio_freqs(wpa_s, freqs,
144 wpa_s->num_multichan_concurrent);
145 os_free(freqs);
146
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800147 unused = wpa_s->num_multichan_concurrent - num;
148 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
149 return unused;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700150}
151
152
153/*
154 * Get the frequencies that are currently in use by one or more of the virtual
155 * interfaces, and that are also valid for P2P operation.
156 */
157static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
158 int *p2p_freqs, unsigned int len)
159{
160 int *freqs;
161 unsigned int num, i, j;
162
163 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
164 if (!freqs)
165 return -1;
166
167 num = get_shared_radio_freqs(wpa_s, freqs,
168 wpa_s->num_multichan_concurrent);
169
170 os_memset(p2p_freqs, 0, sizeof(int) * len);
171
172 for (i = 0, j = 0; i < num && j < len; i++) {
173 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
174 p2p_freqs[j++] = freqs[i];
175 }
176
177 os_free(freqs);
178
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800179 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
180
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700181 return j;
182}
183
184
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700185static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
186 int freq)
187{
188 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
189 return;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800190 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
191 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
192 wpas_p2p_num_unused_channels(wpa_s) > 0) {
193 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
194 freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700195 freq = 0;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -0800196 }
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700197 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
198}
199
200
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
202 struct wpa_scan_results *scan_res)
203{
204 size_t i;
205
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800206 if (wpa_s->p2p_scan_work) {
207 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
208 wpa_s->p2p_scan_work = NULL;
209 radio_work_done(work);
210 }
211
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
213 return;
214
215 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
216 (int) scan_res->num);
217
218 for (i = 0; i < scan_res->num; i++) {
219 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800220 struct os_reltime time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700221 const u8 *ies;
222 size_t ies_len;
223
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800224 time_tmp_age.sec = bss->age / 1000;
225 time_tmp_age.usec = (bss->age % 1000) * 1000;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800226 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700227
228 ies = (const u8 *) (bss + 1);
229 ies_len = bss->ie_len;
230 if (bss->beacon_ie_len > 0 &&
231 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
232 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
233 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
234 MACSTR, MAC2STR(bss->bssid));
235 ies = ies + ies_len;
236 ies_len = bss->beacon_ie_len;
237 }
238
239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800241 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700242 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 break;
244 }
245
246 p2p_scan_res_handled(wpa_s->global->p2p);
247}
248
249
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800250static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
251{
252 struct wpa_supplicant *wpa_s = work->wpa_s;
253 struct wpa_driver_scan_params *params = work->ctx;
254 int ret;
255
256 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800257 if (!work->started) {
258 wpa_scan_free_params(params);
259 return;
260 }
261
262 wpa_s->p2p_scan_work = NULL;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800263 return;
264 }
265
266 ret = wpa_drv_scan(wpa_s, params);
267 wpa_scan_free_params(params);
268 work->ctx = NULL;
269 if (ret) {
270 radio_work_done(work);
271 return;
272 }
273
274 os_get_reltime(&wpa_s->scan_trigger_time);
275 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
276 wpa_s->own_scan_requested = 1;
277 wpa_s->p2p_scan_work = work;
278}
279
280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
282 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700283 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284{
285 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800286 struct wpa_driver_scan_params *params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700287 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800288 size_t ielen;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800289 u8 *n;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290
291 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
292 return -1;
293
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294 if (wpa_s->p2p_scan_work) {
295 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
296 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700297 }
298
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800299 params = os_zalloc(sizeof(*params));
300 if (params == NULL)
301 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302
303 /* P2P Wildcard SSID */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800304 params->num_ssids = 1;
305 n = os_malloc(P2P_WILDCARD_SSID_LEN);
306 if (n == NULL)
307 goto fail;
308 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
309 params->ssids[0].ssid = n;
310 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311
312 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700313 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
314 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 num_req_dev_types, req_dev_types);
316 if (wps_ie == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800317 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800319 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
320 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 if (ies == NULL) {
322 wpabuf_free(wps_ie);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800323 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 }
325 wpabuf_put_buf(ies, wps_ie);
326 wpabuf_free(wps_ie);
327
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800328 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800330 params->p2p_probe = 1;
331 n = os_malloc(wpabuf_len(ies));
332 if (n == NULL) {
333 wpabuf_free(ies);
334 goto fail;
335 }
336 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
337 params->extra_ies = n;
338 params->extra_ies_len = wpabuf_len(ies);
339 wpabuf_free(ies);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340
341 switch (type) {
342 case P2P_SCAN_SOCIAL:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800343 params->freqs = os_malloc(4 * sizeof(int));
344 if (params->freqs == NULL)
345 goto fail;
346 params->freqs[0] = 2412;
347 params->freqs[1] = 2437;
348 params->freqs[2] = 2462;
349 params->freqs[3] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 break;
351 case P2P_SCAN_FULL:
352 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353 case P2P_SCAN_SOCIAL_PLUS_ONE:
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800354 params->freqs = os_malloc(5 * sizeof(int));
355 if (params->freqs == NULL)
356 goto fail;
357 params->freqs[0] = 2412;
358 params->freqs[1] = 2437;
359 params->freqs[2] = 2462;
360 params->freqs[3] = freq;
361 params->freqs[4] = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 break;
363 }
364
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800365 radio_remove_works(wpa_s, "p2p-scan", 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800366 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
367 params) < 0)
368 goto fail;
369 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800371fail:
372 wpa_scan_free_params(params);
373 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374}
375
376
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700377static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
378{
379 switch (p2p_group_interface) {
380 case P2P_GROUP_INTERFACE_PENDING:
381 return WPA_IF_P2P_GROUP;
382 case P2P_GROUP_INTERFACE_GO:
383 return WPA_IF_P2P_GO;
384 case P2P_GROUP_INTERFACE_CLIENT:
385 return WPA_IF_P2P_CLIENT;
386 }
387
388 return WPA_IF_P2P_GROUP;
389}
390
391
392static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
393 const u8 *ssid,
394 size_t ssid_len, int *go)
395{
396 struct wpa_ssid *s;
397
398 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
399 for (s = wpa_s->conf->ssid; s; s = s->next) {
400 if (s->disabled != 0 || !s->p2p_group ||
401 s->ssid_len != ssid_len ||
402 os_memcmp(ssid, s->ssid, ssid_len) != 0)
403 continue;
404 if (s->mode == WPAS_MODE_P2P_GO &&
405 s != wpa_s->current_ssid)
406 continue;
407 if (go)
408 *go = s->mode == WPAS_MODE_P2P_GO;
409 return wpa_s;
410 }
411 }
412
413 return NULL;
414}
415
416
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
418 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419{
420 struct wpa_ssid *ssid;
421 char *gtype;
422 const char *reason;
423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 ssid = wpa_s->current_ssid;
425 if (ssid == NULL) {
426 /*
427 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700428 * pending P2P group interface waiting for provisioning or a
429 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430 */
431 ssid = wpa_s->conf->ssid;
432 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700433 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 break;
435 ssid = ssid->next;
436 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300437 if (ssid == NULL &&
438 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
439 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700440 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
441 "not found");
442 return -1;
443 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 }
445 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
446 gtype = "GO";
447 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
448 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
449 wpa_s->reassociate = 0;
450 wpa_s->disconnected = 1;
451 wpa_supplicant_deauthenticate(wpa_s,
452 WLAN_REASON_DEAUTH_LEAVING);
453 gtype = "client";
454 } else
455 gtype = "GO";
456 if (wpa_s->cross_connect_in_use) {
457 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700458 wpa_msg_global(wpa_s->parent, MSG_INFO,
459 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
460 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700462 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 case P2P_GROUP_REMOVAL_REQUESTED:
464 reason = " reason=REQUESTED";
465 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700466 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
467 reason = " reason=FORMATION_FAILED";
468 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
470 reason = " reason=IDLE";
471 break;
472 case P2P_GROUP_REMOVAL_UNAVAILABLE:
473 reason = " reason=UNAVAILABLE";
474 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700475 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
476 reason = " reason=GO_ENDING_SESSION";
477 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700478 case P2P_GROUP_REMOVAL_PSK_FAILURE:
479 reason = " reason=PSK_FAILURE";
480 break;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800481 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
482 reason = " reason=FREQ_CONFLICT";
483 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 default:
485 reason = "";
486 break;
487 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700488 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700489 wpa_msg_global(wpa_s->parent, MSG_INFO,
490 P2P_EVENT_GROUP_REMOVED "%s %s%s",
491 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700492 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800494 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
495 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
Dmitry Shmidt04949592012-07-19 12:16:46 -0700496 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
497 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800498 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700499 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800500 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
501 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700502 wpa_s->p2p_in_provisioning = 0;
503 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700504
Dmitry Shmidt15907092014-03-25 10:42:57 -0700505 wpa_s->p2p_in_invitation = 0;
506
Dmitry Shmidt96571392013-10-14 12:54:46 -0700507 /*
508 * Make sure wait for the first client does not remain active after the
509 * group has been removed.
510 */
511 wpa_s->global->p2p_go_wait_client.sec = 0;
512
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700513 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
515
516 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
517 struct wpa_global *global;
518 char *ifname;
519 enum wpa_driver_if_type type;
520 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
521 wpa_s->ifname);
522 global = wpa_s->global;
523 ifname = os_strdup(wpa_s->ifname);
524 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700525 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 wpa_s = global->ifaces;
527 if (wpa_s && ifname)
528 wpa_drv_if_remove(wpa_s, type, ifname);
529 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300530 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 }
532
Dmitry Shmidt96571392013-10-14 12:54:46 -0700533 if (!wpa_s->p2p_go_group_formation_completed) {
534 wpa_s->global->p2p_group_formation = NULL;
535 wpa_s->p2p_in_provisioning = 0;
536 }
537
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800538 wpa_s->show_group_started = 0;
539 os_free(wpa_s->go_params);
540 wpa_s->go_params = NULL;
541
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800542 wpa_s->waiting_presence_resp = 0;
543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
545 if (ssid && (ssid->p2p_group ||
546 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
547 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
548 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700549 if (ssid == wpa_s->current_ssid) {
550 wpa_sm_set_config(wpa_s->wpa, NULL);
551 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700553 }
554 /*
555 * Networks objects created during any P2P activities are not
556 * exposed out as they might/will confuse certain non-P2P aware
557 * applications since these network objects won't behave like
558 * regular ones.
559 *
560 * Likewise, we don't send out network removed signals for such
561 * network objects.
562 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 wpa_config_remove_network(wpa_s->conf, id);
564 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800565 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566 } else {
567 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
568 "found");
569 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700570 if (wpa_s->ap_iface)
571 wpa_supplicant_ap_deinit(wpa_s);
572 else
573 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700574
575 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576}
577
578
579static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
580 u8 *go_dev_addr,
581 const u8 *ssid, size_t ssid_len)
582{
583 struct wpa_bss *bss;
584 const u8 *bssid;
585 struct wpabuf *p2p;
586 u8 group_capab;
587 const u8 *addr;
588
589 if (wpa_s->go_params)
590 bssid = wpa_s->go_params->peer_interface_addr;
591 else
592 bssid = wpa_s->bssid;
593
594 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800595 if (bss == NULL && wpa_s->go_params &&
596 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
597 bss = wpa_bss_get_p2p_dev_addr(
598 wpa_s, wpa_s->go_params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 if (bss == NULL) {
600 u8 iface_addr[ETH_ALEN];
601 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
602 iface_addr) == 0)
603 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
604 }
605 if (bss == NULL) {
606 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
607 "group is persistent - BSS " MACSTR " not found",
608 MAC2STR(bssid));
609 return 0;
610 }
611
612 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700613 if (p2p == NULL)
614 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
615 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 if (p2p == NULL) {
617 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
618 "group is persistent - BSS " MACSTR
619 " did not include P2P IE", MAC2STR(bssid));
620 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
621 (u8 *) (bss + 1), bss->ie_len);
622 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
623 ((u8 *) bss + 1) + bss->ie_len,
624 bss->beacon_ie_len);
625 return 0;
626 }
627
628 group_capab = p2p_get_group_capab(p2p);
629 addr = p2p_get_go_dev_addr(p2p);
630 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
631 "group_capab=0x%x", group_capab);
632 if (addr) {
633 os_memcpy(go_dev_addr, addr, ETH_ALEN);
634 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
635 MAC2STR(addr));
636 } else
637 os_memset(go_dev_addr, 0, ETH_ALEN);
638 wpabuf_free(p2p);
639
640 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
641 "go_dev_addr=" MACSTR,
642 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
643
644 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
645}
646
647
Jouni Malinen75ecf522011-06-27 15:19:46 -0700648static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
649 struct wpa_ssid *ssid,
650 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651{
652 struct wpa_ssid *s;
653 int changed = 0;
654
655 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
656 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
657 for (s = wpa_s->conf->ssid; s; s = s->next) {
658 if (s->disabled == 2 &&
659 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
660 s->ssid_len == ssid->ssid_len &&
661 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
662 break;
663 }
664
665 if (s) {
666 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
667 "entry");
668 if (ssid->passphrase && !s->passphrase)
669 changed = 1;
670 else if (ssid->passphrase && s->passphrase &&
671 os_strcmp(ssid->passphrase, s->passphrase) != 0)
672 changed = 1;
673 } else {
674 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
675 "entry");
676 changed = 1;
677 s = wpa_config_add_network(wpa_s->conf);
678 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700679 return -1;
680
681 /*
682 * Instead of network_added we emit persistent_group_added
683 * notification. Also to keep the defense checks in
684 * persistent_group obj registration method, we set the
685 * relevant flags in s to designate it as a persistent group.
686 */
687 s->p2p_group = 1;
688 s->p2p_persistent_group = 1;
689 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 wpa_config_set_network_defaults(s);
691 }
692
693 s->p2p_group = 1;
694 s->p2p_persistent_group = 1;
695 s->disabled = 2;
696 s->bssid_set = 1;
697 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
698 s->mode = ssid->mode;
699 s->auth_alg = WPA_AUTH_ALG_OPEN;
700 s->key_mgmt = WPA_KEY_MGMT_PSK;
701 s->proto = WPA_PROTO_RSN;
702 s->pairwise_cipher = WPA_CIPHER_CCMP;
703 s->export_keys = 1;
704 if (ssid->passphrase) {
705 os_free(s->passphrase);
706 s->passphrase = os_strdup(ssid->passphrase);
707 }
708 if (ssid->psk_set) {
709 s->psk_set = 1;
710 os_memcpy(s->psk, ssid->psk, 32);
711 }
712 if (s->passphrase && !s->psk_set)
713 wpa_config_update_psk(s);
714 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
715 os_free(s->ssid);
716 s->ssid = os_malloc(ssid->ssid_len);
717 }
718 if (s->ssid) {
719 s->ssid_len = ssid->ssid_len;
720 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
721 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700722 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
723 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
724 wpa_s->global->add_psk = NULL;
725 changed = 1;
726 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 if (changed && wpa_s->conf->update_config &&
729 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
730 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
731 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700732
733 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734}
735
736
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800737static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
738 const u8 *addr)
739{
740 struct wpa_ssid *ssid, *s;
741 u8 *n;
742 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700743 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800744
745 ssid = wpa_s->current_ssid;
746 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
747 !ssid->p2p_persistent_group)
748 return;
749
750 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
751 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
752 continue;
753
754 if (s->ssid_len == ssid->ssid_len &&
755 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
756 break;
757 }
758
759 if (s == NULL)
760 return;
761
762 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
763 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700764 ETH_ALEN) != 0)
765 continue;
766
767 if (i == s->num_p2p_clients - 1)
768 return; /* already the most recent entry */
769
770 /* move the entry to mark it most recent */
771 os_memmove(s->p2p_client_list + i * ETH_ALEN,
772 s->p2p_client_list + (i + 1) * ETH_ALEN,
773 (s->num_p2p_clients - i - 1) * ETH_ALEN);
774 os_memcpy(s->p2p_client_list +
775 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
776 found = 1;
777 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800778 }
779
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700780 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
781 n = os_realloc_array(s->p2p_client_list,
782 s->num_p2p_clients + 1, ETH_ALEN);
783 if (n == NULL)
784 return;
785 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
786 s->p2p_client_list = n;
787 s->num_p2p_clients++;
788 } else if (!found) {
789 /* Not enough room for an additional entry - drop the oldest
790 * entry */
791 os_memmove(s->p2p_client_list,
792 s->p2p_client_list + ETH_ALEN,
793 (s->num_p2p_clients - 1) * ETH_ALEN);
794 os_memcpy(s->p2p_client_list +
795 (s->num_p2p_clients - 1) * ETH_ALEN,
796 addr, ETH_ALEN);
797 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800798
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800799 if (wpa_s->parent->conf->update_config &&
800 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
801 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800802}
803
804
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
806 int success)
807{
808 struct wpa_ssid *ssid;
809 const char *ssid_txt;
810 int client;
811 int persistent;
812 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700813 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814
815 /*
816 * This callback is likely called for the main interface. Update wpa_s
817 * to use the group interface if a new interface was created for the
818 * group.
819 */
820 if (wpa_s->global->p2p_group_formation)
821 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700822 if (wpa_s->p2p_go_group_formation_completed) {
823 wpa_s->global->p2p_group_formation = NULL;
824 wpa_s->p2p_in_provisioning = 0;
825 }
Dmitry Shmidt21de2142014-04-08 10:50:52 -0700826 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
828 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700829 wpa_msg_global(wpa_s->parent, MSG_INFO,
830 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700831 wpas_p2p_group_delete(wpa_s,
832 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 return;
834 }
835
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700836 wpa_msg_global(wpa_s->parent, MSG_INFO,
837 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838
839 ssid = wpa_s->current_ssid;
840 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
841 ssid->mode = WPAS_MODE_P2P_GO;
842 p2p_group_notif_formation_done(wpa_s->p2p_group);
843 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
844 }
845
846 persistent = 0;
847 if (ssid) {
848 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
849 client = ssid->mode == WPAS_MODE_INFRA;
850 if (ssid->mode == WPAS_MODE_P2P_GO) {
851 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700852 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
853 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 } else
855 persistent = wpas_p2p_persistent_group(wpa_s,
856 go_dev_addr,
857 ssid->ssid,
858 ssid->ssid_len);
859 } else {
860 ssid_txt = "";
861 client = wpa_s->p2p_group_interface ==
862 P2P_GROUP_INTERFACE_CLIENT;
863 os_memset(go_dev_addr, 0, ETH_ALEN);
864 }
865
866 wpa_s->show_group_started = 0;
867 if (client) {
868 /*
869 * Indicate event only after successfully completed 4-way
870 * handshake, i.e., when the interface is ready for data
871 * packets.
872 */
873 wpa_s->show_group_started = 1;
874 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
875 char psk[65];
876 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700877 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
878 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
879 MACSTR "%s",
880 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
881 MAC2STR(go_dev_addr),
882 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 wpas_p2p_cross_connect_setup(wpa_s);
884 wpas_p2p_set_group_idle_timeout(wpa_s);
885 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700886 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
887 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
888 "go_dev_addr=" MACSTR "%s",
889 wpa_s->ifname, ssid_txt,
890 ssid ? ssid->frequency : 0,
891 ssid && ssid->passphrase ? ssid->passphrase : "",
892 MAC2STR(go_dev_addr),
893 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 wpas_p2p_cross_connect_setup(wpa_s);
895 wpas_p2p_set_group_idle_timeout(wpa_s);
896 }
897
898 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700899 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
900 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700901 else {
902 os_free(wpa_s->global->add_psk);
903 wpa_s->global->add_psk = NULL;
904 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800905 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700906 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700907 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700908 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800909 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911}
912
913
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800914struct send_action_work {
915 unsigned int freq;
916 u8 dst[ETH_ALEN];
917 u8 src[ETH_ALEN];
918 u8 bssid[ETH_ALEN];
919 size_t len;
920 unsigned int wait_time;
921 u8 buf[0];
922};
923
924
925static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
926 void *timeout_ctx)
927{
928 struct wpa_supplicant *wpa_s = eloop_ctx;
929
930 if (!wpa_s->p2p_send_action_work)
931 return;
932
933 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
934 os_free(wpa_s->p2p_send_action_work->ctx);
935 radio_work_done(wpa_s->p2p_send_action_work);
936 wpa_s->p2p_send_action_work = NULL;
937}
938
939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800940static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
941 unsigned int freq,
942 const u8 *dst, const u8 *src,
943 const u8 *bssid,
944 const u8 *data, size_t data_len,
945 enum offchannel_send_action_result
946 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800948 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800950 if (wpa_s->p2p_send_action_work) {
951 struct send_action_work *awork;
952 awork = wpa_s->p2p_send_action_work->ctx;
953 if (awork->wait_time == 0) {
954 os_free(awork);
955 radio_work_done(wpa_s->p2p_send_action_work);
956 wpa_s->p2p_send_action_work = NULL;
957 } else {
958 /*
959 * In theory, this should not be needed, but number of
960 * places in the P2P code is still using non-zero wait
961 * time for the last Action frame in the sequence and
962 * some of these do not call send_action_done().
963 */
964 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
965 wpa_s, NULL);
966 eloop_register_timeout(
967 0, awork->wait_time * 1000,
968 wpas_p2p_send_action_work_timeout,
969 wpa_s, NULL);
970 }
971 }
972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
974 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800976 switch (result) {
977 case OFFCHANNEL_SEND_ACTION_SUCCESS:
978 res = P2P_SEND_ACTION_SUCCESS;
979 break;
980 case OFFCHANNEL_SEND_ACTION_NO_ACK:
981 res = P2P_SEND_ACTION_NO_ACK;
982 break;
983 case OFFCHANNEL_SEND_ACTION_FAILED:
984 res = P2P_SEND_ACTION_FAILED;
985 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 }
987
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800988 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800990 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
991 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800993 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
994 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
997 "during p2p_connect-auto");
998 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
999 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 }
1001}
1002
1003
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001004static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1005{
1006 struct wpa_supplicant *wpa_s = work->wpa_s;
1007 struct send_action_work *awork = work->ctx;
1008
1009 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001010 if (work->started) {
1011 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1012 wpa_s, NULL);
1013 wpa_s->p2p_send_action_work = NULL;
1014 offchannel_send_action_done(wpa_s);
1015 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001016 os_free(awork);
1017 return;
1018 }
1019
1020 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1021 awork->bssid, awork->buf, awork->len,
1022 awork->wait_time,
1023 wpas_p2p_send_action_tx_status, 1) < 0) {
1024 os_free(awork);
1025 radio_work_done(work);
1026 return;
1027 }
1028 wpa_s->p2p_send_action_work = work;
1029}
1030
1031
1032static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1033 unsigned int freq, const u8 *dst,
1034 const u8 *src, const u8 *bssid, const u8 *buf,
1035 size_t len, unsigned int wait_time)
1036{
1037 struct send_action_work *awork;
1038
1039 if (wpa_s->p2p_send_action_work) {
1040 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1041 return -1;
1042 }
1043
1044 awork = os_zalloc(sizeof(*awork) + len);
1045 if (awork == NULL)
1046 return -1;
1047
1048 awork->freq = freq;
1049 os_memcpy(awork->dst, dst, ETH_ALEN);
1050 os_memcpy(awork->src, src, ETH_ALEN);
1051 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1052 awork->len = len;
1053 awork->wait_time = wait_time;
1054 os_memcpy(awork->buf, buf, len);
1055
1056 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1057 wpas_send_action_cb, awork) < 0) {
1058 os_free(awork);
1059 return -1;
1060 }
1061
1062 return 0;
1063}
1064
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1067 const u8 *src, const u8 *bssid, const u8 *buf,
1068 size_t len, unsigned int wait_time)
1069{
1070 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001071 int listen_freq = -1, send_freq = -1;
1072
1073 if (wpa_s->p2p_listen_work)
1074 listen_freq = wpa_s->p2p_listen_work->freq;
1075 if (wpa_s->p2p_send_action_work)
1076 send_freq = wpa_s->p2p_send_action_work->freq;
1077 if (listen_freq != (int) freq && send_freq != (int) freq) {
1078 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1079 listen_freq, send_freq);
1080 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1081 len, wait_time);
1082 }
1083
1084 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001085 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1086 wait_time,
1087 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088}
1089
1090
1091static void wpas_send_action_done(void *ctx)
1092{
1093 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094
1095 if (wpa_s->p2p_send_action_work) {
1096 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1097 wpa_s, NULL);
1098 os_free(wpa_s->p2p_send_action_work->ctx);
1099 radio_work_done(wpa_s->p2p_send_action_work);
1100 wpa_s->p2p_send_action_work = NULL;
1101 }
1102
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001103 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104}
1105
1106
1107static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1108 struct p2p_go_neg_results *params)
1109{
1110 if (wpa_s->go_params == NULL) {
1111 wpa_s->go_params = os_malloc(sizeof(*params));
1112 if (wpa_s->go_params == NULL)
1113 return -1;
1114 }
1115 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1116 return 0;
1117}
1118
1119
1120static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1121 struct p2p_go_neg_results *res)
1122{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001123 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1124 " dev_addr " MACSTR " wps_method %d",
1125 MAC2STR(res->peer_interface_addr),
1126 MAC2STR(res->peer_device_addr), res->wps_method);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1128 res->ssid, res->ssid_len);
1129 wpa_supplicant_ap_deinit(wpa_s);
1130 wpas_copy_go_neg_results(wpa_s, res);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001131 if (res->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001133#ifdef CONFIG_WPS_NFC
1134 } else if (res->wps_method == WPS_NFC) {
1135 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1136 res->peer_interface_addr,
1137 wpa_s->parent->p2p_oob_dev_pw,
1138 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1139 wpa_s->parent->p2p_oob_dev_pw_id ==
1140 DEV_PW_NFC_CONNECTION_HANDOVER ?
1141 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1142 NULL,
1143 NULL, 0, 0);
1144#endif /* CONFIG_WPS_NFC */
1145 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 u16 dev_pw_id = DEV_PW_DEFAULT;
1147 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1148 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1149 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1150 wpa_s->p2p_pin, 1, dev_pw_id);
1151 }
1152}
1153
1154
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001155static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid)
1157{
1158 struct wpa_ssid *persistent;
1159 struct psk_list_entry *psk;
1160 struct hostapd_data *hapd;
1161
1162 if (!wpa_s->ap_iface)
1163 return;
1164
1165 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1166 ssid->ssid_len);
1167 if (persistent == NULL)
1168 return;
1169
1170 hapd = wpa_s->ap_iface->bss[0];
1171
1172 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1173 list) {
1174 struct hostapd_wpa_psk *hpsk;
1175
1176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1177 MACSTR " psk=%d",
1178 MAC2STR(psk->addr), psk->p2p);
1179 hpsk = os_zalloc(sizeof(*hpsk));
1180 if (hpsk == NULL)
1181 break;
1182 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1183 if (psk->p2p)
1184 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1185 else
1186 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1187 hpsk->next = hapd->conf->ssid.wpa_psk;
1188 hapd->conf->ssid.wpa_psk = hpsk;
1189 }
1190}
1191
1192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193static void p2p_go_configured(void *ctx, void *data)
1194{
1195 struct wpa_supplicant *wpa_s = ctx;
1196 struct p2p_go_neg_results *params = data;
1197 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001198 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199
1200 ssid = wpa_s->current_ssid;
1201 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1202 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1203 if (wpa_s->global->p2p_group_formation == wpa_s)
1204 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001205 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001206 wpa_msg_global(wpa_s->parent, MSG_INFO,
1207 P2P_EVENT_GROUP_STARTED
1208 "%s GO ssid=\"%s\" freq=%d "
1209 "passphrase=\"%s\" go_dev_addr=" MACSTR
1210 "%s", wpa_s->ifname,
1211 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1212 ssid->frequency, params->passphrase,
1213 MAC2STR(wpa_s->global->p2p_dev_addr),
1214 params->persistent_group ?
1215 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001216 } else {
1217 char psk[65];
1218 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1219 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001220 wpa_msg_global(wpa_s->parent, MSG_INFO,
1221 P2P_EVENT_GROUP_STARTED
1222 "%s GO ssid=\"%s\" freq=%d psk=%s "
1223 "go_dev_addr=" MACSTR "%s",
1224 wpa_s->ifname,
1225 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1226 ssid->frequency, psk,
1227 MAC2STR(wpa_s->global->p2p_dev_addr),
1228 params->persistent_group ?
1229 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001230 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001231
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001232 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001233 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001234 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001235 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001236 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001237 wpas_p2p_add_psk_list(wpa_s, ssid);
1238 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001239 if (network_id < 0)
1240 network_id = ssid->id;
1241 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242 wpas_p2p_cross_connect_setup(wpa_s);
1243 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001244
1245 if (wpa_s->p2p_first_connection_timeout) {
1246 wpa_dbg(wpa_s, MSG_DEBUG,
1247 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1248 wpa_s->p2p_first_connection_timeout);
1249 wpa_s->p2p_go_group_formation_completed = 0;
1250 wpa_s->global->p2p_group_formation = wpa_s;
1251 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1252 wpa_s->parent, NULL);
1253 eloop_register_timeout(
1254 wpa_s->p2p_first_connection_timeout, 0,
1255 wpas_p2p_group_formation_timeout,
1256 wpa_s->parent, NULL);
1257 }
1258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 return;
1260 }
1261
1262 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1263 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1264 params->peer_interface_addr)) {
1265 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1266 "filtering");
1267 return;
1268 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001269 if (params->wps_method == WPS_PBC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001271 params->peer_device_addr);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001272#ifdef CONFIG_WPS_NFC
1273 } else if (params->wps_method == WPS_NFC) {
1274 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1275 DEV_PW_NFC_CONNECTION_HANDOVER &&
1276 !wpa_s->parent->p2p_oob_dev_pw) {
1277 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1278 return;
1279 }
1280 wpas_ap_wps_add_nfc_pw(
1281 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1282 wpa_s->parent->p2p_oob_dev_pw,
1283 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1284 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1285#endif /* CONFIG_WPS_NFC */
1286 } else if (wpa_s->p2p_pin[0])
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001288 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 os_free(wpa_s->go_params);
1290 wpa_s->go_params = NULL;
1291}
1292
1293
1294static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1295 struct p2p_go_neg_results *params,
1296 int group_formation)
1297{
1298 struct wpa_ssid *ssid;
1299
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1301 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1303 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001305 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001306
1307 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001308 if (ssid == NULL) {
1309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001311 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001313 wpa_s->show_group_started = 0;
1314
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 wpa_config_set_network_defaults(ssid);
1316 ssid->temporary = 1;
1317 ssid->p2p_group = 1;
1318 ssid->p2p_persistent_group = params->persistent_group;
1319 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1320 WPAS_MODE_P2P_GO;
1321 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001322 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001323 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 ssid->ssid = os_zalloc(params->ssid_len + 1);
1325 if (ssid->ssid) {
1326 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1327 ssid->ssid_len = params->ssid_len;
1328 }
1329 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1330 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1331 ssid->proto = WPA_PROTO_RSN;
1332 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001333 if (os_strlen(params->passphrase) > 0) {
1334 ssid->passphrase = os_strdup(params->passphrase);
1335 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001336 wpa_msg_global(wpa_s, MSG_ERROR,
1337 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001338 wpa_config_remove_network(wpa_s->conf, ssid->id);
1339 return;
1340 }
1341 } else
1342 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001343 ssid->psk_set = params->psk_set;
1344 if (ssid->psk_set)
1345 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001346 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001347 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001348 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349
1350 wpa_s->ap_configured_cb = p2p_go_configured;
1351 wpa_s->ap_configured_cb_ctx = wpa_s;
1352 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001353 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 wpa_s->reassociate = 1;
1355 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001356 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1357 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 wpa_supplicant_req_scan(wpa_s, 0, 0);
1359}
1360
1361
1362static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1363 const struct wpa_supplicant *src)
1364{
1365 struct wpa_config *d;
1366 const struct wpa_config *s;
1367
1368 d = dst->conf;
1369 s = src->conf;
1370
1371#define C(n) if (s->n) d->n = os_strdup(s->n)
1372 C(device_name);
1373 C(manufacturer);
1374 C(model_name);
1375 C(model_number);
1376 C(serial_number);
1377 C(config_methods);
1378#undef C
1379
1380 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1381 os_memcpy(d->sec_device_type, s->sec_device_type,
1382 sizeof(d->sec_device_type));
1383 d->num_sec_device_types = s->num_sec_device_types;
1384
1385 d->p2p_group_idle = s->p2p_group_idle;
1386 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001387 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001388 d->max_num_sta = s->max_num_sta;
1389 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001390 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001391 d->beacon_int = s->beacon_int;
Dmitry Shmidt18463232014-01-24 12:29:41 -08001392 d->dtim_period = s->dtim_period;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001393 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001394 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001395
1396 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1397 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1398 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400}
1401
1402
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001403static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1404 char *ifname, size_t len)
1405{
1406 char *ifname_ptr = wpa_s->ifname;
1407
1408 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1409 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1410 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1411 }
1412
1413 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1414 if (os_strlen(ifname) >= IFNAMSIZ &&
1415 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1416 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001417 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001418 }
1419}
1420
1421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1423 enum wpa_driver_if_type type)
1424{
1425 char ifname[120], force_ifname[120];
1426
1427 if (wpa_s->pending_interface_name[0]) {
1428 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1429 "- skip creation of a new one");
1430 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1431 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1432 "unknown?! ifname='%s'",
1433 wpa_s->pending_interface_name);
1434 return -1;
1435 }
1436 return 0;
1437 }
1438
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001439 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 force_ifname[0] = '\0';
1441
1442 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1443 ifname);
1444 wpa_s->p2p_group_idx++;
1445
1446 wpa_s->pending_interface_type = type;
1447 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1448 wpa_s->pending_interface_addr, NULL) < 0) {
1449 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1450 "interface");
1451 return -1;
1452 }
1453
1454 if (force_ifname[0]) {
1455 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1456 force_ifname);
1457 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1458 sizeof(wpa_s->pending_interface_name));
1459 } else
1460 os_strlcpy(wpa_s->pending_interface_name, ifname,
1461 sizeof(wpa_s->pending_interface_name));
1462 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1463 MACSTR, wpa_s->pending_interface_name,
1464 MAC2STR(wpa_s->pending_interface_addr));
1465
1466 return 0;
1467}
1468
1469
1470static void wpas_p2p_remove_pending_group_interface(
1471 struct wpa_supplicant *wpa_s)
1472{
1473 if (!wpa_s->pending_interface_name[0] ||
1474 is_zero_ether_addr(wpa_s->pending_interface_addr))
1475 return; /* No pending virtual interface */
1476
1477 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1478 wpa_s->pending_interface_name);
1479 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1480 wpa_s->pending_interface_name);
1481 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1482 wpa_s->pending_interface_name[0] = '\0';
1483}
1484
1485
1486static struct wpa_supplicant *
1487wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1488{
1489 struct wpa_interface iface;
1490 struct wpa_supplicant *group_wpa_s;
1491
1492 if (!wpa_s->pending_interface_name[0]) {
1493 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1494 if (!wpas_p2p_create_iface(wpa_s))
1495 return NULL;
1496 /*
1497 * Something has forced us to remove the pending interface; try
1498 * to create a new one and hope for the best that we will get
1499 * the same local address.
1500 */
1501 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1502 WPA_IF_P2P_CLIENT) < 0)
1503 return NULL;
1504 }
1505
1506 os_memset(&iface, 0, sizeof(iface));
1507 iface.ifname = wpa_s->pending_interface_name;
1508 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001509 if (wpa_s->conf->ctrl_interface == NULL &&
1510 wpa_s->parent != wpa_s &&
1511 wpa_s->p2p_mgmt &&
1512 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1513 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1514 else
1515 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001516 iface.driver_param = wpa_s->conf->driver_param;
1517 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1518 if (group_wpa_s == NULL) {
1519 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1520 "wpa_supplicant interface");
1521 return NULL;
1522 }
1523 wpa_s->pending_interface_name[0] = '\0';
1524 group_wpa_s->parent = wpa_s;
1525 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1526 P2P_GROUP_INTERFACE_CLIENT;
1527 wpa_s->global->p2p_group_formation = group_wpa_s;
1528
1529 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1530
1531 return group_wpa_s;
1532}
1533
1534
1535static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1536 void *timeout_ctx)
1537{
1538 struct wpa_supplicant *wpa_s = eloop_ctx;
1539 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001540 wpas_p2p_group_formation_failed(wpa_s);
1541}
1542
1543
1544void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1545{
1546 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1547 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001548 if (wpa_s->global->p2p)
1549 p2p_group_formation_failed(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001550 wpas_group_formation_completed(wpa_s, 0);
1551}
1552
1553
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001554static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1555{
1556 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1557 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1558 wpa_s->parent, NULL);
1559 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1560 wpa_s->parent, NULL);
1561 wpa_s->global->p2p_fail_on_wps_complete = 0;
1562}
1563
1564
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001565void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1566{
1567 if (wpa_s->global->p2p_group_formation != wpa_s)
1568 return;
1569 /* Speed up group formation timeout since this cannot succeed */
1570 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1571 wpa_s->parent, NULL);
1572 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1573 wpa_s->parent, NULL);
1574}
1575
1576
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001577static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578{
1579 struct wpa_supplicant *wpa_s = ctx;
1580
1581 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1582 wpa_drv_cancel_remain_on_channel(wpa_s);
1583 wpa_s->off_channel_freq = 0;
1584 wpa_s->roc_waiting_drv_freq = 0;
1585 }
1586
1587 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001588 wpa_msg_global(wpa_s, MSG_INFO,
1589 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1590 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001591 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 wpas_p2p_remove_pending_group_interface(wpa_s);
1593 return;
1594 }
1595
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001596 if (wpa_s->p2p_go_ht40)
1597 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001598 if (wpa_s->p2p_go_vht)
1599 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001600
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001601 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1602 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1603 " wps_method=%s",
1604 res->role_go ? "GO" : "client", res->freq, res->ht40,
1605 MAC2STR(res->peer_device_addr),
1606 MAC2STR(res->peer_interface_addr),
1607 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001608 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609
Dmitry Shmidt04949592012-07-19 12:16:46 -07001610 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1611 struct wpa_ssid *ssid;
1612 ssid = wpa_config_get_network(wpa_s->conf,
1613 wpa_s->p2p_persistent_id);
1614 if (ssid && ssid->disabled == 2 &&
1615 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1616 size_t len = os_strlen(ssid->passphrase);
1617 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1618 "on requested persistent group");
1619 os_memcpy(res->passphrase, ssid->passphrase, len);
1620 res->passphrase[len] = '\0';
1621 }
1622 }
1623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (wpa_s->create_p2p_iface) {
1625 struct wpa_supplicant *group_wpa_s =
1626 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1627 if (group_wpa_s == NULL) {
1628 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001629 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1630 wpa_s, NULL);
1631 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 return;
1633 }
1634 if (group_wpa_s != wpa_s) {
1635 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1636 sizeof(group_wpa_s->p2p_pin));
1637 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1638 }
1639 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1640 wpa_s->pending_interface_name[0] = '\0';
1641 group_wpa_s->p2p_in_provisioning = 1;
1642
1643 if (res->role_go)
1644 wpas_start_wps_go(group_wpa_s, res, 1);
1645 else
1646 wpas_start_wps_enrollee(group_wpa_s, res);
1647 } else {
1648 wpa_s->p2p_in_provisioning = 1;
1649 wpa_s->global->p2p_group_formation = wpa_s;
1650
1651 if (res->role_go)
1652 wpas_start_wps_go(wpa_s, res, 1);
1653 else
1654 wpas_start_wps_enrollee(ctx, res);
1655 }
1656
1657 wpa_s->p2p_long_listen = 0;
1658 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1659
1660 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1661 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1662 (res->peer_config_timeout % 100) * 10000,
1663 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1664}
1665
1666
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001667static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668{
1669 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001670 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1671 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672
1673 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1674}
1675
1676
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001677static void wpas_dev_found(void *ctx, const u8 *addr,
1678 const struct p2p_peer_info *info,
1679 int new_device)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001681#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 struct wpa_supplicant *wpa_s = ctx;
1683 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001684 char *wfd_dev_info_hex = NULL;
1685
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001686#ifdef CONFIG_WIFI_DISPLAY
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001687 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1688 WFD_SUBELEM_DEVICE_INFO);
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001689#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001690
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001691 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1692 " p2p_dev_addr=" MACSTR
1693 " pri_dev_type=%s name='%s' config_methods=0x%x "
1694 "dev_capab=0x%x group_capab=0x%x%s%s",
1695 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1696 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1697 sizeof(devtype)),
1698 info->device_name, info->config_methods,
1699 info->dev_capab, info->group_capab,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001700 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1701 wfd_dev_info_hex ? wfd_dev_info_hex : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001702
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001703 os_free(wfd_dev_info_hex);
Dmitry Shmidt97672262014-02-03 13:02:54 -08001704#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1707}
1708
1709
1710static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1711{
1712 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001714 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1715 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001717 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1718}
1719
1720
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001721static void wpas_find_stopped(void *ctx)
1722{
1723 struct wpa_supplicant *wpa_s = ctx;
1724 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1725}
1726
1727
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001728struct wpas_p2p_listen_work {
1729 unsigned int freq;
1730 unsigned int duration;
1731 struct wpabuf *probe_resp_ie;
1732};
1733
1734
1735static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1736{
1737 if (lwork == NULL)
1738 return;
1739 wpabuf_free(lwork->probe_resp_ie);
1740 os_free(lwork);
1741}
1742
1743
1744static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1745{
1746 struct wpas_p2p_listen_work *lwork;
1747
1748 if (!wpa_s->p2p_listen_work)
1749 return;
1750
1751 lwork = wpa_s->p2p_listen_work->ctx;
1752 wpas_p2p_listen_work_free(lwork);
1753 radio_work_done(wpa_s->p2p_listen_work);
1754 wpa_s->p2p_listen_work = NULL;
1755}
1756
1757
1758static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1759{
1760 struct wpa_supplicant *wpa_s = work->wpa_s;
1761 struct wpas_p2p_listen_work *lwork = work->ctx;
1762
1763 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -08001764 if (work->started) {
1765 wpa_s->p2p_listen_work = NULL;
1766 wpas_stop_listen(wpa_s);
1767 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001768 wpas_p2p_listen_work_free(lwork);
1769 return;
1770 }
1771
1772 wpa_s->p2p_listen_work = work;
1773
1774 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1775
1776 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1777 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1778 "report received Probe Request frames");
1779 wpas_p2p_listen_work_done(wpa_s);
1780 return;
1781 }
1782
1783 wpa_s->pending_listen_freq = lwork->freq;
1784 wpa_s->pending_listen_duration = lwork->duration;
1785
1786 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1787 {
1788 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1789 "to remain on channel (%u MHz) for Listen "
1790 "state", lwork->freq);
1791 wpas_p2p_listen_work_done(wpa_s);
1792 wpa_s->pending_listen_freq = 0;
1793 return;
1794 }
1795 wpa_s->off_channel_freq = 0;
1796 wpa_s->roc_waiting_drv_freq = lwork->freq;
1797}
1798
1799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800static int wpas_start_listen(void *ctx, unsigned int freq,
1801 unsigned int duration,
1802 const struct wpabuf *probe_resp_ie)
1803{
1804 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001805 struct wpas_p2p_listen_work *lwork;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001807 if (wpa_s->p2p_listen_work) {
1808 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 return -1;
1810 }
1811
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001812 lwork = os_zalloc(sizeof(*lwork));
1813 if (lwork == NULL)
1814 return -1;
1815 lwork->freq = freq;
1816 lwork->duration = duration;
1817 if (probe_resp_ie) {
1818 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1819 if (lwork->probe_resp_ie == NULL) {
1820 wpas_p2p_listen_work_free(lwork);
1821 return -1;
1822 }
1823 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001825 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1826 lwork) < 0) {
1827 wpas_p2p_listen_work_free(lwork);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 return -1;
1829 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830
1831 return 0;
1832}
1833
1834
1835static void wpas_stop_listen(void *ctx)
1836{
1837 struct wpa_supplicant *wpa_s = ctx;
1838 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1839 wpa_drv_cancel_remain_on_channel(wpa_s);
1840 wpa_s->off_channel_freq = 0;
1841 wpa_s->roc_waiting_drv_freq = 0;
1842 }
1843 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1844 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001845 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846}
1847
1848
1849static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1850{
1851 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001852 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001853}
1854
1855
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001856/*
1857 * DNS Header section is used only to calculate compression pointers, so the
1858 * contents of this data does not matter, but the length needs to be reserved
1859 * in the virtual packet.
1860 */
1861#define DNS_HEADER_LEN 12
1862
1863/*
1864 * 27-octet in-memory packet from P2P specification containing two implied
1865 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1866 */
1867#define P2P_SD_IN_MEMORY_LEN 27
1868
1869static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1870 u8 **spos, const u8 *end)
1871{
1872 while (*spos < end) {
1873 u8 val = ((*spos)[0] & 0xc0) >> 6;
1874 int len;
1875
1876 if (val == 1 || val == 2) {
1877 /* These are reserved values in RFC 1035 */
1878 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1879 "sequence starting with 0x%x", val);
1880 return -1;
1881 }
1882
1883 if (val == 3) {
1884 u16 offset;
1885 u8 *spos_tmp;
1886
1887 /* Offset */
1888 if (*spos + 2 > end) {
1889 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1890 "DNS offset field");
1891 return -1;
1892 }
1893
1894 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1895 if (offset >= *spos - start) {
1896 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1897 "pointer offset %u", offset);
1898 return -1;
1899 }
1900
1901 (*spos) += 2;
1902 spos_tmp = start + offset;
1903 return p2p_sd_dns_uncompress_label(upos, uend, start,
1904 &spos_tmp,
1905 *spos - 2);
1906 }
1907
1908 /* Label */
1909 len = (*spos)[0] & 0x3f;
1910 if (len == 0)
1911 return 0;
1912
1913 (*spos)++;
1914 if (*spos + len > end) {
1915 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1916 "sequence - no room for label with length "
1917 "%u", len);
1918 return -1;
1919 }
1920
1921 if (*upos + len + 2 > uend)
1922 return -2;
1923
1924 os_memcpy(*upos, *spos, len);
1925 *spos += len;
1926 *upos += len;
1927 (*upos)[0] = '.';
1928 (*upos)++;
1929 (*upos)[0] = '\0';
1930 }
1931
1932 return 0;
1933}
1934
1935
1936/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1937 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1938 * not large enough */
1939static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1940 size_t msg_len, size_t offset)
1941{
1942 /* 27-octet in-memory packet from P2P specification */
1943 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1944 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1945 u8 *tmp, *end, *spos;
1946 char *upos, *uend;
1947 int ret = 0;
1948
1949 if (buf_len < 2)
1950 return -1;
1951 if (offset > msg_len)
1952 return -1;
1953
1954 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1955 if (tmp == NULL)
1956 return -1;
1957 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1958 end = spos + msg_len;
1959 spos += offset;
1960
1961 os_memset(tmp, 0, DNS_HEADER_LEN);
1962 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1963 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1964
1965 upos = buf;
1966 uend = buf + buf_len;
1967
1968 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1969 if (ret) {
1970 os_free(tmp);
1971 return ret;
1972 }
1973
1974 if (upos == buf) {
1975 upos[0] = '.';
1976 upos[1] = '\0';
1977 } else if (upos[-1] == '.')
1978 upos[-1] = '\0';
1979
1980 os_free(tmp);
1981 return 0;
1982}
1983
1984
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985static struct p2p_srv_bonjour *
1986wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1987 const struct wpabuf *query)
1988{
1989 struct p2p_srv_bonjour *bsrv;
1990 size_t len;
1991
1992 len = wpabuf_len(query);
1993 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1994 struct p2p_srv_bonjour, list) {
1995 if (len == wpabuf_len(bsrv->query) &&
1996 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1997 len) == 0)
1998 return bsrv;
1999 }
2000 return NULL;
2001}
2002
2003
2004static struct p2p_srv_upnp *
2005wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2006 const char *service)
2007{
2008 struct p2p_srv_upnp *usrv;
2009
2010 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2011 struct p2p_srv_upnp, list) {
2012 if (version == usrv->version &&
2013 os_strcmp(service, usrv->service) == 0)
2014 return usrv;
2015 }
2016 return NULL;
2017}
2018
2019
2020static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2021 u8 srv_trans_id)
2022{
2023 u8 *len_pos;
2024
2025 if (wpabuf_tailroom(resp) < 5)
2026 return;
2027
2028 /* Length (to be filled) */
2029 len_pos = wpabuf_put(resp, 2);
2030 wpabuf_put_u8(resp, srv_proto);
2031 wpabuf_put_u8(resp, srv_trans_id);
2032 /* Status Code */
2033 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2034 /* Response Data: empty */
2035 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2036}
2037
2038
2039static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2040 struct wpabuf *resp, u8 srv_trans_id)
2041{
2042 struct p2p_srv_bonjour *bsrv;
2043 u8 *len_pos;
2044
2045 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2046
2047 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2048 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2049 return;
2050 }
2051
2052 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2053 struct p2p_srv_bonjour, list) {
2054 if (wpabuf_tailroom(resp) <
2055 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2056 return;
2057 /* Length (to be filled) */
2058 len_pos = wpabuf_put(resp, 2);
2059 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2060 wpabuf_put_u8(resp, srv_trans_id);
2061 /* Status Code */
2062 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2063 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2064 wpabuf_head(bsrv->resp),
2065 wpabuf_len(bsrv->resp));
2066 /* Response Data */
2067 wpabuf_put_buf(resp, bsrv->query); /* Key */
2068 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2069 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2070 2);
2071 }
2072}
2073
2074
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002075static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2076 size_t query_len)
2077{
2078 char str_rx[256], str_srv[256];
2079
2080 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2081 return 0; /* Too short to include DNS Type and Version */
2082 if (os_memcmp(query + query_len - 3,
2083 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2084 3) != 0)
2085 return 0; /* Mismatch in DNS Type or Version */
2086 if (query_len == wpabuf_len(bsrv->query) &&
2087 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2088 return 1; /* Binary match */
2089
2090 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2091 0))
2092 return 0; /* Failed to uncompress query */
2093 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2094 wpabuf_head(bsrv->query),
2095 wpabuf_len(bsrv->query) - 3, 0))
2096 return 0; /* Failed to uncompress service */
2097
2098 return os_strcmp(str_rx, str_srv) == 0;
2099}
2100
2101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2103 struct wpabuf *resp, u8 srv_trans_id,
2104 const u8 *query, size_t query_len)
2105{
2106 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002108 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109
2110 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2111 query, query_len);
2112 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2113 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2114 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2115 srv_trans_id);
2116 return;
2117 }
2118
2119 if (query_len == 0) {
2120 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2121 return;
2122 }
2123
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002124 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2125 struct p2p_srv_bonjour, list) {
2126 if (!match_bonjour_query(bsrv, query, query_len))
2127 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002128
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002129 if (wpabuf_tailroom(resp) <
2130 5 + query_len + wpabuf_len(bsrv->resp))
2131 return;
2132
2133 matches++;
2134
2135 /* Length (to be filled) */
2136 len_pos = wpabuf_put(resp, 2);
2137 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2138 wpabuf_put_u8(resp, srv_trans_id);
2139
2140 /* Status Code */
2141 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2142 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2143 wpabuf_head(bsrv->resp),
2144 wpabuf_len(bsrv->resp));
2145
2146 /* Response Data */
2147 wpabuf_put_data(resp, query, query_len); /* Key */
2148 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2149
2150 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2151 }
2152
2153 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2155 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002156 if (wpabuf_tailroom(resp) < 5)
2157 return;
2158
2159 /* Length (to be filled) */
2160 len_pos = wpabuf_put(resp, 2);
2161 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2162 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163
2164 /* Status Code */
2165 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2166 /* Response Data: empty */
2167 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2168 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170}
2171
2172
2173static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2174 struct wpabuf *resp, u8 srv_trans_id)
2175{
2176 struct p2p_srv_upnp *usrv;
2177 u8 *len_pos;
2178
2179 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2180
2181 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2182 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2183 return;
2184 }
2185
2186 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2187 struct p2p_srv_upnp, list) {
2188 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2189 return;
2190
2191 /* Length (to be filled) */
2192 len_pos = wpabuf_put(resp, 2);
2193 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2194 wpabuf_put_u8(resp, srv_trans_id);
2195
2196 /* Status Code */
2197 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2198 /* Response Data */
2199 wpabuf_put_u8(resp, usrv->version);
2200 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2201 usrv->service);
2202 wpabuf_put_str(resp, usrv->service);
2203 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2204 2);
2205 }
2206}
2207
2208
2209static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2210 struct wpabuf *resp, u8 srv_trans_id,
2211 const u8 *query, size_t query_len)
2212{
2213 struct p2p_srv_upnp *usrv;
2214 u8 *len_pos;
2215 u8 version;
2216 char *str;
2217 int count = 0;
2218
2219 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2220 query, query_len);
2221
2222 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2223 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2224 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2225 srv_trans_id);
2226 return;
2227 }
2228
2229 if (query_len == 0) {
2230 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2231 return;
2232 }
2233
2234 if (wpabuf_tailroom(resp) < 5)
2235 return;
2236
2237 /* Length (to be filled) */
2238 len_pos = wpabuf_put(resp, 2);
2239 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2240 wpabuf_put_u8(resp, srv_trans_id);
2241
2242 version = query[0];
2243 str = os_malloc(query_len);
2244 if (str == NULL)
2245 return;
2246 os_memcpy(str, query + 1, query_len - 1);
2247 str[query_len - 1] = '\0';
2248
2249 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2250 struct p2p_srv_upnp, list) {
2251 if (version != usrv->version)
2252 continue;
2253
2254 if (os_strcmp(str, "ssdp:all") != 0 &&
2255 os_strstr(usrv->service, str) == NULL)
2256 continue;
2257
2258 if (wpabuf_tailroom(resp) < 2)
2259 break;
2260 if (count == 0) {
2261 /* Status Code */
2262 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2263 /* Response Data */
2264 wpabuf_put_u8(resp, version);
2265 } else
2266 wpabuf_put_u8(resp, ',');
2267
2268 count++;
2269
2270 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2271 usrv->service);
2272 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2273 break;
2274 wpabuf_put_str(resp, usrv->service);
2275 }
2276 os_free(str);
2277
2278 if (count == 0) {
2279 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2280 "available");
2281 /* Status Code */
2282 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2283 /* Response Data: empty */
2284 }
2285
2286 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2287}
2288
2289
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002290#ifdef CONFIG_WIFI_DISPLAY
2291static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2292 struct wpabuf *resp, u8 srv_trans_id,
2293 const u8 *query, size_t query_len)
2294{
2295 const u8 *pos;
2296 u8 role;
2297 u8 *len_pos;
2298
2299 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2300
2301 if (!wpa_s->global->wifi_display) {
2302 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2303 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2304 srv_trans_id);
2305 return;
2306 }
2307
2308 if (query_len < 1) {
2309 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2310 "Role");
2311 return;
2312 }
2313
2314 if (wpabuf_tailroom(resp) < 5)
2315 return;
2316
2317 pos = query;
2318 role = *pos++;
2319 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2320
2321 /* TODO: role specific handling */
2322
2323 /* Length (to be filled) */
2324 len_pos = wpabuf_put(resp, 2);
2325 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2326 wpabuf_put_u8(resp, srv_trans_id);
2327 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2328
2329 while (pos < query + query_len) {
2330 if (*pos < MAX_WFD_SUBELEMS &&
2331 wpa_s->global->wfd_subelem[*pos] &&
2332 wpabuf_tailroom(resp) >=
2333 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2334 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2335 "subelement %u", *pos);
2336 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2337 }
2338 pos++;
2339 }
2340
2341 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2342}
2343#endif /* CONFIG_WIFI_DISPLAY */
2344
2345
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002346static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2347 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348{
2349 struct wpa_supplicant *wpa_s = ctx;
2350 const u8 *pos = tlvs;
2351 const u8 *end = tlvs + tlvs_len;
2352 const u8 *tlv_end;
2353 u16 slen;
2354 struct wpabuf *resp;
2355 u8 srv_proto, srv_trans_id;
2356 size_t buf_len;
2357 char *buf;
2358
2359 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2360 tlvs, tlvs_len);
2361 buf_len = 2 * tlvs_len + 1;
2362 buf = os_malloc(buf_len);
2363 if (buf) {
2364 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2365 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2366 MACSTR " %u %u %s",
2367 freq, MAC2STR(sa), dialog_token, update_indic,
2368 buf);
2369 os_free(buf);
2370 }
2371
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002372 if (wpa_s->p2p_sd_over_ctrl_iface) {
2373 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2374 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002376 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377
2378 resp = wpabuf_alloc(10000);
2379 if (resp == NULL)
2380 return;
2381
2382 while (pos + 1 < end) {
2383 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2384 slen = WPA_GET_LE16(pos);
2385 pos += 2;
2386 if (pos + slen > end || slen < 2) {
2387 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2388 "length");
2389 wpabuf_free(resp);
2390 return;
2391 }
2392 tlv_end = pos + slen;
2393
2394 srv_proto = *pos++;
2395 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2396 srv_proto);
2397 srv_trans_id = *pos++;
2398 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2399 srv_trans_id);
2400
2401 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2402 pos, tlv_end - pos);
2403
2404
2405 if (wpa_s->force_long_sd) {
2406 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2407 "response");
2408 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2409 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2410 goto done;
2411 }
2412
2413 switch (srv_proto) {
2414 case P2P_SERV_ALL_SERVICES:
2415 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2416 "for all services");
2417 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2418 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2419 wpa_printf(MSG_DEBUG, "P2P: No service "
2420 "discovery protocols available");
2421 wpas_sd_add_proto_not_avail(
2422 resp, P2P_SERV_ALL_SERVICES,
2423 srv_trans_id);
2424 break;
2425 }
2426 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2427 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2428 break;
2429 case P2P_SERV_BONJOUR:
2430 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2431 pos, tlv_end - pos);
2432 break;
2433 case P2P_SERV_UPNP:
2434 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2435 pos, tlv_end - pos);
2436 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002437#ifdef CONFIG_WIFI_DISPLAY
2438 case P2P_SERV_WIFI_DISPLAY:
2439 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2440 pos, tlv_end - pos);
2441 break;
2442#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443 default:
2444 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2445 "protocol %u", srv_proto);
2446 wpas_sd_add_proto_not_avail(resp, srv_proto,
2447 srv_trans_id);
2448 break;
2449 }
2450
2451 pos = tlv_end;
2452 }
2453
2454done:
2455 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2456 update_indic, tlvs, tlvs_len);
2457
2458 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2459
2460 wpabuf_free(resp);
2461}
2462
2463
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002464static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2465 const u8 *tlvs, size_t tlvs_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466{
2467 struct wpa_supplicant *wpa_s = ctx;
2468 const u8 *pos = tlvs;
2469 const u8 *end = tlvs + tlvs_len;
2470 const u8 *tlv_end;
2471 u16 slen;
2472 size_t buf_len;
2473 char *buf;
2474
2475 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2476 tlvs, tlvs_len);
2477 if (tlvs_len > 1500) {
2478 /* TODO: better way for handling this */
2479 wpa_msg_ctrl(wpa_s, MSG_INFO,
2480 P2P_EVENT_SERV_DISC_RESP MACSTR
2481 " %u <long response: %u bytes>",
2482 MAC2STR(sa), update_indic,
2483 (unsigned int) tlvs_len);
2484 } else {
2485 buf_len = 2 * tlvs_len + 1;
2486 buf = os_malloc(buf_len);
2487 if (buf) {
2488 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2489 wpa_msg_ctrl(wpa_s, MSG_INFO,
2490 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2491 MAC2STR(sa), update_indic, buf);
2492 os_free(buf);
2493 }
2494 }
2495
2496 while (pos < end) {
2497 u8 srv_proto, srv_trans_id, status;
2498
2499 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2500 slen = WPA_GET_LE16(pos);
2501 pos += 2;
2502 if (pos + slen > end || slen < 3) {
2503 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2504 "length");
2505 return;
2506 }
2507 tlv_end = pos + slen;
2508
2509 srv_proto = *pos++;
2510 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2511 srv_proto);
2512 srv_trans_id = *pos++;
2513 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2514 srv_trans_id);
2515 status = *pos++;
2516 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2517 status);
2518
2519 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2520 pos, tlv_end - pos);
2521
2522 pos = tlv_end;
2523 }
2524
2525 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2526}
2527
2528
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002529u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2530 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002531{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 return 0;
2534 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535}
2536
2537
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002538u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2539 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540{
2541 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002542 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543
2544 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2545 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002546 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2548 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2549 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2550 wpabuf_put_u8(tlvs, version);
2551 wpabuf_put_str(tlvs, query);
2552 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2553 wpabuf_free(tlvs);
2554 return ret;
2555}
2556
2557
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002558#ifdef CONFIG_WIFI_DISPLAY
2559
2560static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2561 const struct wpabuf *tlvs)
2562{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002563 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2564 return 0;
2565 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2566}
2567
2568
2569#define MAX_WFD_SD_SUBELEMS 20
2570
2571static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2572 const char *subelems)
2573{
2574 u8 *len;
2575 const char *pos;
2576 int val;
2577 int count = 0;
2578
2579 len = wpabuf_put(tlvs, 2);
2580 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2581 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2582
2583 wpabuf_put_u8(tlvs, role);
2584
2585 pos = subelems;
2586 while (*pos) {
2587 val = atoi(pos);
2588 if (val >= 0 && val < 256) {
2589 wpabuf_put_u8(tlvs, val);
2590 count++;
2591 if (count == MAX_WFD_SD_SUBELEMS)
2592 break;
2593 }
2594 pos = os_strchr(pos + 1, ',');
2595 if (pos == NULL)
2596 break;
2597 pos++;
2598 }
2599
2600 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2601}
2602
2603
2604u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2605 const u8 *dst, const char *role)
2606{
2607 struct wpabuf *tlvs;
2608 u64 ret;
2609 const char *subelems;
2610 u8 id = 1;
2611
2612 subelems = os_strchr(role, ' ');
2613 if (subelems == NULL)
2614 return 0;
2615 subelems++;
2616
2617 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2618 if (tlvs == NULL)
2619 return 0;
2620
2621 if (os_strstr(role, "[source]"))
2622 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2623 if (os_strstr(role, "[pri-sink]"))
2624 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2625 if (os_strstr(role, "[sec-sink]"))
2626 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2627 if (os_strstr(role, "[source+sink]"))
2628 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2629
2630 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2631 wpabuf_free(tlvs);
2632 return ret;
2633}
2634
2635#endif /* CONFIG_WIFI_DISPLAY */
2636
2637
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002638int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002639{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2641 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002642 return p2p_sd_cancel_request(wpa_s->global->p2p,
2643 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644}
2645
2646
2647void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2648 const u8 *dst, u8 dialog_token,
2649 const struct wpabuf *resp_tlvs)
2650{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002651 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2652 return;
2653 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2654 resp_tlvs);
2655}
2656
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002658void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2659{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660 if (wpa_s->global->p2p)
2661 p2p_sd_service_update(wpa_s->global->p2p);
2662}
2663
2664
2665static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2666{
2667 dl_list_del(&bsrv->list);
2668 wpabuf_free(bsrv->query);
2669 wpabuf_free(bsrv->resp);
2670 os_free(bsrv);
2671}
2672
2673
2674static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2675{
2676 dl_list_del(&usrv->list);
2677 os_free(usrv->service);
2678 os_free(usrv);
2679}
2680
2681
2682void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2683{
2684 struct p2p_srv_bonjour *bsrv, *bn;
2685 struct p2p_srv_upnp *usrv, *un;
2686
2687 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2688 struct p2p_srv_bonjour, list)
2689 wpas_p2p_srv_bonjour_free(bsrv);
2690
2691 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2692 struct p2p_srv_upnp, list)
2693 wpas_p2p_srv_upnp_free(usrv);
2694
2695 wpas_p2p_sd_service_update(wpa_s);
2696}
2697
2698
2699int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2700 struct wpabuf *query, struct wpabuf *resp)
2701{
2702 struct p2p_srv_bonjour *bsrv;
2703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 bsrv = os_zalloc(sizeof(*bsrv));
2705 if (bsrv == NULL)
2706 return -1;
2707 bsrv->query = query;
2708 bsrv->resp = resp;
2709 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2710
2711 wpas_p2p_sd_service_update(wpa_s);
2712 return 0;
2713}
2714
2715
2716int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2717 const struct wpabuf *query)
2718{
2719 struct p2p_srv_bonjour *bsrv;
2720
2721 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2722 if (bsrv == NULL)
2723 return -1;
2724 wpas_p2p_srv_bonjour_free(bsrv);
2725 wpas_p2p_sd_service_update(wpa_s);
2726 return 0;
2727}
2728
2729
2730int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2731 const char *service)
2732{
2733 struct p2p_srv_upnp *usrv;
2734
2735 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2736 return 0; /* Already listed */
2737 usrv = os_zalloc(sizeof(*usrv));
2738 if (usrv == NULL)
2739 return -1;
2740 usrv->version = version;
2741 usrv->service = os_strdup(service);
2742 if (usrv->service == NULL) {
2743 os_free(usrv);
2744 return -1;
2745 }
2746 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2747
2748 wpas_p2p_sd_service_update(wpa_s);
2749 return 0;
2750}
2751
2752
2753int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2754 const char *service)
2755{
2756 struct p2p_srv_upnp *usrv;
2757
2758 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2759 if (usrv == NULL)
2760 return -1;
2761 wpas_p2p_srv_upnp_free(usrv);
2762 wpas_p2p_sd_service_update(wpa_s);
2763 return 0;
2764}
2765
2766
2767static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2768 const u8 *peer, const char *params,
2769 unsigned int generated_pin)
2770{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002771 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2772 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773}
2774
2775
2776static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2777 const u8 *peer, const char *params)
2778{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002779 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2780 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781}
2782
2783
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002784static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2785 const u8 *dev_addr, const u8 *pri_dev_type,
2786 const char *dev_name, u16 supp_config_methods,
2787 u8 dev_capab, u8 group_capab, const u8 *group_id,
2788 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789{
2790 struct wpa_supplicant *wpa_s = ctx;
2791 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002792 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 u8 empty_dev_type[8];
2794 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002795 struct wpa_supplicant *group = NULL;
2796
2797 if (group_id) {
2798 for (group = wpa_s->global->ifaces; group; group = group->next)
2799 {
2800 struct wpa_ssid *s = group->current_ssid;
2801 if (s != NULL &&
2802 s->mode == WPAS_MODE_P2P_GO &&
2803 group_id_len - ETH_ALEN == s->ssid_len &&
2804 os_memcmp(group_id + ETH_ALEN, s->ssid,
2805 s->ssid_len) == 0)
2806 break;
2807 }
2808 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002809
2810 if (pri_dev_type == NULL) {
2811 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2812 pri_dev_type = empty_dev_type;
2813 }
2814 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2815 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002816 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817 MAC2STR(dev_addr),
2818 wps_dev_type_bin2str(pri_dev_type, devtype,
2819 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002820 dev_name, supp_config_methods, dev_capab, group_capab,
2821 group ? " group=" : "",
2822 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 if (config_methods & WPS_CONFIG_DISPLAY) {
2826 generated_pin = wps_generate_pin();
2827 wpas_prov_disc_local_display(wpa_s, peer, params,
2828 generated_pin);
2829 } else if (config_methods & WPS_CONFIG_KEYPAD)
2830 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2831 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002832 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2833 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002834
2835 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2836 P2P_PROV_DISC_SUCCESS,
2837 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002838}
2839
2840
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002841static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002842{
2843 struct wpa_supplicant *wpa_s = ctx;
2844 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002845 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002847 if (wpa_s->pending_pd_before_join &&
2848 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2849 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2850 wpa_s->pending_pd_before_join = 0;
2851 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2852 "join-existing-group operation");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002853 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002854 return;
2855 }
2856
Dmitry Shmidt04949592012-07-19 12:16:46 -07002857 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2858 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2859 os_snprintf(params, sizeof(params), " peer_go=%d",
2860 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2861 else
2862 params[0] = '\0';
2863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002864 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002865 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002866 else if (config_methods & WPS_CONFIG_KEYPAD) {
2867 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002868 wpas_prov_disc_local_display(wpa_s, peer, params,
2869 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002871 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2872 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873
Jouni Malinen75ecf522011-06-27 15:19:46 -07002874 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2875 P2P_PROV_DISC_SUCCESS,
2876 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877}
2878
2879
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2881 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002882{
2883 struct wpa_supplicant *wpa_s = ctx;
2884
Dmitry Shmidt04949592012-07-19 12:16:46 -07002885 if (wpa_s->p2p_fallback_to_go_neg) {
2886 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2887 "failed - fall back to GO Negotiation");
2888 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2889 return;
2890 }
2891
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002892 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002893 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002894 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2895 "join-existing-group operation (no ACK for PD "
2896 "Req attempts)");
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002897 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002898 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002899 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002900
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002901 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2902 " p2p_dev_addr=" MACSTR " status=%d",
2903 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002904
Jouni Malinen75ecf522011-06-27 15:19:46 -07002905 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2906 status, 0, 0);
2907}
2908
2909
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002910static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2911{
2912 if (channels == NULL)
2913 return 1; /* Assume no restrictions */
2914 return p2p_channels_includes_freq(channels, freq);
2915
2916}
2917
2918
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2920 const u8 *go_dev_addr, const u8 *ssid,
2921 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002922 int *force_freq, int persistent_group,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002923 const struct p2p_channels *channels,
2924 int dev_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002925{
2926 struct wpa_supplicant *wpa_s = ctx;
2927 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002928 int res;
2929 struct wpa_supplicant *grp;
2930
2931 if (!persistent_group) {
2932 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08002933 " to join an active group (SSID: %s)",
2934 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2936 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2937 == 0 ||
2938 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2939 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2940 "authorized invitation");
2941 goto accept_inv;
2942 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002943
2944#ifdef CONFIG_WPS_NFC
2945 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2946 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2947 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2948 wpa_s->parent->p2p_wps_method = WPS_NFC;
2949 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2950 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2951 go_dev_addr, ETH_ALEN);
2952 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2953 bssid, ETH_ALEN);
2954 goto accept_inv;
2955 }
2956#endif /* CONFIG_WPS_NFC */
2957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958 /*
2959 * Do not accept the invitation automatically; notify user and
2960 * request approval.
2961 */
2962 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2963 }
2964
2965 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2966 if (grp) {
2967 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2968 "running persistent group");
2969 if (*go)
2970 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2971 goto accept_inv;
2972 }
2973
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002974 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2975 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2976 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2977 "invitation to re-invoke a persistent group");
2978 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2980
2981 for (s = wpa_s->conf->ssid; s; s = s->next) {
2982 if (s->disabled == 2 &&
2983 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2984 s->ssid_len == ssid_len &&
2985 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2986 break;
2987 }
2988
2989 if (!s) {
2990 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2991 " requested reinvocation of an unknown group",
2992 MAC2STR(sa));
2993 return P2P_SC_FAIL_UNKNOWN_GROUP;
2994 }
2995
2996 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2997 *go = 1;
2998 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2999 wpa_printf(MSG_DEBUG, "P2P: The only available "
3000 "interface is already in use - reject "
3001 "invitation");
3002 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3003 }
3004 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3005 } else if (s->mode == WPAS_MODE_P2P_GO) {
3006 *go = 1;
3007 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3008 {
3009 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3010 "interface address for the group");
3011 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3012 }
3013 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3014 ETH_ALEN);
3015 }
3016
3017accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003018 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3019
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003020 /* Get one of the frequencies currently in use */
3021 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003022 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003023 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08003024
3025 if (wpa_s->num_multichan_concurrent < 2 ||
3026 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3027 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3028 *force_freq = res;
3029 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030 }
3031
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003032 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3033 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003034 if (*go == 0) {
3035 /* We are the client */
3036 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3037 "running a GO but we are capable of MCC, "
3038 "figure out the best channel to use");
3039 *force_freq = 0;
3040 } else if (!freq_included(channels, *force_freq)) {
3041 /* We are the GO, and *force_freq is not in the
3042 * intersection */
3043 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3044 "in intersection but we are capable of MCC, "
3045 "figure out the best channel to use",
3046 *force_freq);
3047 *force_freq = 0;
3048 }
3049 }
3050
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 return P2P_SC_SUCCESS;
3052}
3053
3054
3055static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3056 const u8 *ssid, size_t ssid_len,
3057 const u8 *go_dev_addr, u8 status,
3058 int op_freq)
3059{
3060 struct wpa_supplicant *wpa_s = ctx;
3061 struct wpa_ssid *s;
3062
3063 for (s = wpa_s->conf->ssid; s; s = s->next) {
3064 if (s->disabled == 2 &&
3065 s->ssid_len == ssid_len &&
3066 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3067 break;
3068 }
3069
3070 if (status == P2P_SC_SUCCESS) {
3071 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003072 " was accepted; op_freq=%d MHz, SSID=%s",
3073 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07003075 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003076 wpas_p2p_group_add_persistent(
Dmitry Shmidt15907092014-03-25 10:42:57 -07003077 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003078 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003079 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003080 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003082 wpa_s->p2p_wps_method, 0, op_freq,
Dmitry Shmidt344abd32014-01-14 13:17:00 -08003083 ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 }
3085 return;
3086 }
3087
3088 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3089 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3090 " was rejected (status %u)", MAC2STR(sa), status);
3091 return;
3092 }
3093
3094 if (!s) {
3095 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003096 wpa_msg_global(wpa_s, MSG_INFO,
3097 P2P_EVENT_INVITATION_RECEIVED
3098 "sa=" MACSTR " go_dev_addr=" MACSTR
3099 " bssid=" MACSTR " unknown-network",
3100 MAC2STR(sa), MAC2STR(go_dev_addr),
3101 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003102 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003103 wpa_msg_global(wpa_s, MSG_INFO,
3104 P2P_EVENT_INVITATION_RECEIVED
3105 "sa=" MACSTR " go_dev_addr=" MACSTR
3106 " unknown-network",
3107 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108 }
3109 return;
3110 }
3111
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003112 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003113 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3114 "sa=" MACSTR " persistent=%d freq=%d",
3115 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003116 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003117 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3118 "sa=" MACSTR " persistent=%d",
3119 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003120 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121}
3122
3123
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003124static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3125 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003126 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003127{
3128 size_t i;
3129
3130 if (ssid == NULL)
3131 return;
3132
3133 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3134 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3135 ETH_ALEN) == 0)
3136 break;
3137 }
3138 if (i >= ssid->num_p2p_clients) {
3139 if (ssid->mode != WPAS_MODE_P2P_GO &&
3140 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3141 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3142 "due to invitation result", ssid->id);
3143 wpas_notify_network_removed(wpa_s, ssid);
3144 wpa_config_remove_network(wpa_s->conf, ssid->id);
3145 return;
3146 }
3147 return; /* Peer not found in client list */
3148 }
3149
3150 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003151 "group %d client list%s",
3152 MAC2STR(peer), ssid->id,
3153 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003154 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3155 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3156 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3157 ssid->num_p2p_clients--;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003158 if (wpa_s->parent->conf->update_config &&
3159 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3160 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003161}
3162
3163
3164static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3165 const u8 *peer)
3166{
3167 struct wpa_ssid *ssid;
3168
3169 wpa_s = wpa_s->global->p2p_invite_group;
3170 if (wpa_s == NULL)
3171 return; /* No known invitation group */
3172 ssid = wpa_s->current_ssid;
3173 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3174 !ssid->p2p_persistent_group)
3175 return; /* Not operating as a GO in persistent group */
3176 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3177 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003178 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003179}
3180
3181
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003182static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003183 const struct p2p_channels *channels,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003184 const u8 *peer, int neg_freq,
3185 int peer_oper_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003186{
3187 struct wpa_supplicant *wpa_s = ctx;
3188 struct wpa_ssid *ssid;
Dmitry Shmidt15907092014-03-25 10:42:57 -07003189 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003190
3191 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003192 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3193 "status=%d " MACSTR,
3194 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003196 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3197 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198 }
3199 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3200
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003201 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3202 status, MAC2STR(peer));
3203 if (wpa_s->pending_invite_ssid_id == -1) {
3204 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3205 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003207 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08003209 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3210 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3211 "invitation exchange to indicate readiness for "
3212 "re-invocation");
3213 }
3214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003216 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3217 ssid = wpa_config_get_network(
3218 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003219 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003220 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003221 wpas_p2p_remove_pending_group_interface(wpa_s);
3222 return;
3223 }
3224
3225 ssid = wpa_config_get_network(wpa_s->conf,
3226 wpa_s->pending_invite_ssid_id);
3227 if (ssid == NULL) {
3228 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3229 "data matching with invitation");
3230 return;
3231 }
3232
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07003233 /*
3234 * The peer could have missed our ctrl::ack frame for Invitation
3235 * Response and continue retransmitting the frame. To reduce the
3236 * likelihood of the peer not getting successful TX status for the
3237 * Invitation Response frame, wait a short time here before starting
3238 * the persistent group so that we will remain on the current channel to
3239 * acknowledge any possible retransmission from the peer.
3240 */
3241 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3242 "starting persistent group");
3243 os_sleep(0, 50000);
3244
Dmitry Shmidt15907092014-03-25 10:42:57 -07003245 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3246 freq_included(channels, neg_freq))
3247 freq = neg_freq;
3248 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3249 freq_included(channels, peer_oper_freq))
3250 freq = peer_oper_freq;
3251 else
3252 freq = 0;
3253
3254 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3255 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003256 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03003257 ssid->mode == WPAS_MODE_P2P_GO,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003258 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003259 freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003260 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3261 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07003262 ssid->mode == WPAS_MODE_P2P_GO ?
3263 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3264 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003265}
3266
3267
Dmitry Shmidt04949592012-07-19 12:16:46 -07003268static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3269 unsigned int freq)
3270{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003271 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3272 return 1;
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07003273 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003274}
3275
3276
3277static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3278{
3279 reg->channel[reg->channels] = chan;
3280 reg->channels++;
3281}
3282
3283
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003285 struct p2p_channels *chan,
3286 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287{
3288 int i, cla = 0;
3289
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003290 os_memset(cli_chan, 0, sizeof(*cli_chan));
3291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3293 "band");
3294
3295 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3296 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003297 chan->reg_class[cla].channels = 0;
3298 for (i = 0; i < 11; i++) {
3299 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3300 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3301 }
3302 if (chan->reg_class[cla].channels)
3303 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304
3305 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3306 "band");
3307
3308 /* Operating class 115 - 5 GHz, channels 36-48 */
3309 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003310 chan->reg_class[cla].channels = 0;
3311 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3312 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3313 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3314 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3315 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3316 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3317 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3318 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3319 if (chan->reg_class[cla].channels)
3320 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321
3322 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3323 "band");
3324
3325 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3326 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003327 chan->reg_class[cla].channels = 0;
3328 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3329 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3330 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3331 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3332 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3333 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3334 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3335 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3336 if (chan->reg_class[cla].channels)
3337 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338
3339 chan->reg_classes = cla;
3340 return 0;
3341}
3342
3343
3344static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3345 u16 num_modes,
3346 enum hostapd_hw_mode mode)
3347{
3348 u16 i;
3349
3350 for (i = 0; i < num_modes; i++) {
3351 if (modes[i].mode == mode)
3352 return &modes[i];
3353 }
3354
3355 return NULL;
3356}
3357
3358
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003359enum chan_allowed {
3360 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3361};
3362
Dmitry Shmidt04949592012-07-19 12:16:46 -07003363static int has_channel(struct wpa_global *global,
3364 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365{
3366 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003367 unsigned int freq;
3368
3369 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3370 chan * 5;
3371 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003372 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003373
3374 for (i = 0; i < mode->num_channels; i++) {
3375 if (mode->channels[i].chan == chan) {
3376 if (flags)
3377 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003378 if (mode->channels[i].flag &
3379 (HOSTAPD_CHAN_DISABLED |
3380 HOSTAPD_CHAN_RADAR))
3381 return NOT_ALLOWED;
3382 if (mode->channels[i].flag &
3383 (HOSTAPD_CHAN_PASSIVE_SCAN |
3384 HOSTAPD_CHAN_NO_IBSS))
3385 return PASSIVE_ONLY;
3386 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387 }
3388 }
3389
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003390 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003391}
3392
3393
3394struct p2p_oper_class_map {
3395 enum hostapd_hw_mode mode;
3396 u8 op_class;
3397 u8 min_chan;
3398 u8 max_chan;
3399 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003400 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401};
3402
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003403static struct p2p_oper_class_map op_class[] = {
3404 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003405#if 0 /* Do not enable HT40 on 2 GHz for now */
3406 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3407 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3408#endif
3409 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3410 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3411 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3412 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3413 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3414 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003415
3416 /*
3417 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3418 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3419 * 80 MHz, but currently use the following definition for simplicity
3420 * (these center frequencies are not actual channels, which makes
3421 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3422 * removing invalid channels.
3423 */
3424 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003425 { -1, 0, 0, 0, 0, BW20 }
3426};
3427
3428
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003429static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3430 struct hostapd_hw_modes *mode,
3431 u8 channel)
3432{
3433 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3434 unsigned int i;
3435
3436 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3437 return 0;
3438
3439 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3440 /*
3441 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3442 * so the center channel is 6 channels away from the start/end.
3443 */
3444 if (channel >= center_channels[i] - 6 &&
3445 channel <= center_channels[i] + 6)
3446 return center_channels[i];
3447
3448 return 0;
3449}
3450
3451
3452static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3453 struct hostapd_hw_modes *mode,
3454 u8 channel, u8 bw)
3455{
3456 u8 center_chan;
3457 int i, flags;
3458 enum chan_allowed res, ret = ALLOWED;
3459
3460 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3461 if (!center_chan)
3462 return NOT_ALLOWED;
3463 if (center_chan >= 58 && center_chan <= 138)
3464 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3465
3466 /* check all the channels are available */
3467 for (i = 0; i < 4; i++) {
3468 int adj_chan = center_chan - 6 + i * 4;
3469
3470 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3471 if (res == NOT_ALLOWED)
3472 return NOT_ALLOWED;
3473 if (res == PASSIVE_ONLY)
3474 ret = PASSIVE_ONLY;
3475
3476 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3477 return NOT_ALLOWED;
3478 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3479 return NOT_ALLOWED;
3480 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3481 return NOT_ALLOWED;
3482 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3483 return NOT_ALLOWED;
3484 }
3485
3486 return ret;
3487}
3488
3489
3490static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3491 struct hostapd_hw_modes *mode,
3492 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003493{
Dmitry Shmidt96be6222014-02-13 10:16:51 -08003494 int flag = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003495 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003496
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003497 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3498 if (bw == BW40MINUS) {
3499 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3500 return NOT_ALLOWED;
3501 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3502 } else if (bw == BW40PLUS) {
3503 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3504 return NOT_ALLOWED;
3505 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3506 } else if (bw == BW80) {
3507 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3508 }
3509
3510 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3511 return NOT_ALLOWED;
3512 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3513 return PASSIVE_ONLY;
3514 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003515}
3516
3517
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003519 struct p2p_channels *chan,
3520 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003522 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003523 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003525 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3527 "of all supported channels; assume dualband "
3528 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003529 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003530 }
3531
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003532 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533
3534 for (op = 0; op_class[op].op_class; op++) {
3535 struct p2p_oper_class_map *o = &op_class[op];
3536 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003537 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003539 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540 if (mode == NULL)
3541 continue;
3542 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003543 enum chan_allowed res;
3544 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3545 if (res == ALLOWED) {
3546 if (reg == NULL) {
3547 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3548 o->op_class);
3549 reg = &chan->reg_class[cla];
3550 cla++;
3551 reg->reg_class = o->op_class;
3552 }
3553 reg->channel[reg->channels] = ch;
3554 reg->channels++;
3555 } else if (res == PASSIVE_ONLY &&
3556 wpa_s->conf->p2p_add_cli_chan) {
3557 if (cli_reg == NULL) {
3558 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3559 o->op_class);
3560 cli_reg = &cli_chan->reg_class[cli_cla];
3561 cli_cla++;
3562 cli_reg->reg_class = o->op_class;
3563 }
3564 cli_reg->channel[cli_reg->channels] = ch;
3565 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003567 }
3568 if (reg) {
3569 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3570 reg->channel, reg->channels);
3571 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003572 if (cli_reg) {
3573 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3574 cli_reg->channel, cli_reg->channels);
3575 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 }
3577
3578 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003579 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 return 0;
3582}
3583
3584
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003585int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3586 struct hostapd_hw_modes *mode, u8 channel)
3587{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003588 int op;
3589 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003590
3591 for (op = 0; op_class[op].op_class; op++) {
3592 struct p2p_oper_class_map *o = &op_class[op];
3593 u8 ch;
3594
3595 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3596 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3597 o->bw == BW20 || ch != channel)
3598 continue;
3599 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003600 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003601 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003602 }
3603 }
3604 return 0;
3605}
3606
3607
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003608int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3609 struct hostapd_hw_modes *mode, u8 channel)
3610{
3611 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3612 return 0;
3613
3614 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3615}
3616
3617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3619 size_t buf_len)
3620{
3621 struct wpa_supplicant *wpa_s = ctx;
3622
3623 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3624 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3625 break;
3626 }
3627 if (wpa_s == NULL)
3628 return -1;
3629
3630 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3631}
3632
3633
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003634static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3635{
3636 struct wpa_supplicant *wpa_s = ctx;
3637
3638 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3639 struct wpa_ssid *ssid = wpa_s->current_ssid;
3640 if (ssid == NULL)
3641 continue;
3642 if (ssid->mode != WPAS_MODE_INFRA)
3643 continue;
3644 if (wpa_s->wpa_state != WPA_COMPLETED &&
3645 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3646 continue;
3647 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3648 return 1;
3649 }
3650
3651 return 0;
3652}
3653
3654
Dmitry Shmidt18463232014-01-24 12:29:41 -08003655static int wpas_is_concurrent_session_active(void *ctx)
3656{
3657 struct wpa_supplicant *wpa_s = ctx;
3658 struct wpa_supplicant *ifs;
3659
3660 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3661 if (ifs == wpa_s)
3662 continue;
3663 if (ifs->wpa_state > WPA_ASSOCIATED)
3664 return 1;
3665 }
3666 return 0;
3667}
3668
3669
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003670static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3671{
3672 struct wpa_supplicant *wpa_s = ctx;
3673 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3674}
3675
3676
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003677int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3678{
3679 struct wpa_interface iface;
3680 struct wpa_supplicant *p2pdev_wpa_s;
3681 char ifname[100];
3682 char force_name[100];
3683 int ret;
3684
3685 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3686 wpa_s->ifname);
3687 force_name[0] = '\0';
3688 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3689 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3690 force_name, wpa_s->pending_interface_addr, NULL);
3691 if (ret < 0) {
3692 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3693 return ret;
3694 }
3695 os_strlcpy(wpa_s->pending_interface_name, ifname,
3696 sizeof(wpa_s->pending_interface_name));
3697
3698 os_memset(&iface, 0, sizeof(iface));
3699 iface.p2p_mgmt = 1;
3700 iface.ifname = wpa_s->pending_interface_name;
3701 iface.driver = wpa_s->driver->name;
3702 iface.driver_param = wpa_s->conf->driver_param;
Dmitry Shmidt2ac5f602014-03-07 10:08:21 -08003703
3704 /*
3705 * If a P2P Device configuration file was given, use it as the interface
3706 * configuration file (instead of using parent's configuration file.
3707 */
3708 if (wpa_s->conf_p2p_dev) {
3709 iface.confname = wpa_s->conf_p2p_dev;
3710 iface.ctrl_interface = NULL;
3711 } else {
3712 iface.confname = wpa_s->confname;
3713 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3714 }
3715 iface.conf_p2p_dev = NULL;
3716
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003717 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3718 if (!p2pdev_wpa_s) {
3719 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3720 return -1;
3721 }
3722 p2pdev_wpa_s->parent = wpa_s;
3723
3724 wpa_s->pending_interface_name[0] = '\0';
3725 return 0;
3726}
3727
3728
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003729static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3730 const u8 *noa, size_t noa_len)
3731{
3732 struct wpa_supplicant *wpa_s, *intf = ctx;
3733 char hex[100];
3734
3735 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3736 if (wpa_s->waiting_presence_resp)
3737 break;
3738 }
3739 if (!wpa_s) {
3740 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3741 return;
3742 }
3743 wpa_s->waiting_presence_resp = 0;
3744
3745 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3746 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3747 " status=%u noa=%s", MAC2STR(src), status, hex);
3748}
3749
3750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751/**
3752 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3753 * @global: Pointer to global data from wpa_supplicant_init()
3754 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3755 * Returns: 0 on success, -1 on failure
3756 */
3757int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3758{
3759 struct p2p_config p2p;
3760 unsigned int r;
3761 int i;
3762
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003763 if (wpa_s->conf->p2p_disabled)
3764 return 0;
3765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3767 return 0;
3768
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 if (global->p2p)
3770 return 0;
3771
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003773 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003774 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 p2p.p2p_scan = wpas_p2p_scan;
3776 p2p.send_action = wpas_send_action;
3777 p2p.send_action_done = wpas_send_action_done;
3778 p2p.go_neg_completed = wpas_go_neg_completed;
3779 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3780 p2p.dev_found = wpas_dev_found;
3781 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003782 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 p2p.start_listen = wpas_start_listen;
3784 p2p.stop_listen = wpas_stop_listen;
3785 p2p.send_probe_resp = wpas_send_probe_resp;
3786 p2p.sd_request = wpas_sd_request;
3787 p2p.sd_response = wpas_sd_response;
3788 p2p.prov_disc_req = wpas_prov_disc_req;
3789 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003790 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003791 p2p.invitation_process = wpas_invitation_process;
3792 p2p.invitation_received = wpas_invitation_received;
3793 p2p.invitation_result = wpas_invitation_result;
3794 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003795 p2p.go_connected = wpas_go_connected;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003796 p2p.presence_resp = wpas_presence_resp;
Dmitry Shmidt18463232014-01-24 12:29:41 -08003797 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003798
3799 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003800 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003801 p2p.dev_name = wpa_s->conf->device_name;
3802 p2p.manufacturer = wpa_s->conf->manufacturer;
3803 p2p.model_name = wpa_s->conf->model_name;
3804 p2p.model_number = wpa_s->conf->model_number;
3805 p2p.serial_number = wpa_s->conf->serial_number;
3806 if (wpa_s->wps) {
3807 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3808 p2p.config_methods = wpa_s->wps->config_methods;
3809 }
3810
3811 if (wpa_s->conf->p2p_listen_reg_class &&
3812 wpa_s->conf->p2p_listen_channel) {
3813 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3814 p2p.channel = wpa_s->conf->p2p_listen_channel;
3815 } else {
3816 p2p.reg_class = 81;
3817 /*
3818 * Pick one of the social channels randomly as the listen
3819 * channel.
3820 */
3821 os_get_random((u8 *) &r, sizeof(r));
3822 p2p.channel = 1 + (r % 3) * 5;
3823 }
3824 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3825
3826 if (wpa_s->conf->p2p_oper_reg_class &&
3827 wpa_s->conf->p2p_oper_channel) {
3828 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3829 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3830 p2p.cfg_op_channel = 1;
3831 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3832 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3833
3834 } else {
3835 p2p.op_reg_class = 81;
3836 /*
3837 * Use random operation channel from (1, 6, 11) if no other
3838 * preference is indicated.
3839 */
3840 os_get_random((u8 *) &r, sizeof(r));
3841 p2p.op_channel = 1 + (r % 3) * 5;
3842 p2p.cfg_op_channel = 0;
3843 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3844 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3845 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003846
3847 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3848 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3849 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3850 }
3851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003852 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3853 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3854 p2p.country[2] = 0x04;
3855 } else
3856 os_memcpy(p2p.country, "XX\x04", 3);
3857
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003858 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003859 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3860 "channel list");
3861 return -1;
3862 }
3863
3864 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3865 WPS_DEV_TYPE_LEN);
3866
3867 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3868 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3869 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3870
3871 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3872 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3873
3874 p2p.max_peers = 100;
3875
3876 if (wpa_s->conf->p2p_ssid_postfix) {
3877 p2p.ssid_postfix_len =
3878 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3879 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3880 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3881 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3882 p2p.ssid_postfix_len);
3883 }
3884
3885 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3886
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003887 p2p.max_listen = wpa_s->max_remain_on_chan;
3888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003889 global->p2p = p2p_init(&p2p);
3890 if (global->p2p == NULL)
3891 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003892 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003893
3894 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3895 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3896 continue;
3897 p2p_add_wps_vendor_extension(
3898 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3899 }
3900
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003901 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003903 return 0;
3904}
3905
3906
3907/**
3908 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3909 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3910 *
3911 * This function deinitialize per-interface P2P data.
3912 */
3913void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3914{
3915 if (wpa_s->driver && wpa_s->drv_priv)
3916 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003917
3918 if (wpa_s->go_params) {
3919 /* Clear any stored provisioning info */
3920 p2p_clear_provisioning_info(
3921 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003922 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003923 }
3924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003925 os_free(wpa_s->go_params);
3926 wpa_s->go_params = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3928 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3929 wpa_s->p2p_long_listen = 0;
3930 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3931 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3932 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08003933 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003934 wpas_p2p_listen_work_done(wpa_s);
3935 if (wpa_s->p2p_send_action_work) {
3936 os_free(wpa_s->p2p_send_action_work->ctx);
3937 radio_work_done(wpa_s->p2p_send_action_work);
3938 wpa_s->p2p_send_action_work = NULL;
3939 }
3940 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003942 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3943 wpa_s->p2p_oob_dev_pw = NULL;
3944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003945 /* TODO: remove group interface from the driver if this wpa_s instance
3946 * is on top of a P2P group interface */
3947}
3948
3949
3950/**
3951 * wpas_p2p_deinit_global - Deinitialize global P2P module
3952 * @global: Pointer to global data from wpa_supplicant_init()
3953 *
3954 * This function deinitializes the global (per device) P2P module.
3955 */
3956void wpas_p2p_deinit_global(struct wpa_global *global)
3957{
3958 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003959
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003960 wpa_s = global->ifaces;
3961 if (wpa_s)
3962 wpas_p2p_service_flush(wpa_s);
3963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003964 if (global->p2p == NULL)
3965 return;
3966
3967 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003968 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3969 wpa_s = wpa_s->next;
3970 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003971 tmp = global->ifaces;
3972 while (tmp &&
3973 (tmp == wpa_s ||
3974 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3975 tmp = tmp->next;
3976 }
3977 if (tmp == NULL)
3978 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003979 /* Disconnect from the P2P group and deinit the interface */
3980 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003981 }
3982
3983 /*
3984 * Deinit GO data on any possibly remaining interface (if main
3985 * interface is used as GO).
3986 */
3987 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3988 if (wpa_s->ap_iface)
3989 wpas_p2p_group_deinit(wpa_s);
3990 }
3991
3992 p2p_deinit(global->p2p);
3993 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003994 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003995}
3996
3997
3998static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3999{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07004000 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4001 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004002 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004003 if (wpa_s->drv_flags &
4004 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4005 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4006 return 1; /* P2P group requires a new interface in every case
4007 */
4008 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4009 return 0; /* driver does not support concurrent operations */
4010 if (wpa_s->global->ifaces->next)
4011 return 1; /* more that one interface already in use */
4012 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4013 return 1; /* this interface is already in use */
4014 return 0;
4015}
4016
4017
4018static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4019 const u8 *peer_addr,
4020 enum p2p_wps_method wps_method,
4021 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004022 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004023 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004024{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004025 if (persistent_group && wpa_s->conf->persistent_reconnect)
4026 persistent_group = 2;
4027
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004028 /*
4029 * Increase GO config timeout if HT40 is used since it takes some time
4030 * to scan channels for coex purposes before the BSS can be started.
4031 */
4032 p2p_set_config_timeout(wpa_s->global->p2p,
4033 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4036 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004037 persistent_group, ssid ? ssid->ssid : NULL,
4038 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004039 wpa_s->p2p_pd_before_go_neg, pref_freq,
4040 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4041 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042}
4043
4044
4045static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4046 const u8 *peer_addr,
4047 enum p2p_wps_method wps_method,
4048 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004049 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004050 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004051{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004052 if (persistent_group && wpa_s->conf->persistent_reconnect)
4053 persistent_group = 2;
4054
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4056 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004057 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004058 ssid ? ssid->ssid_len : 0, pref_freq,
4059 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4060 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061}
4062
4063
4064static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4065{
4066 wpa_s->p2p_join_scan_count++;
4067 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4068 wpa_s->p2p_join_scan_count);
4069 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4070 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4071 " for join operationg - stop join attempt",
4072 MAC2STR(wpa_s->pending_join_iface_addr));
4073 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004074 if (wpa_s->p2p_auto_pd) {
4075 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004076 wpa_msg_global(wpa_s, MSG_INFO,
4077 P2P_EVENT_PROV_DISC_FAILURE
4078 " p2p_dev_addr=" MACSTR " status=N/A",
4079 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004080 return;
4081 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004082 wpa_msg_global(wpa_s->parent, MSG_INFO,
4083 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004084 }
4085}
4086
4087
Dmitry Shmidt04949592012-07-19 12:16:46 -07004088static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4089{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004090 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004091
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004092 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4093 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07004094 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004095 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004096
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004097 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4098 if (!freqs)
4099 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004100
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004101 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4102 wpa_s->num_multichan_concurrent);
4103 if (num < 0) {
4104 res = 1;
4105 goto exit_free;
4106 }
4107
4108 for (i = 0; i < num; i++) {
4109 if (freqs[i] == freq) {
4110 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4111 freq);
4112 res = 0;
4113 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004114 }
4115 }
4116
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004117 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004118
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004119exit_free:
4120 os_free(freqs);
4121 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004122}
4123
4124
4125static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4126 const u8 *peer_dev_addr)
4127{
4128 struct wpa_bss *bss;
4129 int updated;
4130
4131 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4132 if (bss == NULL)
4133 return -1;
4134 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4135 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4136 "last scan");
4137 return 0;
4138 }
4139
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004140 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4141 &bss->last_update);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004142 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4143 "%ld.%06ld (%supdated in last scan)",
4144 bss->last_update.sec, bss->last_update.usec,
4145 updated ? "": "not ");
4146
4147 return updated;
4148}
4149
4150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4152 struct wpa_scan_results *scan_res)
4153{
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004154 struct wpa_bss *bss = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004155 int freq;
4156 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07004157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004158 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4159
4160 if (wpa_s->global->p2p_disabled)
4161 return;
4162
Dmitry Shmidt04949592012-07-19 12:16:46 -07004163 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4164 scan_res ? (int) scan_res->num : -1,
4165 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004166
4167 if (scan_res)
4168 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4169
Dmitry Shmidt04949592012-07-19 12:16:46 -07004170 if (wpa_s->p2p_auto_pd) {
4171 int join = wpas_p2p_peer_go(wpa_s,
4172 wpa_s->pending_join_dev_addr);
4173 if (join == 0 &&
4174 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4175 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004176 bss = wpa_bss_get_bssid_latest(
4177 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004178 if (bss) {
4179 freq = bss->freq;
4180 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4181 "the peer " MACSTR " at %d MHz",
4182 wpa_s->auto_pd_scan_retry,
4183 MAC2STR(wpa_s->
4184 pending_join_dev_addr),
4185 freq);
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004186 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004187 return;
4188 }
4189 }
4190
4191 if (join < 0)
4192 join = 0;
4193
4194 wpa_s->p2p_auto_pd = 0;
4195 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4196 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4197 MAC2STR(wpa_s->pending_join_dev_addr), join);
4198 if (p2p_prov_disc_req(wpa_s->global->p2p,
4199 wpa_s->pending_join_dev_addr,
4200 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004201 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004202 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004203 wpa_msg_global(wpa_s, MSG_INFO,
4204 P2P_EVENT_PROV_DISC_FAILURE
4205 " p2p_dev_addr=" MACSTR " status=N/A",
4206 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004207 }
4208 return;
4209 }
4210
4211 if (wpa_s->p2p_auto_join) {
4212 int join = wpas_p2p_peer_go(wpa_s,
4213 wpa_s->pending_join_dev_addr);
4214 if (join < 0) {
4215 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4216 "running a GO -> use GO Negotiation");
4217 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4218 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4219 wpa_s->p2p_persistent_group, 0, 0, 0,
4220 wpa_s->p2p_go_intent,
4221 wpa_s->p2p_connect_freq,
4222 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004223 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004224 wpa_s->p2p_go_ht40,
4225 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004226 return;
4227 }
4228
4229 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4230 "try to join the group", join ? "" :
4231 " in older scan");
4232 if (!join)
4233 wpa_s->p2p_fallback_to_go_neg = 1;
4234 }
4235
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004236 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4237 wpa_s->pending_join_iface_addr);
4238 if (freq < 0 &&
4239 p2p_get_interface_addr(wpa_s->global->p2p,
4240 wpa_s->pending_join_dev_addr,
4241 iface_addr) == 0 &&
4242 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4243 {
4244 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4245 "address for join from " MACSTR " to " MACSTR
4246 " based on newly discovered P2P peer entry",
4247 MAC2STR(wpa_s->pending_join_iface_addr),
4248 MAC2STR(iface_addr));
4249 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4250 ETH_ALEN);
4251
4252 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4253 wpa_s->pending_join_iface_addr);
4254 }
4255 if (freq >= 0) {
4256 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4257 "from P2P peer table: %d MHz", freq);
4258 }
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004259 if (wpa_s->p2p_join_ssid_len) {
4260 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4261 MACSTR " and SSID %s",
4262 MAC2STR(wpa_s->pending_join_iface_addr),
4263 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4264 wpa_s->p2p_join_ssid_len));
4265 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4266 wpa_s->p2p_join_ssid,
4267 wpa_s->p2p_join_ssid_len);
4268 }
4269 if (!bss) {
4270 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4271 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4272 bss = wpa_bss_get_bssid_latest(wpa_s,
4273 wpa_s->pending_join_iface_addr);
4274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004275 if (bss) {
4276 freq = bss->freq;
4277 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07004278 "from BSS table: %d MHz (SSID %s)", freq,
4279 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004280 }
4281 if (freq > 0) {
4282 u16 method;
4283
Dmitry Shmidt04949592012-07-19 12:16:46 -07004284 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004285 wpa_msg_global(wpa_s->parent, MSG_INFO,
4286 P2P_EVENT_GROUP_FORMATION_FAILURE
4287 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004288 return;
4289 }
4290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004291 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4292 "prior to joining an existing group (GO " MACSTR
4293 " freq=%u MHz)",
4294 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4295 wpa_s->pending_pd_before_join = 1;
4296
4297 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004298 case WPS_PIN_DISPLAY:
4299 method = WPS_CONFIG_KEYPAD;
4300 break;
4301 case WPS_PIN_KEYPAD:
4302 method = WPS_CONFIG_DISPLAY;
4303 break;
4304 case WPS_PBC:
4305 method = WPS_CONFIG_PUSHBUTTON;
4306 break;
4307 default:
4308 method = 0;
4309 break;
4310 }
4311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004312 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4313 wpa_s->pending_join_dev_addr) ==
4314 method)) {
4315 /*
4316 * We have already performed provision discovery for
4317 * joining the group. Proceed directly to join
4318 * operation without duplicated provision discovery. */
4319 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4320 "with " MACSTR " already done - proceed to "
4321 "join",
4322 MAC2STR(wpa_s->pending_join_dev_addr));
4323 wpa_s->pending_pd_before_join = 0;
4324 goto start;
4325 }
4326
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004327 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004328 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004329 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4331 "Discovery Request before joining an "
4332 "existing group");
4333 wpa_s->pending_pd_before_join = 0;
4334 goto start;
4335 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004336 return;
4337 }
4338
4339 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4340 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4341 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4342 wpas_p2p_check_join_scan_limit(wpa_s);
4343 return;
4344
4345start:
4346 /* Start join operation immediately */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004347 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004348}
4349
4350
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004351static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4352 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004353{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354 int ret;
4355 struct wpa_driver_scan_params params;
4356 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004357 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004358 int freqs[2] = { 0, 0 };
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004359
4360 os_memset(&params, 0, sizeof(params));
4361
4362 /* P2P Wildcard SSID */
4363 params.num_ssids = 1;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004364 if (ssid && ssid_len) {
4365 params.ssids[0].ssid = ssid;
4366 params.ssids[0].ssid_len = ssid_len;
4367 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4368 wpa_s->p2p_join_ssid_len = ssid_len;
4369 } else {
4370 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4371 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4372 wpa_s->p2p_join_ssid_len = 0;
4373 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004374
4375 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004376 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4377 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4378 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 if (wps_ie == NULL) {
4380 wpas_p2p_scan_res_join(wpa_s, NULL);
4381 return;
4382 }
4383
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004384 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4385 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004386 if (ies == NULL) {
4387 wpabuf_free(wps_ie);
4388 wpas_p2p_scan_res_join(wpa_s, NULL);
4389 return;
4390 }
4391 wpabuf_put_buf(ies, wps_ie);
4392 wpabuf_free(wps_ie);
4393
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004394 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004395
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004396 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004397 params.extra_ies = wpabuf_head(ies);
4398 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -08004399
4400 if (!freq) {
4401 int oper_freq;
4402 /*
4403 * If freq is not provided, check the operating freq of the GO
4404 * and use a single channel scan on if possible.
4405 */
4406 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4407 wpa_s->pending_join_iface_addr);
4408 if (oper_freq > 0)
4409 freq = oper_freq;
4410 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004411 if (freq > 0) {
4412 freqs[0] = freq;
4413 params.freqs = freqs;
4414 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004415
4416 /*
4417 * Run a scan to update BSS table and start Provision Discovery once
4418 * the new scan results become available.
4419 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004420 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004421 if (!ret) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004422 os_get_reltime(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004423 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004424 wpa_s->own_scan_requested = 1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004425 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426
4427 wpabuf_free(ies);
4428
4429 if (ret) {
4430 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4431 "try again later");
4432 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4433 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4434 wpas_p2p_check_join_scan_limit(wpa_s);
4435 }
4436}
4437
4438
Dmitry Shmidt04949592012-07-19 12:16:46 -07004439static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4440{
4441 struct wpa_supplicant *wpa_s = eloop_ctx;
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004442 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004443}
4444
4445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004446static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004447 const u8 *dev_addr, enum p2p_wps_method wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004448 int auto_join, int op_freq,
4449 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004450{
4451 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004452 MACSTR " dev " MACSTR " op_freq=%d)%s",
4453 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004454 auto_join ? " (auto_join)" : "");
Dmitry Shmidt344abd32014-01-14 13:17:00 -08004455 if (ssid && ssid_len) {
4456 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4457 wpa_ssid_txt(ssid, ssid_len));
4458 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004459
Dmitry Shmidt04949592012-07-19 12:16:46 -07004460 wpa_s->p2p_auto_pd = 0;
4461 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004462 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4463 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4464 wpa_s->pending_join_wps_method = wps_method;
4465
4466 /* Make sure we are not running find during connection establishment */
4467 wpas_p2p_stop_find(wpa_s);
4468
4469 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004470 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004471 return 0;
4472}
4473
4474
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004475static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4476 const u8 *ssid, size_t ssid_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004477{
4478 struct wpa_supplicant *group;
4479 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004480 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004481
4482 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4483 if (group == NULL)
4484 return -1;
4485 if (group != wpa_s) {
4486 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4487 sizeof(group->p2p_pin));
4488 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004489 } else {
4490 /*
4491 * Need to mark the current interface for p2p_group_formation
4492 * when a separate group interface is not used. This is needed
4493 * to allow p2p_cancel stop a pending p2p_connect-join.
4494 * wpas_p2p_init_group_interface() addresses this for the case
4495 * where a separate group interface is used.
4496 */
4497 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004498 }
4499
4500 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004501 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004502
4503 os_memset(&res, 0, sizeof(res));
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004504 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004505 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4506 ETH_ALEN);
4507 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004508 if (freq && ssid && ssid_len) {
4509 res.freq = freq;
4510 res.ssid_len = ssid_len;
4511 os_memcpy(res.ssid, ssid, ssid_len);
4512 } else {
4513 bss = wpa_bss_get_bssid_latest(wpa_s,
4514 wpa_s->pending_join_iface_addr);
4515 if (bss) {
4516 res.freq = bss->freq;
4517 res.ssid_len = bss->ssid_len;
4518 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4519 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4520 bss->freq,
4521 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4522 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004523 }
4524
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004525 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4526 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4527 "starting client");
4528 wpa_drv_cancel_remain_on_channel(wpa_s);
4529 wpa_s->off_channel_freq = 0;
4530 wpa_s->roc_waiting_drv_freq = 0;
4531 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004532 wpas_start_wps_enrollee(group, &res);
4533
4534 /*
4535 * Allow a longer timeout for join-a-running-group than normal 15
4536 * second group formation timeout since the GO may not have authorized
4537 * our connection yet.
4538 */
4539 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4540 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4541 wpa_s, NULL);
4542
4543 return 0;
4544}
4545
4546
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004547static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004548 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004549{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004550 int *freqs, res;
4551 unsigned int freq_in_use = 0, num, i;
4552
4553 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4554 if (!freqs)
4555 return -1;
4556
4557 num = get_shared_radio_freqs(wpa_s, freqs,
4558 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004559 wpa_printf(MSG_DEBUG,
4560 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4561 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004562
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004563 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004564 int ret;
4565 if (go)
4566 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4567 else
4568 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4569 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004570 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4571 "(%u MHz) is not supported for P2P uses",
4572 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004573 res = -3;
4574 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004575 }
4576
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004577 for (i = 0; i < num; i++) {
4578 if (freqs[i] == freq)
4579 freq_in_use = 1;
4580 }
4581
4582 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4583 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4584 freq);
4585 res = -2;
4586 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004587 }
4588 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4589 "requested channel (%u MHz)", freq);
4590 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004591 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004592 }
4593
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004594 for (i = 0; i < num; i++) {
4595 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4596 continue;
4597
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004598 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004599 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4600 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004601 *pref_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004602 } else {
4603 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004604 freqs[i]);
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004605 *force_freq = freqs[i];
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004606 }
4607 break;
4608 }
4609
4610 if (i == num) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004611 if (num < wpa_s->num_multichan_concurrent && num > 0) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004612 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4613 *force_freq = 0;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08004614 } else if (num < wpa_s->num_multichan_concurrent) {
4615 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4616 *force_freq = 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004617 } else {
4618 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4619 res = -2;
4620 goto exit_free;
4621 }
4622 }
4623
4624exit_ok:
4625 res = 0;
4626exit_free:
4627 os_free(freqs);
4628 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004629}
4630
4631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004632/**
4633 * wpas_p2p_connect - Request P2P Group Formation to be started
4634 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4635 * @peer_addr: Address of the peer P2P Device
4636 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4637 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004638 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 * @join: Whether to join an existing group (as a client) instead of starting
4640 * Group Owner negotiation; @peer_addr is BSSID in that case
4641 * @auth: Whether to only authorize the connection instead of doing that and
4642 * initiating Group Owner negotiation
4643 * @go_intent: GO Intent or -1 to use default
4644 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004645 * @persistent_id: Persistent group credentials to use for forcing GO
4646 * parameters or -1 to generate new values (SSID/passphrase)
4647 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4648 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004649 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004650 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004651 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4652 * failure, -2 on failure due to channel not currently available,
4653 * -3 if forced channel is not supported
4654 */
4655int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4656 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004657 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004658 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004659 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004660{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004661 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004662 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004663 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004664 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004665 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004666
4667 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4668 return -1;
4669
Dmitry Shmidt04949592012-07-19 12:16:46 -07004670 if (persistent_id >= 0) {
4671 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4672 if (ssid == NULL || ssid->disabled != 2 ||
4673 ssid->mode != WPAS_MODE_P2P_GO)
4674 return -1;
4675 }
4676
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004677 os_free(wpa_s->global->add_psk);
4678 wpa_s->global->add_psk = NULL;
4679
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004680 wpa_s->global->p2p_fail_on_wps_complete = 0;
4681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004682 if (go_intent < 0)
4683 go_intent = wpa_s->conf->p2p_go_intent;
4684
4685 if (!auth)
4686 wpa_s->p2p_long_listen = 0;
4687
4688 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004689 wpa_s->p2p_persistent_group = !!persistent_group;
4690 wpa_s->p2p_persistent_id = persistent_id;
4691 wpa_s->p2p_go_intent = go_intent;
4692 wpa_s->p2p_connect_freq = freq;
4693 wpa_s->p2p_fallback_to_go_neg = 0;
4694 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004695 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004696 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004697
4698 if (pin)
4699 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4700 else if (wps_method == WPS_PIN_DISPLAY) {
4701 ret = wps_generate_pin();
4702 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4703 ret);
4704 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4705 wpa_s->p2p_pin);
4706 } else
4707 wpa_s->p2p_pin[0] = '\0';
4708
Dmitry Shmidt04949592012-07-19 12:16:46 -07004709 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004710 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4711 if (auth) {
4712 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4713 "connect a running group from " MACSTR,
4714 MAC2STR(peer_addr));
4715 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4716 return ret;
4717 }
4718 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4719 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4720 iface_addr) < 0) {
4721 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4722 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4723 dev_addr);
4724 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004725 if (auto_join) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004726 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004727 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4728 "%ld.%06ld",
4729 wpa_s->p2p_auto_started.sec,
4730 wpa_s->p2p_auto_started.usec);
4731 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004732 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004733 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004734 auto_join, freq, NULL, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004735 return -1;
4736 return ret;
4737 }
4738
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004739 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4740 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004741 if (res)
4742 return res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08004743 wpas_p2p_set_own_freq_preference(wpa_s,
4744 force_freq ? force_freq : pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745
4746 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4747
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004748 if (wpa_s->create_p2p_iface) {
4749 /* Prepare to add a new interface for the group */
4750 iftype = WPA_IF_P2P_GROUP;
4751 if (go_intent == 15)
4752 iftype = WPA_IF_P2P_GO;
4753 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4754 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4755 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004756 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004759 if_addr = wpa_s->pending_interface_addr;
4760 } else
4761 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004762
4763 if (auth) {
4764 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004765 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004766 force_freq, persistent_group, ssid,
4767 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004768 return -1;
4769 return ret;
4770 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004771
4772 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4773 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004774 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004775 if (wpa_s->create_p2p_iface)
4776 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004777 return -1;
4778 }
4779 return ret;
4780}
4781
4782
4783/**
4784 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4785 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4786 * @freq: Frequency of the channel in MHz
4787 * @duration: Duration of the stay on the channel in milliseconds
4788 *
4789 * This callback is called when the driver indicates that it has started the
4790 * requested remain-on-channel duration.
4791 */
4792void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4793 unsigned int freq, unsigned int duration)
4794{
4795 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4796 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004797 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4798 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4799 wpa_s->pending_listen_duration);
4800 wpa_s->pending_listen_freq = 0;
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08004801 } else {
4802 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4803 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4804 freq, duration);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004805 }
4806}
4807
4808
4809static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4810 unsigned int timeout)
4811{
4812 /* Limit maximum Listen state time based on driver limitation. */
4813 if (timeout > wpa_s->max_remain_on_chan)
4814 timeout = wpa_s->max_remain_on_chan;
4815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004816 return p2p_listen(wpa_s->global->p2p, timeout);
4817}
4818
4819
4820/**
4821 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4822 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4823 * @freq: Frequency of the channel in MHz
4824 *
4825 * This callback is called when the driver indicates that a remain-on-channel
4826 * operation has been completed, i.e., the duration on the requested channel
4827 * has timed out.
4828 */
4829void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4830 unsigned int freq)
4831{
4832 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4833 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004834 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004835 wpas_p2p_listen_work_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004836 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4837 return;
4838 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4839 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004840 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004841 return;
4842 if (wpa_s->p2p_long_listen > 0)
4843 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4844 if (wpa_s->p2p_long_listen > 0) {
4845 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4846 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004847 } else {
4848 /*
4849 * When listen duration is over, stop listen & update p2p_state
4850 * to IDLE.
4851 */
4852 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 }
4854}
4855
4856
4857/**
4858 * wpas_p2p_group_remove - Remove a P2P group
4859 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4860 * @ifname: Network interface name of the group interface or "*" to remove all
4861 * groups
4862 * Returns: 0 on success, -1 on failure
4863 *
4864 * This function is used to remove a P2P group. This can be used to disconnect
4865 * from a group in which the local end is a P2P Client or to end a P2P Group in
4866 * case the local end is the Group Owner. If a virtual network interface was
4867 * created for this group, that interface will be removed. Otherwise, only the
4868 * configured P2P group network will be removed from the interface.
4869 */
4870int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4871{
4872 struct wpa_global *global = wpa_s->global;
4873
4874 if (os_strcmp(ifname, "*") == 0) {
4875 struct wpa_supplicant *prev;
4876 wpa_s = global->ifaces;
4877 while (wpa_s) {
4878 prev = wpa_s;
4879 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004880 if (prev->p2p_group_interface !=
4881 NOT_P2P_GROUP_INTERFACE ||
4882 (prev->current_ssid &&
4883 prev->current_ssid->p2p_group))
4884 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004885 }
4886 return 0;
4887 }
4888
4889 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4890 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4891 break;
4892 }
4893
4894 return wpas_p2p_disconnect(wpa_s);
4895}
4896
4897
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004898static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4899{
4900 unsigned int r;
4901
4902 if (freq == 2) {
4903 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4904 "band");
4905 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004906 p2p_supported_freq_go(wpa_s->global->p2p,
4907 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004908 freq = wpa_s->best_24_freq;
4909 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4910 "channel: %d MHz", freq);
4911 } else {
4912 os_get_random((u8 *) &r, sizeof(r));
4913 freq = 2412 + (r % 3) * 25;
4914 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4915 "channel: %d MHz", freq);
4916 }
4917 }
4918
4919 if (freq == 5) {
4920 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4921 "band");
4922 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004923 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004924 wpa_s->best_5_freq)) {
4925 freq = wpa_s->best_5_freq;
4926 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4927 "channel: %d MHz", freq);
4928 } else {
4929 os_get_random((u8 *) &r, sizeof(r));
4930 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004931 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004932 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4933 "5 GHz channel for P2P group");
4934 return -1;
4935 }
4936 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4937 "channel: %d MHz", freq);
4938 }
4939 }
4940
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004941 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004942 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4943 "(%u MHz) is not supported for P2P uses",
4944 freq);
4945 return -1;
4946 }
4947
4948 return freq;
4949}
4950
4951
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004952static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4953 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004954 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004955 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004956{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004957 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004958 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004959 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004960
4961 os_memset(params, 0, sizeof(*params));
4962 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004963 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004964 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004965 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004966 if (!freq_included(channels, freq)) {
4967 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4968 "accepted", freq);
4969 return -1;
4970 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4972 "frequency %d MHz", freq);
4973 params->freq = freq;
4974 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4975 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004976 wpa_s->conf->p2p_oper_channel <= 11 &&
4977 freq_included(channels,
4978 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004979 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4980 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4981 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004982 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4983 wpa_s->conf->p2p_oper_reg_class == 116 ||
4984 wpa_s->conf->p2p_oper_reg_class == 117 ||
4985 wpa_s->conf->p2p_oper_reg_class == 124 ||
4986 wpa_s->conf->p2p_oper_reg_class == 126 ||
4987 wpa_s->conf->p2p_oper_reg_class == 127) &&
4988 freq_included(channels,
4989 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004990 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4991 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4992 "frequency %d MHz", params->freq);
4993 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4994 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004995 p2p_supported_freq_go(wpa_s->global->p2p,
4996 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004997 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004998 params->freq = wpa_s->best_overall_freq;
4999 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5000 "channel %d MHz", params->freq);
5001 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5002 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005003 p2p_supported_freq_go(wpa_s->global->p2p,
5004 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005005 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005006 params->freq = wpa_s->best_24_freq;
5007 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5008 "channel %d MHz", params->freq);
5009 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5010 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005011 p2p_supported_freq_go(wpa_s->global->p2p,
5012 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005013 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005014 params->freq = wpa_s->best_5_freq;
5015 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5016 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07005017 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5018 channels))) {
5019 params->freq = pref_freq;
5020 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5021 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005022 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005023 int chan;
5024 for (chan = 0; chan < 11; chan++) {
5025 params->freq = 2412 + chan * 5;
5026 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005027 params->freq) &&
5028 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07005029 break;
5030 }
5031 if (chan == 11) {
5032 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5033 "allowed");
5034 return -1;
5035 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005036 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5037 "known)", params->freq);
5038 }
5039
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005040 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5041 if (!freqs)
5042 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005043
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005044 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5045 wpa_s->num_multichan_concurrent);
5046 if (res < 0) {
5047 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005048 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005050 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005051
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005052 for (i = 0; i < num; i++) {
5053 if (freq && freqs[i] == freq)
5054 break;
5055 if (!freq && freq_included(channels, freqs[i])) {
5056 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5057 freqs[i]);
5058 params->freq = freqs[i];
5059 break;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005060 }
5061 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08005062
5063 if (i == num) {
5064 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5065 if (freq)
5066 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5067 else
5068 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5069 os_free(freqs);
5070 return -1;
5071 } else if (num == 0) {
5072 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5073 } else {
5074 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5075 }
5076 }
5077
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005078 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005079 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005080}
5081
5082
5083static struct wpa_supplicant *
5084wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5085 int go)
5086{
5087 struct wpa_supplicant *group_wpa_s;
5088
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005089 if (!wpas_p2p_create_iface(wpa_s)) {
5090 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5091 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07005092 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005093 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005094 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005095
5096 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005097 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005098 wpa_msg_global(wpa_s, MSG_ERROR,
5099 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005100 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005101 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5103 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005104 wpa_msg_global(wpa_s, MSG_ERROR,
5105 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005106 wpas_p2p_remove_pending_group_interface(wpa_s);
5107 return NULL;
5108 }
5109
Dmitry Shmidtaa532512012-09-24 10:35:31 -07005110 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5111 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005112 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005113 return group_wpa_s;
5114}
5115
5116
5117/**
5118 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5119 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5120 * @persistent_group: Whether to create a persistent group
5121 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005122 * @ht40: Start GO with 40 MHz channel width
5123 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005124 * Returns: 0 on success, -1 on failure
5125 *
5126 * This function creates a new P2P group with the local end as the Group Owner,
5127 * i.e., without using Group Owner Negotiation.
5128 */
5129int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005130 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005131{
5132 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005133
5134 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5135 return -1;
5136
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005137 os_free(wpa_s->global->add_psk);
5138 wpa_s->global->add_psk = NULL;
5139
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005140 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005141 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005142 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07005143
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005144 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5145 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005146 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005148 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005149 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005150 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005151 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07005152 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5153 "(%u MHz) is not supported for P2P uses",
5154 params.freq);
5155 return -1;
5156 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157 p2p_go_params(wpa_s->global->p2p, &params);
5158 params.persistent_group = persistent_group;
5159
5160 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5161 if (wpa_s == NULL)
5162 return -1;
5163 wpas_start_wps_go(wpa_s, &params, 0);
5164
5165 return 0;
5166}
5167
5168
5169static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
Dmitry Shmidt15907092014-03-25 10:42:57 -07005170 struct wpa_ssid *params, int addr_allocated,
5171 int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005172{
5173 struct wpa_ssid *ssid;
5174
5175 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5176 if (wpa_s == NULL)
5177 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005178 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005179
5180 wpa_supplicant_ap_deinit(wpa_s);
5181
5182 ssid = wpa_config_add_network(wpa_s->conf);
5183 if (ssid == NULL)
5184 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005185 wpa_config_set_network_defaults(ssid);
5186 ssid->temporary = 1;
5187 ssid->proto = WPA_PROTO_RSN;
5188 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5189 ssid->group_cipher = WPA_CIPHER_CCMP;
5190 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5191 ssid->ssid = os_malloc(params->ssid_len);
5192 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005193 wpa_config_remove_network(wpa_s->conf, ssid->id);
5194 return -1;
5195 }
5196 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5197 ssid->ssid_len = params->ssid_len;
5198 ssid->p2p_group = 1;
5199 ssid->export_keys = 1;
5200 if (params->psk_set) {
5201 os_memcpy(ssid->psk, params->psk, 32);
5202 ssid->psk_set = 1;
5203 }
5204 if (params->passphrase)
5205 ssid->passphrase = os_strdup(params->passphrase);
5206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005207 wpa_s->show_group_started = 1;
Dmitry Shmidt15907092014-03-25 10:42:57 -07005208 wpa_s->p2p_in_invitation = 1;
5209 wpa_s->p2p_invite_go_freq = freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210
Dmitry Shmidt15907092014-03-25 10:42:57 -07005211 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5212 NULL);
5213 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5214 wpas_p2p_group_formation_timeout,
5215 wpa_s->parent, NULL);
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08005216 wpa_supplicant_select_network(wpa_s, ssid);
5217
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005218 return 0;
5219}
5220
5221
5222int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5223 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005224 int force_freq, int neg_freq, int ht40,
5225 int vht, const struct p2p_channels *channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07005226 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005227{
5228 struct p2p_go_neg_results params;
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005229 int go = 0, freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005230
5231 if (ssid->disabled != 2 || ssid->ssid == NULL)
5232 return -1;
5233
5234 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5235 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5236 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5237 "already running");
5238 return 0;
5239 }
5240
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07005241 os_free(wpa_s->global->add_psk);
5242 wpa_s->global->add_psk = NULL;
5243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005244 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005245 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005246
Dmitry Shmidt04949592012-07-19 12:16:46 -07005247 wpa_s->p2p_fallback_to_go_neg = 0;
5248
Dmitry Shmidt96be6222014-02-13 10:16:51 -08005249 if (force_freq > 0) {
5250 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5251 if (freq < 0)
5252 return -1;
5253 } else {
5254 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5255 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5256 freq = 0;
5257 }
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07005258
Dmitry Shmidt15907092014-03-25 10:42:57 -07005259 if (ssid->mode == WPAS_MODE_INFRA)
5260 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5261
5262 if (ssid->mode != WPAS_MODE_P2P_GO)
5263 return -1;
5264
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005265 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005266 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267
5268 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005269 params.psk_set = ssid->psk_set;
5270 if (params.psk_set)
5271 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005272 if (ssid->passphrase) {
5273 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5274 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5275 "persistent group");
5276 return -1;
5277 }
5278 os_strlcpy(params.passphrase, ssid->passphrase,
5279 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005280 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005281 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5282 params.ssid_len = ssid->ssid_len;
5283 params.persistent_group = 1;
5284
5285 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5286 if (wpa_s == NULL)
5287 return -1;
5288
Dmitry Shmidt56052862013-10-04 10:23:25 -07005289 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005290 wpas_start_wps_go(wpa_s, &params, 0);
5291
5292 return 0;
5293}
5294
5295
5296static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5297 struct wpabuf *proberesp_ies)
5298{
5299 struct wpa_supplicant *wpa_s = ctx;
5300 if (wpa_s->ap_iface) {
5301 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005302 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5303 wpabuf_free(beacon_ies);
5304 wpabuf_free(proberesp_ies);
5305 return;
5306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005307 if (beacon_ies) {
5308 wpabuf_free(hapd->p2p_beacon_ie);
5309 hapd->p2p_beacon_ie = beacon_ies;
5310 }
5311 wpabuf_free(hapd->p2p_probe_resp_ie);
5312 hapd->p2p_probe_resp_ie = proberesp_ies;
5313 } else {
5314 wpabuf_free(beacon_ies);
5315 wpabuf_free(proberesp_ies);
5316 }
5317 wpa_supplicant_ap_update_beacon(wpa_s);
5318}
5319
5320
5321static void wpas_p2p_idle_update(void *ctx, int idle)
5322{
5323 struct wpa_supplicant *wpa_s = ctx;
5324 if (!wpa_s->ap_iface)
5325 return;
5326 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005327 if (idle) {
5328 if (wpa_s->global->p2p_fail_on_wps_complete &&
5329 wpa_s->p2p_in_provisioning) {
5330 wpas_p2p_grpform_fail_after_wps(wpa_s);
5331 return;
5332 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005333 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005334 } else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005335 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5336}
5337
5338
5339struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005340 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005341{
5342 struct p2p_group *group;
5343 struct p2p_group_config *cfg;
5344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005345 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5346 return NULL;
5347
5348 cfg = os_zalloc(sizeof(*cfg));
5349 if (cfg == NULL)
5350 return NULL;
5351
Dmitry Shmidt04949592012-07-19 12:16:46 -07005352 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005353 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005354 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005355 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005356 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5357 if (wpa_s->max_stations &&
5358 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5359 cfg->max_clients = wpa_s->max_stations;
5360 else
5361 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005362 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5363 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005364 cfg->freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005365 cfg->cb_ctx = wpa_s;
5366 cfg->ie_update = wpas_p2p_ie_update;
5367 cfg->idle_update = wpas_p2p_idle_update;
5368
5369 group = p2p_group_init(wpa_s->global->p2p, cfg);
5370 if (group == NULL)
5371 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005372 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005373 p2p_group_notif_formation_done(group);
5374 wpa_s->p2p_group = group;
5375 return group;
5376}
5377
5378
5379void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5380 int registrar)
5381{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005382 struct wpa_ssid *ssid = wpa_s->current_ssid;
5383
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005384 if (!wpa_s->p2p_in_provisioning) {
5385 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5386 "provisioning not in progress");
5387 return;
5388 }
5389
Dmitry Shmidt04949592012-07-19 12:16:46 -07005390 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5391 u8 go_dev_addr[ETH_ALEN];
5392 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5393 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5394 ssid->ssid_len);
5395 /* Clear any stored provisioning info */
5396 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5397 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005398
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005399 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5400 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005401 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005402 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5403 /*
5404 * Use a separate timeout for initial data connection to
5405 * complete to allow the group to be removed automatically if
5406 * something goes wrong in this step before the P2P group idle
5407 * timeout mechanism is taken into use.
5408 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005409 wpa_dbg(wpa_s, MSG_DEBUG,
5410 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5411 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005412 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5413 wpas_p2p_group_formation_timeout,
5414 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005415 } else if (ssid) {
5416 /*
5417 * Use a separate timeout for initial data connection to
5418 * complete to allow the group to be removed automatically if
5419 * the client does not complete data connection successfully.
5420 */
5421 wpa_dbg(wpa_s, MSG_DEBUG,
5422 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5423 P2P_MAX_INITIAL_CONN_WAIT_GO);
5424 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5425 wpas_p2p_group_formation_timeout,
5426 wpa_s->parent, NULL);
5427 /*
5428 * Complete group formation on first successful data connection
5429 */
5430 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005431 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005432 if (wpa_s->global->p2p)
5433 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005434 wpas_group_formation_completed(wpa_s, 1);
5435}
5436
5437
Jouni Malinen75ecf522011-06-27 15:19:46 -07005438void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5439 struct wps_event_fail *fail)
5440{
5441 if (!wpa_s->p2p_in_provisioning) {
5442 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5443 "provisioning not in progress");
5444 return;
5445 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005446
5447 if (wpa_s->go_params) {
5448 p2p_clear_provisioning_info(
5449 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005450 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005451 }
5452
Jouni Malinen75ecf522011-06-27 15:19:46 -07005453 wpas_notify_p2p_wps_failed(wpa_s, fail);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005454
5455 if (wpa_s == wpa_s->global->p2p_group_formation) {
5456 /*
5457 * Allow some time for the failed WPS negotiation exchange to
5458 * complete, but remove the group since group formation cannot
5459 * succeed after provisioning failure.
5460 */
5461 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5462 wpa_s->global->p2p_fail_on_wps_complete = 1;
5463 eloop_deplete_timeout(0, 50000,
5464 wpas_p2p_group_formation_timeout,
5465 wpa_s->parent, NULL);
5466 }
5467}
5468
5469
5470int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5471{
5472 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5473 !wpa_s->p2p_in_provisioning)
5474 return 0;
5475
5476 wpas_p2p_grpform_fail_after_wps(wpa_s);
5477
5478 return 1;
Jouni Malinen75ecf522011-06-27 15:19:46 -07005479}
5480
5481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005482int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005483 const char *config_method,
5484 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005485{
5486 u16 config_methods;
5487
Dmitry Shmidt04949592012-07-19 12:16:46 -07005488 wpa_s->p2p_fallback_to_go_neg = 0;
5489 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005490 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005491 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005492 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005494 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5495 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005496 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005497 else {
5498 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005499 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005500 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005501
Dmitry Shmidt04949592012-07-19 12:16:46 -07005502 if (use == WPAS_P2P_PD_AUTO) {
5503 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5504 wpa_s->pending_pd_config_methods = config_methods;
5505 wpa_s->p2p_auto_pd = 1;
5506 wpa_s->p2p_auto_join = 0;
5507 wpa_s->pending_pd_before_join = 0;
5508 wpa_s->auto_pd_scan_retry = 0;
5509 wpas_p2p_stop_find(wpa_s);
5510 wpa_s->p2p_join_scan_count = 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005511 os_get_reltime(&wpa_s->p2p_auto_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005512 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5513 wpa_s->p2p_auto_started.sec,
5514 wpa_s->p2p_auto_started.usec);
5515 wpas_p2p_join_scan(wpa_s, NULL);
5516 return 0;
5517 }
5518
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005519 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5520 return -1;
5521
5522 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005523 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005524 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005525}
5526
5527
5528int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5529 char *end)
5530{
5531 return p2p_scan_result_text(ies, ies_len, buf, end);
5532}
5533
5534
5535static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5536{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005537 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005538 return;
5539
5540 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5541 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005542 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005543}
5544
5545
5546int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5547 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005548 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005549 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005550{
5551 wpas_p2p_clear_pending_action_tx(wpa_s);
5552 wpa_s->p2p_long_listen = 0;
5553
Dmitry Shmidt04949592012-07-19 12:16:46 -07005554 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5555 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005556 return -1;
5557
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005558 wpa_supplicant_cancel_sched_scan(wpa_s);
5559
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005560 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005561 num_req_dev_types, req_dev_types, dev_id,
5562 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005563}
5564
5565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005566static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005567{
5568 wpas_p2p_clear_pending_action_tx(wpa_s);
5569 wpa_s->p2p_long_listen = 0;
5570 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5571 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005572
5573 if (wpa_s->global->p2p)
5574 p2p_stop_find(wpa_s->global->p2p);
5575
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005576 return 0;
5577}
5578
5579
5580void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5581{
5582 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5583 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584 wpas_p2p_remove_pending_group_interface(wpa_s);
5585}
5586
5587
5588static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5589{
5590 struct wpa_supplicant *wpa_s = eloop_ctx;
5591 wpa_s->p2p_long_listen = 0;
5592}
5593
5594
5595int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5596{
5597 int res;
5598
5599 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5600 return -1;
5601
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005602 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005603 wpas_p2p_clear_pending_action_tx(wpa_s);
5604
5605 if (timeout == 0) {
5606 /*
5607 * This is a request for unlimited Listen state. However, at
5608 * least for now, this is mapped to a Listen state for one
5609 * hour.
5610 */
5611 timeout = 3600;
5612 }
5613 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5614 wpa_s->p2p_long_listen = 0;
5615
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005616 /*
5617 * Stop previous find/listen operation to avoid trying to request a new
5618 * remain-on-channel operation while the driver is still running the
5619 * previous one.
5620 */
5621 if (wpa_s->global->p2p)
5622 p2p_stop_find(wpa_s->global->p2p);
5623
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005624 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5625 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5626 wpa_s->p2p_long_listen = timeout * 1000;
5627 eloop_register_timeout(timeout, 0,
5628 wpas_p2p_long_listen_timeout,
5629 wpa_s, NULL);
5630 }
5631
5632 return res;
5633}
5634
5635
5636int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5637 u8 *buf, size_t len, int p2p_group)
5638{
5639 struct wpabuf *p2p_ie;
5640 int ret;
5641
5642 if (wpa_s->global->p2p_disabled)
5643 return -1;
5644 if (wpa_s->global->p2p == NULL)
5645 return -1;
5646 if (bss == NULL)
5647 return -1;
5648
5649 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5650 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5651 p2p_group, p2p_ie);
5652 wpabuf_free(p2p_ie);
5653
5654 return ret;
5655}
5656
5657
5658int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005659 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005660 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005661{
5662 if (wpa_s->global->p2p_disabled)
5663 return 0;
5664 if (wpa_s->global->p2p == NULL)
5665 return 0;
5666
Dmitry Shmidt04949592012-07-19 12:16:46 -07005667 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5668 ie, ie_len)) {
5669 case P2P_PREQ_NOT_P2P:
5670 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5671 ssi_signal);
5672 /* fall through */
5673 case P2P_PREQ_MALFORMED:
5674 case P2P_PREQ_NOT_LISTEN:
5675 case P2P_PREQ_NOT_PROCESSED:
5676 default: /* make gcc happy */
5677 return 0;
5678 case P2P_PREQ_PROCESSED:
5679 return 1;
5680 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005681}
5682
5683
5684void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5685 const u8 *sa, const u8 *bssid,
5686 u8 category, const u8 *data, size_t len, int freq)
5687{
5688 if (wpa_s->global->p2p_disabled)
5689 return;
5690 if (wpa_s->global->p2p == NULL)
5691 return;
5692
5693 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5694 freq);
5695}
5696
5697
5698void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5699{
5700 if (wpa_s->global->p2p_disabled)
5701 return;
5702 if (wpa_s->global->p2p == NULL)
5703 return;
5704
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005705 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005706}
5707
5708
5709void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5710{
5711 p2p_group_deinit(wpa_s->p2p_group);
5712 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005713
5714 wpa_s->ap_configured_cb = NULL;
5715 wpa_s->ap_configured_cb_ctx = NULL;
5716 wpa_s->ap_configured_cb_data = NULL;
5717 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005718}
5719
5720
5721int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5722{
5723 wpa_s->p2p_long_listen = 0;
5724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005725 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5726 return -1;
5727
5728 return p2p_reject(wpa_s->global->p2p, addr);
5729}
5730
5731
5732/* Invite to reinvoke a persistent group */
5733int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005734 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005735 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005736{
5737 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005738 u8 *bssid = NULL;
5739 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005740 int res;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005741 int no_pref_freq_given = pref_freq == 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005742
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005743 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005744 if (peer_addr)
5745 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5746 else
5747 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005748
Jouni Malinen31be0a42012-08-31 21:20:51 +03005749 wpa_s->p2p_persistent_go_freq = freq;
5750 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005751 if (ssid->mode == WPAS_MODE_P2P_GO) {
5752 role = P2P_INVITE_ROLE_GO;
5753 if (peer_addr == NULL) {
5754 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5755 "address in invitation command");
5756 return -1;
5757 }
5758 if (wpas_p2p_create_iface(wpa_s)) {
5759 if (wpas_p2p_add_group_interface(wpa_s,
5760 WPA_IF_P2P_GO) < 0) {
5761 wpa_printf(MSG_ERROR, "P2P: Failed to "
5762 "allocate a new interface for the "
5763 "group");
5764 return -1;
5765 }
5766 bssid = wpa_s->pending_interface_addr;
5767 } else
5768 bssid = wpa_s->own_addr;
5769 } else {
5770 role = P2P_INVITE_ROLE_CLIENT;
5771 peer_addr = ssid->bssid;
5772 }
5773 wpa_s->pending_invite_ssid_id = ssid->id;
5774
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005775 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5776 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005777 if (res)
5778 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005780 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5781 return -1;
5782
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08005783 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5784 no_pref_freq_given && pref_freq > 0 &&
5785 wpa_s->num_multichan_concurrent > 1 &&
5786 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5787 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5788 pref_freq);
5789 pref_freq = 0;
5790 }
5791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005792 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005793 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005794 1, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005795}
5796
5797
5798/* Invite to join an active group */
5799int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5800 const u8 *peer_addr, const u8 *go_dev_addr)
5801{
5802 struct wpa_global *global = wpa_s->global;
5803 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005804 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005805 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005806 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005807 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005808 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005809
Jouni Malinen31be0a42012-08-31 21:20:51 +03005810 wpa_s->p2p_persistent_go_freq = 0;
5811 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005812 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005814 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5815 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5816 break;
5817 }
5818 if (wpa_s == NULL) {
5819 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5820 return -1;
5821 }
5822
5823 ssid = wpa_s->current_ssid;
5824 if (ssid == NULL) {
5825 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5826 "invitation");
5827 return -1;
5828 }
5829
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005830 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005831 persistent = ssid->p2p_persistent_group &&
5832 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5833 ssid->ssid, ssid->ssid_len);
5834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005835 if (ssid->mode == WPAS_MODE_P2P_GO) {
5836 role = P2P_INVITE_ROLE_ACTIVE_GO;
5837 bssid = wpa_s->own_addr;
5838 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005839 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005840 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005841 } else {
5842 role = P2P_INVITE_ROLE_CLIENT;
5843 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5844 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5845 "invite to current group");
5846 return -1;
5847 }
5848 bssid = wpa_s->bssid;
5849 if (go_dev_addr == NULL &&
5850 !is_zero_ether_addr(wpa_s->go_dev_addr))
5851 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005852 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5853 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005854 }
5855 wpa_s->parent->pending_invite_ssid_id = -1;
5856
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005857 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5858 return -1;
5859
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005860 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5861 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005862 if (res)
5863 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005864 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005865
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005866 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005867 ssid->ssid, ssid->ssid_len, force_freq,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005868 go_dev_addr, persistent, pref_freq, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005869}
5870
5871
5872void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5873{
5874 struct wpa_ssid *ssid = wpa_s->current_ssid;
5875 const char *ssid_txt;
5876 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005877 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005878 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005879 int freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005880 u8 ip[3 * 4];
5881 char ip_addr[100];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005882
Dmitry Shmidt04949592012-07-19 12:16:46 -07005883 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5884 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5885 wpa_s->parent, NULL);
5886 }
5887
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005888 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005889 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005890
5891 wpa_s->show_group_started = 0;
5892
5893 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5894 os_memset(go_dev_addr, 0, ETH_ALEN);
5895 if (ssid->bssid_set)
5896 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5897 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5898 ssid->ssid_len);
5899 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5900
5901 if (wpa_s->global->p2p_group_formation == wpa_s)
5902 wpa_s->global->p2p_group_formation = NULL;
5903
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005904 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5905 (int) wpa_s->assoc_freq;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005906
5907 ip_addr[0] = '\0';
5908 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5909 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5910 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5911 ip[0], ip[1], ip[2], ip[3],
5912 ip[4], ip[5], ip[6], ip[7],
5913 ip[8], ip[9], ip[10], ip[11]);
5914 }
5915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005916 if (ssid->passphrase == NULL && ssid->psk_set) {
5917 char psk[65];
5918 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005919 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5920 "%s client ssid=\"%s\" freq=%d psk=%s "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005921 "go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005922 wpa_s->ifname, ssid_txt, freq, psk,
5923 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005924 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005925 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005926 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5927 "%s client ssid=\"%s\" freq=%d "
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005928 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005929 wpa_s->ifname, ssid_txt, freq,
5930 ssid->passphrase ? ssid->passphrase : "",
5931 MAC2STR(go_dev_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005932 persistent ? " [PERSISTENT]" : "", ip_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005933 }
5934
5935 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005936 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5937 ssid, go_dev_addr);
5938 if (network_id < 0)
5939 network_id = ssid->id;
5940 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005941}
5942
5943
5944int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5945 u32 interval1, u32 duration2, u32 interval2)
5946{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005947 int ret;
5948
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005949 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5950 return -1;
5951
5952 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5953 wpa_s->current_ssid == NULL ||
5954 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5955 return -1;
5956
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005957 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5958 wpa_s->own_addr, wpa_s->assoc_freq,
5959 duration1, interval1, duration2, interval2);
5960 if (ret == 0)
5961 wpa_s->waiting_presence_resp = 1;
5962
5963 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005964}
5965
5966
5967int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5968 unsigned int interval)
5969{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005970 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5971 return -1;
5972
5973 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5974}
5975
5976
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005977static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5978{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005979 if (wpa_s->current_ssid == NULL) {
5980 /*
5981 * current_ssid can be cleared when P2P client interface gets
5982 * disconnected, so assume this interface was used as P2P
5983 * client.
5984 */
5985 return 1;
5986 }
5987 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005988 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5989}
5990
5991
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005992static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5993{
5994 struct wpa_supplicant *wpa_s = eloop_ctx;
5995
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005996 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5998 "disabled");
5999 return;
6000 }
6001
Dmitry Shmidt04949592012-07-19 12:16:46 -07006002 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6003 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006004 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006005}
6006
6007
6008static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6009{
Dmitry Shmidt04949592012-07-19 12:16:46 -07006010 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006011
Dmitry Shmidt04949592012-07-19 12:16:46 -07006012 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6013 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6014
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006015 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6016 return;
6017
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006018 timeout = wpa_s->conf->p2p_group_idle;
6019 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6020 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6021 timeout = P2P_MAX_CLIENT_IDLE;
6022
6023 if (timeout == 0)
6024 return;
6025
Dmitry Shmidt04949592012-07-19 12:16:46 -07006026 if (timeout < 0) {
6027 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6028 timeout = 0; /* special client mode no-timeout */
6029 else
6030 return;
6031 }
6032
6033 if (wpa_s->p2p_in_provisioning) {
6034 /*
6035 * Use the normal group formation timeout during the
6036 * provisioning phase to avoid terminating this process too
6037 * early due to group idle timeout.
6038 */
6039 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6040 "during provisioning");
6041 return;
6042 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05306043
Dmitry Shmidt04949592012-07-19 12:16:46 -07006044 if (wpa_s->show_group_started) {
6045 /*
6046 * Use the normal group formation timeout between the end of
6047 * the provisioning phase and completion of 4-way handshake to
6048 * avoid terminating this process too early due to group idle
6049 * timeout.
6050 */
6051 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6052 "while waiting for initial 4-way handshake to "
6053 "complete");
6054 return;
6055 }
6056
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006057 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006058 timeout);
6059 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6060 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061}
6062
6063
Jouni Malinen2b89da82012-08-31 22:04:41 +03006064/* Returns 1 if the interface was removed */
6065int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6066 u16 reason_code, const u8 *ie, size_t ie_len,
6067 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006068{
6069 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03006070 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006071
Dmitry Shmidt04949592012-07-19 12:16:46 -07006072 if (!locally_generated)
6073 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6074 ie_len);
6075
6076 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6077 wpa_s->current_ssid &&
6078 wpa_s->current_ssid->p2p_group &&
6079 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6080 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6081 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03006082 if (wpas_p2p_group_delete(wpa_s,
6083 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6084 > 0)
6085 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006086 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03006087
6088 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006089}
6090
6091
6092void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07006093 u16 reason_code, const u8 *ie, size_t ie_len,
6094 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006095{
6096 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6097 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006098
Dmitry Shmidt04949592012-07-19 12:16:46 -07006099 if (!locally_generated)
6100 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6101 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006102}
6103
6104
6105void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6106{
6107 struct p2p_data *p2p = wpa_s->global->p2p;
6108
6109 if (p2p == NULL)
6110 return;
6111
6112 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6113 return;
6114
6115 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6116 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6117
6118 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6119 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6120
6121 if (wpa_s->wps &&
6122 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6123 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6124
6125 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6126 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6127
6128 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6129 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6130 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6131 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6132 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6133 }
6134
6135 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6136 p2p_set_sec_dev_types(p2p,
6137 (void *) wpa_s->conf->sec_device_type,
6138 wpa_s->conf->num_sec_device_types);
6139
6140 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6141 int i;
6142 p2p_remove_wps_vendor_extensions(p2p);
6143 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6144 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6145 continue;
6146 p2p_add_wps_vendor_extension(
6147 p2p, wpa_s->conf->wps_vendor_ext[i]);
6148 }
6149 }
6150
6151 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6152 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6153 char country[3];
6154 country[0] = wpa_s->conf->country[0];
6155 country[1] = wpa_s->conf->country[1];
6156 country[2] = 0x04;
6157 p2p_set_country(p2p, country);
6158 }
6159
6160 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6161 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6162 wpa_s->conf->p2p_ssid_postfix ?
6163 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6164 0);
6165 }
6166
6167 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6168 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07006169
6170 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6171 u8 reg_class, channel;
6172 int ret;
6173 unsigned int r;
6174 if (wpa_s->conf->p2p_listen_reg_class &&
6175 wpa_s->conf->p2p_listen_channel) {
6176 reg_class = wpa_s->conf->p2p_listen_reg_class;
6177 channel = wpa_s->conf->p2p_listen_channel;
6178 } else {
6179 reg_class = 81;
6180 /*
6181 * Pick one of the social channels randomly as the
6182 * listen channel.
6183 */
6184 os_get_random((u8 *) &r, sizeof(r));
6185 channel = 1 + (r % 3) * 5;
6186 }
6187 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6188 if (ret)
6189 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6190 "failed: %d", ret);
6191 }
6192 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6193 u8 op_reg_class, op_channel, cfg_op_channel;
6194 int ret = 0;
6195 unsigned int r;
6196 if (wpa_s->conf->p2p_oper_reg_class &&
6197 wpa_s->conf->p2p_oper_channel) {
6198 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6199 op_channel = wpa_s->conf->p2p_oper_channel;
6200 cfg_op_channel = 1;
6201 } else {
6202 op_reg_class = 81;
6203 /*
6204 * Use random operation channel from (1, 6, 11)
6205 *if no other preference is indicated.
6206 */
6207 os_get_random((u8 *) &r, sizeof(r));
6208 op_channel = 1 + (r % 3) * 5;
6209 cfg_op_channel = 0;
6210 }
6211 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6212 cfg_op_channel);
6213 if (ret)
6214 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6215 "failed: %d", ret);
6216 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006217
6218 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6219 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6220 wpa_s->conf->p2p_pref_chan) < 0) {
6221 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6222 "update failed");
6223 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006224
6225 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6226 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6227 "update failed");
6228 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07006229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006230}
6231
6232
6233int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6234 int duration)
6235{
6236 if (!wpa_s->ap_iface)
6237 return -1;
6238 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6239 duration);
6240}
6241
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006242
6243int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6244{
6245 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6246 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006247
6248 wpa_s->global->cross_connection = enabled;
6249 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6250
6251 if (!enabled) {
6252 struct wpa_supplicant *iface;
6253
6254 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6255 {
6256 if (iface->cross_connect_enabled == 0)
6257 continue;
6258
6259 iface->cross_connect_enabled = 0;
6260 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006261 wpa_msg_global(iface->parent, MSG_INFO,
6262 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6263 iface->ifname,
6264 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006265 }
6266 }
6267
6268 return 0;
6269}
6270
6271
6272static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6273{
6274 struct wpa_supplicant *iface;
6275
6276 if (!uplink->global->cross_connection)
6277 return;
6278
6279 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6280 if (!iface->cross_connect_enabled)
6281 continue;
6282 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6283 0)
6284 continue;
6285 if (iface->ap_iface == NULL)
6286 continue;
6287 if (iface->cross_connect_in_use)
6288 continue;
6289
6290 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006291 wpa_msg_global(iface->parent, MSG_INFO,
6292 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6293 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006294 }
6295}
6296
6297
6298static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6299{
6300 struct wpa_supplicant *iface;
6301
6302 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6303 if (!iface->cross_connect_enabled)
6304 continue;
6305 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6306 0)
6307 continue;
6308 if (!iface->cross_connect_in_use)
6309 continue;
6310
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006311 wpa_msg_global(iface->parent, MSG_INFO,
6312 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6313 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006314 iface->cross_connect_in_use = 0;
6315 }
6316}
6317
6318
6319void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6320{
6321 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6322 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6323 wpa_s->cross_connect_disallowed)
6324 wpas_p2p_disable_cross_connect(wpa_s);
6325 else
6326 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006327 if (!wpa_s->ap_iface &&
6328 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6329 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006330}
6331
6332
6333void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6334{
6335 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006336 if (!wpa_s->ap_iface &&
6337 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6338 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006339 wpas_p2p_set_group_idle_timeout(wpa_s);
6340}
6341
6342
6343static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6344{
6345 struct wpa_supplicant *iface;
6346
6347 if (!wpa_s->global->cross_connection)
6348 return;
6349
6350 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6351 if (iface == wpa_s)
6352 continue;
6353 if (iface->drv_flags &
6354 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6355 continue;
6356 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6357 continue;
6358
6359 wpa_s->cross_connect_enabled = 1;
6360 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6361 sizeof(wpa_s->cross_connect_uplink));
6362 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6363 "%s to %s whenever uplink is available",
6364 wpa_s->ifname, wpa_s->cross_connect_uplink);
6365
6366 if (iface->ap_iface || iface->current_ssid == NULL ||
6367 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6368 iface->cross_connect_disallowed ||
6369 iface->wpa_state != WPA_COMPLETED)
6370 break;
6371
6372 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07006373 wpa_msg_global(wpa_s->parent, MSG_INFO,
6374 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6375 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006376 break;
6377 }
6378}
6379
6380
6381int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6382{
6383 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6384 !wpa_s->p2p_in_provisioning)
6385 return 0; /* not P2P client operation */
6386
6387 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6388 "session overlap");
6389 if (wpa_s != wpa_s->parent)
6390 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07006391 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006392 return 1;
6393}
6394
6395
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07006396void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6397{
6398 struct wpa_supplicant *wpa_s = eloop_ctx;
6399 wpas_p2p_notif_pbc_overlap(wpa_s);
6400}
6401
6402
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006403void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6404{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006405 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006406
6407 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6408 return;
6409
6410 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006411 os_memset(&cli_chan, 0, sizeof(cli_chan));
6412 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006413 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6414 "channel list");
6415 return;
6416 }
6417
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006418 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006419}
6420
6421
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006422static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6423 struct wpa_scan_results *scan_res)
6424{
6425 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6426}
6427
6428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006429int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6430{
6431 struct wpa_global *global = wpa_s->global;
6432 int found = 0;
6433 const u8 *peer;
6434
6435 if (global->p2p == NULL)
6436 return -1;
6437
6438 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6439
6440 if (wpa_s->pending_interface_name[0] &&
6441 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6442 found = 1;
6443
6444 peer = p2p_get_go_neg_peer(global->p2p);
6445 if (peer) {
6446 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6447 MACSTR, MAC2STR(peer));
6448 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006449 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006450 }
6451
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006452 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6453 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6454 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6455 found = 1;
6456 }
6457
6458 if (wpa_s->pending_pd_before_join) {
6459 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6460 wpa_s->pending_pd_before_join = 0;
6461 found = 1;
6462 }
6463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006464 wpas_p2p_stop_find(wpa_s);
6465
6466 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6467 if (wpa_s == global->p2p_group_formation &&
6468 (wpa_s->p2p_in_provisioning ||
6469 wpa_s->parent->pending_interface_type ==
6470 WPA_IF_P2P_CLIENT)) {
6471 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6472 "formation found - cancelling",
6473 wpa_s->ifname);
6474 found = 1;
6475 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6476 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006477 if (wpa_s->p2p_in_provisioning) {
6478 wpas_group_formation_completed(wpa_s, 0);
6479 break;
6480 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006481 wpas_p2p_group_delete(wpa_s,
6482 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006483 break;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006484 } else if (wpa_s->p2p_in_invitation) {
6485 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6486 wpa_s->ifname);
6487 found = 1;
6488 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006489 }
6490 }
6491
6492 if (!found) {
6493 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6494 return -1;
6495 }
6496
6497 return 0;
6498}
6499
6500
6501void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6502{
6503 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6504 return;
6505
6506 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6507 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006508 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006509}
6510
6511
6512void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6513 int freq_24, int freq_5, int freq_overall)
6514{
6515 struct p2p_data *p2p = wpa_s->global->p2p;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006516 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006517 return;
6518 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6519}
6520
6521
6522int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6523{
6524 u8 peer[ETH_ALEN];
6525 struct p2p_data *p2p = wpa_s->global->p2p;
6526
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006527 if (p2p == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006528 return -1;
6529
6530 if (hwaddr_aton(addr, peer))
6531 return -1;
6532
6533 return p2p_unauthorize(p2p, peer);
6534}
6535
6536
6537/**
6538 * wpas_p2p_disconnect - Disconnect from a P2P Group
6539 * @wpa_s: Pointer to wpa_supplicant data
6540 * Returns: 0 on success, -1 on failure
6541 *
6542 * This can be used to disconnect from a group in which the local end is a P2P
6543 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6544 * virtual network interface was created for this group, that interface will be
6545 * removed. Otherwise, only the configured P2P group network will be removed
6546 * from the interface.
6547 */
6548int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6549{
6550
6551 if (wpa_s == NULL)
6552 return -1;
6553
Jouni Malinen2b89da82012-08-31 22:04:41 +03006554 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6555 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006556}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006557
6558
6559int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6560{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006561 int ret;
6562
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006563 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6564 return 0;
6565
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006566 ret = p2p_in_progress(wpa_s->global->p2p);
6567 if (ret == 0) {
6568 /*
6569 * Check whether there is an ongoing WPS provisioning step (or
6570 * other parts of group formation) on another interface since
6571 * p2p_in_progress() does not report this to avoid issues for
6572 * scans during such provisioning step.
6573 */
6574 if (wpa_s->global->p2p_group_formation &&
6575 wpa_s->global->p2p_group_formation != wpa_s) {
6576 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6577 "in group formation",
6578 wpa_s->global->p2p_group_formation->ifname);
6579 ret = 1;
6580 }
6581 }
6582
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006583 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08006584 struct os_reltime now;
6585 os_get_reltime(&now);
6586 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6587 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006588 /* Wait for the first client has expired */
6589 wpa_s->global->p2p_go_wait_client.sec = 0;
6590 } else {
6591 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6592 ret = 1;
6593 }
6594 }
6595
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006596 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006597}
6598
6599
6600void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6601 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006602{
6603 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6604 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6605 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006606 /**
6607 * Remove the network by scheduling the group formation
6608 * timeout to happen immediately. The teardown code
6609 * needs to be scheduled to run asynch later so that we
6610 * don't delete data from under ourselves unexpectedly.
6611 * Calling wpas_p2p_group_formation_timeout directly
6612 * causes a series of crashes in WPS failure scenarios.
6613 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006614 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6615 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006616 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6617 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006618 }
6619}
6620
6621
6622struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006623 const u8 *addr, const u8 *ssid,
6624 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006625{
6626 struct wpa_ssid *s;
6627 size_t i;
6628
6629 for (s = wpa_s->conf->ssid; s; s = s->next) {
6630 if (s->disabled != 2)
6631 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006632 if (ssid &&
6633 (ssid_len != s->ssid_len ||
6634 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6635 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006636 if (addr == NULL) {
6637 if (s->mode == WPAS_MODE_P2P_GO)
6638 return s;
6639 continue;
6640 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006641 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6642 return s; /* peer is GO in the persistent group */
6643 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6644 continue;
6645 for (i = 0; i < s->num_p2p_clients; i++) {
6646 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6647 addr, ETH_ALEN) == 0)
6648 return s; /* peer is P2P client in persistent
6649 * group */
6650 }
6651 }
6652
6653 return NULL;
6654}
6655
6656
6657void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6658 const u8 *addr)
6659{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006660 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6661 wpa_s->parent, NULL) > 0) {
6662 /*
6663 * This can happen if WPS provisioning step is not terminated
6664 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6665 * peer was able to connect, there is no need to time out group
6666 * formation after this, though. In addition, this is used with
6667 * the initial connection wait on the GO as a separate formation
6668 * timeout and as such, expected to be hit after the initial WPS
6669 * provisioning step.
6670 */
6671 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6672 }
6673 if (!wpa_s->p2p_go_group_formation_completed) {
6674 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6675 wpa_s->p2p_go_group_formation_completed = 1;
6676 wpa_s->global->p2p_group_formation = NULL;
6677 wpa_s->p2p_in_provisioning = 0;
Dmitry Shmidt21de2142014-04-08 10:50:52 -07006678 wpa_s->p2p_in_invitation = 0;
Dmitry Shmidt56052862013-10-04 10:23:25 -07006679 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006680 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006681 if (addr == NULL)
6682 return;
6683 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6684}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006685
Dmitry Shmidt04949592012-07-19 12:16:46 -07006686
6687static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6688 int group_added)
6689{
6690 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006691 if (wpa_s->global->p2p_group_formation)
6692 group = wpa_s->global->p2p_group_formation;
6693 wpa_s = wpa_s->parent;
6694 offchannel_send_action_done(wpa_s);
6695 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006696 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006697 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6698 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6699 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6700 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6701 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006702 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006703 wpa_s->p2p_go_ht40,
6704 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006705}
6706
6707
6708int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6709{
6710 if (!wpa_s->p2p_fallback_to_go_neg ||
6711 wpa_s->p2p_in_provisioning <= 5)
6712 return 0;
6713
6714 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6715 return 0; /* peer operating as a GO */
6716
6717 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6718 "fallback to GO Negotiation");
6719 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6720
6721 return 1;
6722}
6723
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006724
6725unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6726{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006727 struct wpa_supplicant *ifs;
6728
6729 if (wpa_s->wpa_state > WPA_SCANNING) {
6730 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6731 "concurrent operation",
6732 P2P_CONCURRENT_SEARCH_DELAY);
6733 return P2P_CONCURRENT_SEARCH_DELAY;
6734 }
6735
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08006736 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6737 radio_list) {
6738 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006739 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6740 "delay due to concurrent operation on "
6741 "interface %s",
6742 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6743 return P2P_CONCURRENT_SEARCH_DELAY;
6744 }
6745 }
6746
6747 return 0;
6748}
6749
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006750
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006751static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6752 struct wpa_ssid *s, const u8 *addr,
6753 int iface_addr)
6754{
6755 struct psk_list_entry *psk, *tmp;
6756 int changed = 0;
6757
6758 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6759 list) {
6760 if ((iface_addr && !psk->p2p &&
6761 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6762 (!iface_addr && psk->p2p &&
6763 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6764 wpa_dbg(wpa_s, MSG_DEBUG,
6765 "P2P: Remove persistent group PSK list entry for "
6766 MACSTR " p2p=%u",
6767 MAC2STR(psk->addr), psk->p2p);
6768 dl_list_del(&psk->list);
6769 os_free(psk);
6770 changed++;
6771 }
6772 }
6773
6774 return changed;
6775}
6776
6777
6778void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6779 const u8 *p2p_dev_addr,
6780 const u8 *psk, size_t psk_len)
6781{
6782 struct wpa_ssid *ssid = wpa_s->current_ssid;
6783 struct wpa_ssid *persistent;
6784 struct psk_list_entry *p;
6785
6786 if (psk_len != sizeof(p->psk))
6787 return;
6788
6789 if (p2p_dev_addr) {
6790 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6791 " p2p_dev_addr=" MACSTR,
6792 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6793 if (is_zero_ether_addr(p2p_dev_addr))
6794 p2p_dev_addr = NULL;
6795 } else {
6796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6797 MAC2STR(mac_addr));
6798 }
6799
6800 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6801 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6802 /* To be added to persistent group once created */
6803 if (wpa_s->global->add_psk == NULL) {
6804 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6805 if (wpa_s->global->add_psk == NULL)
6806 return;
6807 }
6808 p = wpa_s->global->add_psk;
6809 if (p2p_dev_addr) {
6810 p->p2p = 1;
6811 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6812 } else {
6813 p->p2p = 0;
6814 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6815 }
6816 os_memcpy(p->psk, psk, psk_len);
6817 return;
6818 }
6819
6820 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6821 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6822 return;
6823 }
6824
6825 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6826 ssid->ssid_len);
6827 if (!persistent) {
6828 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6829 return;
6830 }
6831
6832 p = os_zalloc(sizeof(*p));
6833 if (p == NULL)
6834 return;
6835 if (p2p_dev_addr) {
6836 p->p2p = 1;
6837 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6838 } else {
6839 p->p2p = 0;
6840 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6841 }
6842 os_memcpy(p->psk, psk, psk_len);
6843
6844 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6845 struct psk_list_entry *last;
6846 last = dl_list_last(&persistent->psk_list,
6847 struct psk_list_entry, list);
6848 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6849 MACSTR " (p2p=%u) to make room for a new one",
6850 MAC2STR(last->addr), last->p2p);
6851 dl_list_del(&last->list);
6852 os_free(last);
6853 }
6854
6855 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6856 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6857 p2p_dev_addr == NULL);
6858 if (p2p_dev_addr) {
6859 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6860 MACSTR, MAC2STR(p2p_dev_addr));
6861 } else {
6862 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6863 MAC2STR(mac_addr));
6864 }
6865 dl_list_add(&persistent->psk_list, &p->list);
6866
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006867 if (wpa_s->parent->conf->update_config &&
6868 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6869 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006870}
6871
6872
6873static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6874 struct wpa_ssid *s, const u8 *addr,
6875 int iface_addr)
6876{
6877 int res;
6878
6879 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08006880 if (res > 0 && wpa_s->conf->update_config &&
6881 wpa_config_write(wpa_s->confname, wpa_s->conf))
6882 wpa_dbg(wpa_s, MSG_DEBUG,
6883 "P2P: Failed to update configuration");
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006884}
6885
6886
6887static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6888 const u8 *peer, int iface_addr)
6889{
6890 struct hostapd_data *hapd;
6891 struct hostapd_wpa_psk *psk, *prev, *rem;
6892 struct sta_info *sta;
6893
6894 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6895 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6896 return;
6897
6898 /* Remove per-station PSK entry */
6899 hapd = wpa_s->ap_iface->bss[0];
6900 prev = NULL;
6901 psk = hapd->conf->ssid.wpa_psk;
6902 while (psk) {
6903 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6904 (!iface_addr &&
6905 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6906 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6907 MACSTR " iface_addr=%d",
6908 MAC2STR(peer), iface_addr);
6909 if (prev)
6910 prev->next = psk->next;
6911 else
6912 hapd->conf->ssid.wpa_psk = psk->next;
6913 rem = psk;
6914 psk = psk->next;
6915 os_free(rem);
6916 } else {
6917 prev = psk;
6918 psk = psk->next;
6919 }
6920 }
6921
6922 /* Disconnect from group */
6923 if (iface_addr)
6924 sta = ap_get_sta(hapd, peer);
6925 else
6926 sta = ap_get_sta_p2p(hapd, peer);
6927 if (sta) {
6928 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6929 " (iface_addr=%d) from group",
6930 MAC2STR(peer), iface_addr);
6931 hostapd_drv_sta_deauth(hapd, sta->addr,
6932 WLAN_REASON_DEAUTH_LEAVING);
6933 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6934 }
6935}
6936
6937
6938void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6939 int iface_addr)
6940{
6941 struct wpa_ssid *s;
6942 struct wpa_supplicant *w;
6943
6944 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6945
6946 /* Remove from any persistent group */
6947 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6948 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6949 continue;
6950 if (!iface_addr)
6951 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6952 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6953 }
6954
6955 /* Remove from any operating group */
6956 for (w = wpa_s->global->ifaces; w; w = w->next)
6957 wpas_p2p_remove_client_go(w, peer, iface_addr);
6958}
6959
6960
6961static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6962{
6963 struct wpa_supplicant *wpa_s = eloop_ctx;
6964 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6965}
6966
6967
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08006968static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6969{
6970 struct wpa_supplicant *wpa_s = eloop_ctx;
6971
6972 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6973 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6974}
6975
6976
6977int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6978 struct wpa_ssid *ssid)
6979{
6980 struct wpa_supplicant *iface;
6981
6982 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6983 if (!iface->current_ssid ||
6984 iface->current_ssid->frequency == freq ||
6985 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6986 !iface->current_ssid->p2p_group))
6987 continue;
6988
6989 /* Remove the connection with least priority */
6990 if (!wpas_is_p2p_prioritized(iface)) {
6991 /* STA connection has priority over existing
6992 * P2P connection, so remove the interface. */
6993 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6994 eloop_register_timeout(0, 0,
6995 wpas_p2p_group_freq_conflict,
6996 iface, NULL);
6997 /* If connection in progress is P2P connection, do not
6998 * proceed for the connection. */
6999 if (wpa_s == iface)
7000 return -1;
7001 else
7002 return 0;
7003 } else {
7004 /* P2P connection has priority, disable the STA network
7005 */
7006 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7007 ssid);
7008 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7009 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7010 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7011 ETH_ALEN);
7012 /* If P2P connection is in progress, continue
7013 * connecting...*/
7014 if (wpa_s == iface)
7015 return 0;
7016 else
7017 return -1;
7018 }
7019 }
7020
7021 return 0;
7022}
7023
7024
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07007025int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7026{
7027 struct wpa_ssid *ssid = wpa_s->current_ssid;
7028
7029 if (ssid == NULL || !ssid->p2p_group)
7030 return 0;
7031
7032 if (wpa_s->p2p_last_4way_hs_fail &&
7033 wpa_s->p2p_last_4way_hs_fail == ssid) {
7034 u8 go_dev_addr[ETH_ALEN];
7035 struct wpa_ssid *persistent;
7036
7037 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7038 ssid->ssid,
7039 ssid->ssid_len) <= 0) {
7040 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7041 goto disconnect;
7042 }
7043
7044 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7045 MACSTR, MAC2STR(go_dev_addr));
7046 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7047 ssid->ssid,
7048 ssid->ssid_len);
7049 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7050 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7051 goto disconnect;
7052 }
7053 wpa_msg_global(wpa_s->parent, MSG_INFO,
7054 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7055 persistent->id);
7056 disconnect:
7057 wpa_s->p2p_last_4way_hs_fail = NULL;
7058 /*
7059 * Remove the group from a timeout to avoid issues with caller
7060 * continuing to use the interface if this is on a P2P group
7061 * interface.
7062 */
7063 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7064 wpa_s, NULL);
7065 return 1;
7066 }
7067
7068 wpa_s->p2p_last_4way_hs_fail = ssid;
7069 return 0;
7070}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007071
7072
7073#ifdef CONFIG_WPS_NFC
7074
7075static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7076 struct wpabuf *p2p)
7077{
7078 struct wpabuf *ret;
7079 size_t wsc_len;
7080
7081 if (p2p == NULL) {
7082 wpabuf_free(wsc);
7083 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7084 return NULL;
7085 }
7086
7087 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7088 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7089 if (ret == NULL) {
7090 wpabuf_free(wsc);
7091 wpabuf_free(p2p);
7092 return NULL;
7093 }
7094
7095 wpabuf_put_be16(ret, wsc_len);
7096 if (wsc)
7097 wpabuf_put_buf(ret, wsc);
7098 wpabuf_put_be16(ret, wpabuf_len(p2p));
7099 wpabuf_put_buf(ret, p2p);
7100
7101 wpabuf_free(wsc);
7102 wpabuf_free(p2p);
7103 wpa_hexdump_buf(MSG_DEBUG,
7104 "P2P: Generated NFC connection handover message", ret);
7105
7106 if (ndef && ret) {
7107 struct wpabuf *tmp;
7108 tmp = ndef_build_p2p(ret);
7109 wpabuf_free(ret);
7110 if (tmp == NULL) {
7111 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7112 return NULL;
7113 }
7114 ret = tmp;
7115 }
7116
7117 return ret;
7118}
7119
7120
7121static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7122 struct wpa_ssid **ssid, u8 *go_dev_addr)
7123{
7124 struct wpa_supplicant *iface;
7125
7126 if (go_dev_addr)
7127 os_memset(go_dev_addr, 0, ETH_ALEN);
7128 if (ssid)
7129 *ssid = NULL;
7130 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7131 if (iface->wpa_state < WPA_ASSOCIATING ||
7132 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7133 !iface->current_ssid->p2p_group ||
7134 iface->current_ssid->mode != WPAS_MODE_INFRA)
7135 continue;
7136 if (ssid)
7137 *ssid = iface->current_ssid;
7138 if (go_dev_addr)
7139 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7140 return iface->assoc_freq;
7141 }
7142 return 0;
7143}
7144
7145
7146struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7147 int ndef)
7148{
7149 struct wpabuf *wsc, *p2p;
7150 struct wpa_ssid *ssid;
7151 u8 go_dev_addr[ETH_ALEN];
7152 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7153
7154 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7155 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7156 return NULL;
7157 }
7158
7159 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7160 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7161 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7162 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7163 return NULL;
7164 }
7165
7166 if (cli_freq == 0) {
7167 wsc = wps_build_nfc_handover_req_p2p(
7168 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7169 } else
7170 wsc = NULL;
7171 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7172 go_dev_addr, ssid ? ssid->ssid : NULL,
7173 ssid ? ssid->ssid_len : 0);
7174
7175 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7176}
7177
7178
7179struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7180 int ndef, int tag)
7181{
7182 struct wpabuf *wsc, *p2p;
7183 struct wpa_ssid *ssid;
7184 u8 go_dev_addr[ETH_ALEN];
7185 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7186
7187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7188 return NULL;
7189
7190 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7191 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7192 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7193 return NULL;
7194
7195 if (cli_freq == 0) {
7196 wsc = wps_build_nfc_handover_sel_p2p(
7197 wpa_s->parent->wps,
7198 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7199 DEV_PW_NFC_CONNECTION_HANDOVER,
7200 wpa_s->conf->wps_nfc_dh_pubkey,
7201 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7202 } else
7203 wsc = NULL;
7204 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7205 go_dev_addr, ssid ? ssid->ssid : NULL,
7206 ssid ? ssid->ssid_len : 0);
7207
7208 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7209}
7210
7211
7212static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7213 struct p2p_nfc_params *params)
7214{
7215 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7216 "connection handover (freq=%d)",
7217 params->go_freq);
7218
7219 if (params->go_freq && params->go_ssid_len) {
7220 wpa_s->p2p_wps_method = WPS_NFC;
7221 wpa_s->pending_join_wps_method = WPS_NFC;
7222 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7223 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7224 ETH_ALEN);
7225 return wpas_p2p_join_start(wpa_s, params->go_freq,
7226 params->go_ssid,
7227 params->go_ssid_len);
7228 }
7229
7230 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7231 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7232 params->go_freq, -1, 0, 1, 1);
7233}
7234
7235
7236static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7237 struct p2p_nfc_params *params, int tag)
7238{
7239 int res, persistent;
7240 struct wpa_ssid *ssid;
7241
7242 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7243 "connection handover");
7244 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7245 ssid = wpa_s->current_ssid;
7246 if (ssid == NULL)
7247 continue;
7248 if (ssid->mode != WPAS_MODE_P2P_GO)
7249 continue;
7250 if (wpa_s->ap_iface == NULL)
7251 continue;
7252 break;
7253 }
7254 if (wpa_s == NULL) {
7255 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7256 return -1;
7257 }
7258
7259 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7260 DEV_PW_NFC_CONNECTION_HANDOVER &&
7261 !wpa_s->parent->p2p_oob_dev_pw) {
7262 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7263 return -1;
7264 }
7265 res = wpas_ap_wps_add_nfc_pw(
7266 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7267 wpa_s->parent->p2p_oob_dev_pw,
7268 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7269 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7270 if (res)
7271 return res;
7272
7273 if (!tag) {
7274 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7275 return 0;
7276 }
7277
7278 if (!params->peer ||
7279 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7280 return 0;
7281
7282 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7283 " to join", MAC2STR(params->peer->p2p_device_addr));
7284
7285 wpa_s->global->p2p_invite_group = wpa_s;
7286 persistent = ssid->p2p_persistent_group &&
7287 wpas_p2p_get_persistent(wpa_s->parent,
7288 params->peer->p2p_device_addr,
7289 ssid->ssid, ssid->ssid_len);
7290 wpa_s->parent->pending_invite_ssid_id = -1;
7291
7292 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7293 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7294 ssid->ssid, ssid->ssid_len, ssid->frequency,
7295 wpa_s->global->p2p_dev_addr, persistent, 0,
7296 wpa_s->parent->p2p_oob_dev_pw_id);
7297}
7298
7299
7300static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7301 struct p2p_nfc_params *params,
7302 int forced_freq)
7303{
7304 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7305 "connection handover");
7306 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7307 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7308 forced_freq, -1, 0, 1, 1);
7309}
7310
7311
7312static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7313 struct p2p_nfc_params *params,
7314 int forced_freq)
7315{
7316 int res;
7317
7318 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7319 "connection handover");
7320 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7321 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7322 forced_freq, -1, 0, 1, 1);
7323 if (res)
7324 return res;
7325
7326 res = wpas_p2p_listen(wpa_s, 60);
7327 if (res) {
7328 p2p_unauthorize(wpa_s->global->p2p,
7329 params->peer->p2p_device_addr);
7330 }
7331
7332 return res;
7333}
7334
7335
7336static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7337 const struct wpabuf *data,
7338 int sel, int tag, int forced_freq)
7339{
7340 const u8 *pos, *end;
7341 u16 len, id;
7342 struct p2p_nfc_params params;
7343 int res;
7344
7345 os_memset(&params, 0, sizeof(params));
7346 params.sel = sel;
7347
7348 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7349
7350 pos = wpabuf_head(data);
7351 end = pos + wpabuf_len(data);
7352
7353 if (end - pos < 2) {
7354 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7355 "attributes");
7356 return -1;
7357 }
7358 len = WPA_GET_BE16(pos);
7359 pos += 2;
7360 if (pos + len > end) {
7361 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7362 "attributes");
7363 return -1;
7364 }
7365 params.wsc_attr = pos;
7366 params.wsc_len = len;
7367 pos += len;
7368
7369 if (end - pos < 2) {
7370 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7371 "attributes");
7372 return -1;
7373 }
7374 len = WPA_GET_BE16(pos);
7375 pos += 2;
7376 if (pos + len > end) {
7377 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7378 "attributes");
7379 return -1;
7380 }
7381 params.p2p_attr = pos;
7382 params.p2p_len = len;
7383 pos += len;
7384
7385 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7386 params.wsc_attr, params.wsc_len);
7387 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7388 params.p2p_attr, params.p2p_len);
7389 if (pos < end) {
7390 wpa_hexdump(MSG_DEBUG,
7391 "P2P: Ignored extra data after P2P attributes",
7392 pos, end - pos);
7393 }
7394
7395 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7396 if (res)
7397 return res;
7398
7399 if (params.next_step == NO_ACTION)
7400 return 0;
7401
7402 if (params.next_step == BOTH_GO) {
7403 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7404 MAC2STR(params.peer->p2p_device_addr));
7405 return 0;
7406 }
7407
7408 if (params.next_step == PEER_CLIENT) {
7409 if (!is_zero_ether_addr(params.go_dev_addr)) {
7410 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7411 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7412 " ssid=\"%s\"",
7413 MAC2STR(params.peer->p2p_device_addr),
7414 params.go_freq,
7415 MAC2STR(params.go_dev_addr),
7416 wpa_ssid_txt(params.go_ssid,
7417 params.go_ssid_len));
7418 } else {
7419 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7420 "peer=" MACSTR " freq=%d",
7421 MAC2STR(params.peer->p2p_device_addr),
7422 params.go_freq);
7423 }
7424 return 0;
7425 }
7426
7427 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7428 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7429 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7430 return 0;
7431 }
7432
7433 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7434 wpa_s->p2p_oob_dev_pw = NULL;
7435
7436 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7437 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7438 "received");
7439 return -1;
7440 }
7441
7442 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7443 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7444 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7445 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7446 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7447 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7448 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7449
7450 if (tag) {
7451 if (id < 0x10) {
7452 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7453 "peer OOB Device Password Id %u", id);
7454 return -1;
7455 }
7456 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7457 "Device Password Id %u", id);
7458 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7459 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7460 params.oob_dev_pw_len -
7461 WPS_OOB_PUBKEY_HASH_LEN - 2);
7462 wpa_s->p2p_oob_dev_pw_id = id;
7463 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7464 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7465 params.oob_dev_pw_len -
7466 WPS_OOB_PUBKEY_HASH_LEN - 2);
7467 if (wpa_s->p2p_oob_dev_pw == NULL)
7468 return -1;
7469
7470 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7471 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7472 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7473 return -1;
7474 } else {
7475 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7476 "without Device Password");
7477 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7478 }
7479
7480 switch (params.next_step) {
7481 case NO_ACTION:
7482 case BOTH_GO:
7483 case PEER_CLIENT:
7484 /* already covered above */
7485 return 0;
7486 case JOIN_GROUP:
7487 return wpas_p2p_nfc_join_group(wpa_s, &params);
7488 case AUTH_JOIN:
7489 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7490 case INIT_GO_NEG:
7491 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7492 case RESP_GO_NEG:
7493 /* TODO: use own OOB Dev Pw */
7494 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7495 }
7496
7497 return -1;
7498}
7499
7500
7501int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7502 const struct wpabuf *data, int forced_freq)
7503{
7504 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7505 return -1;
7506
7507 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7508}
7509
7510
7511int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7512 const struct wpabuf *req,
7513 const struct wpabuf *sel, int forced_freq)
7514{
7515 struct wpabuf *tmp;
7516 int ret;
7517
7518 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7519 return -1;
7520
7521 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7522
7523 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7524 wpabuf_head(req), wpabuf_len(req));
7525 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7526 wpabuf_head(sel), wpabuf_len(sel));
7527 if (forced_freq)
7528 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7529 tmp = ndef_parse_p2p(init ? sel : req);
7530 if (tmp == NULL) {
7531 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7532 return -1;
7533 }
7534
7535 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7536 forced_freq);
7537 wpabuf_free(tmp);
7538
7539 return ret;
7540}
7541
7542
7543int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7544{
7545 const u8 *if_addr;
7546 int go_intent = wpa_s->conf->p2p_go_intent;
7547 struct wpa_supplicant *iface;
7548
7549 if (wpa_s->global->p2p == NULL)
7550 return -1;
7551
7552 if (!enabled) {
7553 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7554 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7555 {
7556 if (!iface->ap_iface)
7557 continue;
7558 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7559 }
7560 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7561 0, NULL);
7562 if (wpa_s->p2p_nfc_tag_enabled)
7563 wpas_p2p_remove_pending_group_interface(wpa_s);
7564 wpa_s->p2p_nfc_tag_enabled = 0;
7565 return 0;
7566 }
7567
7568 if (wpa_s->global->p2p_disabled)
7569 return -1;
7570
7571 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7572 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7573 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7574 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7575 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7576 "to allow static handover cases");
7577 return -1;
7578 }
7579
7580 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7581
7582 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7583 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7584 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7585 if (wpa_s->p2p_oob_dev_pw == NULL)
7586 return -1;
7587 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7588
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07007589 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7590 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7591 /*
7592 * P2P Group Interface present and the command came on group
7593 * interface, so enable the token for the current interface.
7594 */
7595 wpa_s->create_p2p_iface = 0;
7596 } else {
7597 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7598 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08007599
7600 if (wpa_s->create_p2p_iface) {
7601 enum wpa_driver_if_type iftype;
7602 /* Prepare to add a new interface for the group */
7603 iftype = WPA_IF_P2P_GROUP;
7604 if (go_intent == 15)
7605 iftype = WPA_IF_P2P_GO;
7606 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7607 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7608 "interface for the group");
7609 return -1;
7610 }
7611
7612 if_addr = wpa_s->pending_interface_addr;
7613 } else
7614 if_addr = wpa_s->own_addr;
7615
7616 wpa_s->p2p_nfc_tag_enabled = enabled;
7617
7618 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7619 struct hostapd_data *hapd;
7620 if (iface->ap_iface == NULL)
7621 continue;
7622 hapd = iface->ap_iface->bss[0];
7623 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7624 hapd->conf->wps_nfc_dh_pubkey =
7625 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7626 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7627 hapd->conf->wps_nfc_dh_privkey =
7628 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7629 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7630 hapd->conf->wps_nfc_dev_pw =
7631 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7632 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7633
7634 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7635 wpa_dbg(iface, MSG_DEBUG,
7636 "P2P: Failed to enable NFC Tag for GO");
7637 }
7638 }
7639 p2p_set_authorized_oob_dev_pw_id(
7640 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7641 if_addr);
7642
7643 return 0;
7644}
7645
7646#endif /* CONFIG_WPS_NFC */