blob: c4e42eabf09a5634428358e517a144395a14e601 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080019#include "ap/ap_config.h"
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070020#include "ap/sta_info.h"
21#include "ap/ap_drv_ops.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "ap/p2p_hostapd.h"
Jouni Malinen75ecf522011-06-27 15:19:46 -070023#include "eapol_supp/eapol_supp_sm.h"
24#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "wpa_supplicant_i.h"
26#include "driver_i.h"
27#include "ap.h"
28#include "config_ssid.h"
29#include "config.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030#include "notify.h"
31#include "scan.h"
32#include "bss.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080033#include "offchannel.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034#include "wps_supplicant.h"
35#include "p2p_supplicant.h"
36
37
38/*
39 * How many times to try to scan to find the GO before giving up on join
40 * request.
41 */
42#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
43
Dmitry Shmidt04949592012-07-19 12:16:46 -070044#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
45
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046#ifndef P2P_MAX_CLIENT_IDLE
47/*
48 * How many seconds to try to reconnect to the GO when connection in P2P client
49 * role has been lost.
50 */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070051#ifdef ANDROID_P2P
52#define P2P_MAX_CLIENT_IDLE 20
53#else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080054#define P2P_MAX_CLIENT_IDLE 10
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070055#endif /* ANDROID_P2P */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080056#endif /* P2P_MAX_CLIENT_IDLE */
57
Dmitry Shmidt04949592012-07-19 12:16:46 -070058#ifndef P2P_MAX_INITIAL_CONN_WAIT
59/*
60 * How many seconds to wait for initial 4-way handshake to get completed after
61 * WPS provisioning step.
62 */
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 Shmidt391c59f2013-09-03 12:16:28 -070099 P2P_GROUP_REMOVAL_PSK_FAILURE,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700100#ifdef ANDROID_P2P
101 P2P_GROUP_REMOVAL_FREQ_CONFLICT
102#endif
103};
104
Jouni Malinendc7b7132012-09-14 12:53:47 -0700105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
107static struct wpa_supplicant *
108wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
109 int go);
110static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700111static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
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,
115 int auto_join);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800120static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 void *timeout_ctx);
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700122#ifdef ANDROID_P2P
123static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
124#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700125static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
126 int group_added);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800127static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
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;
137 int num;
138
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
147 return wpa_s->num_multichan_concurrent - num;
148}
149
150
151/*
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
154 */
155static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 int *p2p_freqs, unsigned int len)
157{
158 int *freqs;
159 unsigned int num, i, j;
160
161 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
162 if (!freqs)
163 return -1;
164
165 num = get_shared_radio_freqs(wpa_s, freqs,
166 wpa_s->num_multichan_concurrent);
167
168 os_memset(p2p_freqs, 0, sizeof(int) * len);
169
170 for (i = 0, j = 0; i < num && j < len; i++) {
171 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
172 p2p_freqs[j++] = freqs[i];
173 }
174
175 os_free(freqs);
176
177 return j;
178}
179
180
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700181static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
182 int freq)
183{
184 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
185 return;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700186 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
187 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700188 wpa_s->parent->conf->p2p_ignore_shared_freq)
189 freq = 0;
190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
191}
192
193
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
196{
197 size_t i;
198
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
200 return;
201
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
204
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800207 struct os_time time_tmp_age, entry_ts;
Dmitry Shmidt96571392013-10-14 12:54:46 -0700208 const u8 *ies;
209 size_t ies_len;
210
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800211 time_tmp_age.sec = bss->age / 1000;
212 time_tmp_age.usec = (bss->age % 1000) * 1000;
213 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700214
215 ies = (const u8 *) (bss + 1);
216 ies_len = bss->ie_len;
217 if (bss->beacon_ie_len > 0 &&
218 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR, MAC2STR(bss->bssid));
222 ies = ies + ies_len;
223 ies_len = bss->beacon_ie_len;
224 }
225
226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800228 bss->freq, &entry_ts, bss->level,
Dmitry Shmidt96571392013-10-14 12:54:46 -0700229 ies, ies_len) > 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 break;
231 }
232
233 p2p_scan_res_handled(wpa_s->global->p2p);
234}
235
236
237static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238 unsigned int num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700239 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240{
241 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700242 struct wpa_supplicant *ifs;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 struct wpa_driver_scan_params params;
244 int ret;
245 struct wpabuf *wps_ie, *ies;
246 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800247 size_t ielen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248
249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
250 return -1;
251
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700252 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253 if (ifs->sta_scan_pending &&
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -0700254 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700255 wpas_p2p_in_progress(wpa_s) == 2) {
256 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs->ifname);
Jouni Malinendc7b7132012-09-14 12:53:47 -0700259 wpa_s->global->p2p_cb_on_scan_complete = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700260 wpa_supplicant_req_scan(ifs, 0, 0);
261 return 1;
262 }
263 }
264
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 os_memset(&params, 0, sizeof(params));
266
267 /* P2P Wildcard SSID */
268 params.num_ssids = 1;
269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
271
272 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700273 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275 num_req_dev_types, req_dev_types);
276 if (wps_ie == NULL)
277 return -1;
278
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800279 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 if (ies == NULL) {
282 wpabuf_free(wps_ie);
283 return -1;
284 }
285 wpabuf_put_buf(ies, wps_ie);
286 wpabuf_free(wps_ie);
287
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800288 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700289
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800290 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 params.extra_ies = wpabuf_head(ies);
292 params.extra_ies_len = wpabuf_len(ies);
293
294 switch (type) {
295 case P2P_SCAN_SOCIAL:
296 params.freqs = social_channels;
297 break;
298 case P2P_SCAN_FULL:
299 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 case P2P_SCAN_SOCIAL_PLUS_ONE:
301 social_channels[3] = freq;
302 params.freqs = social_channels;
303 break;
304 }
305
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
308 wpabuf_free(ies);
309
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800310 if (ret) {
Jouni Malinen043a5a92012-09-13 18:03:14 -0700311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
312 if (ifs->scanning ||
313 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314 wpa_s->global->p2p_cb_on_scan_complete = 1;
315 ret = 1;
316 break;
317 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800318 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700319 } else {
320 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700322 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324 return ret;
325}
326
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
329{
330 switch (p2p_group_interface) {
331 case P2P_GROUP_INTERFACE_PENDING:
332 return WPA_IF_P2P_GROUP;
333 case P2P_GROUP_INTERFACE_GO:
334 return WPA_IF_P2P_GO;
335 case P2P_GROUP_INTERFACE_CLIENT:
336 return WPA_IF_P2P_CLIENT;
337 }
338
339 return WPA_IF_P2P_GROUP;
340}
341
342
343static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
344 const u8 *ssid,
345 size_t ssid_len, int *go)
346{
347 struct wpa_ssid *s;
348
349 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
350 for (s = wpa_s->conf->ssid; s; s = s->next) {
351 if (s->disabled != 0 || !s->p2p_group ||
352 s->ssid_len != ssid_len ||
353 os_memcmp(ssid, s->ssid, ssid_len) != 0)
354 continue;
355 if (s->mode == WPAS_MODE_P2P_GO &&
356 s != wpa_s->current_ssid)
357 continue;
358 if (go)
359 *go = s->mode == WPAS_MODE_P2P_GO;
360 return wpa_s;
361 }
362 }
363
364 return NULL;
365}
366
367
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700368static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
369 enum p2p_group_removal_reason removal_reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370{
371 struct wpa_ssid *ssid;
372 char *gtype;
373 const char *reason;
374
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 ssid = wpa_s->current_ssid;
376 if (ssid == NULL) {
377 /*
378 * The current SSID was not known, but there may still be a
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 */
382 ssid = wpa_s->conf->ssid;
383 while (ssid) {
Jouni Malinen9d712832012-10-05 11:01:57 -0700384 if (ssid->p2p_group && ssid->disabled != 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 break;
386 ssid = ssid->next;
387 }
Jouni Malinen5c44edb2012-08-31 21:35:32 +0300388 if (ssid == NULL &&
389 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
390 {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
392 "not found");
393 return -1;
394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
397 gtype = "GO";
398 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
399 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
400 wpa_s->reassociate = 0;
401 wpa_s->disconnected = 1;
402 wpa_supplicant_deauthenticate(wpa_s,
403 WLAN_REASON_DEAUTH_LEAVING);
404 gtype = "client";
405 } else
406 gtype = "GO";
407 if (wpa_s->cross_connect_in_use) {
408 wpa_s->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700409 wpa_msg_global(wpa_s->parent, MSG_INFO,
410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
411 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700413 switch (removal_reason) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 case P2P_GROUP_REMOVAL_REQUESTED:
415 reason = " reason=REQUESTED";
416 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
418 reason = " reason=FORMATION_FAILED";
419 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
421 reason = " reason=IDLE";
422 break;
423 case P2P_GROUP_REMOVAL_UNAVAILABLE:
424 reason = " reason=UNAVAILABLE";
425 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
427 reason = " reason=GO_ENDING_SESSION";
428 break;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700429 case P2P_GROUP_REMOVAL_PSK_FAILURE:
430 reason = " reason=PSK_FAILURE";
431 break;
Dmitry Shmidt687922c2012-03-26 14:02:32 -0700432#ifdef ANDROID_P2P
433 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
434 reason = " reason=FREQ_CONFLICT";
435 break;
436#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 default:
438 reason = "";
439 break;
440 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700441 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700442 wpa_msg_global(wpa_s->parent, MSG_INFO,
443 P2P_EVENT_GROUP_REMOVED "%s %s%s",
444 wpa_s->ifname, gtype, reason);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700445 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446
Dmitry Shmidt1cf45732013-04-29 17:36:45 -0700447#ifdef ANDROID_P2P
448 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
449#endif
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800452 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700453 wpa_s->parent, NULL) > 0) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800454 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
455 "timeout");
Dmitry Shmidt2f023192013-03-12 12:44:17 -0700456 wpa_s->p2p_in_provisioning = 0;
457 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458
Dmitry Shmidt96571392013-10-14 12:54:46 -0700459 /*
460 * Make sure wait for the first client does not remain active after the
461 * group has been removed.
462 */
463 wpa_s->global->p2p_go_wait_client.sec = 0;
464
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700465 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
467
468 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
469 struct wpa_global *global;
470 char *ifname;
471 enum wpa_driver_if_type type;
472 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
473 wpa_s->ifname);
474 global = wpa_s->global;
475 ifname = os_strdup(wpa_s->ifname);
476 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
Dmitry Shmidte15c7b52011-08-03 15:04:35 -0700477 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 wpa_s = global->ifaces;
479 if (wpa_s && ifname)
480 wpa_drv_if_remove(wpa_s, type, ifname);
481 os_free(ifname);
Jouni Malinen2b89da82012-08-31 22:04:41 +0300482 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 }
484
Dmitry Shmidt96571392013-10-14 12:54:46 -0700485 if (!wpa_s->p2p_go_group_formation_completed) {
486 wpa_s->global->p2p_group_formation = NULL;
487 wpa_s->p2p_in_provisioning = 0;
488 }
489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
491 if (ssid && (ssid->p2p_group ||
492 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
493 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
494 int id = ssid->id;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700495 if (ssid == wpa_s->current_ssid) {
496 wpa_sm_set_config(wpa_s->wpa, NULL);
497 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 wpa_s->current_ssid = NULL;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700499 }
500 /*
501 * Networks objects created during any P2P activities are not
502 * exposed out as they might/will confuse certain non-P2P aware
503 * applications since these network objects won't behave like
504 * regular ones.
505 *
506 * Likewise, we don't send out network removed signals for such
507 * network objects.
508 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 wpa_config_remove_network(wpa_s->conf, id);
510 wpa_supplicant_clear_status(wpa_s);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800511 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -0700512 wpa_s->sta_scan_pending = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 } else {
514 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
515 "found");
516 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700517 if (wpa_s->ap_iface)
518 wpa_supplicant_ap_deinit(wpa_s);
519 else
520 wpa_drv_deinit_p2p_cli(wpa_s);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700521
522 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523}
524
525
526static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
527 u8 *go_dev_addr,
528 const u8 *ssid, size_t ssid_len)
529{
530 struct wpa_bss *bss;
531 const u8 *bssid;
532 struct wpabuf *p2p;
533 u8 group_capab;
534 const u8 *addr;
535
536 if (wpa_s->go_params)
537 bssid = wpa_s->go_params->peer_interface_addr;
538 else
539 bssid = wpa_s->bssid;
540
541 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
542 if (bss == NULL) {
543 u8 iface_addr[ETH_ALEN];
544 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
545 iface_addr) == 0)
546 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
547 }
548 if (bss == NULL) {
549 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
550 "group is persistent - BSS " MACSTR " not found",
551 MAC2STR(bssid));
552 return 0;
553 }
554
555 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700556 if (p2p == NULL)
557 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
558 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 if (p2p == NULL) {
560 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
561 "group is persistent - BSS " MACSTR
562 " did not include P2P IE", MAC2STR(bssid));
563 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
564 (u8 *) (bss + 1), bss->ie_len);
565 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
566 ((u8 *) bss + 1) + bss->ie_len,
567 bss->beacon_ie_len);
568 return 0;
569 }
570
571 group_capab = p2p_get_group_capab(p2p);
572 addr = p2p_get_go_dev_addr(p2p);
573 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
574 "group_capab=0x%x", group_capab);
575 if (addr) {
576 os_memcpy(go_dev_addr, addr, ETH_ALEN);
577 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
578 MAC2STR(addr));
579 } else
580 os_memset(go_dev_addr, 0, ETH_ALEN);
581 wpabuf_free(p2p);
582
583 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
584 "go_dev_addr=" MACSTR,
585 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
586
587 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
588}
589
590
Jouni Malinen75ecf522011-06-27 15:19:46 -0700591static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
592 struct wpa_ssid *ssid,
593 const u8 *go_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594{
595 struct wpa_ssid *s;
596 int changed = 0;
597
598 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
599 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
600 for (s = wpa_s->conf->ssid; s; s = s->next) {
601 if (s->disabled == 2 &&
602 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
603 s->ssid_len == ssid->ssid_len &&
604 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
605 break;
606 }
607
608 if (s) {
609 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
610 "entry");
611 if (ssid->passphrase && !s->passphrase)
612 changed = 1;
613 else if (ssid->passphrase && s->passphrase &&
614 os_strcmp(ssid->passphrase, s->passphrase) != 0)
615 changed = 1;
616 } else {
617 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
618 "entry");
619 changed = 1;
620 s = wpa_config_add_network(wpa_s->conf);
621 if (s == NULL)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700622 return -1;
623
624 /*
625 * Instead of network_added we emit persistent_group_added
626 * notification. Also to keep the defense checks in
627 * persistent_group obj registration method, we set the
628 * relevant flags in s to designate it as a persistent group.
629 */
630 s->p2p_group = 1;
631 s->p2p_persistent_group = 1;
632 wpas_notify_persistent_group_added(wpa_s, s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 wpa_config_set_network_defaults(s);
634 }
635
636 s->p2p_group = 1;
637 s->p2p_persistent_group = 1;
638 s->disabled = 2;
639 s->bssid_set = 1;
640 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
641 s->mode = ssid->mode;
642 s->auth_alg = WPA_AUTH_ALG_OPEN;
643 s->key_mgmt = WPA_KEY_MGMT_PSK;
644 s->proto = WPA_PROTO_RSN;
645 s->pairwise_cipher = WPA_CIPHER_CCMP;
646 s->export_keys = 1;
647 if (ssid->passphrase) {
648 os_free(s->passphrase);
649 s->passphrase = os_strdup(ssid->passphrase);
650 }
651 if (ssid->psk_set) {
652 s->psk_set = 1;
653 os_memcpy(s->psk, ssid->psk, 32);
654 }
655 if (s->passphrase && !s->psk_set)
656 wpa_config_update_psk(s);
657 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
658 os_free(s->ssid);
659 s->ssid = os_malloc(ssid->ssid_len);
660 }
661 if (s->ssid) {
662 s->ssid_len = ssid->ssid_len;
663 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
664 }
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700665 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
666 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
667 wpa_s->global->add_psk = NULL;
668 changed = 1;
669 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670
671#ifndef CONFIG_NO_CONFIG_WRITE
672 if (changed && wpa_s->conf->update_config &&
673 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
674 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
675 }
676#endif /* CONFIG_NO_CONFIG_WRITE */
Jouni Malinen75ecf522011-06-27 15:19:46 -0700677
678 return s->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679}
680
681
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800682static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
683 const u8 *addr)
684{
685 struct wpa_ssid *ssid, *s;
686 u8 *n;
687 size_t i;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700688 int found = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800689
690 ssid = wpa_s->current_ssid;
691 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
692 !ssid->p2p_persistent_group)
693 return;
694
695 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
696 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
697 continue;
698
699 if (s->ssid_len == ssid->ssid_len &&
700 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
701 break;
702 }
703
704 if (s == NULL)
705 return;
706
707 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
708 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700709 ETH_ALEN) != 0)
710 continue;
711
712 if (i == s->num_p2p_clients - 1)
713 return; /* already the most recent entry */
714
715 /* move the entry to mark it most recent */
716 os_memmove(s->p2p_client_list + i * ETH_ALEN,
717 s->p2p_client_list + (i + 1) * ETH_ALEN,
718 (s->num_p2p_clients - i - 1) * ETH_ALEN);
719 os_memcpy(s->p2p_client_list +
720 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
721 found = 1;
722 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800723 }
724
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700725 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
726 n = os_realloc_array(s->p2p_client_list,
727 s->num_p2p_clients + 1, ETH_ALEN);
728 if (n == NULL)
729 return;
730 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
731 s->p2p_client_list = n;
732 s->num_p2p_clients++;
733 } else if (!found) {
734 /* Not enough room for an additional entry - drop the oldest
735 * entry */
736 os_memmove(s->p2p_client_list,
737 s->p2p_client_list + ETH_ALEN,
738 (s->num_p2p_clients - 1) * ETH_ALEN);
739 os_memcpy(s->p2p_client_list +
740 (s->num_p2p_clients - 1) * ETH_ALEN,
741 addr, ETH_ALEN);
742 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800743
744#ifndef CONFIG_NO_CONFIG_WRITE
745 if (wpa_s->parent->conf->update_config &&
746 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
747 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
748#endif /* CONFIG_NO_CONFIG_WRITE */
749}
750
751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
753 int success)
754{
755 struct wpa_ssid *ssid;
756 const char *ssid_txt;
757 int client;
758 int persistent;
759 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -0700760 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761
762 /*
763 * This callback is likely called for the main interface. Update wpa_s
764 * to use the group interface if a new interface was created for the
765 * group.
766 */
767 if (wpa_s->global->p2p_group_formation)
768 wpa_s = wpa_s->global->p2p_group_formation;
Dmitry Shmidt56052862013-10-04 10:23:25 -0700769 if (wpa_s->p2p_go_group_formation_completed) {
770 wpa_s->global->p2p_group_formation = NULL;
771 wpa_s->p2p_in_provisioning = 0;
772 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700773
774 if (!success) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700775 wpa_msg_global(wpa_s->parent, MSG_INFO,
776 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700777 wpas_p2p_group_delete(wpa_s,
778 P2P_GROUP_REMOVAL_FORMATION_FAILED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 return;
780 }
781
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700782 wpa_msg_global(wpa_s->parent, MSG_INFO,
783 P2P_EVENT_GROUP_FORMATION_SUCCESS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784
785 ssid = wpa_s->current_ssid;
786 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
787 ssid->mode = WPAS_MODE_P2P_GO;
788 p2p_group_notif_formation_done(wpa_s->p2p_group);
789 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
790 }
791
792 persistent = 0;
793 if (ssid) {
794 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
795 client = ssid->mode == WPAS_MODE_INFRA;
796 if (ssid->mode == WPAS_MODE_P2P_GO) {
797 persistent = ssid->p2p_persistent_group;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700798 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
799 ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 } else
801 persistent = wpas_p2p_persistent_group(wpa_s,
802 go_dev_addr,
803 ssid->ssid,
804 ssid->ssid_len);
805 } else {
806 ssid_txt = "";
807 client = wpa_s->p2p_group_interface ==
808 P2P_GROUP_INTERFACE_CLIENT;
809 os_memset(go_dev_addr, 0, ETH_ALEN);
810 }
811
812 wpa_s->show_group_started = 0;
813 if (client) {
814 /*
815 * Indicate event only after successfully completed 4-way
816 * handshake, i.e., when the interface is ready for data
817 * packets.
818 */
819 wpa_s->show_group_started = 1;
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700820#ifdef ANDROID_P2P
821 /* For client Second phase of Group formation (4-way handshake) can be still pending
822 * So we need to restore wpa_s->global->p2p_group_formation */
Dmitry Shmidta2854ab2012-09-10 16:15:47 -0700823 wpa_printf(MSG_INFO, "Restoring back wpa_s->global->p2p_group_formation to wpa_s %p\n", wpa_s);
Dmitry Shmidt4b86ea52012-09-04 11:06:50 -0700824 wpa_s->global->p2p_group_formation = wpa_s;
825#endif
826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
828 char psk[65];
829 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700830 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
831 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
832 MACSTR "%s",
833 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
834 MAC2STR(go_dev_addr),
835 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 wpas_p2p_cross_connect_setup(wpa_s);
837 wpas_p2p_set_group_idle_timeout(wpa_s);
838 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700839 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
840 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
841 "go_dev_addr=" MACSTR "%s",
842 wpa_s->ifname, ssid_txt,
843 ssid ? ssid->frequency : 0,
844 ssid && ssid->passphrase ? ssid->passphrase : "",
845 MAC2STR(go_dev_addr),
846 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 wpas_p2p_cross_connect_setup(wpa_s);
848 wpas_p2p_set_group_idle_timeout(wpa_s);
849 }
850
851 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700852 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
853 ssid, go_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700854 else {
855 os_free(wpa_s->global->add_psk);
856 wpa_s->global->add_psk = NULL;
857 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858 if (network_id < 0 && ssid)
Jouni Malinen75ecf522011-06-27 15:19:46 -0700859 network_id = ssid->id;
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700860 if (!client) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700861 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt92c368d2013-08-29 12:37:21 -0700862 os_get_time(&wpa_s->global->p2p_go_wait_client);
863 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864}
865
866
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
868 unsigned int freq,
869 const u8 *dst, const u8 *src,
870 const u8 *bssid,
871 const u8 *data, size_t data_len,
872 enum offchannel_send_action_result
873 result)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
878 return;
879 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
880 return;
881
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800882 switch (result) {
883 case OFFCHANNEL_SEND_ACTION_SUCCESS:
884 res = P2P_SEND_ACTION_SUCCESS;
885 break;
886 case OFFCHANNEL_SEND_ACTION_NO_ACK:
887 res = P2P_SEND_ACTION_NO_ACK;
888 break;
889 case OFFCHANNEL_SEND_ACTION_FAILED:
890 res = P2P_SEND_ACTION_FAILED;
891 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 }
893
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800894 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800896 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
897 wpa_s->pending_pd_before_join &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800898 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800899 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
900 wpa_s->p2p_fallback_to_go_neg) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800902 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
903 "during p2p_connect-auto");
904 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
905 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906 }
907}
908
909
910static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
911 const u8 *src, const u8 *bssid, const u8 *buf,
912 size_t len, unsigned int wait_time)
913{
914 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800915 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
916 wait_time,
917 wpas_p2p_send_action_tx_status, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918}
919
920
921static void wpas_send_action_done(void *ctx)
922{
923 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800924 offchannel_send_action_done(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925}
926
927
928static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
929 struct p2p_go_neg_results *params)
930{
931 if (wpa_s->go_params == NULL) {
932 wpa_s->go_params = os_malloc(sizeof(*params));
933 if (wpa_s->go_params == NULL)
934 return -1;
935 }
936 os_memcpy(wpa_s->go_params, params, sizeof(*params));
937 return 0;
938}
939
940
941static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
942 struct p2p_go_neg_results *res)
943{
944 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
945 MAC2STR(res->peer_interface_addr));
946 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
947 res->ssid, res->ssid_len);
948 wpa_supplicant_ap_deinit(wpa_s);
949 wpas_copy_go_neg_results(wpa_s, res);
950 if (res->wps_method == WPS_PBC)
951 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
952 else {
953 u16 dev_pw_id = DEV_PW_DEFAULT;
954 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
955 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
956 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
957 wpa_s->p2p_pin, 1, dev_pw_id);
958 }
959}
960
961
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700962static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
963 struct wpa_ssid *ssid)
964{
965 struct wpa_ssid *persistent;
966 struct psk_list_entry *psk;
967 struct hostapd_data *hapd;
968
969 if (!wpa_s->ap_iface)
970 return;
971
972 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
973 ssid->ssid_len);
974 if (persistent == NULL)
975 return;
976
977 hapd = wpa_s->ap_iface->bss[0];
978
979 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
980 list) {
981 struct hostapd_wpa_psk *hpsk;
982
983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
984 MACSTR " psk=%d",
985 MAC2STR(psk->addr), psk->p2p);
986 hpsk = os_zalloc(sizeof(*hpsk));
987 if (hpsk == NULL)
988 break;
989 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
990 if (psk->p2p)
991 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
992 else
993 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
994 hpsk->next = hapd->conf->ssid.wpa_psk;
995 hapd->conf->ssid.wpa_psk = hpsk;
996 }
997}
998
999
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000static void p2p_go_configured(void *ctx, void *data)
1001{
1002 struct wpa_supplicant *wpa_s = ctx;
1003 struct p2p_go_neg_results *params = data;
1004 struct wpa_ssid *ssid;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001005 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
1007 ssid = wpa_s->current_ssid;
1008 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1009 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1010 if (wpa_s->global->p2p_group_formation == wpa_s)
1011 wpa_s->global->p2p_group_formation = NULL;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001012 if (os_strlen(params->passphrase) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001013 wpa_msg_global(wpa_s->parent, MSG_INFO,
1014 P2P_EVENT_GROUP_STARTED
1015 "%s GO ssid=\"%s\" freq=%d "
1016 "passphrase=\"%s\" go_dev_addr=" MACSTR
1017 "%s", wpa_s->ifname,
1018 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1019 ssid->frequency, params->passphrase,
1020 MAC2STR(wpa_s->global->p2p_dev_addr),
1021 params->persistent_group ?
1022 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001023 } else {
1024 char psk[65];
1025 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1026 sizeof(params->psk));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001027 wpa_msg_global(wpa_s->parent, MSG_INFO,
1028 P2P_EVENT_GROUP_STARTED
1029 "%s GO ssid=\"%s\" freq=%d psk=%s "
1030 "go_dev_addr=" MACSTR "%s",
1031 wpa_s->ifname,
1032 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1033 ssid->frequency, psk,
1034 MAC2STR(wpa_s->global->p2p_dev_addr),
1035 params->persistent_group ?
1036 " [PERSISTENT]" : "");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001037 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001038
Dmitry Shmidt56052862013-10-04 10:23:25 -07001039 os_get_time(&wpa_s->global->p2p_go_wait_client);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001040 if (params->persistent_group) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07001041 network_id = wpas_p2p_store_persistent_group(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 wpa_s->parent, ssid,
Dmitry Shmidt497c1d52011-07-21 15:19:46 -07001043 wpa_s->global->p2p_dev_addr);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001044 wpas_p2p_add_psk_list(wpa_s, ssid);
1045 }
Jouni Malinen75ecf522011-06-27 15:19:46 -07001046 if (network_id < 0)
1047 network_id = ssid->id;
1048 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpas_p2p_cross_connect_setup(wpa_s);
1050 wpas_p2p_set_group_idle_timeout(wpa_s);
Dmitry Shmidt56052862013-10-04 10:23:25 -07001051
1052 if (wpa_s->p2p_first_connection_timeout) {
1053 wpa_dbg(wpa_s, MSG_DEBUG,
1054 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1055 wpa_s->p2p_first_connection_timeout);
1056 wpa_s->p2p_go_group_formation_completed = 0;
1057 wpa_s->global->p2p_group_formation = wpa_s;
1058 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1059 wpa_s->parent, NULL);
1060 eloop_register_timeout(
1061 wpa_s->p2p_first_connection_timeout, 0,
1062 wpas_p2p_group_formation_timeout,
1063 wpa_s->parent, NULL);
1064 }
1065
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 return;
1067 }
1068
1069 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1070 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1071 params->peer_interface_addr)) {
1072 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1073 "filtering");
1074 return;
1075 }
1076 if (params->wps_method == WPS_PBC)
1077 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001078 params->peer_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 else if (wpa_s->p2p_pin[0])
1080 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001081 wpa_s->p2p_pin, NULL, 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 os_free(wpa_s->go_params);
1083 wpa_s->go_params = NULL;
1084}
1085
1086
1087static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1088 struct p2p_go_neg_results *params,
1089 int group_formation)
1090{
1091 struct wpa_ssid *ssid;
1092
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1094 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1095 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1096 "results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001098 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099
1100 ssid = wpa_config_add_network(wpa_s->conf);
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001101 if (ssid == NULL) {
1102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 return;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001104 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001106 wpa_s->show_group_started = 0;
1107
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 wpa_config_set_network_defaults(ssid);
1109 ssid->temporary = 1;
1110 ssid->p2p_group = 1;
1111 ssid->p2p_persistent_group = params->persistent_group;
1112 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1113 WPAS_MODE_P2P_GO;
1114 ssid->frequency = params->freq;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001115 ssid->ht40 = params->ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001116 ssid->vht = params->vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001117 ssid->ssid = os_zalloc(params->ssid_len + 1);
1118 if (ssid->ssid) {
1119 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1120 ssid->ssid_len = params->ssid_len;
1121 }
1122 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1123 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1124 ssid->proto = WPA_PROTO_RSN;
1125 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001126 if (os_strlen(params->passphrase) > 0) {
1127 ssid->passphrase = os_strdup(params->passphrase);
1128 if (ssid->passphrase == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001129 wpa_msg_global(wpa_s, MSG_ERROR,
1130 "P2P: Failed to copy passphrase for GO");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001131 wpa_config_remove_network(wpa_s->conf, ssid->id);
1132 return;
1133 }
1134 } else
1135 ssid->passphrase = NULL;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001136 ssid->psk_set = params->psk_set;
1137 if (ssid->psk_set)
1138 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001139 else if (ssid->passphrase)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001140 wpa_config_update_psk(ssid);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001141 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142
1143 wpa_s->ap_configured_cb = p2p_go_configured;
1144 wpa_s->ap_configured_cb_ctx = wpa_s;
1145 wpa_s->ap_configured_cb_data = wpa_s->go_params;
Jouni Malinen75ecf522011-06-27 15:19:46 -07001146 wpa_s->connect_without_scan = ssid;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 wpa_s->reassociate = 1;
1148 wpa_s->disconnected = 0;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07001149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1150 "start GO)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 wpa_supplicant_req_scan(wpa_s, 0, 0);
1152}
1153
1154
1155static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1156 const struct wpa_supplicant *src)
1157{
1158 struct wpa_config *d;
1159 const struct wpa_config *s;
1160
1161 d = dst->conf;
1162 s = src->conf;
1163
1164#define C(n) if (s->n) d->n = os_strdup(s->n)
1165 C(device_name);
1166 C(manufacturer);
1167 C(model_name);
1168 C(model_number);
1169 C(serial_number);
1170 C(config_methods);
1171#undef C
1172
1173 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1174 os_memcpy(d->sec_device_type, s->sec_device_type,
1175 sizeof(d->sec_device_type));
1176 d->num_sec_device_types = s->num_sec_device_types;
1177
1178 d->p2p_group_idle = s->p2p_group_idle;
1179 d->p2p_intra_bss = s->p2p_intra_bss;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001180 d->persistent_reconnect = s->persistent_reconnect;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001181 d->max_num_sta = s->max_num_sta;
1182 d->pbc_in_m1 = s->pbc_in_m1;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001183 d->ignore_old_scan_res = s->ignore_old_scan_res;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001184 d->beacon_int = s->beacon_int;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001185 d->disassoc_low_ack = s->disassoc_low_ack;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001186 d->disable_scan_offload = s->disable_scan_offload;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187}
1188
1189
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001190static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1191 char *ifname, size_t len)
1192{
1193 char *ifname_ptr = wpa_s->ifname;
1194
1195 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1196 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1197 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1198 }
1199
1200 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1201 if (os_strlen(ifname) >= IFNAMSIZ &&
1202 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1203 /* Try to avoid going over the IFNAMSIZ length limit */
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001204 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001205 }
1206}
1207
1208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001209static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1210 enum wpa_driver_if_type type)
1211{
1212 char ifname[120], force_ifname[120];
1213
1214 if (wpa_s->pending_interface_name[0]) {
1215 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1216 "- skip creation of a new one");
1217 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1218 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1219 "unknown?! ifname='%s'",
1220 wpa_s->pending_interface_name);
1221 return -1;
1222 }
1223 return 0;
1224 }
1225
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001226 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 force_ifname[0] = '\0';
1228
1229 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1230 ifname);
1231 wpa_s->p2p_group_idx++;
1232
1233 wpa_s->pending_interface_type = type;
1234 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1235 wpa_s->pending_interface_addr, NULL) < 0) {
1236 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1237 "interface");
1238 return -1;
1239 }
1240
1241 if (force_ifname[0]) {
1242 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1243 force_ifname);
1244 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1245 sizeof(wpa_s->pending_interface_name));
1246 } else
1247 os_strlcpy(wpa_s->pending_interface_name, ifname,
1248 sizeof(wpa_s->pending_interface_name));
1249 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1250 MACSTR, wpa_s->pending_interface_name,
1251 MAC2STR(wpa_s->pending_interface_addr));
1252
1253 return 0;
1254}
1255
1256
1257static void wpas_p2p_remove_pending_group_interface(
1258 struct wpa_supplicant *wpa_s)
1259{
1260 if (!wpa_s->pending_interface_name[0] ||
1261 is_zero_ether_addr(wpa_s->pending_interface_addr))
1262 return; /* No pending virtual interface */
1263
1264 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1265 wpa_s->pending_interface_name);
1266 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1267 wpa_s->pending_interface_name);
1268 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1269 wpa_s->pending_interface_name[0] = '\0';
1270}
1271
1272
1273static struct wpa_supplicant *
1274wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1275{
1276 struct wpa_interface iface;
1277 struct wpa_supplicant *group_wpa_s;
1278
1279 if (!wpa_s->pending_interface_name[0]) {
1280 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1281 if (!wpas_p2p_create_iface(wpa_s))
1282 return NULL;
1283 /*
1284 * Something has forced us to remove the pending interface; try
1285 * to create a new one and hope for the best that we will get
1286 * the same local address.
1287 */
1288 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1289 WPA_IF_P2P_CLIENT) < 0)
1290 return NULL;
1291 }
1292
1293 os_memset(&iface, 0, sizeof(iface));
1294 iface.ifname = wpa_s->pending_interface_name;
1295 iface.driver = wpa_s->driver->name;
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001296 if (wpa_s->conf->ctrl_interface == NULL &&
1297 wpa_s->parent != wpa_s &&
1298 wpa_s->p2p_mgmt &&
1299 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1300 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1301 else
1302 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 iface.driver_param = wpa_s->conf->driver_param;
1304 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1305 if (group_wpa_s == NULL) {
1306 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1307 "wpa_supplicant interface");
1308 return NULL;
1309 }
1310 wpa_s->pending_interface_name[0] = '\0';
1311 group_wpa_s->parent = wpa_s;
1312 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1313 P2P_GROUP_INTERFACE_CLIENT;
1314 wpa_s->global->p2p_group_formation = group_wpa_s;
1315
1316 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1317
1318 return group_wpa_s;
1319}
1320
1321
1322static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1323 void *timeout_ctx)
1324{
1325 struct wpa_supplicant *wpa_s = eloop_ctx;
1326 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
Dmitry Shmidt34af3062013-07-11 10:46:32 -07001327 wpas_p2p_group_formation_failed(wpa_s);
1328}
1329
1330
1331void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1332{
1333 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1334 wpa_s->parent, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 if (wpa_s->global->p2p)
1336 p2p_group_formation_failed(wpa_s->global->p2p);
1337 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1338 wpa_drv_p2p_group_formation_failed(wpa_s);
1339 wpas_group_formation_completed(wpa_s, 0);
1340}
1341
1342
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001343void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1344{
1345 if (wpa_s->global->p2p_group_formation != wpa_s)
1346 return;
1347 /* Speed up group formation timeout since this cannot succeed */
1348 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1349 wpa_s->parent, NULL);
1350 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1351 wpa_s->parent, NULL);
1352}
1353
1354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1356{
1357 struct wpa_supplicant *wpa_s = ctx;
1358
1359 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1360 wpa_drv_cancel_remain_on_channel(wpa_s);
1361 wpa_s->off_channel_freq = 0;
1362 wpa_s->roc_waiting_drv_freq = 0;
1363 }
1364
1365 if (res->status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001366 wpa_msg_global(wpa_s, MSG_INFO,
1367 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1368 res->status);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001369 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 wpas_p2p_remove_pending_group_interface(wpa_s);
1371 return;
1372 }
1373
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001374 if (wpa_s->p2p_go_ht40)
1375 res->ht40 = 1;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001376 if (wpa_s->p2p_go_vht)
1377 res->vht = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001378
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001379 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1380 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1381 " wps_method=%s",
1382 res->role_go ? "GO" : "client", res->freq, res->ht40,
1383 MAC2STR(res->peer_device_addr),
1384 MAC2STR(res->peer_interface_addr),
1385 p2p_wps_method_text(res->wps_method));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001386 wpas_notify_p2p_go_neg_completed(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387
Dmitry Shmidt04949592012-07-19 12:16:46 -07001388 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1389 struct wpa_ssid *ssid;
1390 ssid = wpa_config_get_network(wpa_s->conf,
1391 wpa_s->p2p_persistent_id);
1392 if (ssid && ssid->disabled == 2 &&
1393 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1394 size_t len = os_strlen(ssid->passphrase);
1395 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1396 "on requested persistent group");
1397 os_memcpy(res->passphrase, ssid->passphrase, len);
1398 res->passphrase[len] = '\0';
1399 }
1400 }
1401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001402 if (wpa_s->create_p2p_iface) {
1403 struct wpa_supplicant *group_wpa_s =
1404 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1405 if (group_wpa_s == NULL) {
1406 wpas_p2p_remove_pending_group_interface(wpa_s);
1407 return;
1408 }
1409 if (group_wpa_s != wpa_s) {
1410 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1411 sizeof(group_wpa_s->p2p_pin));
1412 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1413 }
1414 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1415 wpa_s->pending_interface_name[0] = '\0';
1416 group_wpa_s->p2p_in_provisioning = 1;
1417
1418 if (res->role_go)
1419 wpas_start_wps_go(group_wpa_s, res, 1);
1420 else
1421 wpas_start_wps_enrollee(group_wpa_s, res);
1422 } else {
1423 wpa_s->p2p_in_provisioning = 1;
1424 wpa_s->global->p2p_group_formation = wpa_s;
1425
1426 if (res->role_go)
1427 wpas_start_wps_go(wpa_s, res, 1);
1428 else
1429 wpas_start_wps_enrollee(ctx, res);
1430 }
1431
1432 wpa_s->p2p_long_listen = 0;
1433 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1434
1435 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1436 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1437 (res->peer_config_timeout % 100) * 10000,
1438 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1439}
1440
1441
1442void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1443{
1444 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001445 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1446 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447
1448 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1449}
1450
1451
1452void wpas_dev_found(void *ctx, const u8 *addr,
1453 const struct p2p_peer_info *info,
1454 int new_device)
1455{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456#ifndef CONFIG_NO_STDOUT_DEBUG
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 struct wpa_supplicant *wpa_s = ctx;
1458 char devtype[WPS_DEV_TYPE_BUFSIZE];
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001459#define WFD_DEV_INFO_SIZE 9
1460 char wfd_dev_info_hex[2 * WFD_DEV_INFO_SIZE + 1];
Irfan Sheriff8d965182012-09-11 08:58:24 -07001461 os_memset(wfd_dev_info_hex, 0, sizeof(wfd_dev_info_hex));
Irfan Sheriff8367dc92012-09-09 17:08:19 -07001462#ifdef CONFIG_WIFI_DISPLAY
1463 if (info->wfd_subelems) {
1464 wpa_snprintf_hex(wfd_dev_info_hex, sizeof(wfd_dev_info_hex),
1465 wpabuf_head(info->wfd_subelems),
1466 WFD_DEV_INFO_SIZE);
1467 }
1468#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001469 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1470 " p2p_dev_addr=" MACSTR
1471 " pri_dev_type=%s name='%s' config_methods=0x%x "
1472 "dev_capab=0x%x group_capab=0x%x%s%s",
1473 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1474 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1475 sizeof(devtype)),
1476 info->device_name, info->config_methods,
1477 info->dev_capab, info->group_capab,
1478 wfd_dev_info_hex[0] ? " wfd_dev_info=0x" : "", wfd_dev_info_hex);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001479#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001480
1481 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1482}
1483
1484
1485static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1486{
1487 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001488
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001489 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1490 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001491
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1493}
1494
1495
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001496static void wpas_find_stopped(void *ctx)
1497{
1498 struct wpa_supplicant *wpa_s = ctx;
1499 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1500}
1501
1502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503static int wpas_start_listen(void *ctx, unsigned int freq,
1504 unsigned int duration,
1505 const struct wpabuf *probe_resp_ie)
1506{
1507 struct wpa_supplicant *wpa_s = ctx;
1508
1509 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1510
1511 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1512 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1513 "report received Probe Request frames");
1514 return -1;
1515 }
1516
1517 wpa_s->pending_listen_freq = freq;
1518 wpa_s->pending_listen_duration = duration;
1519
1520 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1521 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1522 "to remain on channel (%u MHz) for Listen "
1523 "state", freq);
1524 wpa_s->pending_listen_freq = 0;
1525 return -1;
1526 }
1527 wpa_s->off_channel_freq = 0;
1528 wpa_s->roc_waiting_drv_freq = freq;
1529
1530 return 0;
1531}
1532
1533
1534static void wpas_stop_listen(void *ctx)
1535{
1536 struct wpa_supplicant *wpa_s = ctx;
1537 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1538 wpa_drv_cancel_remain_on_channel(wpa_s);
1539 wpa_s->off_channel_freq = 0;
1540 wpa_s->roc_waiting_drv_freq = 0;
1541 }
1542 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1543 wpa_drv_probe_req_report(wpa_s, 0);
1544}
1545
1546
1547static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1548{
1549 struct wpa_supplicant *wpa_s = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001550 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551}
1552
1553
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001554/*
1555 * DNS Header section is used only to calculate compression pointers, so the
1556 * contents of this data does not matter, but the length needs to be reserved
1557 * in the virtual packet.
1558 */
1559#define DNS_HEADER_LEN 12
1560
1561/*
1562 * 27-octet in-memory packet from P2P specification containing two implied
1563 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1564 */
1565#define P2P_SD_IN_MEMORY_LEN 27
1566
1567static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1568 u8 **spos, const u8 *end)
1569{
1570 while (*spos < end) {
1571 u8 val = ((*spos)[0] & 0xc0) >> 6;
1572 int len;
1573
1574 if (val == 1 || val == 2) {
1575 /* These are reserved values in RFC 1035 */
1576 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1577 "sequence starting with 0x%x", val);
1578 return -1;
1579 }
1580
1581 if (val == 3) {
1582 u16 offset;
1583 u8 *spos_tmp;
1584
1585 /* Offset */
1586 if (*spos + 2 > end) {
1587 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1588 "DNS offset field");
1589 return -1;
1590 }
1591
1592 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1593 if (offset >= *spos - start) {
1594 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1595 "pointer offset %u", offset);
1596 return -1;
1597 }
1598
1599 (*spos) += 2;
1600 spos_tmp = start + offset;
1601 return p2p_sd_dns_uncompress_label(upos, uend, start,
1602 &spos_tmp,
1603 *spos - 2);
1604 }
1605
1606 /* Label */
1607 len = (*spos)[0] & 0x3f;
1608 if (len == 0)
1609 return 0;
1610
1611 (*spos)++;
1612 if (*spos + len > end) {
1613 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1614 "sequence - no room for label with length "
1615 "%u", len);
1616 return -1;
1617 }
1618
1619 if (*upos + len + 2 > uend)
1620 return -2;
1621
1622 os_memcpy(*upos, *spos, len);
1623 *spos += len;
1624 *upos += len;
1625 (*upos)[0] = '.';
1626 (*upos)++;
1627 (*upos)[0] = '\0';
1628 }
1629
1630 return 0;
1631}
1632
1633
1634/* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1635 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1636 * not large enough */
1637static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1638 size_t msg_len, size_t offset)
1639{
1640 /* 27-octet in-memory packet from P2P specification */
1641 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1642 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1643 u8 *tmp, *end, *spos;
1644 char *upos, *uend;
1645 int ret = 0;
1646
1647 if (buf_len < 2)
1648 return -1;
1649 if (offset > msg_len)
1650 return -1;
1651
1652 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1653 if (tmp == NULL)
1654 return -1;
1655 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1656 end = spos + msg_len;
1657 spos += offset;
1658
1659 os_memset(tmp, 0, DNS_HEADER_LEN);
1660 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1661 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1662
1663 upos = buf;
1664 uend = buf + buf_len;
1665
1666 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1667 if (ret) {
1668 os_free(tmp);
1669 return ret;
1670 }
1671
1672 if (upos == buf) {
1673 upos[0] = '.';
1674 upos[1] = '\0';
1675 } else if (upos[-1] == '.')
1676 upos[-1] = '\0';
1677
1678 os_free(tmp);
1679 return 0;
1680}
1681
1682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683static struct p2p_srv_bonjour *
1684wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1685 const struct wpabuf *query)
1686{
1687 struct p2p_srv_bonjour *bsrv;
1688 size_t len;
1689
1690 len = wpabuf_len(query);
1691 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1692 struct p2p_srv_bonjour, list) {
1693 if (len == wpabuf_len(bsrv->query) &&
1694 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1695 len) == 0)
1696 return bsrv;
1697 }
1698 return NULL;
1699}
1700
1701
1702static struct p2p_srv_upnp *
1703wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1704 const char *service)
1705{
1706 struct p2p_srv_upnp *usrv;
1707
1708 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1709 struct p2p_srv_upnp, list) {
1710 if (version == usrv->version &&
1711 os_strcmp(service, usrv->service) == 0)
1712 return usrv;
1713 }
1714 return NULL;
1715}
1716
1717
1718static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1719 u8 srv_trans_id)
1720{
1721 u8 *len_pos;
1722
1723 if (wpabuf_tailroom(resp) < 5)
1724 return;
1725
1726 /* Length (to be filled) */
1727 len_pos = wpabuf_put(resp, 2);
1728 wpabuf_put_u8(resp, srv_proto);
1729 wpabuf_put_u8(resp, srv_trans_id);
1730 /* Status Code */
1731 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1732 /* Response Data: empty */
1733 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1734}
1735
1736
1737static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1738 struct wpabuf *resp, u8 srv_trans_id)
1739{
1740 struct p2p_srv_bonjour *bsrv;
1741 u8 *len_pos;
1742
1743 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1744
1745 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1746 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1747 return;
1748 }
1749
1750 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1751 struct p2p_srv_bonjour, list) {
1752 if (wpabuf_tailroom(resp) <
1753 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1754 return;
1755 /* Length (to be filled) */
1756 len_pos = wpabuf_put(resp, 2);
1757 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1758 wpabuf_put_u8(resp, srv_trans_id);
1759 /* Status Code */
1760 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1761 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1762 wpabuf_head(bsrv->resp),
1763 wpabuf_len(bsrv->resp));
1764 /* Response Data */
1765 wpabuf_put_buf(resp, bsrv->query); /* Key */
1766 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1767 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1768 2);
1769 }
1770}
1771
1772
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001773static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1774 size_t query_len)
1775{
1776 char str_rx[256], str_srv[256];
1777
1778 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1779 return 0; /* Too short to include DNS Type and Version */
1780 if (os_memcmp(query + query_len - 3,
1781 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1782 3) != 0)
1783 return 0; /* Mismatch in DNS Type or Version */
1784 if (query_len == wpabuf_len(bsrv->query) &&
1785 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1786 return 1; /* Binary match */
1787
1788 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1789 0))
1790 return 0; /* Failed to uncompress query */
1791 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1792 wpabuf_head(bsrv->query),
1793 wpabuf_len(bsrv->query) - 3, 0))
1794 return 0; /* Failed to uncompress service */
1795
1796 return os_strcmp(str_rx, str_srv) == 0;
1797}
1798
1799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001800static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1801 struct wpabuf *resp, u8 srv_trans_id,
1802 const u8 *query, size_t query_len)
1803{
1804 struct p2p_srv_bonjour *bsrv;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 u8 *len_pos;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001806 int matches = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001807
1808 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1809 query, query_len);
1810 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1811 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1812 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1813 srv_trans_id);
1814 return;
1815 }
1816
1817 if (query_len == 0) {
1818 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1819 return;
1820 }
1821
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001822 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1823 struct p2p_srv_bonjour, list) {
1824 if (!match_bonjour_query(bsrv, query, query_len))
1825 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001827 if (wpabuf_tailroom(resp) <
1828 5 + query_len + wpabuf_len(bsrv->resp))
1829 return;
1830
1831 matches++;
1832
1833 /* Length (to be filled) */
1834 len_pos = wpabuf_put(resp, 2);
1835 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1836 wpabuf_put_u8(resp, srv_trans_id);
1837
1838 /* Status Code */
1839 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1840 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1841 wpabuf_head(bsrv->resp),
1842 wpabuf_len(bsrv->resp));
1843
1844 /* Response Data */
1845 wpabuf_put_data(resp, query, query_len); /* Key */
1846 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1847
1848 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1849 }
1850
1851 if (matches == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1853 "available");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001854 if (wpabuf_tailroom(resp) < 5)
1855 return;
1856
1857 /* Length (to be filled) */
1858 len_pos = wpabuf_put(resp, 2);
1859 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1860 wpabuf_put_u8(resp, srv_trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001861
1862 /* Status Code */
1863 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1864 /* Response Data: empty */
1865 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1866 2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001868}
1869
1870
1871static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1872 struct wpabuf *resp, u8 srv_trans_id)
1873{
1874 struct p2p_srv_upnp *usrv;
1875 u8 *len_pos;
1876
1877 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1878
1879 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1880 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1881 return;
1882 }
1883
1884 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1885 struct p2p_srv_upnp, list) {
1886 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1887 return;
1888
1889 /* Length (to be filled) */
1890 len_pos = wpabuf_put(resp, 2);
1891 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1892 wpabuf_put_u8(resp, srv_trans_id);
1893
1894 /* Status Code */
1895 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1896 /* Response Data */
1897 wpabuf_put_u8(resp, usrv->version);
1898 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1899 usrv->service);
1900 wpabuf_put_str(resp, usrv->service);
1901 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1902 2);
1903 }
1904}
1905
1906
1907static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1908 struct wpabuf *resp, u8 srv_trans_id,
1909 const u8 *query, size_t query_len)
1910{
1911 struct p2p_srv_upnp *usrv;
1912 u8 *len_pos;
1913 u8 version;
1914 char *str;
1915 int count = 0;
1916
1917 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1918 query, query_len);
1919
1920 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1921 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1922 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1923 srv_trans_id);
1924 return;
1925 }
1926
1927 if (query_len == 0) {
1928 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1929 return;
1930 }
1931
1932 if (wpabuf_tailroom(resp) < 5)
1933 return;
1934
1935 /* Length (to be filled) */
1936 len_pos = wpabuf_put(resp, 2);
1937 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1938 wpabuf_put_u8(resp, srv_trans_id);
1939
1940 version = query[0];
1941 str = os_malloc(query_len);
1942 if (str == NULL)
1943 return;
1944 os_memcpy(str, query + 1, query_len - 1);
1945 str[query_len - 1] = '\0';
1946
1947 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1948 struct p2p_srv_upnp, list) {
1949 if (version != usrv->version)
1950 continue;
1951
1952 if (os_strcmp(str, "ssdp:all") != 0 &&
1953 os_strstr(usrv->service, str) == NULL)
1954 continue;
1955
1956 if (wpabuf_tailroom(resp) < 2)
1957 break;
1958 if (count == 0) {
1959 /* Status Code */
1960 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1961 /* Response Data */
1962 wpabuf_put_u8(resp, version);
1963 } else
1964 wpabuf_put_u8(resp, ',');
1965
1966 count++;
1967
1968 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1969 usrv->service);
1970 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1971 break;
1972 wpabuf_put_str(resp, usrv->service);
1973 }
1974 os_free(str);
1975
1976 if (count == 0) {
1977 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1978 "available");
1979 /* Status Code */
1980 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1981 /* Response Data: empty */
1982 }
1983
1984 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1985}
1986
1987
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001988#ifdef CONFIG_WIFI_DISPLAY
1989static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1990 struct wpabuf *resp, u8 srv_trans_id,
1991 const u8 *query, size_t query_len)
1992{
1993 const u8 *pos;
1994 u8 role;
1995 u8 *len_pos;
1996
1997 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1998
1999 if (!wpa_s->global->wifi_display) {
2000 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2001 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2002 srv_trans_id);
2003 return;
2004 }
2005
2006 if (query_len < 1) {
2007 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2008 "Role");
2009 return;
2010 }
2011
2012 if (wpabuf_tailroom(resp) < 5)
2013 return;
2014
2015 pos = query;
2016 role = *pos++;
2017 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2018
2019 /* TODO: role specific handling */
2020
2021 /* Length (to be filled) */
2022 len_pos = wpabuf_put(resp, 2);
2023 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2024 wpabuf_put_u8(resp, srv_trans_id);
2025 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2026
2027 while (pos < query + query_len) {
2028 if (*pos < MAX_WFD_SUBELEMS &&
2029 wpa_s->global->wfd_subelem[*pos] &&
2030 wpabuf_tailroom(resp) >=
2031 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2032 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2033 "subelement %u", *pos);
2034 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2035 }
2036 pos++;
2037 }
2038
2039 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2040}
2041#endif /* CONFIG_WIFI_DISPLAY */
2042
2043
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2045 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2046{
2047 struct wpa_supplicant *wpa_s = ctx;
2048 const u8 *pos = tlvs;
2049 const u8 *end = tlvs + tlvs_len;
2050 const u8 *tlv_end;
2051 u16 slen;
2052 struct wpabuf *resp;
2053 u8 srv_proto, srv_trans_id;
2054 size_t buf_len;
2055 char *buf;
2056
2057 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2058 tlvs, tlvs_len);
2059 buf_len = 2 * tlvs_len + 1;
2060 buf = os_malloc(buf_len);
2061 if (buf) {
2062 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2063 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2064 MACSTR " %u %u %s",
2065 freq, MAC2STR(sa), dialog_token, update_indic,
2066 buf);
2067 os_free(buf);
2068 }
2069
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002070 if (wpa_s->p2p_sd_over_ctrl_iface) {
2071 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2072 update_indic, tlvs, tlvs_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 return; /* to be processed by an external program */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002074 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002075
2076 resp = wpabuf_alloc(10000);
2077 if (resp == NULL)
2078 return;
2079
2080 while (pos + 1 < end) {
2081 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2082 slen = WPA_GET_LE16(pos);
2083 pos += 2;
2084 if (pos + slen > end || slen < 2) {
2085 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2086 "length");
2087 wpabuf_free(resp);
2088 return;
2089 }
2090 tlv_end = pos + slen;
2091
2092 srv_proto = *pos++;
2093 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2094 srv_proto);
2095 srv_trans_id = *pos++;
2096 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2097 srv_trans_id);
2098
2099 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2100 pos, tlv_end - pos);
2101
2102
2103 if (wpa_s->force_long_sd) {
2104 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2105 "response");
2106 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2107 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2108 goto done;
2109 }
2110
2111 switch (srv_proto) {
2112 case P2P_SERV_ALL_SERVICES:
2113 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2114 "for all services");
2115 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2116 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2117 wpa_printf(MSG_DEBUG, "P2P: No service "
2118 "discovery protocols available");
2119 wpas_sd_add_proto_not_avail(
2120 resp, P2P_SERV_ALL_SERVICES,
2121 srv_trans_id);
2122 break;
2123 }
2124 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2125 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2126 break;
2127 case P2P_SERV_BONJOUR:
2128 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2129 pos, tlv_end - pos);
2130 break;
2131 case P2P_SERV_UPNP:
2132 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2133 pos, tlv_end - pos);
2134 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002135#ifdef CONFIG_WIFI_DISPLAY
2136 case P2P_SERV_WIFI_DISPLAY:
2137 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2138 pos, tlv_end - pos);
2139 break;
2140#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141 default:
2142 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2143 "protocol %u", srv_proto);
2144 wpas_sd_add_proto_not_avail(resp, srv_proto,
2145 srv_trans_id);
2146 break;
2147 }
2148
2149 pos = tlv_end;
2150 }
2151
2152done:
2153 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2154 update_indic, tlvs, tlvs_len);
2155
2156 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2157
2158 wpabuf_free(resp);
2159}
2160
2161
2162void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2163 const u8 *tlvs, size_t tlvs_len)
2164{
2165 struct wpa_supplicant *wpa_s = ctx;
2166 const u8 *pos = tlvs;
2167 const u8 *end = tlvs + tlvs_len;
2168 const u8 *tlv_end;
2169 u16 slen;
2170 size_t buf_len;
2171 char *buf;
2172
2173 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2174 tlvs, tlvs_len);
2175 if (tlvs_len > 1500) {
2176 /* TODO: better way for handling this */
2177 wpa_msg_ctrl(wpa_s, MSG_INFO,
2178 P2P_EVENT_SERV_DISC_RESP MACSTR
2179 " %u <long response: %u bytes>",
2180 MAC2STR(sa), update_indic,
2181 (unsigned int) tlvs_len);
2182 } else {
2183 buf_len = 2 * tlvs_len + 1;
2184 buf = os_malloc(buf_len);
2185 if (buf) {
2186 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2187 wpa_msg_ctrl(wpa_s, MSG_INFO,
2188 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2189 MAC2STR(sa), update_indic, buf);
2190 os_free(buf);
2191 }
2192 }
2193
2194 while (pos < end) {
2195 u8 srv_proto, srv_trans_id, status;
2196
2197 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2198 slen = WPA_GET_LE16(pos);
2199 pos += 2;
2200 if (pos + slen > end || slen < 3) {
2201 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2202 "length");
2203 return;
2204 }
2205 tlv_end = pos + slen;
2206
2207 srv_proto = *pos++;
2208 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2209 srv_proto);
2210 srv_trans_id = *pos++;
2211 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2212 srv_trans_id);
2213 status = *pos++;
2214 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2215 status);
2216
2217 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2218 pos, tlv_end - pos);
2219
2220 pos = tlv_end;
2221 }
2222
2223 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2224}
2225
2226
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002227u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2228 const struct wpabuf *tlvs)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229{
2230 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002231 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002233 return 0;
2234 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235}
2236
2237
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002238u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2239 u8 version, const char *query)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240{
2241 struct wpabuf *tlvs;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002242 u64 ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243
2244 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2245 if (tlvs == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002246 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2248 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2249 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2250 wpabuf_put_u8(tlvs, version);
2251 wpabuf_put_str(tlvs, query);
2252 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2253 wpabuf_free(tlvs);
2254 return ret;
2255}
2256
2257
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002258#ifdef CONFIG_WIFI_DISPLAY
2259
2260static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2261 const struct wpabuf *tlvs)
2262{
2263 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2264 return 0;
2265 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2266 return 0;
2267 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2268}
2269
2270
2271#define MAX_WFD_SD_SUBELEMS 20
2272
2273static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2274 const char *subelems)
2275{
2276 u8 *len;
2277 const char *pos;
2278 int val;
2279 int count = 0;
2280
2281 len = wpabuf_put(tlvs, 2);
2282 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2283 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2284
2285 wpabuf_put_u8(tlvs, role);
2286
2287 pos = subelems;
2288 while (*pos) {
2289 val = atoi(pos);
2290 if (val >= 0 && val < 256) {
2291 wpabuf_put_u8(tlvs, val);
2292 count++;
2293 if (count == MAX_WFD_SD_SUBELEMS)
2294 break;
2295 }
2296 pos = os_strchr(pos + 1, ',');
2297 if (pos == NULL)
2298 break;
2299 pos++;
2300 }
2301
2302 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2303}
2304
2305
2306u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2307 const u8 *dst, const char *role)
2308{
2309 struct wpabuf *tlvs;
2310 u64 ret;
2311 const char *subelems;
2312 u8 id = 1;
2313
2314 subelems = os_strchr(role, ' ');
2315 if (subelems == NULL)
2316 return 0;
2317 subelems++;
2318
2319 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2320 if (tlvs == NULL)
2321 return 0;
2322
2323 if (os_strstr(role, "[source]"))
2324 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2325 if (os_strstr(role, "[pri-sink]"))
2326 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2327 if (os_strstr(role, "[sec-sink]"))
2328 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2329 if (os_strstr(role, "[source+sink]"))
2330 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2331
2332 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2333 wpabuf_free(tlvs);
2334 return ret;
2335}
2336
2337#endif /* CONFIG_WIFI_DISPLAY */
2338
2339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002340int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341{
2342 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002343 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002344 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2345 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002346 return p2p_sd_cancel_request(wpa_s->global->p2p,
2347 (void *) (uintptr_t) req);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348}
2349
2350
2351void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2352 const u8 *dst, u8 dialog_token,
2353 const struct wpabuf *resp_tlvs)
2354{
2355 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2356 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2357 resp_tlvs);
2358 return;
2359 }
2360 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2361 return;
2362 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2363 resp_tlvs);
2364}
2365
Dmitry Shmidteaf261d2013-08-14 15:30:08 -07002366
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002367void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2368{
2369 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2370 wpa_drv_p2p_service_update(wpa_s);
2371 return;
2372 }
2373 if (wpa_s->global->p2p)
2374 p2p_sd_service_update(wpa_s->global->p2p);
2375}
2376
2377
2378static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2379{
2380 dl_list_del(&bsrv->list);
2381 wpabuf_free(bsrv->query);
2382 wpabuf_free(bsrv->resp);
2383 os_free(bsrv);
2384}
2385
2386
2387static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2388{
2389 dl_list_del(&usrv->list);
2390 os_free(usrv->service);
2391 os_free(usrv);
2392}
2393
2394
2395void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2396{
2397 struct p2p_srv_bonjour *bsrv, *bn;
2398 struct p2p_srv_upnp *usrv, *un;
2399
2400 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2401 struct p2p_srv_bonjour, list)
2402 wpas_p2p_srv_bonjour_free(bsrv);
2403
2404 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2405 struct p2p_srv_upnp, list)
2406 wpas_p2p_srv_upnp_free(usrv);
2407
2408 wpas_p2p_sd_service_update(wpa_s);
2409}
2410
2411
2412int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2413 struct wpabuf *query, struct wpabuf *resp)
2414{
2415 struct p2p_srv_bonjour *bsrv;
2416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 bsrv = os_zalloc(sizeof(*bsrv));
2418 if (bsrv == NULL)
2419 return -1;
2420 bsrv->query = query;
2421 bsrv->resp = resp;
2422 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2423
2424 wpas_p2p_sd_service_update(wpa_s);
2425 return 0;
2426}
2427
2428
2429int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2430 const struct wpabuf *query)
2431{
2432 struct p2p_srv_bonjour *bsrv;
2433
2434 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2435 if (bsrv == NULL)
2436 return -1;
2437 wpas_p2p_srv_bonjour_free(bsrv);
2438 wpas_p2p_sd_service_update(wpa_s);
2439 return 0;
2440}
2441
2442
2443int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2444 const char *service)
2445{
2446 struct p2p_srv_upnp *usrv;
2447
2448 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2449 return 0; /* Already listed */
2450 usrv = os_zalloc(sizeof(*usrv));
2451 if (usrv == NULL)
2452 return -1;
2453 usrv->version = version;
2454 usrv->service = os_strdup(service);
2455 if (usrv->service == NULL) {
2456 os_free(usrv);
2457 return -1;
2458 }
2459 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2460
2461 wpas_p2p_sd_service_update(wpa_s);
2462 return 0;
2463}
2464
2465
2466int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2467 const char *service)
2468{
2469 struct p2p_srv_upnp *usrv;
2470
2471 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2472 if (usrv == NULL)
2473 return -1;
2474 wpas_p2p_srv_upnp_free(usrv);
2475 wpas_p2p_sd_service_update(wpa_s);
2476 return 0;
2477}
2478
2479
2480static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2481 const u8 *peer, const char *params,
2482 unsigned int generated_pin)
2483{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002484 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2485 " %08d%s", MAC2STR(peer), generated_pin, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486}
2487
2488
2489static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2490 const u8 *peer, const char *params)
2491{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002492 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2493 "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494}
2495
2496
2497void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2498 const u8 *dev_addr, const u8 *pri_dev_type,
2499 const char *dev_name, u16 supp_config_methods,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002500 u8 dev_capab, u8 group_capab, const u8 *group_id,
2501 size_t group_id_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002502{
2503 struct wpa_supplicant *wpa_s = ctx;
2504 char devtype[WPS_DEV_TYPE_BUFSIZE];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002505 char params[300];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002506 u8 empty_dev_type[8];
2507 unsigned int generated_pin = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002508 struct wpa_supplicant *group = NULL;
2509
2510 if (group_id) {
2511 for (group = wpa_s->global->ifaces; group; group = group->next)
2512 {
2513 struct wpa_ssid *s = group->current_ssid;
2514 if (s != NULL &&
2515 s->mode == WPAS_MODE_P2P_GO &&
2516 group_id_len - ETH_ALEN == s->ssid_len &&
2517 os_memcmp(group_id + ETH_ALEN, s->ssid,
2518 s->ssid_len) == 0)
2519 break;
2520 }
2521 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002522
2523 if (pri_dev_type == NULL) {
2524 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2525 pri_dev_type = empty_dev_type;
2526 }
2527 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2528 " pri_dev_type=%s name='%s' config_methods=0x%x "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002529 "dev_capab=0x%x group_capab=0x%x%s%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 MAC2STR(dev_addr),
2531 wps_dev_type_bin2str(pri_dev_type, devtype,
2532 sizeof(devtype)),
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002533 dev_name, supp_config_methods, dev_capab, group_capab,
2534 group ? " group=" : "",
2535 group ? group->ifname : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 params[sizeof(params) - 1] = '\0';
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 if (config_methods & WPS_CONFIG_DISPLAY) {
2539 generated_pin = wps_generate_pin();
2540 wpas_prov_disc_local_display(wpa_s, peer, params,
2541 generated_pin);
2542 } else if (config_methods & WPS_CONFIG_KEYPAD)
2543 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2544 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002545 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2546 MACSTR "%s", MAC2STR(peer), params);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002547
2548 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2549 P2P_PROV_DISC_SUCCESS,
2550 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551}
2552
2553
2554void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2555{
2556 struct wpa_supplicant *wpa_s = ctx;
2557 unsigned int generated_pin = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002558 char params[20];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002560 if (wpa_s->pending_pd_before_join &&
2561 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2562 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2563 wpa_s->pending_pd_before_join = 0;
2564 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2565 "join-existing-group operation");
2566 wpas_p2p_join_start(wpa_s);
2567 return;
2568 }
2569
Dmitry Shmidt04949592012-07-19 12:16:46 -07002570 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2571 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2572 os_snprintf(params, sizeof(params), " peer_go=%d",
2573 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2574 else
2575 params[0] = '\0';
2576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 if (config_methods & WPS_CONFIG_DISPLAY)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002578 wpas_prov_disc_local_keypad(wpa_s, peer, params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 else if (config_methods & WPS_CONFIG_KEYPAD) {
2580 generated_pin = wps_generate_pin();
Dmitry Shmidt04949592012-07-19 12:16:46 -07002581 wpas_prov_disc_local_display(wpa_s, peer, params,
2582 generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002584 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2585 MACSTR "%s", MAC2STR(peer), params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586
Jouni Malinen75ecf522011-06-27 15:19:46 -07002587 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2588 P2P_PROV_DISC_SUCCESS,
2589 config_methods, generated_pin);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590}
2591
2592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002593static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2594 enum p2p_prov_disc_status status)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002595{
2596 struct wpa_supplicant *wpa_s = ctx;
2597
Dmitry Shmidt04949592012-07-19 12:16:46 -07002598 if (wpa_s->p2p_fallback_to_go_neg) {
2599 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2600 "failed - fall back to GO Negotiation");
2601 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2602 return;
2603 }
2604
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002605 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002606 wpa_s->pending_pd_before_join = 0;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002607 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2608 "join-existing-group operation (no ACK for PD "
2609 "Req attempts)");
2610 wpas_p2p_join_start(wpa_s);
2611 return;
Dmitry Shmidt2b380482012-09-13 10:52:13 -07002612 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002613
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002614 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2615 " p2p_dev_addr=" MACSTR " status=%d",
2616 MAC2STR(peer), status);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002617
Jouni Malinen75ecf522011-06-27 15:19:46 -07002618 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2619 status, 0, 0);
2620}
2621
2622
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002623static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2624{
2625 if (channels == NULL)
2626 return 1; /* Assume no restrictions */
2627 return p2p_channels_includes_freq(channels, freq);
2628
2629}
2630
2631
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2633 const u8 *go_dev_addr, const u8 *ssid,
2634 size_t ssid_len, int *go, u8 *group_bssid,
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002635 int *force_freq, int persistent_group,
2636 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637{
2638 struct wpa_supplicant *wpa_s = ctx;
2639 struct wpa_ssid *s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 int res;
2641 struct wpa_supplicant *grp;
2642
2643 if (!persistent_group) {
2644 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2645 " to join an active group", MAC2STR(sa));
2646 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2647 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2648 == 0 ||
2649 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2650 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2651 "authorized invitation");
2652 goto accept_inv;
2653 }
2654 /*
2655 * Do not accept the invitation automatically; notify user and
2656 * request approval.
2657 */
2658 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2659 }
2660
2661 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2662 if (grp) {
2663 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2664 "running persistent group");
2665 if (*go)
2666 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2667 goto accept_inv;
2668 }
2669
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002670 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2671 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2672 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2673 "invitation to re-invoke a persistent group");
2674 } else if (!wpa_s->conf->persistent_reconnect)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2676
2677 for (s = wpa_s->conf->ssid; s; s = s->next) {
2678 if (s->disabled == 2 &&
2679 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2680 s->ssid_len == ssid_len &&
2681 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2682 break;
2683 }
2684
2685 if (!s) {
2686 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2687 " requested reinvocation of an unknown group",
2688 MAC2STR(sa));
2689 return P2P_SC_FAIL_UNKNOWN_GROUP;
2690 }
2691
2692 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2693 *go = 1;
2694 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2695 wpa_printf(MSG_DEBUG, "P2P: The only available "
2696 "interface is already in use - reject "
2697 "invitation");
2698 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2699 }
2700 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2701 } else if (s->mode == WPAS_MODE_P2P_GO) {
2702 *go = 1;
2703 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2704 {
2705 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2706 "interface address for the group");
2707 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2708 }
2709 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2710 ETH_ALEN);
2711 }
2712
2713accept_inv:
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002714 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2715
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002716 /* Get one of the frequencies currently in use */
2717 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2718 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 *force_freq = res;
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002720 wpas_p2p_set_own_freq_preference(wpa_s, res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002721 }
2722
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07002723 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2724 wpas_p2p_num_unused_channels(wpa_s) > 0) {
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002725 if (*go == 0) {
2726 /* We are the client */
2727 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2728 "running a GO but we are capable of MCC, "
2729 "figure out the best channel to use");
2730 *force_freq = 0;
2731 } else if (!freq_included(channels, *force_freq)) {
2732 /* We are the GO, and *force_freq is not in the
2733 * intersection */
2734 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2735 "in intersection but we are capable of MCC, "
2736 "figure out the best channel to use",
2737 *force_freq);
2738 *force_freq = 0;
2739 }
2740 }
2741
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002742 return P2P_SC_SUCCESS;
2743}
2744
2745
2746static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2747 const u8 *ssid, size_t ssid_len,
2748 const u8 *go_dev_addr, u8 status,
2749 int op_freq)
2750{
2751 struct wpa_supplicant *wpa_s = ctx;
2752 struct wpa_ssid *s;
2753
2754 for (s = wpa_s->conf->ssid; s; s = s->next) {
2755 if (s->disabled == 2 &&
2756 s->ssid_len == ssid_len &&
2757 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2758 break;
2759 }
2760
2761 if (status == P2P_SC_SUCCESS) {
2762 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2763 " was accepted; op_freq=%d MHz",
2764 MAC2STR(sa), op_freq);
2765 if (s) {
Dmitry Shmidt91c40cd2012-09-25 14:23:53 -07002766 int go = s->mode == WPAS_MODE_P2P_GO;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 wpas_p2p_group_add_persistent(
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002768 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002769 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 } else if (bssid) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002771 wpa_s->user_initiated_pd = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07002773 wpa_s->p2p_wps_method, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002774 }
2775 return;
2776 }
2777
2778 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2779 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2780 " was rejected (status %u)", MAC2STR(sa), status);
2781 return;
2782 }
2783
2784 if (!s) {
2785 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002786 wpa_msg_global(wpa_s, MSG_INFO,
2787 P2P_EVENT_INVITATION_RECEIVED
2788 "sa=" MACSTR " go_dev_addr=" MACSTR
2789 " bssid=" MACSTR " unknown-network",
2790 MAC2STR(sa), MAC2STR(go_dev_addr),
2791 MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002792 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002793 wpa_msg_global(wpa_s, MSG_INFO,
2794 P2P_EVENT_INVITATION_RECEIVED
2795 "sa=" MACSTR " go_dev_addr=" MACSTR
2796 " unknown-network",
2797 MAC2STR(sa), MAC2STR(go_dev_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002798 }
2799 return;
2800 }
2801
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002802 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002803 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2804 "sa=" MACSTR " persistent=%d freq=%d",
2805 MAC2STR(sa), s->id, op_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002806 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002807 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2808 "sa=" MACSTR " persistent=%d",
2809 MAC2STR(sa), s->id);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002810 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002811}
2812
2813
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002814static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2815 struct wpa_ssid *ssid,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002816 const u8 *peer, int inv)
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002817{
2818 size_t i;
2819
2820 if (ssid == NULL)
2821 return;
2822
2823 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2824 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2825 ETH_ALEN) == 0)
2826 break;
2827 }
2828 if (i >= ssid->num_p2p_clients) {
2829 if (ssid->mode != WPAS_MODE_P2P_GO &&
2830 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2831 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2832 "due to invitation result", ssid->id);
2833 wpas_notify_network_removed(wpa_s, ssid);
2834 wpa_config_remove_network(wpa_s->conf, ssid->id);
2835 return;
2836 }
2837 return; /* Peer not found in client list */
2838 }
2839
2840 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002841 "group %d client list%s",
2842 MAC2STR(peer), ssid->id,
2843 inv ? " due to invitation result" : "");
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002844 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2845 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2846 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2847 ssid->num_p2p_clients--;
2848#ifndef CONFIG_NO_CONFIG_WRITE
2849 if (wpa_s->parent->conf->update_config &&
2850 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2851 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2852#endif /* CONFIG_NO_CONFIG_WRITE */
2853}
2854
2855
2856static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2857 const u8 *peer)
2858{
2859 struct wpa_ssid *ssid;
2860
2861 wpa_s = wpa_s->global->p2p_invite_group;
2862 if (wpa_s == NULL)
2863 return; /* No known invitation group */
2864 ssid = wpa_s->current_ssid;
2865 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2866 !ssid->p2p_persistent_group)
2867 return; /* Not operating as a GO in persistent group */
2868 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2869 ssid->ssid, ssid->ssid_len);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002870 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002871}
2872
2873
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002874static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002875 const struct p2p_channels *channels,
2876 const u8 *peer)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002877{
2878 struct wpa_supplicant *wpa_s = ctx;
2879 struct wpa_ssid *ssid;
2880
2881 if (bssid) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002882 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2883 "status=%d " MACSTR,
2884 status, MAC2STR(bssid));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002886 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2887 "status=%d ", status);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002888 }
2889 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2890
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002891 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2892 status, MAC2STR(peer));
2893 if (wpa_s->pending_invite_ssid_id == -1) {
2894 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2895 wpas_remove_persistent_client(wpa_s, peer);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 return; /* Invitation to active group */
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08002899 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2900 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2901 "invitation exchange to indicate readiness for "
2902 "re-invocation");
2903 }
2904
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002905 if (status != P2P_SC_SUCCESS) {
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002906 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2907 ssid = wpa_config_get_network(
2908 wpa_s->conf, wpa_s->pending_invite_ssid_id);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002909 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07002910 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 wpas_p2p_remove_pending_group_interface(wpa_s);
2912 return;
2913 }
2914
2915 ssid = wpa_config_get_network(wpa_s->conf,
2916 wpa_s->pending_invite_ssid_id);
2917 if (ssid == NULL) {
2918 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2919 "data matching with invitation");
2920 return;
2921 }
2922
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002923 /*
2924 * The peer could have missed our ctrl::ack frame for Invitation
2925 * Response and continue retransmitting the frame. To reduce the
2926 * likelihood of the peer not getting successful TX status for the
2927 * Invitation Response frame, wait a short time here before starting
2928 * the persistent group so that we will remain on the current channel to
2929 * acknowledge any possible retransmission from the peer.
2930 */
Irfan Sheriff81931b82012-10-16 21:40:46 -07002931#ifndef ANDROID_P2P
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2933 "starting persistent group");
2934 os_sleep(0, 50000);
Irfan Sheriff81931b82012-10-16 21:40:46 -07002935#else
2936 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 100 ms wait on current channel before "
2937 "starting persistent group");
2938 os_sleep(0, 100000);
2939#endif
Jouni Malinen2c5b17d2012-10-13 21:52:46 -07002940
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 wpas_p2p_group_add_persistent(wpa_s, ssid,
Jouni Malinen31be0a42012-08-31 21:20:51 +03002942 ssid->mode == WPAS_MODE_P2P_GO,
2943 wpa_s->p2p_persistent_go_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002944 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2945 channels,
Dmitry Shmidt56052862013-10-04 10:23:25 -07002946 ssid->mode == WPAS_MODE_P2P_GO ?
2947 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2948 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002949}
2950
2951
Dmitry Shmidt04949592012-07-19 12:16:46 -07002952static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2953 unsigned int freq)
2954{
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -07002955 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002956}
2957
2958
2959static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2960{
2961 reg->channel[reg->channels] = chan;
2962 reg->channels++;
2963}
2964
2965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002967 struct p2p_channels *chan,
2968 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969{
2970 int i, cla = 0;
2971
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002972 os_memset(cli_chan, 0, sizeof(*cli_chan));
2973
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2975 "band");
2976
2977 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2978 chan->reg_class[cla].reg_class = 81;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002979 chan->reg_class[cla].channels = 0;
2980 for (i = 0; i < 11; i++) {
2981 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2982 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2983 }
2984 if (chan->reg_class[cla].channels)
2985 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002986
2987 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2988 "band");
2989
2990 /* Operating class 115 - 5 GHz, channels 36-48 */
2991 chan->reg_class[cla].reg_class = 115;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002992 chan->reg_class[cla].channels = 0;
2993 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2994 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2995 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2996 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2997 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2998 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2999 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3000 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3001 if (chan->reg_class[cla].channels)
3002 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003
3004 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3005 "band");
3006
3007 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3008 chan->reg_class[cla].reg_class = 124;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003009 chan->reg_class[cla].channels = 0;
3010 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3011 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3012 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3013 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3014 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3015 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3016 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3017 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3018 if (chan->reg_class[cla].channels)
3019 cla++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020
3021 chan->reg_classes = cla;
3022 return 0;
3023}
3024
3025
3026static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3027 u16 num_modes,
3028 enum hostapd_hw_mode mode)
3029{
3030 u16 i;
3031
3032 for (i = 0; i < num_modes; i++) {
3033 if (modes[i].mode == mode)
3034 return &modes[i];
3035 }
3036
3037 return NULL;
3038}
3039
3040
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003041enum chan_allowed {
3042 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3043};
3044
Dmitry Shmidt04949592012-07-19 12:16:46 -07003045static int has_channel(struct wpa_global *global,
3046 struct hostapd_hw_modes *mode, u8 chan, int *flags)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047{
3048 int i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003049 unsigned int freq;
3050
3051 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3052 chan * 5;
3053 if (wpas_p2p_disallowed_freq(global, freq))
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003054 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055
3056 for (i = 0; i < mode->num_channels; i++) {
3057 if (mode->channels[i].chan == chan) {
3058 if (flags)
3059 *flags = mode->channels[i].flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003060 if (mode->channels[i].flag &
3061 (HOSTAPD_CHAN_DISABLED |
3062 HOSTAPD_CHAN_RADAR))
3063 return NOT_ALLOWED;
3064 if (mode->channels[i].flag &
3065 (HOSTAPD_CHAN_PASSIVE_SCAN |
3066 HOSTAPD_CHAN_NO_IBSS))
3067 return PASSIVE_ONLY;
3068 return ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003069 }
3070 }
3071
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003072 return NOT_ALLOWED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073}
3074
3075
3076struct p2p_oper_class_map {
3077 enum hostapd_hw_mode mode;
3078 u8 op_class;
3079 u8 min_chan;
3080 u8 max_chan;
3081 u8 inc;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003082 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083};
3084
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003085static struct p2p_oper_class_map op_class[] = {
3086 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003087#if 0 /* Do not enable HT40 on 2 GHz for now */
3088 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3089 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3090#endif
3091 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3092 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3093 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3094 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3095 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3096 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003097
3098 /*
3099 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3100 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3101 * 80 MHz, but currently use the following definition for simplicity
3102 * (these center frequencies are not actual channels, which makes
3103 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3104 * removing invalid channels.
3105 */
3106 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003107 { -1, 0, 0, 0, 0, BW20 }
3108};
3109
3110
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003111static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3112 struct hostapd_hw_modes *mode,
3113 u8 channel)
3114{
3115 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3116 unsigned int i;
3117
3118 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3119 return 0;
3120
3121 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3122 /*
3123 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3124 * so the center channel is 6 channels away from the start/end.
3125 */
3126 if (channel >= center_channels[i] - 6 &&
3127 channel <= center_channels[i] + 6)
3128 return center_channels[i];
3129
3130 return 0;
3131}
3132
3133
3134static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3135 struct hostapd_hw_modes *mode,
3136 u8 channel, u8 bw)
3137{
3138 u8 center_chan;
3139 int i, flags;
3140 enum chan_allowed res, ret = ALLOWED;
3141
3142 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3143 if (!center_chan)
3144 return NOT_ALLOWED;
3145 if (center_chan >= 58 && center_chan <= 138)
3146 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3147
3148 /* check all the channels are available */
3149 for (i = 0; i < 4; i++) {
3150 int adj_chan = center_chan - 6 + i * 4;
3151
3152 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3153 if (res == NOT_ALLOWED)
3154 return NOT_ALLOWED;
3155 if (res == PASSIVE_ONLY)
3156 ret = PASSIVE_ONLY;
3157
3158 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3159 return NOT_ALLOWED;
3160 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3161 return NOT_ALLOWED;
3162 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3163 return NOT_ALLOWED;
3164 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3165 return NOT_ALLOWED;
3166 }
3167
3168 return ret;
3169}
3170
3171
3172static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3173 struct hostapd_hw_modes *mode,
3174 u8 channel, u8 bw)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003175{
3176 int flag;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003177 enum chan_allowed res, res2;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003178
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003179 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3180 if (bw == BW40MINUS) {
3181 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3182 return NOT_ALLOWED;
3183 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3184 } else if (bw == BW40PLUS) {
3185 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3186 return NOT_ALLOWED;
3187 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3188 } else if (bw == BW80) {
3189 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3190 }
3191
3192 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3193 return NOT_ALLOWED;
3194 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3195 return PASSIVE_ONLY;
3196 return res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003197}
3198
3199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003201 struct p2p_channels *chan,
3202 struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003204 struct hostapd_hw_modes *mode;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003205 int cla, op, cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003206
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003207 if (wpa_s->hw.modes == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3209 "of all supported channels; assume dualband "
3210 "support");
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003211 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 }
3213
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003214 cla = cli_cla = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003215
3216 for (op = 0; op_class[op].op_class; op++) {
3217 struct p2p_oper_class_map *o = &op_class[op];
3218 u8 ch;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003219 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003221 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003222 if (mode == NULL)
3223 continue;
3224 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003225 enum chan_allowed res;
3226 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3227 if (res == ALLOWED) {
3228 if (reg == NULL) {
3229 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3230 o->op_class);
3231 reg = &chan->reg_class[cla];
3232 cla++;
3233 reg->reg_class = o->op_class;
3234 }
3235 reg->channel[reg->channels] = ch;
3236 reg->channels++;
3237 } else if (res == PASSIVE_ONLY &&
3238 wpa_s->conf->p2p_add_cli_chan) {
3239 if (cli_reg == NULL) {
3240 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3241 o->op_class);
3242 cli_reg = &cli_chan->reg_class[cli_cla];
3243 cli_cla++;
3244 cli_reg->reg_class = o->op_class;
3245 }
3246 cli_reg->channel[cli_reg->channels] = ch;
3247 cli_reg->channels++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003248 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003249 }
3250 if (reg) {
3251 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3252 reg->channel, reg->channels);
3253 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003254 if (cli_reg) {
3255 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3256 cli_reg->channel, cli_reg->channels);
3257 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 }
3259
3260 chan->reg_classes = cla;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003261 cli_chan->reg_classes = cli_cla;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003262
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003263 return 0;
3264}
3265
3266
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003267int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3268 struct hostapd_hw_modes *mode, u8 channel)
3269{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003270 int op;
3271 enum chan_allowed ret;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003272
3273 for (op = 0; op_class[op].op_class; op++) {
3274 struct p2p_oper_class_map *o = &op_class[op];
3275 u8 ch;
3276
3277 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3278 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3279 o->bw == BW20 || ch != channel)
3280 continue;
3281 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003282 if (ret == ALLOWED)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003283 return (o->bw == BW40MINUS) ? -1 : 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003284 }
3285 }
3286 return 0;
3287}
3288
3289
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003290int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3291 struct hostapd_hw_modes *mode, u8 channel)
3292{
3293 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3294 return 0;
3295
3296 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3297}
3298
3299
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003300static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3301 size_t buf_len)
3302{
3303 struct wpa_supplicant *wpa_s = ctx;
3304
3305 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3306 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3307 break;
3308 }
3309 if (wpa_s == NULL)
3310 return -1;
3311
3312 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3313}
3314
3315
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003316static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3317{
3318 struct wpa_supplicant *wpa_s = ctx;
3319
3320 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3321 struct wpa_ssid *ssid = wpa_s->current_ssid;
3322 if (ssid == NULL)
3323 continue;
3324 if (ssid->mode != WPAS_MODE_INFRA)
3325 continue;
3326 if (wpa_s->wpa_state != WPA_COMPLETED &&
3327 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3328 continue;
3329 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3330 return 1;
3331 }
3332
3333 return 0;
3334}
3335
3336
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003337static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3338{
3339 struct wpa_supplicant *wpa_s = ctx;
3340 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3341}
3342
3343
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003344int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3345{
3346 struct wpa_interface iface;
3347 struct wpa_supplicant *p2pdev_wpa_s;
3348 char ifname[100];
3349 char force_name[100];
3350 int ret;
3351
3352 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3353 wpa_s->ifname);
3354 force_name[0] = '\0';
3355 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3356 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3357 force_name, wpa_s->pending_interface_addr, NULL);
3358 if (ret < 0) {
3359 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3360 return ret;
3361 }
3362 os_strlcpy(wpa_s->pending_interface_name, ifname,
3363 sizeof(wpa_s->pending_interface_name));
3364
3365 os_memset(&iface, 0, sizeof(iface));
3366 iface.p2p_mgmt = 1;
3367 iface.ifname = wpa_s->pending_interface_name;
3368 iface.driver = wpa_s->driver->name;
3369 iface.driver_param = wpa_s->conf->driver_param;
3370 iface.confname = wpa_s->confname;
3371 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3372 if (!p2pdev_wpa_s) {
3373 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3374 return -1;
3375 }
3376 p2pdev_wpa_s->parent = wpa_s;
3377
3378 wpa_s->pending_interface_name[0] = '\0';
3379 return 0;
3380}
3381
3382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003383/**
3384 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3385 * @global: Pointer to global data from wpa_supplicant_init()
3386 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3387 * Returns: 0 on success, -1 on failure
3388 */
3389int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3390{
3391 struct p2p_config p2p;
3392 unsigned int r;
3393 int i;
3394
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003395 if (wpa_s->conf->p2p_disabled)
3396 return 0;
3397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003398 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3399 return 0;
3400
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401 if (global->p2p)
3402 return 0;
3403
3404 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3405 struct p2p_params params;
3406
3407 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3408 os_memset(&params, 0, sizeof(params));
3409 params.dev_name = wpa_s->conf->device_name;
3410 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3411 WPS_DEV_TYPE_LEN);
3412 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3413 os_memcpy(params.sec_dev_type,
3414 wpa_s->conf->sec_device_type,
3415 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3416
3417 if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
3418 return -1;
3419
3420 return 0;
3421 }
3422
3423 os_memset(&p2p, 0, sizeof(p2p));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424 p2p.cb_ctx = wpa_s;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003425 p2p.debug_print = wpas_p2p_debug_print;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003426 p2p.p2p_scan = wpas_p2p_scan;
3427 p2p.send_action = wpas_send_action;
3428 p2p.send_action_done = wpas_send_action_done;
3429 p2p.go_neg_completed = wpas_go_neg_completed;
3430 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3431 p2p.dev_found = wpas_dev_found;
3432 p2p.dev_lost = wpas_dev_lost;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003433 p2p.find_stopped = wpas_find_stopped;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 p2p.start_listen = wpas_start_listen;
3435 p2p.stop_listen = wpas_stop_listen;
3436 p2p.send_probe_resp = wpas_send_probe_resp;
3437 p2p.sd_request = wpas_sd_request;
3438 p2p.sd_response = wpas_sd_response;
3439 p2p.prov_disc_req = wpas_prov_disc_req;
3440 p2p.prov_disc_resp = wpas_prov_disc_resp;
Jouni Malinen75ecf522011-06-27 15:19:46 -07003441 p2p.prov_disc_fail = wpas_prov_disc_fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003442 p2p.invitation_process = wpas_invitation_process;
3443 p2p.invitation_received = wpas_invitation_received;
3444 p2p.invitation_result = wpas_invitation_result;
3445 p2p.get_noa = wpas_get_noa;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003446 p2p.go_connected = wpas_go_connected;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003447
3448 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003449 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003450 p2p.dev_name = wpa_s->conf->device_name;
3451 p2p.manufacturer = wpa_s->conf->manufacturer;
3452 p2p.model_name = wpa_s->conf->model_name;
3453 p2p.model_number = wpa_s->conf->model_number;
3454 p2p.serial_number = wpa_s->conf->serial_number;
3455 if (wpa_s->wps) {
3456 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3457 p2p.config_methods = wpa_s->wps->config_methods;
3458 }
3459
3460 if (wpa_s->conf->p2p_listen_reg_class &&
3461 wpa_s->conf->p2p_listen_channel) {
3462 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3463 p2p.channel = wpa_s->conf->p2p_listen_channel;
3464 } else {
3465 p2p.reg_class = 81;
3466 /*
3467 * Pick one of the social channels randomly as the listen
3468 * channel.
3469 */
3470 os_get_random((u8 *) &r, sizeof(r));
3471 p2p.channel = 1 + (r % 3) * 5;
3472 }
3473 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3474
3475 if (wpa_s->conf->p2p_oper_reg_class &&
3476 wpa_s->conf->p2p_oper_channel) {
3477 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3478 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3479 p2p.cfg_op_channel = 1;
3480 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3481 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3482
3483 } else {
3484 p2p.op_reg_class = 81;
3485 /*
3486 * Use random operation channel from (1, 6, 11) if no other
3487 * preference is indicated.
3488 */
3489 os_get_random((u8 *) &r, sizeof(r));
3490 p2p.op_channel = 1 + (r % 3) * 5;
3491 p2p.cfg_op_channel = 0;
3492 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3493 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3494 }
Dmitry Shmidt44c95782013-05-17 09:51:35 -07003495
3496 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3497 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3498 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3499 }
3500
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003501 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3502 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3503 p2p.country[2] = 0x04;
3504 } else
3505 os_memcpy(p2p.country, "XX\x04", 3);
3506
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003507 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003508 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3509 "channel list");
3510 return -1;
3511 }
3512
3513 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3514 WPS_DEV_TYPE_LEN);
3515
3516 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3517 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3518 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3519
3520 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3521 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3522
3523 p2p.max_peers = 100;
3524
3525 if (wpa_s->conf->p2p_ssid_postfix) {
3526 p2p.ssid_postfix_len =
3527 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3528 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3529 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3530 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3531 p2p.ssid_postfix_len);
3532 }
3533
3534 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3535
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003536 p2p.max_listen = wpa_s->max_remain_on_chan;
3537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 global->p2p = p2p_init(&p2p);
3539 if (global->p2p == NULL)
3540 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003541 global->p2p_init_wpa_s = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542
3543 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3544 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3545 continue;
3546 p2p_add_wps_vendor_extension(
3547 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3548 }
3549
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003550 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 return 0;
3553}
3554
3555
3556/**
3557 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3558 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3559 *
3560 * This function deinitialize per-interface P2P data.
3561 */
3562void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3563{
3564 if (wpa_s->driver && wpa_s->drv_priv)
3565 wpa_drv_probe_req_report(wpa_s, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003566
3567 if (wpa_s->go_params) {
3568 /* Clear any stored provisioning info */
3569 p2p_clear_provisioning_info(
3570 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003571 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003572 }
3573
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003574 os_free(wpa_s->go_params);
3575 wpa_s->go_params = NULL;
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07003576#ifdef ANDROID_P2P
3577 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3578#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003579 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3580 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3581 wpa_s->p2p_long_listen = 0;
3582 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3583 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3584 wpas_p2p_remove_pending_group_interface(wpa_s);
3585
3586 /* TODO: remove group interface from the driver if this wpa_s instance
3587 * is on top of a P2P group interface */
3588}
3589
3590
3591/**
3592 * wpas_p2p_deinit_global - Deinitialize global P2P module
3593 * @global: Pointer to global data from wpa_supplicant_init()
3594 *
3595 * This function deinitializes the global (per device) P2P module.
3596 */
3597void wpas_p2p_deinit_global(struct wpa_global *global)
3598{
3599 struct wpa_supplicant *wpa_s, *tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003600
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003601 wpa_s = global->ifaces;
3602 if (wpa_s)
3603 wpas_p2p_service_flush(wpa_s);
3604
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003605 if (global->p2p == NULL)
3606 return;
3607
3608 /* Remove remaining P2P group interfaces */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3610 wpa_s = wpa_s->next;
3611 while (wpa_s) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 tmp = global->ifaces;
3613 while (tmp &&
3614 (tmp == wpa_s ||
3615 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3616 tmp = tmp->next;
3617 }
3618 if (tmp == NULL)
3619 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003620 /* Disconnect from the P2P group and deinit the interface */
3621 wpas_p2p_disconnect(tmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 }
3623
3624 /*
3625 * Deinit GO data on any possibly remaining interface (if main
3626 * interface is used as GO).
3627 */
3628 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3629 if (wpa_s->ap_iface)
3630 wpas_p2p_group_deinit(wpa_s);
3631 }
3632
3633 p2p_deinit(global->p2p);
3634 global->p2p = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003635 global->p2p_init_wpa_s = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636}
3637
3638
3639static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3640{
Dmitry Shmidt34af3062013-07-11 10:46:32 -07003641 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3642 wpa_s->conf->p2p_no_group_iface)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003643 return 0; /* separate interface disabled per configuration */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003644 if (wpa_s->drv_flags &
3645 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3646 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3647 return 1; /* P2P group requires a new interface in every case
3648 */
3649 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3650 return 0; /* driver does not support concurrent operations */
3651 if (wpa_s->global->ifaces->next)
3652 return 1; /* more that one interface already in use */
3653 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3654 return 1; /* this interface is already in use */
3655 return 0;
3656}
3657
3658
3659static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3660 const u8 *peer_addr,
3661 enum p2p_wps_method wps_method,
3662 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003663 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003664 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003666 if (persistent_group && wpa_s->conf->persistent_reconnect)
3667 persistent_group = 2;
3668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003669 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3670 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3671 go_intent, own_interface_addr,
3672 force_freq, persistent_group);
3673 }
3674
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003675 /*
3676 * Increase GO config timeout if HT40 is used since it takes some time
3677 * to scan channels for coex purposes before the BSS can be started.
3678 */
3679 p2p_set_config_timeout(wpa_s->global->p2p,
3680 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003682 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3683 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003684 persistent_group, ssid ? ssid->ssid : NULL,
3685 ssid ? ssid->ssid_len : 0,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003686 wpa_s->p2p_pd_before_go_neg, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003687}
3688
3689
3690static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3691 const u8 *peer_addr,
3692 enum p2p_wps_method wps_method,
3693 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003694 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003695 struct wpa_ssid *ssid, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003697 if (persistent_group && wpa_s->conf->persistent_reconnect)
3698 persistent_group = 2;
3699
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003700 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3701 return -1;
3702
3703 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3704 go_intent, own_interface_addr, force_freq,
Dmitry Shmidt04949592012-07-19 12:16:46 -07003705 persistent_group, ssid ? ssid->ssid : NULL,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003706 ssid ? ssid->ssid_len : 0, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003707}
3708
3709
3710static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3711{
3712 wpa_s->p2p_join_scan_count++;
3713 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3714 wpa_s->p2p_join_scan_count);
3715 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3716 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3717 " for join operationg - stop join attempt",
3718 MAC2STR(wpa_s->pending_join_iface_addr));
3719 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003720 if (wpa_s->p2p_auto_pd) {
3721 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003722 wpa_msg_global(wpa_s, MSG_INFO,
3723 P2P_EVENT_PROV_DISC_FAILURE
3724 " p2p_dev_addr=" MACSTR " status=N/A",
3725 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003726 return;
3727 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003728 wpa_msg_global(wpa_s->parent, MSG_INFO,
3729 P2P_EVENT_GROUP_FORMATION_FAILURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003730 }
3731}
3732
3733
Dmitry Shmidt04949592012-07-19 12:16:46 -07003734static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3735{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003736 int *freqs, res, num, i;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003737
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003738 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3739 /* Multiple channels are supported and not all are in use */
Dmitry Shmidt04949592012-07-19 12:16:46 -07003740 return 0;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003741 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003742
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003743 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3744 if (!freqs)
3745 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003746
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003747 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3748 wpa_s->num_multichan_concurrent);
3749 if (num < 0) {
3750 res = 1;
3751 goto exit_free;
3752 }
3753
3754 for (i = 0; i < num; i++) {
3755 if (freqs[i] == freq) {
3756 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3757 freq);
3758 res = 0;
3759 goto exit_free;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003760 }
3761 }
3762
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003763 res = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003764
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07003765exit_free:
3766 os_free(freqs);
3767 return res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003768}
3769
3770
3771static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3772 const u8 *peer_dev_addr)
3773{
3774 struct wpa_bss *bss;
3775 int updated;
3776
3777 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3778 if (bss == NULL)
3779 return -1;
3780 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3781 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3782 "last scan");
3783 return 0;
3784 }
3785
3786 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3787 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3788 "%ld.%06ld (%supdated in last scan)",
3789 bss->last_update.sec, bss->last_update.usec,
3790 updated ? "": "not ");
3791
3792 return updated;
3793}
3794
3795
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003796static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3797 struct wpa_scan_results *scan_res)
3798{
3799 struct wpa_bss *bss;
3800 int freq;
3801 u8 iface_addr[ETH_ALEN];
Dmitry Shmidt04949592012-07-19 12:16:46 -07003802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003803 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3804
3805 if (wpa_s->global->p2p_disabled)
3806 return;
3807
Dmitry Shmidt04949592012-07-19 12:16:46 -07003808 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3809 scan_res ? (int) scan_res->num : -1,
3810 wpa_s->p2p_auto_join ? "auto_" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811
3812 if (scan_res)
3813 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3814
Dmitry Shmidt04949592012-07-19 12:16:46 -07003815 if (wpa_s->p2p_auto_pd) {
3816 int join = wpas_p2p_peer_go(wpa_s,
3817 wpa_s->pending_join_dev_addr);
3818 if (join == 0 &&
3819 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3820 wpa_s->auto_pd_scan_retry++;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003821 bss = wpa_bss_get_bssid_latest(
3822 wpa_s, wpa_s->pending_join_dev_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003823 if (bss) {
3824 freq = bss->freq;
3825 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3826 "the peer " MACSTR " at %d MHz",
3827 wpa_s->auto_pd_scan_retry,
3828 MAC2STR(wpa_s->
3829 pending_join_dev_addr),
3830 freq);
3831 wpas_p2p_join_scan_req(wpa_s, freq);
3832 return;
3833 }
3834 }
3835
3836 if (join < 0)
3837 join = 0;
3838
3839 wpa_s->p2p_auto_pd = 0;
3840 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3841 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3842 MAC2STR(wpa_s->pending_join_dev_addr), join);
3843 if (p2p_prov_disc_req(wpa_s->global->p2p,
3844 wpa_s->pending_join_dev_addr,
3845 wpa_s->pending_pd_config_methods, join,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003846 0, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07003847 wpa_s->p2p_auto_pd = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003848 wpa_msg_global(wpa_s, MSG_INFO,
3849 P2P_EVENT_PROV_DISC_FAILURE
3850 " p2p_dev_addr=" MACSTR " status=N/A",
3851 MAC2STR(wpa_s->pending_join_dev_addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -07003852 }
3853 return;
3854 }
3855
3856 if (wpa_s->p2p_auto_join) {
3857 int join = wpas_p2p_peer_go(wpa_s,
3858 wpa_s->pending_join_dev_addr);
3859 if (join < 0) {
3860 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3861 "running a GO -> use GO Negotiation");
3862 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3863 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3864 wpa_s->p2p_persistent_group, 0, 0, 0,
3865 wpa_s->p2p_go_intent,
3866 wpa_s->p2p_connect_freq,
3867 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003868 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003869 wpa_s->p2p_go_ht40,
3870 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003871 return;
3872 }
3873
3874 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3875 "try to join the group", join ? "" :
3876 " in older scan");
3877 if (!join)
3878 wpa_s->p2p_fallback_to_go_neg = 1;
3879 }
3880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003881 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3882 wpa_s->pending_join_iface_addr);
3883 if (freq < 0 &&
3884 p2p_get_interface_addr(wpa_s->global->p2p,
3885 wpa_s->pending_join_dev_addr,
3886 iface_addr) == 0 &&
3887 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3888 {
3889 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3890 "address for join from " MACSTR " to " MACSTR
3891 " based on newly discovered P2P peer entry",
3892 MAC2STR(wpa_s->pending_join_iface_addr),
3893 MAC2STR(iface_addr));
3894 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3895 ETH_ALEN);
3896
3897 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3898 wpa_s->pending_join_iface_addr);
3899 }
3900 if (freq >= 0) {
3901 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3902 "from P2P peer table: %d MHz", freq);
3903 }
Dmitry Shmidt444d5672013-04-01 13:08:44 -07003904 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003905 if (bss) {
3906 freq = bss->freq;
3907 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
Dmitry Shmidt051af732013-10-22 13:52:46 -07003908 "from BSS table: %d MHz (SSID %s)", freq,
3909 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003910 }
3911 if (freq > 0) {
3912 u16 method;
3913
Dmitry Shmidt04949592012-07-19 12:16:46 -07003914 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003915 wpa_msg_global(wpa_s->parent, MSG_INFO,
3916 P2P_EVENT_GROUP_FORMATION_FAILURE
3917 "reason=FREQ_CONFLICT");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003918 return;
3919 }
3920
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3922 "prior to joining an existing group (GO " MACSTR
3923 " freq=%u MHz)",
3924 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3925 wpa_s->pending_pd_before_join = 1;
3926
3927 switch (wpa_s->pending_join_wps_method) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003928 case WPS_PIN_DISPLAY:
3929 method = WPS_CONFIG_KEYPAD;
3930 break;
3931 case WPS_PIN_KEYPAD:
3932 method = WPS_CONFIG_DISPLAY;
3933 break;
3934 case WPS_PBC:
3935 method = WPS_CONFIG_PUSHBUTTON;
3936 break;
3937 default:
3938 method = 0;
3939 break;
3940 }
3941
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003942 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3943 wpa_s->pending_join_dev_addr) ==
3944 method)) {
3945 /*
3946 * We have already performed provision discovery for
3947 * joining the group. Proceed directly to join
3948 * operation without duplicated provision discovery. */
3949 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3950 "with " MACSTR " already done - proceed to "
3951 "join",
3952 MAC2STR(wpa_s->pending_join_dev_addr));
3953 wpa_s->pending_pd_before_join = 0;
3954 goto start;
3955 }
3956
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003957 if (p2p_prov_disc_req(wpa_s->global->p2p,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003958 wpa_s->pending_join_dev_addr, method, 1,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003959 freq, wpa_s->user_initiated_pd) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003960 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3961 "Discovery Request before joining an "
3962 "existing group");
3963 wpa_s->pending_pd_before_join = 0;
3964 goto start;
3965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 return;
3967 }
3968
3969 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3970 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3971 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3972 wpas_p2p_check_join_scan_limit(wpa_s);
3973 return;
3974
3975start:
3976 /* Start join operation immediately */
3977 wpas_p2p_join_start(wpa_s);
3978}
3979
3980
Dmitry Shmidt04949592012-07-19 12:16:46 -07003981static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983 int ret;
3984 struct wpa_driver_scan_params params;
3985 struct wpabuf *wps_ie, *ies;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003986 size_t ielen;
Dmitry Shmidt04949592012-07-19 12:16:46 -07003987 int freqs[2] = { 0, 0 };
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003988#ifdef ANDROID_P2P
3989 int oper_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003990
Dmitry Shmidtad266fb2012-08-24 17:03:35 -07003991 /* If freq is not provided, check the operating freq of the GO and do a
3992 * a directed scan to save time
3993 */
3994 if(!freq) {
3995 freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3996 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3997 }
3998#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003999 os_memset(&params, 0, sizeof(params));
4000
4001 /* P2P Wildcard SSID */
4002 params.num_ssids = 1;
4003 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4004 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4005
4006 wpa_s->wps->dev.p2p = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004007 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4008 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4009 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004010 if (wps_ie == NULL) {
4011 wpas_p2p_scan_res_join(wpa_s, NULL);
4012 return;
4013 }
4014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004015 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4016 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004017 if (ies == NULL) {
4018 wpabuf_free(wps_ie);
4019 wpas_p2p_scan_res_join(wpa_s, NULL);
4020 return;
4021 }
4022 wpabuf_put_buf(ies, wps_ie);
4023 wpabuf_free(wps_ie);
4024
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004025 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004026
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004027 params.p2p_probe = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004028 params.extra_ies = wpabuf_head(ies);
4029 params.extra_ies_len = wpabuf_len(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004030 if (freq > 0) {
4031 freqs[0] = freq;
4032 params.freqs = freqs;
4033 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034
4035 /*
4036 * Run a scan to update BSS table and start Provision Discovery once
4037 * the new scan results become available.
4038 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004039 ret = wpa_drv_scan(wpa_s, &params);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004040 if (!ret) {
4041 os_get_time(&wpa_s->scan_trigger_time);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004042 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004043 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004044
4045 wpabuf_free(ies);
4046
4047 if (ret) {
4048 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4049 "try again later");
4050 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4051 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4052 wpas_p2p_check_join_scan_limit(wpa_s);
4053 }
4054}
4055
4056
Dmitry Shmidt04949592012-07-19 12:16:46 -07004057static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4058{
4059 struct wpa_supplicant *wpa_s = eloop_ctx;
4060 wpas_p2p_join_scan_req(wpa_s, 0);
4061}
4062
4063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004064static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004065 const u8 *dev_addr, enum p2p_wps_method wps_method,
4066 int auto_join)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004067{
4068 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
Dmitry Shmidt04949592012-07-19 12:16:46 -07004069 MACSTR " dev " MACSTR ")%s",
4070 MAC2STR(iface_addr), MAC2STR(dev_addr),
4071 auto_join ? " (auto_join)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004072
Dmitry Shmidt04949592012-07-19 12:16:46 -07004073 wpa_s->p2p_auto_pd = 0;
4074 wpa_s->p2p_auto_join = !!auto_join;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004075 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4076 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4077 wpa_s->pending_join_wps_method = wps_method;
4078
4079 /* Make sure we are not running find during connection establishment */
4080 wpas_p2p_stop_find(wpa_s);
4081
4082 wpa_s->p2p_join_scan_count = 0;
4083 wpas_p2p_join_scan(wpa_s, NULL);
4084 return 0;
4085}
4086
4087
4088static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4089{
4090 struct wpa_supplicant *group;
4091 struct p2p_go_neg_results res;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004092 struct wpa_bss *bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004093
4094 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4095 if (group == NULL)
4096 return -1;
4097 if (group != wpa_s) {
4098 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4099 sizeof(group->p2p_pin));
4100 group->p2p_wps_method = wpa_s->p2p_wps_method;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08004101 } else {
4102 /*
4103 * Need to mark the current interface for p2p_group_formation
4104 * when a separate group interface is not used. This is needed
4105 * to allow p2p_cancel stop a pending p2p_connect-join.
4106 * wpas_p2p_init_group_interface() addresses this for the case
4107 * where a separate group interface is used.
4108 */
4109 wpa_s->global->p2p_group_formation = wpa_s;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004110 }
4111
4112 group->p2p_in_provisioning = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004113 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114
4115 os_memset(&res, 0, sizeof(res));
4116 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4117 ETH_ALEN);
4118 res.wps_method = wpa_s->pending_join_wps_method;
Dmitry Shmidt444d5672013-04-01 13:08:44 -07004119 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004120 if (bss) {
4121 res.freq = bss->freq;
4122 res.ssid_len = bss->ssid_len;
4123 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
Dmitry Shmidt051af732013-10-22 13:52:46 -07004124 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4125 "from BSS table: %d MHz (SSID %s)", bss->freq,
4126 wpa_ssid_txt(bss->ssid, bss->ssid_len));
Dmitry Shmidt04949592012-07-19 12:16:46 -07004127 }
4128
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08004129 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4130 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4131 "starting client");
4132 wpa_drv_cancel_remain_on_channel(wpa_s);
4133 wpa_s->off_channel_freq = 0;
4134 wpa_s->roc_waiting_drv_freq = 0;
4135 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004136 wpas_start_wps_enrollee(group, &res);
4137
4138 /*
4139 * Allow a longer timeout for join-a-running-group than normal 15
4140 * second group formation timeout since the GO may not have authorized
4141 * our connection yet.
4142 */
4143 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4144 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4145 wpa_s, NULL);
4146
4147 return 0;
4148}
4149
4150
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004151static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004152 int *force_freq, int *pref_freq, int go)
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004153{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004154 int *freqs, res;
4155 unsigned int freq_in_use = 0, num, i;
4156
4157 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4158 if (!freqs)
4159 return -1;
4160
4161 num = get_shared_radio_freqs(wpa_s, freqs,
4162 wpa_s->num_multichan_concurrent);
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004163 wpa_printf(MSG_DEBUG,
4164 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4165 freq, wpa_s->num_multichan_concurrent, num);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004166
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004167 if (freq > 0) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004168 int ret;
4169 if (go)
4170 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4171 else
4172 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4173 if (!ret) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004174 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4175 "(%u MHz) is not supported for P2P uses",
4176 freq);
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004177 res = -3;
4178 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004179 }
4180
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004181 for (i = 0; i < num; i++) {
4182 if (freqs[i] == freq)
4183 freq_in_use = 1;
4184 }
4185
4186 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4187 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4188 freq);
4189 res = -2;
4190 goto exit_free;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004191 }
4192 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4193 "requested channel (%u MHz)", freq);
4194 *force_freq = freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004195 goto exit_ok;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004196 }
4197
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004198 for (i = 0; i < num; i++) {
4199 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4200 continue;
4201
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004202#ifndef ANDROID_P2P
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004203 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4204 *force_freq);
4205 *force_freq = freqs[i];
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004206#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004207
4208 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4209 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
Dmitry Shmidt51a47d52013-09-10 10:52:57 -07004210 *pref_freq = freqs[i];
4211#ifdef ANDROID_P2P
4212 } else {
4213 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4214 *force_freq);
4215 *force_freq = freqs[i];
4216#endif
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004217 }
4218 break;
4219 }
4220
4221 if (i == num) {
4222 if (num < wpa_s->num_multichan_concurrent) {
4223 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4224 *force_freq = 0;
4225 } else {
4226 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4227 res = -2;
4228 goto exit_free;
4229 }
4230 }
4231
4232exit_ok:
4233 res = 0;
4234exit_free:
4235 os_free(freqs);
4236 return res;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004237}
4238
4239
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004240/**
4241 * wpas_p2p_connect - Request P2P Group Formation to be started
4242 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4243 * @peer_addr: Address of the peer P2P Device
4244 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4245 * @persistent_group: Whether to create a persistent group
Dmitry Shmidt04949592012-07-19 12:16:46 -07004246 * @auto_join: Whether to select join vs. GO Negotiation automatically
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004247 * @join: Whether to join an existing group (as a client) instead of starting
4248 * Group Owner negotiation; @peer_addr is BSSID in that case
4249 * @auth: Whether to only authorize the connection instead of doing that and
4250 * initiating Group Owner negotiation
4251 * @go_intent: GO Intent or -1 to use default
4252 * @freq: Frequency for the group or 0 for auto-selection
Dmitry Shmidt04949592012-07-19 12:16:46 -07004253 * @persistent_id: Persistent group credentials to use for forcing GO
4254 * parameters or -1 to generate new values (SSID/passphrase)
4255 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4256 * interoperability workaround when initiating group formation
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004257 * @ht40: Start GO with 40 MHz channel width
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004258 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4260 * failure, -2 on failure due to channel not currently available,
4261 * -3 if forced channel is not supported
4262 */
4263int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4264 const char *pin, enum p2p_wps_method wps_method,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004265 int persistent_group, int auto_join, int join, int auth,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004266 int go_intent, int freq, int persistent_id, int pd,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004267 int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004268{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004269 int force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004270 int ret = 0, res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004271 enum wpa_driver_if_type iftype;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004272 const u8 *if_addr;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004273 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274
4275 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4276 return -1;
4277
Dmitry Shmidt04949592012-07-19 12:16:46 -07004278 if (persistent_id >= 0) {
4279 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4280 if (ssid == NULL || ssid->disabled != 2 ||
4281 ssid->mode != WPAS_MODE_P2P_GO)
4282 return -1;
4283 }
4284
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004285 os_free(wpa_s->global->add_psk);
4286 wpa_s->global->add_psk = NULL;
4287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004288 if (go_intent < 0)
4289 go_intent = wpa_s->conf->p2p_go_intent;
4290
4291 if (!auth)
4292 wpa_s->p2p_long_listen = 0;
4293
4294 wpa_s->p2p_wps_method = wps_method;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004295 wpa_s->p2p_persistent_group = !!persistent_group;
4296 wpa_s->p2p_persistent_id = persistent_id;
4297 wpa_s->p2p_go_intent = go_intent;
4298 wpa_s->p2p_connect_freq = freq;
4299 wpa_s->p2p_fallback_to_go_neg = 0;
4300 wpa_s->p2p_pd_before_go_neg = !!pd;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004301 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004302 wpa_s->p2p_go_vht = !!vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004303
4304 if (pin)
4305 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4306 else if (wps_method == WPS_PIN_DISPLAY) {
4307 ret = wps_generate_pin();
4308 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4309 ret);
4310 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4311 wpa_s->p2p_pin);
4312 } else
4313 wpa_s->p2p_pin[0] = '\0';
4314
Dmitry Shmidt04949592012-07-19 12:16:46 -07004315 if (join || auto_join) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004316 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4317 if (auth) {
4318 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4319 "connect a running group from " MACSTR,
4320 MAC2STR(peer_addr));
4321 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4322 return ret;
4323 }
4324 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4325 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4326 iface_addr) < 0) {
4327 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4328 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4329 dev_addr);
4330 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07004331 if (auto_join) {
4332 os_get_time(&wpa_s->p2p_auto_started);
4333 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4334 "%ld.%06ld",
4335 wpa_s->p2p_auto_started.sec,
4336 wpa_s->p2p_auto_started.usec);
4337 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004338 wpa_s->user_initiated_pd = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004339 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4340 auto_join) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004341 return -1;
4342 return ret;
4343 }
4344
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004345 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4346 go_intent == 15);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004347 if (res)
4348 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004349 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004350
4351 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4352
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004353 if (wpa_s->create_p2p_iface) {
4354 /* Prepare to add a new interface for the group */
4355 iftype = WPA_IF_P2P_GROUP;
4356 if (go_intent == 15)
4357 iftype = WPA_IF_P2P_GO;
4358 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4359 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4360 "interface for the group");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004361 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004364 if_addr = wpa_s->pending_interface_addr;
4365 } else
4366 if_addr = wpa_s->own_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004367
4368 if (auth) {
4369 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004370 go_intent, if_addr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004371 force_freq, persistent_group, ssid,
4372 pref_freq) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004373 return -1;
4374 return ret;
4375 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004376
4377 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4378 go_intent, if_addr, force_freq,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004379 persistent_group, ssid, pref_freq) < 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004380 if (wpa_s->create_p2p_iface)
4381 wpas_p2p_remove_pending_group_interface(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004382 return -1;
4383 }
4384 return ret;
4385}
4386
4387
4388/**
4389 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4390 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4391 * @freq: Frequency of the channel in MHz
4392 * @duration: Duration of the stay on the channel in milliseconds
4393 *
4394 * This callback is called when the driver indicates that it has started the
4395 * requested remain-on-channel duration.
4396 */
4397void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4398 unsigned int freq, unsigned int duration)
4399{
4400 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4401 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004402 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4403 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4404 wpa_s->pending_listen_duration);
4405 wpa_s->pending_listen_freq = 0;
4406 }
4407}
4408
4409
4410static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4411 unsigned int timeout)
4412{
4413 /* Limit maximum Listen state time based on driver limitation. */
4414 if (timeout > wpa_s->max_remain_on_chan)
4415 timeout = wpa_s->max_remain_on_chan;
4416
4417 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4418 return wpa_drv_p2p_listen(wpa_s, timeout);
4419
4420 return p2p_listen(wpa_s->global->p2p, timeout);
4421}
4422
4423
4424/**
4425 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4426 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4427 * @freq: Frequency of the channel in MHz
4428 *
4429 * This callback is called when the driver indicates that a remain-on-channel
4430 * operation has been completed, i.e., the duration on the requested channel
4431 * has timed out.
4432 */
4433void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4434 unsigned int freq)
4435{
4436 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4437 "(p2p_long_listen=%d ms pending_action_tx=%p)",
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004438 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004439 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4440 return;
4441 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4442 return; /* P2P module started a new operation */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004443 if (offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004444 return;
4445 if (wpa_s->p2p_long_listen > 0)
4446 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4447 if (wpa_s->p2p_long_listen > 0) {
4448 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4449 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004450 } else {
4451 /*
4452 * When listen duration is over, stop listen & update p2p_state
4453 * to IDLE.
4454 */
4455 p2p_stop_listen(wpa_s->global->p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004456 }
4457}
4458
4459
4460/**
4461 * wpas_p2p_group_remove - Remove a P2P group
4462 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4463 * @ifname: Network interface name of the group interface or "*" to remove all
4464 * groups
4465 * Returns: 0 on success, -1 on failure
4466 *
4467 * This function is used to remove a P2P group. This can be used to disconnect
4468 * from a group in which the local end is a P2P Client or to end a P2P Group in
4469 * case the local end is the Group Owner. If a virtual network interface was
4470 * created for this group, that interface will be removed. Otherwise, only the
4471 * configured P2P group network will be removed from the interface.
4472 */
4473int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4474{
4475 struct wpa_global *global = wpa_s->global;
4476
4477 if (os_strcmp(ifname, "*") == 0) {
4478 struct wpa_supplicant *prev;
4479 wpa_s = global->ifaces;
4480 while (wpa_s) {
4481 prev = wpa_s;
4482 wpa_s = wpa_s->next;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07004483 if (prev->p2p_group_interface !=
4484 NOT_P2P_GROUP_INTERFACE ||
4485 (prev->current_ssid &&
4486 prev->current_ssid->p2p_group))
4487 wpas_p2p_disconnect(prev);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004488 }
4489 return 0;
4490 }
4491
4492 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4493 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4494 break;
4495 }
4496
4497 return wpas_p2p_disconnect(wpa_s);
4498}
4499
4500
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004501static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4502{
4503 unsigned int r;
4504
4505 if (freq == 2) {
4506 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4507 "band");
4508 if (wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004509 p2p_supported_freq_go(wpa_s->global->p2p,
4510 wpa_s->best_24_freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004511 freq = wpa_s->best_24_freq;
4512 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4513 "channel: %d MHz", freq);
4514 } else {
4515 os_get_random((u8 *) &r, sizeof(r));
4516 freq = 2412 + (r % 3) * 25;
4517 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4518 "channel: %d MHz", freq);
4519 }
4520 }
4521
4522 if (freq == 5) {
4523 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4524 "band");
4525 if (wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004526 p2p_supported_freq_go(wpa_s->global->p2p,
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004527 wpa_s->best_5_freq)) {
4528 freq = wpa_s->best_5_freq;
4529 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4530 "channel: %d MHz", freq);
4531 } else {
4532 os_get_random((u8 *) &r, sizeof(r));
4533 freq = 5180 + (r % 4) * 20;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004534 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004535 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4536 "5 GHz channel for P2P group");
4537 return -1;
4538 }
4539 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4540 "channel: %d MHz", freq);
4541 }
4542 }
4543
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004544 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004545 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4546 "(%u MHz) is not supported for P2P uses",
4547 freq);
4548 return -1;
4549 }
4550
4551 return freq;
4552}
4553
4554
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004555static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4556 struct p2p_go_neg_results *params,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004557 int freq, int ht40, int vht,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004558 const struct p2p_channels *channels)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004559{
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004560 int res, *freqs;
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004561 unsigned int pref_freq;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004562 unsigned int num, i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004563
4564 os_memset(params, 0, sizeof(*params));
4565 params->role_go = 1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004566 params->ht40 = ht40;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004567 params->vht = vht;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004568 if (freq) {
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004569 if (!freq_included(channels, freq)) {
4570 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4571 "accepted", freq);
4572 return -1;
4573 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004574 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4575 "frequency %d MHz", freq);
4576 params->freq = freq;
4577 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4578 wpa_s->conf->p2p_oper_channel >= 1 &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004579 wpa_s->conf->p2p_oper_channel <= 11 &&
4580 freq_included(channels,
4581 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004582 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4583 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4584 "frequency %d MHz", params->freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004585 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4586 wpa_s->conf->p2p_oper_reg_class == 116 ||
4587 wpa_s->conf->p2p_oper_reg_class == 117 ||
4588 wpa_s->conf->p2p_oper_reg_class == 124 ||
4589 wpa_s->conf->p2p_oper_reg_class == 126 ||
4590 wpa_s->conf->p2p_oper_reg_class == 127) &&
4591 freq_included(channels,
4592 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004593 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4594 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4595 "frequency %d MHz", params->freq);
4596 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4597 wpa_s->best_overall_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004598 p2p_supported_freq_go(wpa_s->global->p2p,
4599 wpa_s->best_overall_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004600 freq_included(channels, wpa_s->best_overall_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 params->freq = wpa_s->best_overall_freq;
4602 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4603 "channel %d MHz", params->freq);
4604 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4605 wpa_s->best_24_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004606 p2p_supported_freq_go(wpa_s->global->p2p,
4607 wpa_s->best_24_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004608 freq_included(channels, wpa_s->best_24_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004609 params->freq = wpa_s->best_24_freq;
4610 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4611 "channel %d MHz", params->freq);
4612 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4613 wpa_s->best_5_freq > 0 &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004614 p2p_supported_freq_go(wpa_s->global->p2p,
4615 wpa_s->best_5_freq) &&
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004616 freq_included(channels, wpa_s->best_5_freq)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004617 params->freq = wpa_s->best_5_freq;
4618 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4619 "channel %d MHz", params->freq);
Dmitry Shmidt44c95782013-05-17 09:51:35 -07004620 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4621 channels))) {
4622 params->freq = pref_freq;
4623 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4624 "channels", params->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004625 } else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004626 int chan;
4627 for (chan = 0; chan < 11; chan++) {
4628 params->freq = 2412 + chan * 5;
4629 if (!wpas_p2p_disallowed_freq(wpa_s->global,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08004630 params->freq) &&
4631 freq_included(channels, params->freq))
Dmitry Shmidt04949592012-07-19 12:16:46 -07004632 break;
4633 }
4634 if (chan == 11) {
4635 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4636 "allowed");
4637 return -1;
4638 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4640 "known)", params->freq);
4641 }
4642
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004643 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4644 if (!freqs)
4645 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004646
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004647 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4648 wpa_s->num_multichan_concurrent);
4649 if (res < 0) {
4650 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004651 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004652 }
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004653 num = res;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004654
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07004655 if (!freq) {
4656 for (i = 0; i < num; i++) {
4657 if (freq_included(channels, freqs[i])) {
4658 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4659 freqs[i]);
4660 params->freq = freqs[i];
4661 break;
4662 }
4663 }
4664
4665 if (i == num) {
4666 if (num == wpa_s->num_multichan_concurrent) {
4667 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4668 os_free(freqs);
4669 return -1;
4670 } else {
4671 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4672 }
4673 }
4674 } else {
4675 for (i = 0; i < num; i++) {
4676 if (freqs[i] == freq)
4677 break;
4678 }
4679
4680 if (i == num) {
4681 if (num == wpa_s->num_multichan_concurrent) {
4682 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4683 os_free(freqs);
4684 return -1;
4685 } else {
4686 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4687 }
4688 }
4689 }
4690 os_free(freqs);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004691 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004692}
4693
4694
4695static struct wpa_supplicant *
4696wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4697 int go)
4698{
4699 struct wpa_supplicant *group_wpa_s;
4700
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004701 if (!wpas_p2p_create_iface(wpa_s)) {
4702 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4703 "operations");
Dmitry Shmidt56052862013-10-04 10:23:25 -07004704 wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004705 return wpa_s;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004706 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004707
4708 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004709 WPA_IF_P2P_CLIENT) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004710 wpa_msg_global(wpa_s, MSG_ERROR,
4711 "P2P: Failed to add group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004712 return NULL;
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004713 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004714 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4715 if (group_wpa_s == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004716 wpa_msg_global(wpa_s, MSG_ERROR,
4717 "P2P: Failed to initialize group interface");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004718 wpas_p2p_remove_pending_group_interface(wpa_s);
4719 return NULL;
4720 }
4721
Dmitry Shmidtaa532512012-09-24 10:35:31 -07004722 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4723 group_wpa_s->ifname);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004724 group_wpa_s->p2p_first_connection_timeout = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004725 return group_wpa_s;
4726}
4727
4728
4729/**
4730 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4731 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4732 * @persistent_group: Whether to create a persistent group
4733 * @freq: Frequency for the group or 0 to indicate no hardcoding
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004734 * @ht40: Start GO with 40 MHz channel width
4735 * @vht: Start GO with VHT support
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004736 * Returns: 0 on success, -1 on failure
4737 *
4738 * This function creates a new P2P group with the local end as the Group Owner,
4739 * i.e., without using Group Owner Negotiation.
4740 */
4741int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004742 int freq, int ht40, int vht)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004743{
4744 struct p2p_go_neg_results params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004745
4746 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4747 return -1;
4748
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004749 os_free(wpa_s->global->add_psk);
4750 wpa_s->global->add_psk = NULL;
4751
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004752 /* Make sure we are not running find during connection establishment */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004753 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004754 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidtdca39792011-09-06 11:17:33 -07004755
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004756 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4757 if (freq < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004759
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004760 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004761 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004762 if (params.freq &&
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004763 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07004764 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4765 "(%u MHz) is not supported for P2P uses",
4766 params.freq);
4767 return -1;
4768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004769 p2p_go_params(wpa_s->global->p2p, &params);
4770 params.persistent_group = persistent_group;
4771
4772 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4773 if (wpa_s == NULL)
4774 return -1;
4775 wpas_start_wps_go(wpa_s, &params, 0);
4776
4777 return 0;
4778}
4779
4780
4781static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4782 struct wpa_ssid *params, int addr_allocated)
4783{
4784 struct wpa_ssid *ssid;
4785
4786 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4787 if (wpa_s == NULL)
4788 return -1;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004789 wpa_s->p2p_last_4way_hs_fail = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004790
4791 wpa_supplicant_ap_deinit(wpa_s);
4792
4793 ssid = wpa_config_add_network(wpa_s->conf);
4794 if (ssid == NULL)
4795 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004796 wpa_config_set_network_defaults(ssid);
4797 ssid->temporary = 1;
4798 ssid->proto = WPA_PROTO_RSN;
4799 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4800 ssid->group_cipher = WPA_CIPHER_CCMP;
4801 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4802 ssid->ssid = os_malloc(params->ssid_len);
4803 if (ssid->ssid == NULL) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004804 wpa_config_remove_network(wpa_s->conf, ssid->id);
4805 return -1;
4806 }
4807 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4808 ssid->ssid_len = params->ssid_len;
4809 ssid->p2p_group = 1;
4810 ssid->export_keys = 1;
4811 if (params->psk_set) {
4812 os_memcpy(ssid->psk, params->psk, 32);
4813 ssid->psk_set = 1;
4814 }
4815 if (params->passphrase)
4816 ssid->passphrase = os_strdup(params->passphrase);
4817
4818 wpa_supplicant_select_network(wpa_s, ssid);
4819
4820 wpa_s->show_group_started = 1;
4821
4822 return 0;
4823}
4824
4825
4826int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4827 struct wpa_ssid *ssid, int addr_allocated,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004828 int freq, int ht40, int vht,
Dmitry Shmidt56052862013-10-04 10:23:25 -07004829 const struct p2p_channels *channels,
4830 int connection_timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004831{
4832 struct p2p_go_neg_results params;
4833 int go = 0;
4834
4835 if (ssid->disabled != 2 || ssid->ssid == NULL)
4836 return -1;
4837
4838 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4839 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4840 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4841 "already running");
4842 return 0;
4843 }
4844
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07004845 os_free(wpa_s->global->add_psk);
4846 wpa_s->global->add_psk = NULL;
4847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004848 /* Make sure we are not running find during connection establishment */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004849 wpas_p2p_stop_find_oper(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004850
Dmitry Shmidt04949592012-07-19 12:16:46 -07004851 wpa_s->p2p_fallback_to_go_neg = 0;
4852
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004853 if (ssid->mode == WPAS_MODE_INFRA)
4854 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4855
4856 if (ssid->mode != WPAS_MODE_P2P_GO)
4857 return -1;
4858
Dmitry Shmidt8bae4132013-06-06 11:25:10 -07004859 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4860 if (freq < 0)
4861 return -1;
4862
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004863 if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004864 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004865
4866 params.role_go = 1;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004867 params.psk_set = ssid->psk_set;
4868 if (params.psk_set)
4869 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004870 if (ssid->passphrase) {
4871 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4872 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4873 "persistent group");
4874 return -1;
4875 }
4876 os_strlcpy(params.passphrase, ssid->passphrase,
4877 sizeof(params.passphrase));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004879 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4880 params.ssid_len = ssid->ssid_len;
4881 params.persistent_group = 1;
4882
4883 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4884 if (wpa_s == NULL)
4885 return -1;
4886
Dmitry Shmidt56052862013-10-04 10:23:25 -07004887 wpa_s->p2p_first_connection_timeout = connection_timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004888 wpas_start_wps_go(wpa_s, &params, 0);
4889
4890 return 0;
4891}
4892
4893
4894static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4895 struct wpabuf *proberesp_ies)
4896{
4897 struct wpa_supplicant *wpa_s = ctx;
4898 if (wpa_s->ap_iface) {
4899 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004900 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4901 wpabuf_free(beacon_ies);
4902 wpabuf_free(proberesp_ies);
4903 return;
4904 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004905 if (beacon_ies) {
4906 wpabuf_free(hapd->p2p_beacon_ie);
4907 hapd->p2p_beacon_ie = beacon_ies;
4908 }
4909 wpabuf_free(hapd->p2p_probe_resp_ie);
4910 hapd->p2p_probe_resp_ie = proberesp_ies;
4911 } else {
4912 wpabuf_free(beacon_ies);
4913 wpabuf_free(proberesp_ies);
4914 }
4915 wpa_supplicant_ap_update_beacon(wpa_s);
4916}
4917
4918
4919static void wpas_p2p_idle_update(void *ctx, int idle)
4920{
4921 struct wpa_supplicant *wpa_s = ctx;
4922 if (!wpa_s->ap_iface)
4923 return;
4924 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
Dmitry Shmidt04949592012-07-19 12:16:46 -07004925 if (idle)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004926 wpas_p2p_set_group_idle_timeout(wpa_s);
4927 else
4928 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4929}
4930
4931
4932struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
Dmitry Shmidt04949592012-07-19 12:16:46 -07004933 struct wpa_ssid *ssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004934{
4935 struct p2p_group *group;
4936 struct p2p_group_config *cfg;
4937
4938 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4939 return NULL;
4940 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4941 return NULL;
4942
4943 cfg = os_zalloc(sizeof(*cfg));
4944 if (cfg == NULL)
4945 return NULL;
4946
Dmitry Shmidt04949592012-07-19 12:16:46 -07004947 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004948 cfg->persistent_group = 2;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004949 else if (ssid->p2p_persistent_group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004950 cfg->persistent_group = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004951 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4952 if (wpa_s->max_stations &&
4953 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4954 cfg->max_clients = wpa_s->max_stations;
4955 else
4956 cfg->max_clients = wpa_s->conf->max_num_sta;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004957 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4958 cfg->ssid_len = ssid->ssid_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004959 cfg->cb_ctx = wpa_s;
4960 cfg->ie_update = wpas_p2p_ie_update;
4961 cfg->idle_update = wpas_p2p_idle_update;
4962
4963 group = p2p_group_init(wpa_s->global->p2p, cfg);
4964 if (group == NULL)
4965 os_free(cfg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07004966 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004967 p2p_group_notif_formation_done(group);
4968 wpa_s->p2p_group = group;
4969 return group;
4970}
4971
4972
4973void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4974 int registrar)
4975{
Dmitry Shmidt04949592012-07-19 12:16:46 -07004976 struct wpa_ssid *ssid = wpa_s->current_ssid;
4977
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004978 if (!wpa_s->p2p_in_provisioning) {
4979 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4980 "provisioning not in progress");
4981 return;
4982 }
4983
Dmitry Shmidt04949592012-07-19 12:16:46 -07004984 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4985 u8 go_dev_addr[ETH_ALEN];
4986 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4987 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4988 ssid->ssid_len);
4989 /* Clear any stored provisioning info */
4990 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4991 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004992
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004993 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4994 NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07004995 wpa_s->p2p_go_group_formation_completed = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07004996 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4997 /*
4998 * Use a separate timeout for initial data connection to
4999 * complete to allow the group to be removed automatically if
5000 * something goes wrong in this step before the P2P group idle
5001 * timeout mechanism is taken into use.
5002 */
Dmitry Shmidt56052862013-10-04 10:23:25 -07005003 wpa_dbg(wpa_s, MSG_DEBUG,
5004 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5005 P2P_MAX_INITIAL_CONN_WAIT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005006 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5007 wpas_p2p_group_formation_timeout,
5008 wpa_s->parent, NULL);
Dmitry Shmidt56052862013-10-04 10:23:25 -07005009 } else if (ssid) {
5010 /*
5011 * Use a separate timeout for initial data connection to
5012 * complete to allow the group to be removed automatically if
5013 * the client does not complete data connection successfully.
5014 */
5015 wpa_dbg(wpa_s, MSG_DEBUG,
5016 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5017 P2P_MAX_INITIAL_CONN_WAIT_GO);
5018 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5019 wpas_p2p_group_formation_timeout,
5020 wpa_s->parent, NULL);
5021 /*
5022 * Complete group formation on first successful data connection
5023 */
5024 wpa_s->p2p_go_group_formation_completed = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005025 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005026 if (wpa_s->global->p2p)
5027 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5028 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5029 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5030 wpas_group_formation_completed(wpa_s, 1);
5031}
5032
5033
Jouni Malinen75ecf522011-06-27 15:19:46 -07005034void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5035 struct wps_event_fail *fail)
5036{
5037 if (!wpa_s->p2p_in_provisioning) {
5038 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5039 "provisioning not in progress");
5040 return;
5041 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005042
5043 if (wpa_s->go_params) {
5044 p2p_clear_provisioning_info(
5045 wpa_s->global->p2p,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005046 wpa_s->go_params->peer_device_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005047 }
5048
Jouni Malinen75ecf522011-06-27 15:19:46 -07005049 wpas_notify_p2p_wps_failed(wpa_s, fail);
5050}
5051
5052
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005053int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005054 const char *config_method,
5055 enum wpas_p2p_prov_disc_use use)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005056{
5057 u16 config_methods;
5058
Dmitry Shmidt04949592012-07-19 12:16:46 -07005059 wpa_s->p2p_fallback_to_go_neg = 0;
5060 wpa_s->pending_pd_use = NORMAL_PD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005061 if (os_strncmp(config_method, "display", 7) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005062 config_methods = WPS_CONFIG_DISPLAY;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005063 else if (os_strncmp(config_method, "keypad", 6) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005064 config_methods = WPS_CONFIG_KEYPAD;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005065 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5066 os_strncmp(config_method, "pushbutton", 10) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005067 config_methods = WPS_CONFIG_PUSHBUTTON;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005068 else {
5069 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005070 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005071 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005072
Dmitry Shmidt04949592012-07-19 12:16:46 -07005073 if (use == WPAS_P2P_PD_AUTO) {
5074 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5075 wpa_s->pending_pd_config_methods = config_methods;
5076 wpa_s->p2p_auto_pd = 1;
5077 wpa_s->p2p_auto_join = 0;
5078 wpa_s->pending_pd_before_join = 0;
5079 wpa_s->auto_pd_scan_retry = 0;
5080 wpas_p2p_stop_find(wpa_s);
5081 wpa_s->p2p_join_scan_count = 0;
5082 os_get_time(&wpa_s->p2p_auto_started);
5083 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5084 wpa_s->p2p_auto_started.sec,
5085 wpa_s->p2p_auto_started.usec);
5086 wpas_p2p_join_scan(wpa_s, NULL);
5087 return 0;
5088 }
5089
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005090 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5091 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005092 config_methods,
5093 use == WPAS_P2P_PD_FOR_JOIN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005094 }
5095
5096 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5097 return -1;
5098
5099 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005100 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005101 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005102}
5103
5104
5105int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5106 char *end)
5107{
5108 return p2p_scan_result_text(ies, ies_len, buf, end);
5109}
5110
5111
5112static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5113{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005114 if (!offchannel_pending_action_tx(wpa_s))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005115 return;
5116
5117 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5118 "operation request");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005119 offchannel_clear_pending_action_tx(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005120}
5121
5122
5123int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5124 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005125 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005126 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005127{
5128 wpas_p2p_clear_pending_action_tx(wpa_s);
5129 wpa_s->p2p_long_listen = 0;
5130
5131 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5132 return wpa_drv_p2p_find(wpa_s, timeout, type);
5133
Dmitry Shmidt04949592012-07-19 12:16:46 -07005134 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5135 wpa_s->p2p_in_provisioning)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005136 return -1;
5137
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005138 wpa_supplicant_cancel_sched_scan(wpa_s);
5139
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005140 return p2p_find(wpa_s->global->p2p, timeout, type,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005141 num_req_dev_types, req_dev_types, dev_id,
5142 search_delay);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005143}
5144
5145
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005146static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005147{
5148 wpas_p2p_clear_pending_action_tx(wpa_s);
5149 wpa_s->p2p_long_listen = 0;
5150 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5151 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
Jouni Malinendc7b7132012-09-14 12:53:47 -07005152 wpa_s->global->p2p_cb_on_scan_complete = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005153
5154 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5155 wpa_drv_p2p_stop_find(wpa_s);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005156 return 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005157 }
5158
5159 if (wpa_s->global->p2p)
5160 p2p_stop_find(wpa_s->global->p2p);
5161
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005162 return 0;
5163}
5164
5165
5166void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5167{
5168 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5169 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005170 wpas_p2p_remove_pending_group_interface(wpa_s);
5171}
5172
5173
5174static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5175{
5176 struct wpa_supplicant *wpa_s = eloop_ctx;
5177 wpa_s->p2p_long_listen = 0;
5178}
5179
5180
5181int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5182{
5183 int res;
5184
5185 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5186 return -1;
5187
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005188 wpa_supplicant_cancel_sched_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005189 wpas_p2p_clear_pending_action_tx(wpa_s);
5190
5191 if (timeout == 0) {
5192 /*
5193 * This is a request for unlimited Listen state. However, at
5194 * least for now, this is mapped to a Listen state for one
5195 * hour.
5196 */
5197 timeout = 3600;
5198 }
5199 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5200 wpa_s->p2p_long_listen = 0;
5201
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005202 /*
5203 * Stop previous find/listen operation to avoid trying to request a new
5204 * remain-on-channel operation while the driver is still running the
5205 * previous one.
5206 */
5207 if (wpa_s->global->p2p)
5208 p2p_stop_find(wpa_s->global->p2p);
5209
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005210 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5211 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5212 wpa_s->p2p_long_listen = timeout * 1000;
5213 eloop_register_timeout(timeout, 0,
5214 wpas_p2p_long_listen_timeout,
5215 wpa_s, NULL);
5216 }
5217
5218 return res;
5219}
5220
5221
5222int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5223 u8 *buf, size_t len, int p2p_group)
5224{
5225 struct wpabuf *p2p_ie;
5226 int ret;
5227
5228 if (wpa_s->global->p2p_disabled)
5229 return -1;
5230 if (wpa_s->global->p2p == NULL)
5231 return -1;
5232 if (bss == NULL)
5233 return -1;
5234
5235 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5236 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5237 p2p_group, p2p_ie);
5238 wpabuf_free(p2p_ie);
5239
5240 return ret;
5241}
5242
5243
5244int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005245 const u8 *dst, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005246 const u8 *ie, size_t ie_len, int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005247{
5248 if (wpa_s->global->p2p_disabled)
5249 return 0;
5250 if (wpa_s->global->p2p == NULL)
5251 return 0;
5252
Dmitry Shmidt04949592012-07-19 12:16:46 -07005253 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5254 ie, ie_len)) {
5255 case P2P_PREQ_NOT_P2P:
5256 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5257 ssi_signal);
5258 /* fall through */
5259 case P2P_PREQ_MALFORMED:
5260 case P2P_PREQ_NOT_LISTEN:
5261 case P2P_PREQ_NOT_PROCESSED:
5262 default: /* make gcc happy */
5263 return 0;
5264 case P2P_PREQ_PROCESSED:
5265 return 1;
5266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005267}
5268
5269
5270void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5271 const u8 *sa, const u8 *bssid,
5272 u8 category, const u8 *data, size_t len, int freq)
5273{
5274 if (wpa_s->global->p2p_disabled)
5275 return;
5276 if (wpa_s->global->p2p == NULL)
5277 return;
5278
5279 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5280 freq);
5281}
5282
5283
5284void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5285{
5286 if (wpa_s->global->p2p_disabled)
5287 return;
5288 if (wpa_s->global->p2p == NULL)
5289 return;
5290
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005291 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005292}
5293
5294
5295void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5296{
5297 p2p_group_deinit(wpa_s->p2p_group);
5298 wpa_s->p2p_group = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005299
5300 wpa_s->ap_configured_cb = NULL;
5301 wpa_s->ap_configured_cb_ctx = NULL;
5302 wpa_s->ap_configured_cb_data = NULL;
5303 wpa_s->connect_without_scan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005304}
5305
5306
5307int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5308{
5309 wpa_s->p2p_long_listen = 0;
5310
5311 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5312 return wpa_drv_p2p_reject(wpa_s, addr);
5313
5314 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5315 return -1;
5316
5317 return p2p_reject(wpa_s->global->p2p, addr);
5318}
5319
5320
5321/* Invite to reinvoke a persistent group */
5322int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
Jouni Malinen31be0a42012-08-31 21:20:51 +03005323 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005324 int ht40, int vht, int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005325{
5326 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005327 u8 *bssid = NULL;
5328 int force_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005329 int res;
5330
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005331 wpa_s->global->p2p_invite_group = NULL;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005332 if (peer_addr)
5333 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5334 else
5335 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005336
Jouni Malinen31be0a42012-08-31 21:20:51 +03005337 wpa_s->p2p_persistent_go_freq = freq;
5338 wpa_s->p2p_go_ht40 = !!ht40;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005339 if (ssid->mode == WPAS_MODE_P2P_GO) {
5340 role = P2P_INVITE_ROLE_GO;
5341 if (peer_addr == NULL) {
5342 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5343 "address in invitation command");
5344 return -1;
5345 }
5346 if (wpas_p2p_create_iface(wpa_s)) {
5347 if (wpas_p2p_add_group_interface(wpa_s,
5348 WPA_IF_P2P_GO) < 0) {
5349 wpa_printf(MSG_ERROR, "P2P: Failed to "
5350 "allocate a new interface for the "
5351 "group");
5352 return -1;
5353 }
5354 bssid = wpa_s->pending_interface_addr;
5355 } else
5356 bssid = wpa_s->own_addr;
5357 } else {
5358 role = P2P_INVITE_ROLE_CLIENT;
5359 peer_addr = ssid->bssid;
5360 }
5361 wpa_s->pending_invite_ssid_id = ssid->id;
5362
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005363 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5364 role == P2P_INVITE_ROLE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005365 if (res)
5366 return res;
Irfan Sheriffaf84a572012-09-22 16:59:30 -07005367
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005368 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5369 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5370 ssid->ssid, ssid->ssid_len,
5371 go_dev_addr, 1);
5372
5373 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5374 return -1;
5375
5376 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005377 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5378 1, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005379}
5380
5381
5382/* Invite to join an active group */
5383int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5384 const u8 *peer_addr, const u8 *go_dev_addr)
5385{
5386 struct wpa_global *global = wpa_s->global;
5387 enum p2p_invite_role role;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005388 u8 *bssid = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005389 struct wpa_ssid *ssid;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005390 int persistent;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005391 int freq = 0, force_freq = 0, pref_freq = 0;
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005392 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005393
Jouni Malinen31be0a42012-08-31 21:20:51 +03005394 wpa_s->p2p_persistent_go_freq = 0;
5395 wpa_s->p2p_go_ht40 = 0;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005396 wpa_s->p2p_go_vht = 0;
Jouni Malinen31be0a42012-08-31 21:20:51 +03005397
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005398 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5399 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5400 break;
5401 }
5402 if (wpa_s == NULL) {
5403 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5404 return -1;
5405 }
5406
5407 ssid = wpa_s->current_ssid;
5408 if (ssid == NULL) {
5409 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5410 "invitation");
5411 return -1;
5412 }
5413
Dmitry Shmidt700a1372013-03-15 14:14:44 -07005414 wpa_s->global->p2p_invite_group = wpa_s;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005415 persistent = ssid->p2p_persistent_group &&
5416 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5417 ssid->ssid, ssid->ssid_len);
5418
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005419 if (ssid->mode == WPAS_MODE_P2P_GO) {
5420 role = P2P_INVITE_ROLE_ACTIVE_GO;
5421 bssid = wpa_s->own_addr;
5422 if (go_dev_addr == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005423 go_dev_addr = wpa_s->global->p2p_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005424 freq = ssid->frequency;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005425 } else {
5426 role = P2P_INVITE_ROLE_CLIENT;
5427 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5428 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5429 "invite to current group");
5430 return -1;
5431 }
5432 bssid = wpa_s->bssid;
5433 if (go_dev_addr == NULL &&
5434 !is_zero_ether_addr(wpa_s->go_dev_addr))
5435 go_dev_addr = wpa_s->go_dev_addr;
Dmitry Shmidt051af732013-10-22 13:52:46 -07005436 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5437 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005438 }
5439 wpa_s->parent->pending_invite_ssid_id = -1;
5440
5441 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5442 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5443 ssid->ssid, ssid->ssid_len,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005444 go_dev_addr, persistent);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005445
5446 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5447 return -1;
5448
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005449 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5450 role == P2P_INVITE_ROLE_ACTIVE_GO);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005451 if (res)
5452 return res;
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07005453 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005454
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005455 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08005456 ssid->ssid, ssid->ssid_len, force_freq,
5457 go_dev_addr, persistent, pref_freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005458}
5459
5460
5461void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5462{
5463 struct wpa_ssid *ssid = wpa_s->current_ssid;
5464 const char *ssid_txt;
5465 u8 go_dev_addr[ETH_ALEN];
Jouni Malinen75ecf522011-06-27 15:19:46 -07005466 int network_id = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005467 int persistent;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005468 int freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005469
Dmitry Shmidt04949592012-07-19 12:16:46 -07005470 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5471 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5472 wpa_s->parent, NULL);
5473 }
5474
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005475 if (!wpa_s->show_group_started || !ssid)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005476 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005477
5478 wpa_s->show_group_started = 0;
5479
5480 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5481 os_memset(go_dev_addr, 0, ETH_ALEN);
5482 if (ssid->bssid_set)
5483 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5484 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5485 ssid->ssid_len);
5486 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5487
5488 if (wpa_s->global->p2p_group_formation == wpa_s)
5489 wpa_s->global->p2p_group_formation = NULL;
5490
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005491 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5492 (int) wpa_s->assoc_freq;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005493 if (ssid->passphrase == NULL && ssid->psk_set) {
5494 char psk[65];
5495 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005496 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5497 "%s client ssid=\"%s\" freq=%d psk=%s "
5498 "go_dev_addr=" MACSTR "%s",
5499 wpa_s->ifname, ssid_txt, freq, psk,
5500 MAC2STR(go_dev_addr),
5501 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005502 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005503 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5504 "%s client ssid=\"%s\" freq=%d "
5505 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5506 wpa_s->ifname, ssid_txt, freq,
5507 ssid->passphrase ? ssid->passphrase : "",
5508 MAC2STR(go_dev_addr),
5509 persistent ? " [PERSISTENT]" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005510 }
5511
5512 if (persistent)
Jouni Malinen75ecf522011-06-27 15:19:46 -07005513 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5514 ssid, go_dev_addr);
5515 if (network_id < 0)
5516 network_id = ssid->id;
5517 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005518
5519done:
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07005520 wpas_p2p_continue_after_scan(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005521}
5522
5523
5524int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5525 u32 interval1, u32 duration2, u32 interval2)
5526{
5527 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5528 return -1;
5529 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5530 return -1;
5531
5532 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5533 wpa_s->current_ssid == NULL ||
5534 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5535 return -1;
5536
5537 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5538 wpa_s->own_addr, wpa_s->assoc_freq,
5539 duration1, interval1, duration2, interval2);
5540}
5541
5542
5543int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5544 unsigned int interval)
5545{
5546 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5547 return -1;
5548
5549 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5550 return -1;
5551
5552 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5553}
5554
5555
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005556static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5557{
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08005558 if (wpa_s->current_ssid == NULL) {
5559 /*
5560 * current_ssid can be cleared when P2P client interface gets
5561 * disconnected, so assume this interface was used as P2P
5562 * client.
5563 */
5564 return 1;
5565 }
5566 return wpa_s->current_ssid->p2p_group &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005567 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5568}
5569
5570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005571static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5572{
5573 struct wpa_supplicant *wpa_s = eloop_ctx;
5574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005575 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005576 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5577 "disabled");
5578 return;
5579 }
5580
Dmitry Shmidt04949592012-07-19 12:16:46 -07005581 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5582 "group");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005583 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005584}
5585
5586
5587static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5588{
Dmitry Shmidt04949592012-07-19 12:16:46 -07005589 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005590
Dmitry Shmidt04949592012-07-19 12:16:46 -07005591 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5592 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005594 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5595 return;
5596
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005597 timeout = wpa_s->conf->p2p_group_idle;
5598 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5599 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5600 timeout = P2P_MAX_CLIENT_IDLE;
5601
5602 if (timeout == 0)
5603 return;
5604
Dmitry Shmidt04949592012-07-19 12:16:46 -07005605 if (timeout < 0) {
5606 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5607 timeout = 0; /* special client mode no-timeout */
5608 else
5609 return;
5610 }
5611
5612 if (wpa_s->p2p_in_provisioning) {
5613 /*
5614 * Use the normal group formation timeout during the
5615 * provisioning phase to avoid terminating this process too
5616 * early due to group idle timeout.
5617 */
5618 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5619 "during provisioning");
5620 return;
5621 }
Deepthi Gowri9e4e8ac2013-04-16 11:57:05 +05305622
Dmitry Shmidt04949592012-07-19 12:16:46 -07005623 if (wpa_s->show_group_started) {
5624 /*
5625 * Use the normal group formation timeout between the end of
5626 * the provisioning phase and completion of 4-way handshake to
5627 * avoid terminating this process too early due to group idle
5628 * timeout.
5629 */
5630 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5631 "while waiting for initial 4-way handshake to "
5632 "complete");
5633 return;
5634 }
5635
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005636 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005637 timeout);
5638 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5639 wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005640}
5641
5642
Jouni Malinen2b89da82012-08-31 22:04:41 +03005643/* Returns 1 if the interface was removed */
5644int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5645 u16 reason_code, const u8 *ie, size_t ie_len,
5646 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005647{
5648 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005649 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005650 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
Jouni Malinen2b89da82012-08-31 22:04:41 +03005651 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005652
Dmitry Shmidt04949592012-07-19 12:16:46 -07005653 if (!locally_generated)
5654 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5655 ie_len);
5656
5657 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5658 wpa_s->current_ssid &&
5659 wpa_s->current_ssid->p2p_group &&
5660 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5661 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5662 "session is ending");
Jouni Malinen2b89da82012-08-31 22:04:41 +03005663 if (wpas_p2p_group_delete(wpa_s,
5664 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5665 > 0)
5666 return 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07005667 }
Jouni Malinen2b89da82012-08-31 22:04:41 +03005668
5669 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005670}
5671
5672
5673void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
Dmitry Shmidt04949592012-07-19 12:16:46 -07005674 u16 reason_code, const u8 *ie, size_t ie_len,
5675 int locally_generated)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005676{
5677 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5678 return;
5679 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5680 return;
5681
Dmitry Shmidt04949592012-07-19 12:16:46 -07005682 if (!locally_generated)
5683 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5684 ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005685}
5686
5687
5688void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5689{
5690 struct p2p_data *p2p = wpa_s->global->p2p;
5691
5692 if (p2p == NULL)
5693 return;
5694
5695 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5696 return;
5697
5698 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5699 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5700
5701 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5702 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5703
5704 if (wpa_s->wps &&
5705 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5706 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5707
5708 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5709 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5710
5711 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5712 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5713 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5714 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5715 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5716 }
5717
5718 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5719 p2p_set_sec_dev_types(p2p,
5720 (void *) wpa_s->conf->sec_device_type,
5721 wpa_s->conf->num_sec_device_types);
5722
5723 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5724 int i;
5725 p2p_remove_wps_vendor_extensions(p2p);
5726 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5727 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5728 continue;
5729 p2p_add_wps_vendor_extension(
5730 p2p, wpa_s->conf->wps_vendor_ext[i]);
5731 }
5732 }
5733
5734 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5735 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5736 char country[3];
5737 country[0] = wpa_s->conf->country[0];
5738 country[1] = wpa_s->conf->country[1];
5739 country[2] = 0x04;
5740 p2p_set_country(p2p, country);
5741 }
5742
5743 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5744 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5745 wpa_s->conf->p2p_ssid_postfix ?
5746 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5747 0);
5748 }
5749
5750 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5751 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
Jouni Malinen75ecf522011-06-27 15:19:46 -07005752
5753 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5754 u8 reg_class, channel;
5755 int ret;
5756 unsigned int r;
5757 if (wpa_s->conf->p2p_listen_reg_class &&
5758 wpa_s->conf->p2p_listen_channel) {
5759 reg_class = wpa_s->conf->p2p_listen_reg_class;
5760 channel = wpa_s->conf->p2p_listen_channel;
5761 } else {
5762 reg_class = 81;
5763 /*
5764 * Pick one of the social channels randomly as the
5765 * listen channel.
5766 */
5767 os_get_random((u8 *) &r, sizeof(r));
5768 channel = 1 + (r % 3) * 5;
5769 }
5770 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5771 if (ret)
5772 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5773 "failed: %d", ret);
5774 }
5775 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5776 u8 op_reg_class, op_channel, cfg_op_channel;
5777 int ret = 0;
5778 unsigned int r;
5779 if (wpa_s->conf->p2p_oper_reg_class &&
5780 wpa_s->conf->p2p_oper_channel) {
5781 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5782 op_channel = wpa_s->conf->p2p_oper_channel;
5783 cfg_op_channel = 1;
5784 } else {
5785 op_reg_class = 81;
5786 /*
5787 * Use random operation channel from (1, 6, 11)
5788 *if no other preference is indicated.
5789 */
5790 os_get_random((u8 *) &r, sizeof(r));
5791 op_channel = 1 + (r % 3) * 5;
5792 cfg_op_channel = 0;
5793 }
5794 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5795 cfg_op_channel);
5796 if (ret)
5797 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5798 "failed: %d", ret);
5799 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005800
5801 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5802 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5803 wpa_s->conf->p2p_pref_chan) < 0) {
5804 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5805 "update failed");
5806 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005807
5808 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5809 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5810 "update failed");
5811 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07005812 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005813}
5814
5815
5816int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5817 int duration)
5818{
5819 if (!wpa_s->ap_iface)
5820 return -1;
5821 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5822 duration);
5823}
5824
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005825
5826int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5827{
5828 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5829 return -1;
5830 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5831 return -1;
5832
5833 wpa_s->global->cross_connection = enabled;
5834 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5835
5836 if (!enabled) {
5837 struct wpa_supplicant *iface;
5838
5839 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5840 {
5841 if (iface->cross_connect_enabled == 0)
5842 continue;
5843
5844 iface->cross_connect_enabled = 0;
5845 iface->cross_connect_in_use = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005846 wpa_msg_global(iface->parent, MSG_INFO,
5847 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5848 iface->ifname,
5849 iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005850 }
5851 }
5852
5853 return 0;
5854}
5855
5856
5857static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5858{
5859 struct wpa_supplicant *iface;
5860
5861 if (!uplink->global->cross_connection)
5862 return;
5863
5864 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5865 if (!iface->cross_connect_enabled)
5866 continue;
5867 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5868 0)
5869 continue;
5870 if (iface->ap_iface == NULL)
5871 continue;
5872 if (iface->cross_connect_in_use)
5873 continue;
5874
5875 iface->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005876 wpa_msg_global(iface->parent, MSG_INFO,
5877 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5878 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005879 }
5880}
5881
5882
5883static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5884{
5885 struct wpa_supplicant *iface;
5886
5887 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5888 if (!iface->cross_connect_enabled)
5889 continue;
5890 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5891 0)
5892 continue;
5893 if (!iface->cross_connect_in_use)
5894 continue;
5895
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005896 wpa_msg_global(iface->parent, MSG_INFO,
5897 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5898 iface->ifname, iface->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005899 iface->cross_connect_in_use = 0;
5900 }
5901}
5902
5903
5904void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5905{
5906 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5907 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5908 wpa_s->cross_connect_disallowed)
5909 wpas_p2p_disable_cross_connect(wpa_s);
5910 else
5911 wpas_p2p_enable_cross_connect(wpa_s);
Dmitry Shmidt04949592012-07-19 12:16:46 -07005912 if (!wpa_s->ap_iface &&
5913 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5914 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005915}
5916
5917
5918void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5919{
5920 wpas_p2p_disable_cross_connect(wpa_s);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005921 if (!wpa_s->ap_iface &&
5922 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5923 wpa_s, NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005924 wpas_p2p_set_group_idle_timeout(wpa_s);
5925}
5926
5927
5928static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5929{
5930 struct wpa_supplicant *iface;
5931
5932 if (!wpa_s->global->cross_connection)
5933 return;
5934
5935 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5936 if (iface == wpa_s)
5937 continue;
5938 if (iface->drv_flags &
5939 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5940 continue;
5941 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5942 continue;
5943
5944 wpa_s->cross_connect_enabled = 1;
5945 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5946 sizeof(wpa_s->cross_connect_uplink));
5947 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5948 "%s to %s whenever uplink is available",
5949 wpa_s->ifname, wpa_s->cross_connect_uplink);
5950
5951 if (iface->ap_iface || iface->current_ssid == NULL ||
5952 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5953 iface->cross_connect_disallowed ||
5954 iface->wpa_state != WPA_COMPLETED)
5955 break;
5956
5957 wpa_s->cross_connect_in_use = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07005958 wpa_msg_global(wpa_s->parent, MSG_INFO,
5959 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5960 wpa_s->ifname, wpa_s->cross_connect_uplink);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005961 break;
5962 }
5963}
5964
5965
5966int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5967{
5968 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5969 !wpa_s->p2p_in_provisioning)
5970 return 0; /* not P2P client operation */
5971
5972 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5973 "session overlap");
5974 if (wpa_s != wpa_s->parent)
5975 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
Dmitry Shmidt34af3062013-07-11 10:46:32 -07005976 wpas_p2p_group_formation_failed(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005977 return 1;
5978}
5979
5980
5981void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5982{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005983 struct p2p_channels chan, cli_chan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005984
5985 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5986 return;
5987
5988 os_memset(&chan, 0, sizeof(chan));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005989 os_memset(&cli_chan, 0, sizeof(cli_chan));
5990 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005991 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5992 "channel list");
5993 return;
5994 }
5995
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07005996 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005997}
5998
5999
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006000static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6001 struct wpa_scan_results *scan_res)
6002{
6003 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6004}
6005
6006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006007int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6008{
6009 struct wpa_global *global = wpa_s->global;
6010 int found = 0;
6011 const u8 *peer;
6012
6013 if (global->p2p == NULL)
6014 return -1;
6015
6016 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6017
6018 if (wpa_s->pending_interface_name[0] &&
6019 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6020 found = 1;
6021
6022 peer = p2p_get_go_neg_peer(global->p2p);
6023 if (peer) {
6024 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6025 MACSTR, MAC2STR(peer));
6026 p2p_unauthorize(global->p2p, peer);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006027 found = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006028 }
6029
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08006030 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6031 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6032 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6033 found = 1;
6034 }
6035
6036 if (wpa_s->pending_pd_before_join) {
6037 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6038 wpa_s->pending_pd_before_join = 0;
6039 found = 1;
6040 }
6041
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006042 wpas_p2p_stop_find(wpa_s);
6043
6044 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6045 if (wpa_s == global->p2p_group_formation &&
6046 (wpa_s->p2p_in_provisioning ||
6047 wpa_s->parent->pending_interface_type ==
6048 WPA_IF_P2P_CLIENT)) {
6049 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6050 "formation found - cancelling",
6051 wpa_s->ifname);
6052 found = 1;
6053 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6054 wpa_s->parent, NULL);
Jouni Malinenadddfc42012-10-03 14:31:41 -07006055 if (wpa_s->p2p_in_provisioning) {
6056 wpas_group_formation_completed(wpa_s, 0);
6057 break;
6058 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006059 wpas_p2p_group_delete(wpa_s,
6060 P2P_GROUP_REMOVAL_REQUESTED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006061 break;
6062 }
6063 }
6064
6065 if (!found) {
6066 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6067 return -1;
6068 }
6069
6070 return 0;
6071}
6072
6073
6074void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6075{
6076 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6077 return;
6078
6079 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6080 "being available anymore");
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006081 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006082}
6083
6084
6085void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6086 int freq_24, int freq_5, int freq_overall)
6087{
6088 struct p2p_data *p2p = wpa_s->global->p2p;
6089 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6090 return;
6091 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6092}
6093
6094
6095int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6096{
6097 u8 peer[ETH_ALEN];
6098 struct p2p_data *p2p = wpa_s->global->p2p;
6099
6100 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6101 return -1;
6102
6103 if (hwaddr_aton(addr, peer))
6104 return -1;
6105
6106 return p2p_unauthorize(p2p, peer);
6107}
6108
6109
6110/**
6111 * wpas_p2p_disconnect - Disconnect from a P2P Group
6112 * @wpa_s: Pointer to wpa_supplicant data
6113 * Returns: 0 on success, -1 on failure
6114 *
6115 * This can be used to disconnect from a group in which the local end is a P2P
6116 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6117 * virtual network interface was created for this group, that interface will be
6118 * removed. Otherwise, only the configured P2P group network will be removed
6119 * from the interface.
6120 */
6121int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6122{
6123
6124 if (wpa_s == NULL)
6125 return -1;
6126
Jouni Malinen2b89da82012-08-31 22:04:41 +03006127 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6128 -1 : 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07006129}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006130
6131
6132int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6133{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006134 int ret;
6135
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006136 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6137 return 0;
6138
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006139 ret = p2p_in_progress(wpa_s->global->p2p);
6140 if (ret == 0) {
6141 /*
6142 * Check whether there is an ongoing WPS provisioning step (or
6143 * other parts of group formation) on another interface since
6144 * p2p_in_progress() does not report this to avoid issues for
6145 * scans during such provisioning step.
6146 */
6147 if (wpa_s->global->p2p_group_formation &&
6148 wpa_s->global->p2p_group_formation != wpa_s) {
6149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6150 "in group formation",
6151 wpa_s->global->p2p_group_formation->ifname);
6152 ret = 1;
6153 }
6154 }
6155
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006156 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6157 struct os_time now;
6158 os_get_time(&now);
6159 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6160 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6161 /* Wait for the first client has expired */
6162 wpa_s->global->p2p_go_wait_client.sec = 0;
6163 } else {
6164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6165 ret = 1;
6166 }
6167 }
6168
Dmitry Shmidtf8623282013-02-20 14:34:59 -08006169 return ret;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006170}
6171
6172
6173void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6174 struct wpa_ssid *ssid)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006175{
6176 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6177 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6178 wpa_s->parent, NULL) > 0) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07006179 /**
6180 * Remove the network by scheduling the group formation
6181 * timeout to happen immediately. The teardown code
6182 * needs to be scheduled to run asynch later so that we
6183 * don't delete data from under ourselves unexpectedly.
6184 * Calling wpas_p2p_group_formation_timeout directly
6185 * causes a series of crashes in WPS failure scenarios.
6186 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006187 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6188 "P2P group network getting removed");
Dmitry Shmidt04949592012-07-19 12:16:46 -07006189 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6190 wpa_s->parent, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006191 }
6192}
6193
6194
6195struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006196 const u8 *addr, const u8 *ssid,
6197 size_t ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006198{
6199 struct wpa_ssid *s;
6200 size_t i;
6201
6202 for (s = wpa_s->conf->ssid; s; s = s->next) {
6203 if (s->disabled != 2)
6204 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006205 if (ssid &&
6206 (ssid_len != s->ssid_len ||
6207 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6208 continue;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006209 if (addr == NULL) {
6210 if (s->mode == WPAS_MODE_P2P_GO)
6211 return s;
6212 continue;
6213 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006214 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6215 return s; /* peer is GO in the persistent group */
6216 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6217 continue;
6218 for (i = 0; i < s->num_p2p_clients; i++) {
6219 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6220 addr, ETH_ALEN) == 0)
6221 return s; /* peer is P2P client in persistent
6222 * group */
6223 }
6224 }
6225
6226 return NULL;
6227}
6228
6229
6230void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6231 const u8 *addr)
6232{
Dmitry Shmidt56052862013-10-04 10:23:25 -07006233 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6234 wpa_s->parent, NULL) > 0) {
6235 /*
6236 * This can happen if WPS provisioning step is not terminated
6237 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6238 * peer was able to connect, there is no need to time out group
6239 * formation after this, though. In addition, this is used with
6240 * the initial connection wait on the GO as a separate formation
6241 * timeout and as such, expected to be hit after the initial WPS
6242 * provisioning step.
6243 */
6244 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6245 }
6246 if (!wpa_s->p2p_go_group_formation_completed) {
6247 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6248 wpa_s->p2p_go_group_formation_completed = 1;
6249 wpa_s->global->p2p_group_formation = NULL;
6250 wpa_s->p2p_in_provisioning = 0;
6251 }
Dmitry Shmidt92c368d2013-08-29 12:37:21 -07006252 wpa_s->global->p2p_go_wait_client.sec = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08006253 if (addr == NULL)
6254 return;
6255 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6256}
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006257
Dmitry Shmidt04949592012-07-19 12:16:46 -07006258
6259static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6260 int group_added)
6261{
6262 struct wpa_supplicant *group = wpa_s;
Dmitry Shmidt04949592012-07-19 12:16:46 -07006263 if (wpa_s->global->p2p_group_formation)
6264 group = wpa_s->global->p2p_group_formation;
6265 wpa_s = wpa_s->parent;
6266 offchannel_send_action_done(wpa_s);
6267 if (group_added)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006268 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006269 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6270 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6271 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6272 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6273 wpa_s->p2p_persistent_id,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006274 wpa_s->p2p_pd_before_go_neg,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07006275 wpa_s->p2p_go_ht40,
6276 wpa_s->p2p_go_vht);
Dmitry Shmidt04949592012-07-19 12:16:46 -07006277}
6278
6279
6280int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6281{
6282 if (!wpa_s->p2p_fallback_to_go_neg ||
6283 wpa_s->p2p_in_provisioning <= 5)
6284 return 0;
6285
6286 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6287 return 0; /* peer operating as a GO */
6288
6289 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6290 "fallback to GO Negotiation");
6291 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6292
6293 return 1;
6294}
6295
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07006296
6297unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6298{
6299 const char *rn, *rn2;
6300 struct wpa_supplicant *ifs;
6301
6302 if (wpa_s->wpa_state > WPA_SCANNING) {
6303 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6304 "concurrent operation",
6305 P2P_CONCURRENT_SEARCH_DELAY);
6306 return P2P_CONCURRENT_SEARCH_DELAY;
6307 }
6308
6309 if (!wpa_s->driver->get_radio_name)
6310 return 0;
6311 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6312 if (rn == NULL || rn[0] == '\0')
6313 return 0;
6314
6315 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6316 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6317 continue;
6318
6319 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6320 if (!rn2 || os_strcmp(rn, rn2) != 0)
6321 continue;
6322 if (ifs->wpa_state > WPA_SCANNING) {
6323 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6324 "delay due to concurrent operation on "
6325 "interface %s",
6326 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6327 return P2P_CONCURRENT_SEARCH_DELAY;
6328 }
6329 }
6330
6331 return 0;
6332}
6333
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07006334
6335void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6336{
6337 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6338 "pending anymore (sta_scan_pending=%d "
6339 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6340 wpa_s->global->p2p_cb_on_scan_complete);
6341 wpa_s->sta_scan_pending = 0;
6342
6343 if (!wpa_s->global->p2p_cb_on_scan_complete)
6344 return;
6345 wpa_s->global->p2p_cb_on_scan_complete = 0;
6346
6347 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6348 return;
6349
6350 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6352 "continued after successful connection");
6353 p2p_increase_search_delay(wpa_s->global->p2p,
6354 wpas_p2p_search_delay(wpa_s));
6355 }
6356}
6357
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07006358
6359static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6360 struct wpa_ssid *s, const u8 *addr,
6361 int iface_addr)
6362{
6363 struct psk_list_entry *psk, *tmp;
6364 int changed = 0;
6365
6366 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6367 list) {
6368 if ((iface_addr && !psk->p2p &&
6369 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6370 (!iface_addr && psk->p2p &&
6371 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6372 wpa_dbg(wpa_s, MSG_DEBUG,
6373 "P2P: Remove persistent group PSK list entry for "
6374 MACSTR " p2p=%u",
6375 MAC2STR(psk->addr), psk->p2p);
6376 dl_list_del(&psk->list);
6377 os_free(psk);
6378 changed++;
6379 }
6380 }
6381
6382 return changed;
6383}
6384
6385
6386void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6387 const u8 *p2p_dev_addr,
6388 const u8 *psk, size_t psk_len)
6389{
6390 struct wpa_ssid *ssid = wpa_s->current_ssid;
6391 struct wpa_ssid *persistent;
6392 struct psk_list_entry *p;
6393
6394 if (psk_len != sizeof(p->psk))
6395 return;
6396
6397 if (p2p_dev_addr) {
6398 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6399 " p2p_dev_addr=" MACSTR,
6400 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6401 if (is_zero_ether_addr(p2p_dev_addr))
6402 p2p_dev_addr = NULL;
6403 } else {
6404 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6405 MAC2STR(mac_addr));
6406 }
6407
6408 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6409 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6410 /* To be added to persistent group once created */
6411 if (wpa_s->global->add_psk == NULL) {
6412 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6413 if (wpa_s->global->add_psk == NULL)
6414 return;
6415 }
6416 p = wpa_s->global->add_psk;
6417 if (p2p_dev_addr) {
6418 p->p2p = 1;
6419 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6420 } else {
6421 p->p2p = 0;
6422 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6423 }
6424 os_memcpy(p->psk, psk, psk_len);
6425 return;
6426 }
6427
6428 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6429 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6430 return;
6431 }
6432
6433 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6434 ssid->ssid_len);
6435 if (!persistent) {
6436 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6437 return;
6438 }
6439
6440 p = os_zalloc(sizeof(*p));
6441 if (p == NULL)
6442 return;
6443 if (p2p_dev_addr) {
6444 p->p2p = 1;
6445 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6446 } else {
6447 p->p2p = 0;
6448 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6449 }
6450 os_memcpy(p->psk, psk, psk_len);
6451
6452 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6453 struct psk_list_entry *last;
6454 last = dl_list_last(&persistent->psk_list,
6455 struct psk_list_entry, list);
6456 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6457 MACSTR " (p2p=%u) to make room for a new one",
6458 MAC2STR(last->addr), last->p2p);
6459 dl_list_del(&last->list);
6460 os_free(last);
6461 }
6462
6463 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6464 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6465 p2p_dev_addr == NULL);
6466 if (p2p_dev_addr) {
6467 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6468 MACSTR, MAC2STR(p2p_dev_addr));
6469 } else {
6470 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6471 MAC2STR(mac_addr));
6472 }
6473 dl_list_add(&persistent->psk_list, &p->list);
6474
6475#ifndef CONFIG_NO_CONFIG_WRITE
6476 if (wpa_s->parent->conf->update_config &&
6477 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6478 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6479#endif /* CONFIG_NO_CONFIG_WRITE */
6480}
6481
6482
6483static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6484 struct wpa_ssid *s, const u8 *addr,
6485 int iface_addr)
6486{
6487 int res;
6488
6489 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6490 if (res > 0) {
6491#ifndef CONFIG_NO_CONFIG_WRITE
6492 if (wpa_s->conf->update_config &&
6493 wpa_config_write(wpa_s->confname, wpa_s->conf))
6494 wpa_dbg(wpa_s, MSG_DEBUG,
6495 "P2P: Failed to update configuration");
6496#endif /* CONFIG_NO_CONFIG_WRITE */
6497 }
6498}
6499
6500
6501static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6502 const u8 *peer, int iface_addr)
6503{
6504 struct hostapd_data *hapd;
6505 struct hostapd_wpa_psk *psk, *prev, *rem;
6506 struct sta_info *sta;
6507
6508 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6509 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6510 return;
6511
6512 /* Remove per-station PSK entry */
6513 hapd = wpa_s->ap_iface->bss[0];
6514 prev = NULL;
6515 psk = hapd->conf->ssid.wpa_psk;
6516 while (psk) {
6517 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6518 (!iface_addr &&
6519 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6520 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6521 MACSTR " iface_addr=%d",
6522 MAC2STR(peer), iface_addr);
6523 if (prev)
6524 prev->next = psk->next;
6525 else
6526 hapd->conf->ssid.wpa_psk = psk->next;
6527 rem = psk;
6528 psk = psk->next;
6529 os_free(rem);
6530 } else {
6531 prev = psk;
6532 psk = psk->next;
6533 }
6534 }
6535
6536 /* Disconnect from group */
6537 if (iface_addr)
6538 sta = ap_get_sta(hapd, peer);
6539 else
6540 sta = ap_get_sta_p2p(hapd, peer);
6541 if (sta) {
6542 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6543 " (iface_addr=%d) from group",
6544 MAC2STR(peer), iface_addr);
6545 hostapd_drv_sta_deauth(hapd, sta->addr,
6546 WLAN_REASON_DEAUTH_LEAVING);
6547 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6548 }
6549}
6550
6551
6552void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6553 int iface_addr)
6554{
6555 struct wpa_ssid *s;
6556 struct wpa_supplicant *w;
6557
6558 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6559
6560 /* Remove from any persistent group */
6561 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6562 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6563 continue;
6564 if (!iface_addr)
6565 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6566 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6567 }
6568
6569 /* Remove from any operating group */
6570 for (w = wpa_s->global->ifaces; w; w = w->next)
6571 wpas_p2p_remove_client_go(w, peer, iface_addr);
6572}
6573
6574
6575static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6576{
6577 struct wpa_supplicant *wpa_s = eloop_ctx;
6578 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6579}
6580
6581
6582int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6583{
6584 struct wpa_ssid *ssid = wpa_s->current_ssid;
6585
6586 if (ssid == NULL || !ssid->p2p_group)
6587 return 0;
6588
6589 if (wpa_s->p2p_last_4way_hs_fail &&
6590 wpa_s->p2p_last_4way_hs_fail == ssid) {
6591 u8 go_dev_addr[ETH_ALEN];
6592 struct wpa_ssid *persistent;
6593
6594 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6595 ssid->ssid,
6596 ssid->ssid_len) <= 0) {
6597 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6598 goto disconnect;
6599 }
6600
6601 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6602 MACSTR, MAC2STR(go_dev_addr));
6603 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6604 ssid->ssid,
6605 ssid->ssid_len);
6606 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6607 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6608 goto disconnect;
6609 }
6610 wpa_msg_global(wpa_s->parent, MSG_INFO,
6611 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6612 persistent->id);
6613 disconnect:
6614 wpa_s->p2p_last_4way_hs_fail = NULL;
6615 /*
6616 * Remove the group from a timeout to avoid issues with caller
6617 * continuing to use the interface if this is on a P2P group
6618 * interface.
6619 */
6620 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6621 wpa_s, NULL);
6622 return 1;
6623 }
6624
6625 wpa_s->p2p_last_4way_hs_fail = ssid;
6626 return 0;
6627}
6628
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006629#ifdef ANDROID_P2P
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006630static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6631{
6632 struct wpa_supplicant *wpa_s = eloop_ctx;
6633
6634 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6635 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6636}
6637
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006638int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6639 struct wpa_ssid *ssid)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006640{
6641 struct wpa_supplicant *iface = NULL;
6642 struct p2p_data *p2p = wpa_s->global->p2p;
6643
6644 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006645 if ((iface->current_ssid) &&
6646 (iface->current_ssid->frequency != freq) &&
6647 ((iface->p2p_group_interface) ||
6648 (iface->current_ssid->p2p_group))) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006649
Jeff Johnson12b1cd92012-10-07 19:34:24 -07006650 if ((iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) ||
6651 (iface->current_ssid->mode == WPAS_MODE_P2P_GO)) {
6652 /* Try to see whether we can move the GO. If it
6653 * is not possible, remove the GO interface
6654 */
6655 if (wpa_drv_switch_channel(iface, freq) == 0) {
6656 wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
6657 iface->current_ssid->frequency = freq;
6658 continue;
6659 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006660 }
6661
6662 /* If GO cannot be moved or if the conflicting interface is a
6663 * P2P Client, remove the interface depending up on the connection
6664 * priority */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006665 if(!wpas_is_p2p_prioritized(iface)) {
Dmitry Shmidtf4f5db32012-09-11 14:36:56 -07006666 /* STA connection has priority over existing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006667 * P2P connection. So remove the interface */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006668 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
Dmitry Shmidt687922c2012-03-26 14:02:32 -07006669 "concurrent mode frequency conflict");
Dmitry Shmidt1cf45732013-04-29 17:36:45 -07006670 eloop_register_timeout(0, 0, wpas_p2p_group_freq_conflict,
6671 iface, NULL);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006672 /* If connection in progress is p2p connection, do not proceed for the connection */
6673 if (wpa_s == iface)
6674 return -1;
6675 else
6676 /* If connection in progress is STA connection, proceed for the connection */
6677 return 0;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006678 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08006679 /* P2p connection has priority, disable the STA network*/
6680 wpa_supplicant_disable_network(wpa_s->global->ifaces, ssid);
6681 wpa_msg(wpa_s->global->ifaces, MSG_INFO, WPA_EVENT_FREQ_CONFLICT
6682 " id=%d", ssid->id);
6683 os_memset(wpa_s->global->ifaces->pending_bssid, 0, ETH_ALEN);
6684 if (wpa_s == iface) {
6685 /* p2p connection is in progress, continue connecting...*/
6686 return 0;
6687 }
6688 else {
6689 /* STA connection is in progress, do not allow to continue */
6690 return -1;
6691 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006692 }
6693 }
6694 }
6695 return 0;
6696}
6697#endif